blob: 7157cb81acf85171b94e331ad02e8404323dd0c5 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006// Defined when linking against shared lib on Windows.
7#if defined(USING_V8_SHARED) && !defined(V8_SHARED)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008#define V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000010
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000011#include <errno.h>
12#include <stdlib.h>
13#include <string.h>
Ben Murdoch69a99ed2011-11-30 16:03:39 +000014#include <sys/stat.h>
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000015
Ben Murdoch69a99ed2011-11-30 16:03:39 +000016#ifdef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000017#include <assert.h>
Ben Murdoch69a99ed2011-11-30 16:03:39 +000018#endif // V8_SHARED
Steve Block44f0eee2011-05-26 01:26:41 +010019
Ben Murdoch69a99ed2011-11-30 16:03:39 +000020#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021#include <algorithm>
22#endif // !V8_SHARED
23
24#ifdef V8_SHARED
25#include "include/v8-testing.h"
Ben Murdoch69a99ed2011-11-30 16:03:39 +000026#endif // V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +000027
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028#if !defined(V8_SHARED) && defined(ENABLE_GDB_JIT_INTERFACE)
29#include "src/gdb-jit.h"
30#endif
31
32#ifdef ENABLE_VTUNE_JIT_INTERFACE
33#include "src/third_party/vtune/v8-vtune.h"
34#endif
35
36#include "src/d8.h"
37
38#include "include/libplatform/libplatform.h"
39#ifndef V8_SHARED
40#include "src/api.h"
41#include "src/base/cpu.h"
42#include "src/base/logging.h"
43#include "src/base/platform/platform.h"
44#include "src/base/sys-info.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045#include "src/basic-block-profiler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046#include "src/d8-debug.h"
47#include "src/debug.h"
48#include "src/natives.h"
49#include "src/v8.h"
50#endif // !V8_SHARED
51
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000052#if !defined(_WIN32) && !defined(_WIN64)
53#include <unistd.h> // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054#else
55#include <windows.h> // NOLINT
56#if defined(_MSC_VER)
57#include <crtdbg.h> // NOLINT
58#endif // defined(_MSC_VER)
59#endif // !defined(_WIN32) && !defined(_WIN64)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000060
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061#ifndef DCHECK
62#define DCHECK(condition) assert(condition)
Ben Murdoch69a99ed2011-11-30 16:03:39 +000063#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65namespace v8 {
66
Steve Blocka7e24c12009-10-30 11:49:00 +000067
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068static Handle<Value> Throw(Isolate* isolate, const char* message) {
69 return isolate->ThrowException(String::NewFromUtf8(isolate, message));
Steve Blocka7e24c12009-10-30 11:49:00 +000070}
71
72
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073
74class PerIsolateData {
75 public:
76 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) {
77 HandleScope scope(isolate);
78 isolate->SetData(0, this);
Steve Blocka7e24c12009-10-30 11:49:00 +000079 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080
81 ~PerIsolateData() {
82 isolate_->SetData(0, NULL); // Not really needed, just to be sure...
83 }
84
85 inline static PerIsolateData* Get(Isolate* isolate) {
86 return reinterpret_cast<PerIsolateData*>(isolate->GetData(0));
87 }
88
89 class RealmScope {
90 public:
91 explicit RealmScope(PerIsolateData* data);
92 ~RealmScope();
93 private:
94 PerIsolateData* data_;
95 };
96
97 private:
98 friend class Shell;
99 friend class RealmScope;
100 Isolate* isolate_;
101 int realm_count_;
102 int realm_current_;
103 int realm_switch_;
104 Persistent<Context>* realms_;
105 Persistent<Value> realm_shared_;
106
107 int RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value>& args,
108 int arg_offset);
109 int RealmFind(Handle<Context> context);
110};
111
112
113LineEditor *LineEditor::current_ = NULL;
114
115
116LineEditor::LineEditor(Type type, const char* name)
117 : type_(type), name_(name) {
118 if (current_ == NULL || current_->type_ < type) current_ = this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000119}
120
121
122class DumbLineEditor: public LineEditor {
123 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 explicit DumbLineEditor(Isolate* isolate)
125 : LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100126 virtual Handle<String> Prompt(const char* prompt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 private:
128 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000129};
130
131
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100132Handle<String> DumbLineEditor::Prompt(const char* prompt) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 printf("%s", prompt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000134#if defined(__native_client__)
135 // Native Client libc is used to being embedded in Chrome and
136 // has trouble recognizing when to flush.
137 fflush(stdout);
138#endif
139 return Shell::ReadFromStdin(isolate_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000140}
141
142
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143#ifndef V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000144CounterMap* Shell::counter_map_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145base::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000146CounterCollection Shell::local_counters_;
147CounterCollection* Shell::counters_ = &local_counters_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148base::Mutex Shell::context_mutex_;
149const base::TimeTicks Shell::kInitialTicks =
150 base::TimeTicks::HighResolutionNow();
Steve Blocka7e24c12009-10-30 11:49:00 +0000151Persistent<Context> Shell::utility_context_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000153
Steve Blocka7e24c12009-10-30 11:49:00 +0000154Persistent<Context> Shell::evaluation_context_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000155ShellOptions Shell::options;
156const char* Shell::kPrompt = "d8> ";
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
158
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159#ifndef V8_SHARED
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100160const int MB = 1024 * 1024;
161
Steve Blocka7e24c12009-10-30 11:49:00 +0000162bool CounterMap::Match(void* key1, void* key2) {
163 const char* name1 = reinterpret_cast<const char*>(key1);
164 const char* name2 = reinterpret_cast<const char*>(key2);
165 return strcmp(name1, name2) == 0;
166}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000168
169
170// Converts a V8 value to a C string.
Steve Block6ded16b2010-05-10 14:33:55 +0100171const char* Shell::ToCString(const v8::String::Utf8Value& value) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 return *value ? *value : "<string conversion failed>";
173}
174
175
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400176ScriptCompiler::CachedData* CompileForCachedData(
177 Local<String> source, Local<Value> name,
178 ScriptCompiler::CompileOptions compile_options) {
179 int source_length = source->Length();
180 uint16_t* source_buffer = new uint16_t[source_length];
181 source->Write(source_buffer, 0, source_length);
182 int name_length = 0;
183 uint16_t* name_buffer = NULL;
184 if (name->IsString()) {
185 Local<String> name_string = Local<String>::Cast(name);
186 name_length = name_string->Length();
187 name_buffer = new uint16_t[name_length];
188 name_string->Write(name_buffer, 0, name_length);
189 }
190 Isolate* temp_isolate = Isolate::New();
191 ScriptCompiler::CachedData* result = NULL;
192 {
193 Isolate::Scope isolate_scope(temp_isolate);
194 HandleScope handle_scope(temp_isolate);
195 Context::Scope context_scope(Context::New(temp_isolate));
196 Local<String> source_copy = v8::String::NewFromTwoByte(
197 temp_isolate, source_buffer, v8::String::kNormalString, source_length);
198 Local<Value> name_copy;
199 if (name_buffer) {
200 name_copy = v8::String::NewFromTwoByte(
201 temp_isolate, name_buffer, v8::String::kNormalString, name_length);
202 } else {
203 name_copy = v8::Undefined(temp_isolate);
204 }
205 ScriptCompiler::Source script_source(source_copy, ScriptOrigin(name_copy));
206 ScriptCompiler::CompileUnbound(temp_isolate, &script_source,
207 compile_options);
208 if (script_source.GetCachedData()) {
209 int length = script_source.GetCachedData()->length;
210 uint8_t* cache = new uint8_t[length];
211 memcpy(cache, script_source.GetCachedData()->data, length);
212 result = new ScriptCompiler::CachedData(
213 cache, length, ScriptCompiler::CachedData::BufferOwned);
214 }
215 }
216 temp_isolate->Dispose();
217 delete[] source_buffer;
218 delete[] name_buffer;
219 return result;
220}
221
222
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223// Compile a string within the current v8 context.
224Local<UnboundScript> Shell::CompileString(
225 Isolate* isolate, Local<String> source, Local<Value> name,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 ScriptCompiler::CompileOptions compile_options) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000227 ScriptOrigin origin(name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400228 if (compile_options == ScriptCompiler::kNoCompileOptions) {
229 ScriptCompiler::Source script_source(source, origin);
230 return ScriptCompiler::CompileUnbound(isolate, &script_source,
231 compile_options);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400233
234 ScriptCompiler::CachedData* data =
235 CompileForCachedData(source, name, compile_options);
236 ScriptCompiler::Source cached_source(source, origin, data);
237 if (compile_options == ScriptCompiler::kProduceCodeCache) {
238 compile_options = ScriptCompiler::kConsumeCodeCache;
239 } else if (compile_options == ScriptCompiler::kProduceParserCache) {
240 compile_options = ScriptCompiler::kConsumeParserCache;
241 } else {
242 DCHECK(false); // A new compile option?
243 }
244 if (data == NULL) compile_options = ScriptCompiler::kNoCompileOptions;
245 return ScriptCompiler::CompileUnbound(isolate, &cached_source,
246 compile_options);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247}
248
249
Steve Blocka7e24c12009-10-30 11:49:00 +0000250// Executes a string within the current v8 context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251bool Shell::ExecuteString(Isolate* isolate,
252 Handle<String> source,
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 Handle<Value> name,
254 bool print_result,
255 bool report_exceptions) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000257 bool FLAG_debugger = i::FLAG_debugger;
258#else
259 bool FLAG_debugger = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260#endif // !V8_SHARED
261 HandleScope handle_scope(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 TryCatch try_catch;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000263 options.script_executed = true;
264 if (FLAG_debugger) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 // When debugging make exceptions appear to be uncaught.
266 try_catch.SetVerbose(true);
267 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268
269 Handle<UnboundScript> script =
270 Shell::CompileString(isolate, source, name, options.compile_options);
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 if (script.IsEmpty()) {
272 // Print errors that happened during compilation.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000273 if (report_exceptions && !FLAG_debugger)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000274 ReportException(isolate, &try_catch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 return false;
276 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 PerIsolateData* data = PerIsolateData::Get(isolate);
278 Local<Context> realm =
279 Local<Context>::New(isolate, data->realms_[data->realm_current_]);
280 realm->Enter();
281 Handle<Value> result = script->BindToCurrentContext()->Run();
282 realm->Exit();
283 data->realm_current_ = data->realm_switch_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000284 if (result.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 DCHECK(try_catch.HasCaught());
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 // Print errors that happened during execution.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000287 if (report_exceptions && !FLAG_debugger)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000288 ReportException(isolate, &try_catch);
Steve Blocka7e24c12009-10-30 11:49:00 +0000289 return false;
290 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 DCHECK(!try_catch.HasCaught());
292 if (print_result) {
293#if !defined(V8_SHARED)
294 if (options.test_shell) {
295#endif
296 if (!result->IsUndefined()) {
297 // If all went well and the result wasn't undefined then print
298 // the returned value.
299 v8::String::Utf8Value str(result);
300 fwrite(*str, sizeof(**str), str.length(), stdout);
301 printf("\n");
302 }
303#if !defined(V8_SHARED)
304 } else {
305 v8::TryCatch try_catch;
306 v8::Local<v8::Context> context =
307 v8::Local<v8::Context>::New(isolate, utility_context_);
308 v8::Context::Scope context_scope(context);
309 Handle<Object> global = context->Global();
310 Handle<Value> fun =
311 global->Get(String::NewFromUtf8(isolate, "Stringify"));
312 Handle<Value> argv[1] = { result };
313 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv);
314 if (try_catch.HasCaught()) return true;
315 v8::String::Utf8Value str(s);
316 fwrite(*str, sizeof(**str), str.length(), stdout);
317 printf("\n");
318 }
319#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 }
321 return true;
322 }
323 }
324}
325
326
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
328 data_->realm_count_ = 1;
329 data_->realm_current_ = 0;
330 data_->realm_switch_ = 0;
331 data_->realms_ = new Persistent<Context>[1];
332 data_->realms_[0].Reset(data_->isolate_,
333 data_->isolate_->GetEnteredContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000334}
335
336
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337PerIsolateData::RealmScope::~RealmScope() {
338 // Drop realms to avoid keeping them alive.
339 for (int i = 0; i < data_->realm_count_; ++i)
340 data_->realms_[i].Reset();
341 delete[] data_->realms_;
342 if (!data_->realm_shared_.IsEmpty())
343 data_->realm_shared_.Reset();
344}
345
346
347int PerIsolateData::RealmFind(Handle<Context> context) {
348 for (int i = 0; i < realm_count_; ++i) {
349 if (realms_[i] == context) return i;
350 }
351 return -1;
352}
353
354
355int PerIsolateData::RealmIndexOrThrow(
356 const v8::FunctionCallbackInfo<v8::Value>& args,
357 int arg_offset) {
358 if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) {
359 Throw(args.GetIsolate(), "Invalid argument");
360 return -1;
361 }
362 int index = args[arg_offset]->Int32Value();
363 if (index < 0 ||
364 index >= realm_count_ ||
365 realms_[index].IsEmpty()) {
366 Throw(args.GetIsolate(), "Invalid realm index");
367 return -1;
368 }
369 return index;
370}
371
372
373#ifndef V8_SHARED
374// performance.now() returns a time stamp as double, measured in milliseconds.
375// When FLAG_verify_predictable mode is enabled it returns current value
376// of Heap::allocations_count().
377void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) {
378 if (i::FLAG_verify_predictable) {
379 Isolate* v8_isolate = args.GetIsolate();
380 i::Heap* heap = reinterpret_cast<i::Isolate*>(v8_isolate)->heap();
381 args.GetReturnValue().Set(heap->synthetic_time());
382 } else {
383 base::TimeDelta delta =
384 base::TimeTicks::HighResolutionNow() - kInitialTicks;
385 args.GetReturnValue().Set(delta.InMillisecondsF());
386 }
387}
388#endif // !V8_SHARED
389
390
391// Realm.current() returns the index of the currently active realm.
392void Shell::RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args) {
393 Isolate* isolate = args.GetIsolate();
394 PerIsolateData* data = PerIsolateData::Get(isolate);
395 int index = data->RealmFind(isolate->GetEnteredContext());
396 if (index == -1) return;
397 args.GetReturnValue().Set(index);
398}
399
400
401// Realm.owner(o) returns the index of the realm that created o.
402void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
403 Isolate* isolate = args.GetIsolate();
404 PerIsolateData* data = PerIsolateData::Get(isolate);
405 if (args.Length() < 1 || !args[0]->IsObject()) {
406 Throw(args.GetIsolate(), "Invalid argument");
407 return;
408 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400409 int index = data->RealmFind(args[0]->ToObject(isolate)->CreationContext());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410 if (index == -1) return;
411 args.GetReturnValue().Set(index);
412}
413
414
415// Realm.global(i) returns the global object of realm i.
416// (Note that properties of global objects cannot be read/written cross-realm.)
417void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
418 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate());
419 int index = data->RealmIndexOrThrow(args, 0);
420 if (index == -1) return;
421 args.GetReturnValue().Set(
422 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global());
423}
424
425
426// Realm.create() creates a new realm and returns its index.
427void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) {
428 Isolate* isolate = args.GetIsolate();
429 PerIsolateData* data = PerIsolateData::Get(isolate);
430 Persistent<Context>* old_realms = data->realms_;
431 int index = data->realm_count_;
432 data->realms_ = new Persistent<Context>[++data->realm_count_];
433 for (int i = 0; i < index; ++i) {
434 data->realms_[i].Reset(isolate, old_realms[i]);
435 }
436 delete[] old_realms;
437 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
438 data->realms_[index].Reset(
439 isolate, Context::New(isolate, NULL, global_template));
440 args.GetReturnValue().Set(index);
441}
442
443
444// Realm.dispose(i) disposes the reference to the realm i.
445void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) {
446 Isolate* isolate = args.GetIsolate();
447 PerIsolateData* data = PerIsolateData::Get(isolate);
448 int index = data->RealmIndexOrThrow(args, 0);
449 if (index == -1) return;
450 if (index == 0 ||
451 index == data->realm_current_ || index == data->realm_switch_) {
452 Throw(args.GetIsolate(), "Invalid realm index");
453 return;
454 }
455 data->realms_[index].Reset();
456}
457
458
459// Realm.switch(i) switches to the realm i for consecutive interactive inputs.
460void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
461 Isolate* isolate = args.GetIsolate();
462 PerIsolateData* data = PerIsolateData::Get(isolate);
463 int index = data->RealmIndexOrThrow(args, 0);
464 if (index == -1) return;
465 data->realm_switch_ = index;
466}
467
468
469// Realm.eval(i, s) evaluates s in realm i and returns the result.
470void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) {
471 Isolate* isolate = args.GetIsolate();
472 PerIsolateData* data = PerIsolateData::Get(isolate);
473 int index = data->RealmIndexOrThrow(args, 0);
474 if (index == -1) return;
475 if (args.Length() < 2 || !args[1]->IsString()) {
476 Throw(args.GetIsolate(), "Invalid argument");
477 return;
478 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400479 ScriptCompiler::Source script_source(args[1]->ToString(isolate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 Handle<UnboundScript> script = ScriptCompiler::CompileUnbound(
481 isolate, &script_source);
482 if (script.IsEmpty()) return;
483 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]);
484 realm->Enter();
485 Handle<Value> result = script->BindToCurrentContext()->Run();
486 realm->Exit();
487 args.GetReturnValue().Set(result);
488}
489
490
491// Realm.shared is an accessor for a single shared value across realms.
492void Shell::RealmSharedGet(Local<String> property,
493 const PropertyCallbackInfo<Value>& info) {
494 Isolate* isolate = info.GetIsolate();
495 PerIsolateData* data = PerIsolateData::Get(isolate);
496 if (data->realm_shared_.IsEmpty()) return;
497 info.GetReturnValue().Set(data->realm_shared_);
498}
499
500void Shell::RealmSharedSet(Local<String> property,
501 Local<Value> value,
502 const PropertyCallbackInfo<void>& info) {
503 Isolate* isolate = info.GetIsolate();
504 PerIsolateData* data = PerIsolateData::Get(isolate);
505 data->realm_shared_.Reset(isolate, value);
506}
507
508
509void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
510 Write(args);
511 printf("\n");
512 fflush(stdout);
513}
514
515
516void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 for (int i = 0; i < args.Length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 HandleScope handle_scope(args.GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 if (i != 0) {
520 printf(" ");
521 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522
523 // Explicitly catch potential exceptions in toString().
524 v8::TryCatch try_catch;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400525 Handle<String> str_obj = args[i]->ToString(args.GetIsolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 if (try_catch.HasCaught()) {
527 try_catch.ReThrow();
528 return;
529 }
530
531 v8::String::Utf8Value str(str_obj);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000532 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout));
Steve Blockd0582a62009-12-15 09:54:21 +0000533 if (n != str.length()) {
534 printf("Error in fwrite\n");
Ben Murdoch589d6972011-11-30 16:04:58 +0000535 Exit(1);
Steve Blockd0582a62009-12-15 09:54:21 +0000536 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000538}
539
540
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 String::Utf8Value file(args[0]);
543 if (*file == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 Throw(args.GetIsolate(), "Error loading file");
545 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 Handle<String> source = ReadFile(args.GetIsolate(), *file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000548 if (source.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 Throw(args.GetIsolate(), "Error loading file");
550 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000551 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552 args.GetReturnValue().Set(source);
Steve Blocka7e24c12009-10-30 11:49:00 +0000553}
554
555
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556Handle<String> Shell::ReadFromStdin(Isolate* isolate) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000557 static const int kBufferSize = 256;
558 char buffer[kBufferSize];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559 Handle<String> accumulator = String::NewFromUtf8(isolate, "");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000560 int length;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000561 while (true) {
562 // Continue reading if the line ends with an escape '\\' or the line has
563 // not been fully read into the buffer yet (does not end with '\n').
564 // If fgets gets an error, just give up.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100565 char* input = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 input = fgets(buffer, kBufferSize, stdin);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100567 if (input == NULL) return Handle<String>();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000568 length = static_cast<int>(strlen(buffer));
569 if (length == 0) {
570 return accumulator;
571 } else if (buffer[length-1] != '\n') {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 accumulator = String::Concat(
573 accumulator,
574 String::NewFromUtf8(isolate, buffer, String::kNormalString, length));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000575 } else if (length > 1 && buffer[length-2] == '\\') {
576 buffer[length-2] = '\n';
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 accumulator = String::Concat(
578 accumulator, String::NewFromUtf8(isolate, buffer,
579 String::kNormalString, length - 1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000580 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 return String::Concat(
582 accumulator, String::NewFromUtf8(isolate, buffer,
583 String::kNormalString, length - 1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000584 }
585 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000586}
587
588
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 for (int i = 0; i < args.Length(); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 HandleScope handle_scope(args.GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 String::Utf8Value file(args[i]);
593 if (*file == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594 Throw(args.GetIsolate(), "Error loading file");
595 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 Handle<String> source = ReadFile(args.GetIsolate(), *file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 if (source.IsEmpty()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000599 Throw(args.GetIsolate(), "Error loading file");
600 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602 if (!ExecuteString(args.GetIsolate(),
603 source,
604 String::NewFromUtf8(args.GetIsolate(), *file),
605 false,
606 true)) {
607 Throw(args.GetIsolate(), "Error executing file");
608 return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 }
610 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100611}
612
613
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000614void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000615 int exit_code = args[0]->Int32Value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400616 OnExit(args.GetIsolate());
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 exit(exit_code);
Steve Blocka7e24c12009-10-30 11:49:00 +0000618}
619
620
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000621void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
622 args.GetReturnValue().Set(
623 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000624}
625
626
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) {
628 HandleScope handle_scope(isolate);
629#ifndef V8_SHARED
630 Handle<Context> utility_context;
631 bool enter_context = !isolate->InContext();
632 if (enter_context) {
633 utility_context = Local<Context>::New(isolate, utility_context_);
634 utility_context->Enter();
635 }
636#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000637 v8::String::Utf8Value exception(try_catch->Exception());
638 const char* exception_string = ToCString(exception);
639 Handle<Message> message = try_catch->Message();
640 if (message.IsEmpty()) {
641 // V8 didn't provide any extra information about this error; just
642 // print the exception.
643 printf("%s\n", exception_string);
644 } else {
645 // Print (filename):(line number): (message).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName());
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 const char* filename_string = ToCString(filename);
648 int linenum = message->GetLineNumber();
649 printf("%s:%i: %s\n", filename_string, linenum, exception_string);
650 // Print line of source code.
651 v8::String::Utf8Value sourceline(message->GetSourceLine());
652 const char* sourceline_string = ToCString(sourceline);
653 printf("%s\n", sourceline_string);
654 // Print wavy underline (GetUnderline is deprecated).
655 int start = message->GetStartColumn();
656 for (int i = 0; i < start; i++) {
657 printf(" ");
658 }
659 int end = message->GetEndColumn();
660 for (int i = start; i < end; i++) {
661 printf("^");
662 }
663 printf("\n");
Ben Murdoch257744e2011-11-30 15:57:28 +0000664 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
665 if (stack_trace.length() > 0) {
666 const char* stack_trace_string = ToCString(stack_trace);
667 printf("%s\n", stack_trace_string);
668 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000669 }
Ben Murdoch589d6972011-11-30 16:04:58 +0000670 printf("\n");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000671#ifndef V8_SHARED
672 if (enter_context) utility_context->Exit();
673#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +0000674}
675
676
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000677#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678Handle<Array> Shell::GetCompletions(Isolate* isolate,
679 Handle<String> text,
680 Handle<String> full) {
681 EscapableHandleScope handle_scope(isolate);
682 v8::Local<v8::Context> utility_context =
683 v8::Local<v8::Context>::New(isolate, utility_context_);
684 v8::Context::Scope context_scope(utility_context);
685 Handle<Object> global = utility_context->Global();
686 Local<Value> fun =
687 global->Get(String::NewFromUtf8(isolate, "GetCompletions"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 static const int kArgc = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 v8::Local<v8::Context> evaluation_context =
690 v8::Local<v8::Context>::New(isolate, evaluation_context_);
691 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full };
692 Local<Value> val = Local<Function>::Cast(fun)->Call(global, kArgc, argv);
693 return handle_scope.Escape(Local<Array>::Cast(val));
Steve Blocka7e24c12009-10-30 11:49:00 +0000694}
695
696
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000697Local<Object> Shell::DebugMessageDetails(Isolate* isolate,
698 Handle<String> message) {
699 EscapableHandleScope handle_scope(isolate);
700 v8::Local<v8::Context> context =
701 v8::Local<v8::Context>::New(isolate, utility_context_);
702 v8::Context::Scope context_scope(context);
703 Handle<Object> global = context->Global();
704 Handle<Value> fun =
705 global->Get(String::NewFromUtf8(isolate, "DebugMessageDetails"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000706 static const int kArgc = 1;
707 Handle<Value> argv[kArgc] = { message };
708 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 return handle_scope.Escape(Local<Object>(Handle<Object>::Cast(val)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000710}
711
712
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000713Local<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate,
714 Handle<String> command) {
715 EscapableHandleScope handle_scope(isolate);
716 v8::Local<v8::Context> context =
717 v8::Local<v8::Context>::New(isolate, utility_context_);
718 v8::Context::Scope context_scope(context);
719 Handle<Object> global = context->Global();
720 Handle<Value> fun =
721 global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest"));
Steve Blocka7e24c12009-10-30 11:49:00 +0000722 static const int kArgc = 1;
723 Handle<Value> argv[kArgc] = { command };
724 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000725 return handle_scope.Escape(Local<Value>(val));
Steve Blocka7e24c12009-10-30 11:49:00 +0000726}
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100727
728
Steve Blocka7e24c12009-10-30 11:49:00 +0000729int32_t* Counter::Bind(const char* name, bool is_histogram) {
730 int i;
731 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
732 name_[i] = static_cast<char>(name[i]);
733 name_[i] = '\0';
734 is_histogram_ = is_histogram;
735 return ptr();
736}
737
738
739void Counter::AddSample(int32_t sample) {
740 count_++;
741 sample_total_ += sample;
742}
743
744
745CounterCollection::CounterCollection() {
746 magic_number_ = 0xDEADFACE;
747 max_counters_ = kMaxCounters;
748 max_name_size_ = Counter::kMaxNameSize;
749 counters_in_use_ = 0;
750}
751
752
753Counter* CounterCollection::GetNextCounter() {
754 if (counters_in_use_ == kMaxCounters) return NULL;
755 return &counters_[counters_in_use_++];
756}
757
758
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000759void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
760 counters_file_ = base::OS::MemoryMappedFile::create(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000761 name, sizeof(CounterCollection), &local_counters_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000762 void* memory = (counters_file_ == NULL) ?
763 NULL : counters_file_->memory();
764 if (memory == NULL) {
765 printf("Could not map counters file %s\n", name);
Ben Murdoch589d6972011-11-30 16:04:58 +0000766 Exit(1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000767 }
768 counters_ = static_cast<CounterCollection*>(memory);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 isolate->SetCounterFunction(LookupCounter);
770 isolate->SetCreateHistogramFunction(CreateHistogram);
771 isolate->SetAddHistogramSampleFunction(AddHistogramSample);
Steve Blocka7e24c12009-10-30 11:49:00 +0000772}
773
774
775int CounterMap::Hash(const char* name) {
776 int h = 0;
777 int c;
778 while ((c = *name++) != 0) {
779 h += h << 5;
780 h += c;
781 }
782 return h;
783}
784
785
786Counter* Shell::GetCounter(const char* name, bool is_histogram) {
787 Counter* counter = counter_map_->Lookup(name);
788
789 if (counter == NULL) {
790 counter = counters_->GetNextCounter();
791 if (counter != NULL) {
792 counter_map_->Set(name, counter);
793 counter->Bind(name, is_histogram);
794 }
795 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000796 DCHECK(counter->is_histogram() == is_histogram);
Steve Blocka7e24c12009-10-30 11:49:00 +0000797 }
798 return counter;
799}
800
801
802int* Shell::LookupCounter(const char* name) {
803 Counter* counter = GetCounter(name, false);
804
805 if (counter != NULL) {
806 return counter->ptr();
807 } else {
808 return NULL;
809 }
810}
811
812
813void* Shell::CreateHistogram(const char* name,
814 int min,
815 int max,
816 size_t buckets) {
817 return GetCounter(name, true);
818}
819
820
821void Shell::AddHistogramSample(void* histogram, int sample) {
822 Counter* counter = reinterpret_cast<Counter*>(histogram);
823 counter->AddSample(sample);
824}
825
826
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827void Shell::InstallUtilityScript(Isolate* isolate) {
828 HandleScope scope(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000829 // If we use the utility context, we have to set the security tokens so that
830 // utility, evaluation and debug context can all access each other.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000831 v8::Local<v8::Context> utility_context =
832 v8::Local<v8::Context>::New(isolate, utility_context_);
833 v8::Local<v8::Context> evaluation_context =
834 v8::Local<v8::Context>::New(isolate, evaluation_context_);
835 utility_context->SetSecurityToken(Undefined(isolate));
836 evaluation_context->SetSecurityToken(Undefined(isolate));
837 v8::Context::Scope context_scope(utility_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000838
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100839 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000840 // Install the debugger object in the utility scope
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000841 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug();
Steve Block44f0eee2011-05-26 01:26:41 +0100842 debug->Load();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000843 i::Handle<i::Context> debug_context = debug->debug_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100844 i::Handle<i::JSObject> js_debug
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000845 = i::Handle<i::JSObject>(debug_context->global_object());
846 utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"),
847 Utils::ToLocal(js_debug));
848 debug_context->set_security_token(
849 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000850
851 // Run the d8 shell utility script in the utility context
852 int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000853 i::Vector<const char> shell_source =
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400854 i::NativesCollection<i::D8>::GetScriptSource(source_index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000855 i::Vector<const char> shell_source_name =
856 i::NativesCollection<i::D8>::GetScriptName(source_index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000857 Handle<String> source =
858 String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString,
859 shell_source.length());
860 Handle<String> name =
861 String::NewFromUtf8(isolate, shell_source_name.start(),
862 String::kNormalString, shell_source_name.length());
863 ScriptOrigin origin(name);
864 Handle<Script> script = Script::Compile(source, &origin);
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 script->Run();
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 // Mark the d8 shell script as native to avoid it showing up as normal source
867 // in the debugger.
Steve Block6ded16b2010-05-10 14:33:55 +0100868 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script);
869 i::Handle<i::Script> script_object = compiled_script->IsJSFunction()
870 ? i::Handle<i::Script>(i::Script::cast(
871 i::JSFunction::cast(*compiled_script)->shared()->script()))
872 : i::Handle<i::Script>(i::Script::cast(
873 i::SharedFunctionInfo::cast(*compiled_script)->script()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000874 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE));
875
Steve Blocka7e24c12009-10-30 11:49:00 +0000876 // Start the in-process debugger if requested.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000877 if (i::FLAG_debugger) v8::Debug::SetDebugEventListener(HandleDebugEvent);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000878}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000879#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000880
881
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000882Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
883 Handle<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
884 global_template->Set(String::NewFromUtf8(isolate, "print"),
885 FunctionTemplate::New(isolate, Print));
886 global_template->Set(String::NewFromUtf8(isolate, "write"),
887 FunctionTemplate::New(isolate, Write));
888 global_template->Set(String::NewFromUtf8(isolate, "read"),
889 FunctionTemplate::New(isolate, Read));
890 global_template->Set(String::NewFromUtf8(isolate, "readbuffer"),
891 FunctionTemplate::New(isolate, ReadBuffer));
892 global_template->Set(String::NewFromUtf8(isolate, "readline"),
893 FunctionTemplate::New(isolate, ReadLine));
894 global_template->Set(String::NewFromUtf8(isolate, "load"),
895 FunctionTemplate::New(isolate, Load));
896 global_template->Set(String::NewFromUtf8(isolate, "quit"),
897 FunctionTemplate::New(isolate, Quit));
898 global_template->Set(String::NewFromUtf8(isolate, "version"),
899 FunctionTemplate::New(isolate, Version));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000900
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 // Bind the Realm object.
902 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(isolate);
903 realm_template->Set(String::NewFromUtf8(isolate, "current"),
904 FunctionTemplate::New(isolate, RealmCurrent));
905 realm_template->Set(String::NewFromUtf8(isolate, "owner"),
906 FunctionTemplate::New(isolate, RealmOwner));
907 realm_template->Set(String::NewFromUtf8(isolate, "global"),
908 FunctionTemplate::New(isolate, RealmGlobal));
909 realm_template->Set(String::NewFromUtf8(isolate, "create"),
910 FunctionTemplate::New(isolate, RealmCreate));
911 realm_template->Set(String::NewFromUtf8(isolate, "dispose"),
912 FunctionTemplate::New(isolate, RealmDispose));
913 realm_template->Set(String::NewFromUtf8(isolate, "switch"),
914 FunctionTemplate::New(isolate, RealmSwitch));
915 realm_template->Set(String::NewFromUtf8(isolate, "eval"),
916 FunctionTemplate::New(isolate, RealmEval));
917 realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"),
918 RealmSharedGet, RealmSharedSet);
919 global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000920
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921#ifndef V8_SHARED
922 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
923 performance_template->Set(String::NewFromUtf8(isolate, "now"),
924 FunctionTemplate::New(isolate, PerformanceNow));
925 global_template->Set(String::NewFromUtf8(isolate, "performance"),
926 performance_template);
927#endif // !V8_SHARED
928
929 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(isolate);
930 AddOSMethods(isolate, os_templ);
931 global_template->Set(String::NewFromUtf8(isolate, "os"), os_templ);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000932
933 return global_template;
Steve Blocka7e24c12009-10-30 11:49:00 +0000934}
935
936
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937void Shell::Initialize(Isolate* isolate) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000938#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000939 Shell::counter_map_ = new CounterMap();
940 // Set up counters
941 if (i::StrLength(i::FLAG_map_counters) != 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 MapCounters(isolate, i::FLAG_map_counters);
943 if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
944 isolate->SetCounterFunction(LookupCounter);
945 isolate->SetCreateHistogramFunction(CreateHistogram);
946 isolate->SetAddHistogramSampleFunction(AddHistogramSample);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000947 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000949}
950
951
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952void Shell::InitializeDebugger(Isolate* isolate) {
953 if (options.test_shell) return;
954#ifndef V8_SHARED
955 HandleScope scope(isolate);
956 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
957 utility_context_.Reset(isolate,
958 Context::New(isolate, NULL, global_template));
959#endif // !V8_SHARED
960}
961
962
963Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000964#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000965 // This needs to be a critical section since this is not thread-safe
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000966 base::LockGuard<base::Mutex> lock_guard(&context_mutex_);
967#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000968 // Initialize the global objects
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000969 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
970 EscapableHandleScope handle_scope(isolate);
971 Local<Context> context = Context::New(isolate, NULL, global_template);
972 DCHECK(!context.IsEmpty());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000973 Context::Scope scope(context);
974
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000975#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000976 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000977 i::JSArguments js_args = i::FLAG_js_arguments;
978 i::Handle<i::FixedArray> arguments_array =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 factory->NewFixedArray(js_args.argc);
980 for (int j = 0; j < js_args.argc; j++) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000981 i::Handle<i::String> arg =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982 factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000983 arguments_array->set(j, *arg);
984 }
985 i::Handle<i::JSArray> arguments_jsarray =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000986 factory->NewJSArrayWithElements(arguments_array);
987 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000988 Utils::ToLocal(arguments_jsarray));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989#endif // !V8_SHARED
990 return handle_scope.Escape(context);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000991}
992
993
Ben Murdoch589d6972011-11-30 16:04:58 +0000994void Shell::Exit(int exit_code) {
995 // Use _exit instead of exit to avoid races between isolate
996 // threads and static destructors.
997 fflush(stdout);
998 fflush(stderr);
999 _exit(exit_code);
1000}
1001
1002
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001003#ifndef V8_SHARED
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001004struct CounterAndKey {
1005 Counter* counter;
1006 const char* key;
1007};
1008
1009
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010inline bool operator<(const CounterAndKey& lhs, const CounterAndKey& rhs) {
1011 return strcmp(lhs.key, rhs.key) < 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001012}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013#endif // !V8_SHARED
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001014
1015
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001016void Shell::OnExit(v8::Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 LineEditor* line_editor = LineEditor::Get();
1018 if (line_editor) line_editor->Close();
1019#ifndef V8_SHARED
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001020 reinterpret_cast<i::Isolate*>(isolate)->DumpAndResetCompilationStats();
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 if (i::FLAG_dump_counters) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001022 int number_of_counters = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00001023 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001024 number_of_counters++;
1025 }
1026 CounterAndKey* counters = new CounterAndKey[number_of_counters];
1027 int j = 0;
1028 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) {
1029 counters[j].counter = i.CurrentValue();
1030 counters[j].key = i.CurrentKey();
1031 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 std::sort(counters, counters + number_of_counters);
1033 printf("+----------------------------------------------------------------+"
1034 "-------------+\n");
1035 printf("| Name |"
1036 " Value |\n");
1037 printf("+----------------------------------------------------------------+"
1038 "-------------+\n");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001039 for (j = 0; j < number_of_counters; j++) {
1040 Counter* counter = counters[j].counter;
1041 const char* key = counters[j].key;
Steve Blocka7e24c12009-10-30 11:49:00 +00001042 if (counter->is_histogram()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043 printf("| c:%-60s | %11i |\n", key, counter->count());
1044 printf("| t:%-60s | %11i |\n", key, counter->sample_total());
Steve Blocka7e24c12009-10-30 11:49:00 +00001045 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001046 printf("| %-62s | %11i |\n", key, counter->count());
Steve Blocka7e24c12009-10-30 11:49:00 +00001047 }
1048 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 printf("+----------------------------------------------------------------+"
1050 "-------------+\n");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001051 delete [] counters;
Steve Blocka7e24c12009-10-30 11:49:00 +00001052 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053 delete counters_file_;
1054 delete counter_map_;
1055#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +00001056}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001058
1059
1060static FILE* FOpen(const char* path, const char* mode) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001061#if defined(_MSC_VER) && (defined(_WIN32) || defined(_WIN64))
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001062 FILE* result;
1063 if (fopen_s(&result, path, mode) == 0) {
1064 return result;
1065 } else {
1066 return NULL;
1067 }
1068#else
1069 FILE* file = fopen(path, mode);
1070 if (file == NULL) return NULL;
1071 struct stat file_stat;
1072 if (fstat(fileno(file), &file_stat) != 0) return NULL;
1073 bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0);
1074 if (is_regular_file) return file;
1075 fclose(file);
1076 return NULL;
1077#endif
1078}
Steve Blocka7e24c12009-10-30 11:49:00 +00001079
1080
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001081static char* ReadChars(Isolate* isolate, const char* name, int* size_out) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001082 FILE* file = FOpen(name, "rb");
Steve Blocka7e24c12009-10-30 11:49:00 +00001083 if (file == NULL) return NULL;
1084
1085 fseek(file, 0, SEEK_END);
1086 int size = ftell(file);
1087 rewind(file);
1088
1089 char* chars = new char[size + 1];
1090 chars[size] = '\0';
1091 for (int i = 0; i < size;) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001092 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
Steve Blocka7e24c12009-10-30 11:49:00 +00001093 i += read;
1094 }
1095 fclose(file);
1096 *size_out = size;
1097 return chars;
1098}
1099
1100
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001101struct DataAndPersistent {
1102 uint8_t* data;
1103 Persistent<ArrayBuffer> handle;
1104};
1105
1106
1107static void ReadBufferWeakCallback(
1108 const v8::WeakCallbackData<ArrayBuffer, DataAndPersistent>& data) {
1109 size_t byte_length = data.GetValue()->ByteLength();
1110 data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory(
1111 -static_cast<intptr_t>(byte_length));
1112
1113 delete[] data.GetParameter()->data;
1114 data.GetParameter()->handle.Reset();
1115 delete data.GetParameter();
1116}
1117
1118
1119void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
1120 DCHECK(sizeof(char) == sizeof(uint8_t)); // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001121 String::Utf8Value filename(args[0]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001122 int length;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001123 if (*filename == NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124 Throw(args.GetIsolate(), "Error loading file");
1125 return;
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 }
1127
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128 Isolate* isolate = args.GetIsolate();
1129 DataAndPersistent* data = new DataAndPersistent;
1130 data->data = reinterpret_cast<uint8_t*>(
1131 ReadChars(args.GetIsolate(), *filename, &length));
1132 if (data->data == NULL) {
1133 delete data;
1134 Throw(args.GetIsolate(), "Error reading file");
1135 return;
1136 }
1137 Handle<v8::ArrayBuffer> buffer =
1138 ArrayBuffer::New(isolate, data->data, length);
1139 data->handle.Reset(isolate, buffer);
1140 data->handle.SetWeak(data, ReadBufferWeakCallback);
1141 data->handle.MarkIndependent();
1142 isolate->AdjustAmountOfExternalAllocatedMemory(length);
1143
1144 args.GetReturnValue().Set(buffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00001145}
1146
1147
Steve Blocka7e24c12009-10-30 11:49:00 +00001148// Reads a file into a v8 string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001150 int size = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001151 char* chars = ReadChars(isolate, name, &size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001152 if (chars == NULL) return Handle<String>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153 Handle<String> result =
1154 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
Steve Blocka7e24c12009-10-30 11:49:00 +00001155 delete[] chars;
1156 return result;
1157}
1158
1159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160void Shell::RunShell(Isolate* isolate) {
1161 HandleScope outer_scope(isolate);
1162 v8::Local<v8::Context> context =
1163 v8::Local<v8::Context>::New(isolate, evaluation_context_);
1164 v8::Context::Scope context_scope(context);
1165 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1166 Handle<String> name = String::NewFromUtf8(isolate, "(d8)");
1167 LineEditor* console = LineEditor::Get();
Ben Murdoch589d6972011-11-30 16:04:58 +00001168 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001169 console->Open(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00001170 while (true) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001171 HandleScope inner_scope(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001172 Handle<String> input = console->Prompt(Shell::kPrompt);
1173 if (input.IsEmpty()) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001174 ExecuteString(isolate, input, name, true, true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001175 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001176 printf("\n");
1177}
1178
1179
Ben Murdoch589d6972011-11-30 16:04:58 +00001180SourceGroup::~SourceGroup() {
1181#ifndef V8_SHARED
Ben Murdoch589d6972011-11-30 16:04:58 +00001182 delete thread_;
1183 thread_ = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001184#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001185}
Steve Blocka7e24c12009-10-30 11:49:00 +00001186
Steve Blocka7e24c12009-10-30 11:49:00 +00001187
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001188void SourceGroup::Execute(Isolate* isolate) {
1189 bool exception_was_thrown = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001190 for (int i = begin_offset_; i < end_offset_; ++i) {
1191 const char* arg = argv_[i];
1192 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
1193 // Execute argument given to -e option directly.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001194 HandleScope handle_scope(isolate);
1195 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed");
1196 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]);
1197 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1198 exception_was_thrown = true;
1199 break;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001200 }
1201 ++i;
1202 } else if (arg[0] == '-') {
1203 // Ignore other options. They have been parsed already.
1204 } else {
1205 // Use all other arguments as names of files to load and run.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001206 HandleScope handle_scope(isolate);
1207 Handle<String> file_name = String::NewFromUtf8(isolate, arg);
1208 Handle<String> source = ReadFile(isolate, arg);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001209 if (source.IsEmpty()) {
1210 printf("Error reading '%s'\n", arg);
Ben Murdoch589d6972011-11-30 16:04:58 +00001211 Shell::Exit(1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001212 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1214 exception_was_thrown = true;
1215 break;
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 }
1217 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001218 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 if (exception_was_thrown != Shell::options.expected_to_throw) {
1220 Shell::Exit(1);
1221 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001222}
Steve Blocka7e24c12009-10-30 11:49:00 +00001223
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001224
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001225Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001226 int size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227 char* chars = ReadChars(isolate, name, &size);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001228 if (chars == NULL) return Handle<String>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001229 Handle<String> result =
1230 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001231 delete[] chars;
1232 return result;
1233}
1234
1235
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001236#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001237base::Thread::Options SourceGroup::GetThreadOptions() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001238 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
1239 // which is not enough to parse the big literal expressions used in tests.
1240 // The stack size should be at least StackGuard::kLimitSize + some
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001241 // OS-specific padding for thread startup code. 2Mbytes seems to be enough.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001242 return base::Thread::Options("IsolateThread", 2 * MB);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001243}
1244
1245
1246void SourceGroup::ExecuteInThread() {
1247 Isolate* isolate = Isolate::New();
1248 do {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 next_semaphore_.Wait();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001250 {
1251 Isolate::Scope iscope(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001252 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001253 HandleScope scope(isolate);
1254 PerIsolateData data(isolate);
1255 Local<Context> context = Shell::CreateEvaluationContext(isolate);
1256 {
1257 Context::Scope cscope(context);
1258 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1259 Execute(isolate);
1260 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001261 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001262 if (Shell::options.send_idle_notification) {
1263 const int kLongIdlePauseInMs = 1000;
1264 isolate->ContextDisposedNotification();
1265 isolate->IdleNotification(kLongIdlePauseInMs);
1266 }
1267 if (Shell::options.invoke_weak_callbacks) {
1268 // By sending a low memory notifications, we will try hard to collect
1269 // all garbage and will therefore also invoke all weak callbacks of
1270 // actually unreachable persistent handles.
1271 isolate->LowMemoryNotification();
1272 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001273 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274 done_semaphore_.Signal();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001275 } while (!Shell::options.last_run);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001276
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001277 isolate->Dispose();
1278}
1279
1280
1281void SourceGroup::StartExecuteInThread() {
1282 if (thread_ == NULL) {
1283 thread_ = new IsolateThread(this);
1284 thread_->Start();
1285 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001286 next_semaphore_.Signal();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001287}
1288
1289
1290void SourceGroup::WaitForThread() {
1291 if (thread_ == NULL) return;
1292 if (Shell::options.last_run) {
1293 thread_->Join();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001294 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001295 done_semaphore_.Wait();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001296 }
1297}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001298#endif // !V8_SHARED
1299
1300
1301void SetFlagsFromString(const char* flags) {
1302 v8::V8::SetFlagsFromString(flags, static_cast<int>(strlen(flags)));
1303}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001304
1305
1306bool Shell::SetOptions(int argc, char* argv[]) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001307 bool logfile_per_isolate = false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001308 for (int i = 0; i < argc; i++) {
1309 if (strcmp(argv[i], "--stress-opt") == 0) {
1310 options.stress_opt = true;
1311 argv[i] = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 } else if (strcmp(argv[i], "--nostress-opt") == 0) {
1313 options.stress_opt = false;
1314 argv[i] = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001315 } else if (strcmp(argv[i], "--stress-deopt") == 0) {
1316 options.stress_deopt = true;
1317 argv[i] = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318 } else if (strcmp(argv[i], "--mock-arraybuffer-allocator") == 0) {
1319 options.mock_arraybuffer_allocator = true;
1320 argv[i] = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001321 } else if (strcmp(argv[i], "--noalways-opt") == 0) {
1322 // No support for stressing if we can't use --always-opt.
1323 options.stress_opt = false;
1324 options.stress_deopt = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001325 } else if (strcmp(argv[i], "--logfile-per-isolate") == 0) {
1326 logfile_per_isolate = true;
1327 argv[i] = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001328 } else if (strcmp(argv[i], "--shell") == 0) {
1329 options.interactive_shell = true;
1330 argv[i] = NULL;
1331 } else if (strcmp(argv[i], "--test") == 0) {
1332 options.test_shell = true;
1333 argv[i] = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334 } else if (strcmp(argv[i], "--send-idle-notification") == 0) {
1335 options.send_idle_notification = true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001336 argv[i] = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337 } else if (strcmp(argv[i], "--invoke-weak-callbacks") == 0) {
1338 options.invoke_weak_callbacks = true;
1339 // TODO(jochen) See issue 3351
1340 options.send_idle_notification = true;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001341 argv[i] = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001342 } else if (strcmp(argv[i], "-f") == 0) {
1343 // Ignore any -f flags for compatibility with other stand-alone
1344 // JavaScript engines.
1345 continue;
1346 } else if (strcmp(argv[i], "--isolate") == 0) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001347#ifdef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001348 printf("D8 with shared library does not support multi-threading\n");
1349 return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001350#endif // V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001351 options.num_isolates++;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352 } else if (strcmp(argv[i], "--dump-heap-constants") == 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001353#ifdef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001354 printf("D8 with shared library does not support constant dumping\n");
Ben Murdoch589d6972011-11-30 16:04:58 +00001355 return false;
1356#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001357 options.dump_heap_constants = true;
1358 argv[i] = NULL;
Ben Murdoch589d6972011-11-30 16:04:58 +00001359#endif // V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001360 } else if (strcmp(argv[i], "--throws") == 0) {
1361 options.expected_to_throw = true;
1362 argv[i] = NULL;
1363 } else if (strncmp(argv[i], "--icu-data-file=", 16) == 0) {
1364 options.icu_data_file = argv[i] + 16;
1365 argv[i] = NULL;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001366#ifdef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001367 } else if (strcmp(argv[i], "--dump-counters") == 0) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001368 printf("D8 with shared library does not include counters\n");
1369 return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001370 } else if (strcmp(argv[i], "--debugger") == 0) {
1371 printf("Javascript debugger not included\n");
1372 return false;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001373#endif // V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001374#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1375 } else if (strncmp(argv[i], "--natives_blob=", 15) == 0) {
1376 options.natives_blob = argv[i] + 15;
1377 argv[i] = NULL;
1378 } else if (strncmp(argv[i], "--snapshot_blob=", 16) == 0) {
1379 options.snapshot_blob = argv[i] + 16;
1380 argv[i] = NULL;
1381#endif // V8_USE_EXTERNAL_STARTUP_DATA
1382 } else if (strcmp(argv[i], "--cache") == 0 ||
1383 strncmp(argv[i], "--cache=", 8) == 0) {
1384 const char* value = argv[i] + 7;
1385 if (!*value || strncmp(value, "=code", 6) == 0) {
1386 options.compile_options = v8::ScriptCompiler::kProduceCodeCache;
1387 } else if (strncmp(value, "=parse", 7) == 0) {
1388 options.compile_options = v8::ScriptCompiler::kProduceParserCache;
1389 } else if (strncmp(value, "=none", 6) == 0) {
1390 options.compile_options = v8::ScriptCompiler::kNoCompileOptions;
1391 } else {
1392 printf("Unknown option to --cache.\n");
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001393 return false;
1394 }
1395 argv[i] = NULL;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001396 }
1397 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001398
1399 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
1400
Ben Murdoch589d6972011-11-30 16:04:58 +00001401 // Set up isolated source groups.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001402 options.isolate_sources = new SourceGroup[options.num_isolates];
1403 SourceGroup* current = options.isolate_sources;
1404 current->Begin(argv, 1);
1405 for (int i = 1; i < argc; i++) {
1406 const char* str = argv[i];
1407 if (strcmp(str, "--isolate") == 0) {
1408 current->End(i);
1409 current++;
1410 current->Begin(argv, i + 1);
1411 } else if (strncmp(argv[i], "--", 2) == 0) {
1412 printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]);
1413 }
1414 }
1415 current->End(argc);
1416
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001417 if (!logfile_per_isolate && options.num_isolates) {
1418 SetFlagsFromString("--nologfile_per_isolate");
1419 }
1420
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001421 return true;
1422}
1423
1424
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001425int Shell::RunMain(Isolate* isolate, int argc, char* argv[]) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001426#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001427 for (int i = 1; i < options.num_isolates; ++i) {
1428 options.isolate_sources[i].StartExecuteInThread();
1429 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001430#endif // !V8_SHARED
1431 {
1432 HandleScope scope(isolate);
1433 Local<Context> context = CreateEvaluationContext(isolate);
1434 if (options.last_run && options.use_interactive_shell()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001435 // Keep using the same context in the interactive shell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001436 evaluation_context_.Reset(isolate, context);
1437#ifndef V8_SHARED
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001438 // If the interactive debugger is enabled make sure to activate
1439 // it before running the files passed on the command line.
1440 if (i::FLAG_debugger) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001441 InstallUtilityScript(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001442 }
1443#endif // !V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001444 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 {
1446 Context::Scope cscope(context);
1447 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1448 options.isolate_sources[0].Execute(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001449 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450 }
1451 if (options.send_idle_notification) {
1452 const int kLongIdlePauseInMs = 1000;
1453 isolate->ContextDisposedNotification();
1454 isolate->IdleNotification(kLongIdlePauseInMs);
1455 }
1456 if (options.invoke_weak_callbacks) {
1457 // By sending a low memory notifications, we will try hard to collect all
1458 // garbage and will therefore also invoke all weak callbacks of actually
1459 // unreachable persistent handles.
1460 isolate->LowMemoryNotification();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001461 }
1462
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001463#ifndef V8_SHARED
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001464 for (int i = 1; i < options.num_isolates; ++i) {
1465 options.isolate_sources[i].WaitForThread();
1466 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467#endif // !V8_SHARED
Steve Blocka7e24c12009-10-30 11:49:00 +00001468 return 0;
1469}
1470
1471
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001472#ifndef V8_SHARED
1473static void DumpHeapConstants(i::Isolate* isolate) {
1474 i::Heap* heap = isolate->heap();
1475
1476 // Dump the INSTANCE_TYPES table to the console.
1477 printf("# List of known V8 instance types.\n");
1478#define DUMP_TYPE(T) printf(" %d: \"%s\",\n", i::T, #T);
1479 printf("INSTANCE_TYPES = {\n");
1480 INSTANCE_TYPE_LIST(DUMP_TYPE)
1481 printf("}\n");
1482#undef DUMP_TYPE
1483
1484 // Dump the KNOWN_MAP table to the console.
1485 printf("\n# List of known V8 maps.\n");
1486#define ROOT_LIST_CASE(type, name, camel_name) \
1487 if (n == NULL && o == heap->name()) n = #camel_name;
1488#define STRUCT_LIST_CASE(upper_name, camel_name, name) \
1489 if (n == NULL && o == heap->name##_map()) n = #camel_name "Map";
1490 i::HeapObjectIterator it(heap->map_space());
1491 printf("KNOWN_MAPS = {\n");
1492 for (i::Object* o = it.Next(); o != NULL; o = it.Next()) {
1493 i::Map* m = i::Map::cast(o);
1494 const char* n = NULL;
1495 intptr_t p = reinterpret_cast<intptr_t>(m) & 0xfffff;
1496 int t = m->instance_type();
1497 ROOT_LIST(ROOT_LIST_CASE)
1498 STRUCT_LIST(STRUCT_LIST_CASE)
1499 if (n == NULL) continue;
1500 printf(" 0x%05" V8PRIxPTR ": (%d, \"%s\"),\n", p, t, n);
1501 }
1502 printf("}\n");
1503#undef STRUCT_LIST_CASE
1504#undef ROOT_LIST_CASE
1505
1506 // Dump the KNOWN_OBJECTS table to the console.
1507 printf("\n# List of known V8 objects.\n");
1508#define ROOT_LIST_CASE(type, name, camel_name) \
1509 if (n == NULL && o == heap->name()) n = #camel_name;
1510 i::OldSpaces spit(heap);
1511 printf("KNOWN_OBJECTS = {\n");
1512 for (i::PagedSpace* s = spit.next(); s != NULL; s = spit.next()) {
1513 i::HeapObjectIterator it(s);
1514 const char* sname = AllocationSpaceName(s->identity());
1515 for (i::Object* o = it.Next(); o != NULL; o = it.Next()) {
1516 const char* n = NULL;
1517 intptr_t p = reinterpret_cast<intptr_t>(o) & 0xfffff;
1518 ROOT_LIST(ROOT_LIST_CASE)
1519 if (n == NULL) continue;
1520 printf(" (\"%s\", 0x%05" V8PRIxPTR "): \"%s\",\n", sname, p, n);
1521 }
1522 }
1523 printf("}\n");
1524#undef ROOT_LIST_CASE
1525}
1526#endif // !V8_SHARED
1527
1528
1529class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
1530 public:
1531 virtual void* Allocate(size_t length) {
1532 void* data = AllocateUninitialized(length);
1533 return data == NULL ? data : memset(data, 0, length);
1534 }
1535 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
1536 virtual void Free(void* data, size_t) { free(data); }
1537};
1538
1539
1540class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
1541 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001542 void* Allocate(size_t) OVERRIDE { return malloc(0); }
1543 void* AllocateUninitialized(size_t length) OVERRIDE { return malloc(0); }
1544 void Free(void* p, size_t) OVERRIDE { free(p); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001545};
1546
1547
1548#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1549class StartupDataHandler {
1550 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001551 StartupDataHandler(const char* exec_path, const char* natives_blob,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001552 const char* snapshot_blob) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001553 // If we have (at least one) explicitly given blob, use those.
1554 // If not, use the default blob locations next to the d8 binary.
1555 if (natives_blob || snapshot_blob) {
1556 LoadFromFiles(natives_blob, snapshot_blob);
1557 } else {
1558 char* natives;
1559 char* snapshot;
1560 LoadFromFiles(RelativePath(&natives, exec_path, "natives_blob.bin"),
1561 RelativePath(&snapshot, exec_path, "snapshot_blob.bin"));
1562
1563 free(natives);
1564 free(snapshot);
1565 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001566 }
1567
1568 ~StartupDataHandler() {
1569 delete[] natives_.data;
1570 delete[] snapshot_.data;
1571 }
1572
1573 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001574 static char* RelativePath(char** buffer, const char* exec_path,
1575 const char* name) {
1576 DCHECK(exec_path);
1577 const char* last_slash = strrchr(exec_path, '/');
1578 if (last_slash) {
1579 int after_slash = last_slash - exec_path + 1;
1580 int name_length = static_cast<int>(strlen(name));
1581 *buffer =
1582 reinterpret_cast<char*>(calloc(after_slash + name_length + 1, 1));
1583 strncpy(*buffer, exec_path, after_slash);
1584 strncat(*buffer, name, name_length);
1585 } else {
1586 *buffer = strdup(name);
1587 }
1588 return *buffer;
1589 }
1590
1591 void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
1592 Load(natives_blob, &natives_, v8::V8::SetNativesDataBlob);
1593 Load(snapshot_blob, &snapshot_, v8::V8::SetSnapshotDataBlob);
1594 }
1595
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001596 void Load(const char* blob_file,
1597 v8::StartupData* startup_data,
1598 void (*setter_fn)(v8::StartupData*)) {
1599 startup_data->data = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001600 startup_data->raw_size = 0;
1601
1602 if (!blob_file)
1603 return;
1604
1605 FILE* file = fopen(blob_file, "rb");
1606 if (!file)
1607 return;
1608
1609 fseek(file, 0, SEEK_END);
1610 startup_data->raw_size = ftell(file);
1611 rewind(file);
1612
1613 startup_data->data = new char[startup_data->raw_size];
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001614 int read_size =
1615 static_cast<int>(fread(const_cast<char*>(startup_data->data), 1,
1616 startup_data->raw_size, file));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001617 fclose(file);
1618
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001619 if (startup_data->raw_size == read_size) (*setter_fn)(startup_data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001620 }
1621
1622 v8::StartupData natives_;
1623 v8::StartupData snapshot_;
1624
1625 // Disallow copy & assign.
1626 StartupDataHandler(const StartupDataHandler& other);
1627 void operator=(const StartupDataHandler& other);
1628};
1629#endif // V8_USE_EXTERNAL_STARTUP_DATA
1630
1631
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001632int Shell::Main(int argc, char* argv[]) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001633#if (defined(_WIN32) || defined(_WIN64))
1634 UINT new_flags =
1635 SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
1636 UINT existing_flags = SetErrorMode(new_flags);
1637 SetErrorMode(existing_flags | new_flags);
1638#if defined(_MSC_VER)
1639 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1640 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
1641 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1642 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1643 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE);
1644 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
1645 _set_error_mode(_OUT_TO_STDERR);
1646#endif // defined(_MSC_VER)
1647#endif // defined(_WIN32) || defined(_WIN64)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001648 if (!SetOptions(argc, argv)) return 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001649 v8::V8::InitializeICU(options.icu_data_file);
1650 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
1651 v8::V8::InitializePlatform(platform);
1652 v8::V8::Initialize();
1653#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001654 StartupDataHandler startup_data(argv[0], options.natives_blob,
1655 options.snapshot_blob);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001656#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001657 SetFlagsFromString("--trace-hydrogen-file=hydrogen.cfg");
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001658 SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 SetFlagsFromString("--redirect-code-traces-to=code.asm");
1660 ShellArrayBufferAllocator array_buffer_allocator;
1661 MockArrayBufferAllocator mock_arraybuffer_allocator;
1662 if (options.mock_arraybuffer_allocator) {
1663 v8::V8::SetArrayBufferAllocator(&mock_arraybuffer_allocator);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001664 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001665 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001666 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001667 int result = 0;
1668 Isolate::CreateParams create_params;
1669#if !defined(V8_SHARED) && defined(ENABLE_GDB_JIT_INTERFACE)
1670 if (i::FLAG_gdbjit) {
1671 create_params.code_event_handler = i::GDBJITInterface::EventHandler;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001672 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001673#endif
1674#ifdef ENABLE_VTUNE_JIT_INTERFACE
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001675 create_params.code_event_handler = vTune::GetVtuneCodeEventHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001676#endif
1677#ifndef V8_SHARED
1678 create_params.constraints.ConfigureDefaults(
1679 base::SysInfo::AmountOfPhysicalMemory(),
1680 base::SysInfo::AmountOfVirtualMemory(),
1681 base::SysInfo::NumberOfProcessors());
1682#endif
1683 Isolate* isolate = Isolate::New(create_params);
1684 DumbLineEditor dumb_line_editor(isolate);
1685 {
1686 Isolate::Scope scope(isolate);
1687 Initialize(isolate);
1688 PerIsolateData data(isolate);
1689 InitializeDebugger(isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001690
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001691#ifndef V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001692 if (options.dump_heap_constants) {
1693 DumpHeapConstants(reinterpret_cast<i::Isolate*>(isolate));
1694 return 0;
1695 }
1696#endif
1697
1698 if (options.stress_opt || options.stress_deopt) {
1699 Testing::SetStressRunType(options.stress_opt
1700 ? Testing::kStressTypeOpt
1701 : Testing::kStressTypeDeopt);
1702 int stress_runs = Testing::GetStressRuns();
1703 for (int i = 0; i < stress_runs && result == 0; i++) {
1704 printf("============ Stress %d/%d ============\n", i + 1, stress_runs);
1705 Testing::PrepareStressRun(i);
1706 options.last_run = (i == stress_runs - 1);
1707 result = RunMain(isolate, argc, argv);
1708 }
1709 printf("======== Full Deoptimization =======\n");
1710 Testing::DeoptimizeAll();
1711#if !defined(V8_SHARED)
1712 } else if (i::FLAG_stress_runs > 0) {
1713 int stress_runs = i::FLAG_stress_runs;
1714 for (int i = 0; i < stress_runs && result == 0; i++) {
1715 printf("============ Run %d/%d ============\n", i + 1, stress_runs);
1716 options.last_run = (i == stress_runs - 1);
1717 result = RunMain(isolate, argc, argv);
1718 }
1719#endif
1720 } else {
1721 result = RunMain(isolate, argc, argv);
1722 }
1723
1724 // Run interactive shell if explicitly requested or if no script has been
1725 // executed, but never on --test
1726 if (options.use_interactive_shell()) {
1727#ifndef V8_SHARED
1728 if (!i::FLAG_debugger) {
1729 InstallUtilityScript(isolate);
1730 }
1731#endif // !V8_SHARED
1732 RunShell(isolate);
1733 }
1734 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001735 OnExit(isolate);
1736#ifndef V8_SHARED
1737 // Dump basic block profiling data.
1738 if (i::BasicBlockProfiler* profiler =
1739 reinterpret_cast<i::Isolate*>(isolate)->basic_block_profiler()) {
1740 i::OFStream os(stdout);
1741 os << *profiler;
1742 }
1743#endif // !V8_SHARED
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001744 isolate->Dispose();
1745 V8::Dispose();
1746 V8::ShutdownPlatform();
1747 delete platform;
1748
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001749 return result;
1750}
1751
Steve Blocka7e24c12009-10-30 11:49:00 +00001752} // namespace v8
1753
1754
Ben Murdoch257744e2011-11-30 15:57:28 +00001755#ifndef GOOGLE3
Steve Blocka7e24c12009-10-30 11:49:00 +00001756int main(int argc, char* argv[]) {
1757 return v8::Shell::Main(argc, argv);
1758}
Ben Murdoch257744e2011-11-30 15:57:28 +00001759#endif