Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(716)

Side by Side Diff: src/d8.cc

Issue 110573004: Merge bleeding_edge 17696:18016. (Closed) Base URL: https://v8.googlecode.com/svn/branches/experimental/parser
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 #ifdef V8_SHARED 43 #ifdef V8_SHARED
44 #include <assert.h> 44 #include <assert.h>
45 #endif // V8_SHARED 45 #endif // V8_SHARED
46 46
47 #ifndef V8_SHARED 47 #ifndef V8_SHARED
48 #include <algorithm> 48 #include <algorithm>
49 #endif // !V8_SHARED 49 #endif // !V8_SHARED
50 50
51 #ifdef V8_SHARED 51 #ifdef V8_SHARED
52 #include "../include/v8-defaults.h"
53 #include "../include/v8-testing.h" 52 #include "../include/v8-testing.h"
54 #endif // V8_SHARED 53 #endif // V8_SHARED
55 54
56 #ifdef ENABLE_VTUNE_JIT_INTERFACE 55 #ifdef ENABLE_VTUNE_JIT_INTERFACE
57 #include "third_party/vtune/v8-vtune.h" 56 #include "third_party/vtune/v8-vtune.h"
58 #endif 57 #endif
59 58
60 #include "d8.h" 59 #include "d8.h"
61 60
62 #ifndef V8_SHARED 61 #ifndef V8_SHARED
63 #include "api.h" 62 #include "api.h"
64 #include "checks.h" 63 #include "checks.h"
64 #include "cpu.h"
65 #include "d8-debug.h" 65 #include "d8-debug.h"
66 #include "debug.h" 66 #include "debug.h"
67 #include "natives.h" 67 #include "natives.h"
68 #include "platform.h" 68 #include "platform.h"
69 #include "v8.h" 69 #include "v8.h"
70 #include "v8-defaults.h"
71 #endif // V8_SHARED 70 #endif // V8_SHARED
72 71
73 #if !defined(_WIN32) && !defined(_WIN64) 72 #if !defined(_WIN32) && !defined(_WIN64)
74 #include <unistd.h> // NOLINT 73 #include <unistd.h> // NOLINT
75 #endif 74 #endif
76 75
77 #ifndef ASSERT 76 #ifndef ASSERT
78 #define ASSERT(condition) assert(condition) 77 #define ASSERT(condition) assert(condition)
79 #endif 78 #endif
80 79
81 namespace v8 { 80 namespace v8 {
82 81
83 82
84 static Handle<Value> Throw(const char* message) { 83 static Handle<Value> Throw(Isolate* isolate, const char* message) {
85 return ThrowException(String::New(message)); 84 return isolate->ThrowException(String::NewFromUtf8(isolate, message));
86 } 85 }
87 86
88 87
89 88
90 class PerIsolateData { 89 class PerIsolateData {
91 public: 90 public:
92 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) { 91 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) {
93 HandleScope scope(isolate); 92 HandleScope scope(isolate);
94 isolate->SetData(this); 93 isolate->SetData(0, this);
95 } 94 }
96 95
97 ~PerIsolateData() { 96 ~PerIsolateData() {
98 isolate_->SetData(NULL); // Not really needed, just to be sure... 97 isolate_->SetData(0, NULL); // Not really needed, just to be sure...
99 } 98 }
100 99
101 inline static PerIsolateData* Get(Isolate* isolate) { 100 inline static PerIsolateData* Get(Isolate* isolate) {
102 return reinterpret_cast<PerIsolateData*>(isolate->GetData()); 101 return reinterpret_cast<PerIsolateData*>(isolate->GetData(0));
103 } 102 }
104 103
105 class RealmScope { 104 class RealmScope {
106 public: 105 public:
107 explicit RealmScope(PerIsolateData* data); 106 explicit RealmScope(PerIsolateData* data);
108 ~RealmScope(); 107 ~RealmScope();
109 private: 108 private:
110 PerIsolateData* data_; 109 PerIsolateData* data_;
111 }; 110 };
112 111
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 fwrite(*str, sizeof(**str), str.length(), stdout); 237 fwrite(*str, sizeof(**str), str.length(), stdout);
239 printf("\n"); 238 printf("\n");
240 } 239 }
241 #if !defined(V8_SHARED) 240 #if !defined(V8_SHARED)
242 } else { 241 } else {
243 v8::TryCatch try_catch; 242 v8::TryCatch try_catch;
244 v8::Local<v8::Context> context = 243 v8::Local<v8::Context> context =
245 v8::Local<v8::Context>::New(isolate, utility_context_); 244 v8::Local<v8::Context>::New(isolate, utility_context_);
246 v8::Context::Scope context_scope(context); 245 v8::Context::Scope context_scope(context);
247 Handle<Object> global = context->Global(); 246 Handle<Object> global = context->Global();
248 Handle<Value> fun = global->Get(String::New("Stringify")); 247 Handle<Value> fun =
248 global->Get(String::NewFromUtf8(isolate, "Stringify"));
249 Handle<Value> argv[1] = { result }; 249 Handle<Value> argv[1] = { result };
250 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); 250 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv);
251 if (try_catch.HasCaught()) return true; 251 if (try_catch.HasCaught()) return true;
252 v8::String::Utf8Value str(s); 252 v8::String::Utf8Value str(s);
253 fwrite(*str, sizeof(**str), str.length(), stdout); 253 fwrite(*str, sizeof(**str), str.length(), stdout);
254 printf("\n"); 254 printf("\n");
255 } 255 }
256 #endif 256 #endif
257 } 257 }
258 return true; 258 return true;
259 } 259 }
260 } 260 }
261 } 261 }
262 262
263 263
264 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { 264 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
265 data_->realm_count_ = 1; 265 data_->realm_count_ = 1;
266 data_->realm_current_ = 0; 266 data_->realm_current_ = 0;
267 data_->realm_switch_ = 0; 267 data_->realm_switch_ = 0;
268 data_->realms_ = new Persistent<Context>[1]; 268 data_->realms_ = new Persistent<Context>[1];
269 data_->realms_[0].Reset(data_->isolate_, 269 data_->realms_[0].Reset(data_->isolate_,
270 data_->isolate_->GetEnteredContext()); 270 data_->isolate_->GetEnteredContext());
271 data_->realm_shared_.Clear();
272 } 271 }
273 272
274 273
275 PerIsolateData::RealmScope::~RealmScope() { 274 PerIsolateData::RealmScope::~RealmScope() {
276 // Drop realms to avoid keeping them alive. 275 // Drop realms to avoid keeping them alive.
277 for (int i = 0; i < data_->realm_count_; ++i) 276 for (int i = 0; i < data_->realm_count_; ++i)
278 data_->realms_[i].Dispose(); 277 data_->realms_[i].Reset();
279 delete[] data_->realms_; 278 delete[] data_->realms_;
280 if (!data_->realm_shared_.IsEmpty()) 279 if (!data_->realm_shared_.IsEmpty())
281 data_->realm_shared_.Dispose(); 280 data_->realm_shared_.Reset();
282 } 281 }
283 282
284 283
285 int PerIsolateData::RealmFind(Handle<Context> context) { 284 int PerIsolateData::RealmFind(Handle<Context> context) {
286 for (int i = 0; i < realm_count_; ++i) { 285 for (int i = 0; i < realm_count_; ++i) {
287 if (realms_[i] == context) return i; 286 if (realms_[i] == context) return i;
288 } 287 }
289 return -1; 288 return -1;
290 } 289 }
291 290
(...skipping 15 matching lines...) Expand all
307 if (index == -1) return; 306 if (index == -1) return;
308 args.GetReturnValue().Set(index); 307 args.GetReturnValue().Set(index);
309 } 308 }
310 309
311 310
312 // Realm.owner(o) returns the index of the realm that created o. 311 // Realm.owner(o) returns the index of the realm that created o.
313 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) { 312 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
314 Isolate* isolate = args.GetIsolate(); 313 Isolate* isolate = args.GetIsolate();
315 PerIsolateData* data = PerIsolateData::Get(isolate); 314 PerIsolateData* data = PerIsolateData::Get(isolate);
316 if (args.Length() < 1 || !args[0]->IsObject()) { 315 if (args.Length() < 1 || !args[0]->IsObject()) {
317 Throw("Invalid argument"); 316 Throw(args.GetIsolate(), "Invalid argument");
318 return; 317 return;
319 } 318 }
320 int index = data->RealmFind(args[0]->ToObject()->CreationContext()); 319 int index = data->RealmFind(args[0]->ToObject()->CreationContext());
321 if (index == -1) return; 320 if (index == -1) return;
322 args.GetReturnValue().Set(index); 321 args.GetReturnValue().Set(index);
323 } 322 }
324 323
325 324
326 // Realm.global(i) returns the global object of realm i. 325 // Realm.global(i) returns the global object of realm i.
327 // (Note that properties of global objects cannot be read/written cross-realm.) 326 // (Note that properties of global objects cannot be read/written cross-realm.)
328 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { 327 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
329 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); 328 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate());
330 if (args.Length() < 1 || !args[0]->IsNumber()) { 329 if (args.Length() < 1 || !args[0]->IsNumber()) {
331 Throw("Invalid argument"); 330 Throw(args.GetIsolate(), "Invalid argument");
332 return; 331 return;
333 } 332 }
334 int index = args[0]->Uint32Value(); 333 int index = args[0]->Uint32Value();
335 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { 334 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
336 Throw("Invalid realm index"); 335 Throw(args.GetIsolate(), "Invalid realm index");
337 return; 336 return;
338 } 337 }
339 args.GetReturnValue().Set( 338 args.GetReturnValue().Set(
340 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); 339 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global());
341 } 340 }
342 341
343 342
344 // Realm.create() creates a new realm and returns its index. 343 // Realm.create() creates a new realm and returns its index.
345 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { 344 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) {
346 Isolate* isolate = args.GetIsolate(); 345 Isolate* isolate = args.GetIsolate();
(...skipping 10 matching lines...) Expand all
357 isolate, Context::New(isolate, NULL, global_template)); 356 isolate, Context::New(isolate, NULL, global_template));
358 args.GetReturnValue().Set(index); 357 args.GetReturnValue().Set(index);
359 } 358 }
360 359
361 360
362 // Realm.dispose(i) disposes the reference to the realm i. 361 // Realm.dispose(i) disposes the reference to the realm i.
363 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { 362 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) {
364 Isolate* isolate = args.GetIsolate(); 363 Isolate* isolate = args.GetIsolate();
365 PerIsolateData* data = PerIsolateData::Get(isolate); 364 PerIsolateData* data = PerIsolateData::Get(isolate);
366 if (args.Length() < 1 || !args[0]->IsNumber()) { 365 if (args.Length() < 1 || !args[0]->IsNumber()) {
367 Throw("Invalid argument"); 366 Throw(args.GetIsolate(), "Invalid argument");
368 return; 367 return;
369 } 368 }
370 int index = args[0]->Uint32Value(); 369 int index = args[0]->Uint32Value();
371 if (index >= data->realm_count_ || data->realms_[index].IsEmpty() || 370 if (index >= data->realm_count_ || data->realms_[index].IsEmpty() ||
372 index == 0 || 371 index == 0 ||
373 index == data->realm_current_ || index == data->realm_switch_) { 372 index == data->realm_current_ || index == data->realm_switch_) {
374 Throw("Invalid realm index"); 373 Throw(args.GetIsolate(), "Invalid realm index");
375 return; 374 return;
376 } 375 }
377 data->realms_[index].Dispose(); 376 data->realms_[index].Reset();
378 data->realms_[index].Clear();
379 } 377 }
380 378
381 379
382 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. 380 // Realm.switch(i) switches to the realm i for consecutive interactive inputs.
383 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { 381 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
384 Isolate* isolate = args.GetIsolate(); 382 Isolate* isolate = args.GetIsolate();
385 PerIsolateData* data = PerIsolateData::Get(isolate); 383 PerIsolateData* data = PerIsolateData::Get(isolate);
386 if (args.Length() < 1 || !args[0]->IsNumber()) { 384 if (args.Length() < 1 || !args[0]->IsNumber()) {
387 Throw("Invalid argument"); 385 Throw(args.GetIsolate(), "Invalid argument");
388 return; 386 return;
389 } 387 }
390 int index = args[0]->Uint32Value(); 388 int index = args[0]->Uint32Value();
391 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { 389 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
392 Throw("Invalid realm index"); 390 Throw(args.GetIsolate(), "Invalid realm index");
393 return; 391 return;
394 } 392 }
395 data->realm_switch_ = index; 393 data->realm_switch_ = index;
396 } 394 }
397 395
398 396
399 // Realm.eval(i, s) evaluates s in realm i and returns the result. 397 // Realm.eval(i, s) evaluates s in realm i and returns the result.
400 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { 398 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) {
401 Isolate* isolate = args.GetIsolate(); 399 Isolate* isolate = args.GetIsolate();
402 PerIsolateData* data = PerIsolateData::Get(isolate); 400 PerIsolateData* data = PerIsolateData::Get(isolate);
403 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) { 401 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) {
404 Throw("Invalid argument"); 402 Throw(args.GetIsolate(), "Invalid argument");
405 return; 403 return;
406 } 404 }
407 int index = args[0]->Uint32Value(); 405 int index = args[0]->Uint32Value();
408 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { 406 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
409 Throw("Invalid realm index"); 407 Throw(args.GetIsolate(), "Invalid realm index");
410 return; 408 return;
411 } 409 }
412 Handle<Script> script = Script::New(args[1]->ToString()); 410 Handle<Script> script = Script::New(args[1]->ToString());
413 if (script.IsEmpty()) return; 411 if (script.IsEmpty()) return;
414 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); 412 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]);
415 realm->Enter(); 413 realm->Enter();
416 Handle<Value> result = script->Run(); 414 Handle<Value> result = script->Run();
417 realm->Exit(); 415 realm->Exit();
418 args.GetReturnValue().Set(result); 416 args.GetReturnValue().Set(result);
419 } 417 }
420 418
421 419
422 // Realm.shared is an accessor for a single shared value across realms. 420 // Realm.shared is an accessor for a single shared value across realms.
423 void Shell::RealmSharedGet(Local<String> property, 421 void Shell::RealmSharedGet(Local<String> property,
424 const PropertyCallbackInfo<Value>& info) { 422 const PropertyCallbackInfo<Value>& info) {
425 Isolate* isolate = info.GetIsolate(); 423 Isolate* isolate = info.GetIsolate();
426 PerIsolateData* data = PerIsolateData::Get(isolate); 424 PerIsolateData* data = PerIsolateData::Get(isolate);
427 if (data->realm_shared_.IsEmpty()) return; 425 if (data->realm_shared_.IsEmpty()) return;
428 info.GetReturnValue().Set(data->realm_shared_); 426 info.GetReturnValue().Set(data->realm_shared_);
429 } 427 }
430 428
431 void Shell::RealmSharedSet(Local<String> property, 429 void Shell::RealmSharedSet(Local<String> property,
432 Local<Value> value, 430 Local<Value> value,
433 const PropertyCallbackInfo<void>& info) { 431 const PropertyCallbackInfo<void>& info) {
434 Isolate* isolate = info.GetIsolate(); 432 Isolate* isolate = info.GetIsolate();
435 PerIsolateData* data = PerIsolateData::Get(isolate); 433 PerIsolateData* data = PerIsolateData::Get(isolate);
436 if (!data->realm_shared_.IsEmpty()) data->realm_shared_.Dispose();
437 data->realm_shared_.Reset(isolate, value); 434 data->realm_shared_.Reset(isolate, value);
438 } 435 }
439 436
440 437
441 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { 438 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
442 Write(args); 439 Write(args);
443 printf("\n"); 440 printf("\n");
444 fflush(stdout); 441 fflush(stdout);
445 } 442 }
446 443
(...skipping 19 matching lines...) Expand all
466 printf("Error in fwrite\n"); 463 printf("Error in fwrite\n");
467 Exit(1); 464 Exit(1);
468 } 465 }
469 } 466 }
470 } 467 }
471 468
472 469
473 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { 470 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
474 String::Utf8Value file(args[0]); 471 String::Utf8Value file(args[0]);
475 if (*file == NULL) { 472 if (*file == NULL) {
476 Throw("Error loading file"); 473 Throw(args.GetIsolate(), "Error loading file");
477 return; 474 return;
478 } 475 }
479 Handle<String> source = ReadFile(args.GetIsolate(), *file); 476 Handle<String> source = ReadFile(args.GetIsolate(), *file);
480 if (source.IsEmpty()) { 477 if (source.IsEmpty()) {
481 Throw("Error loading file"); 478 Throw(args.GetIsolate(), "Error loading file");
482 return; 479 return;
483 } 480 }
484 args.GetReturnValue().Set(source); 481 args.GetReturnValue().Set(source);
485 } 482 }
486 483
487 484
488 Handle<String> Shell::ReadFromStdin(Isolate* isolate) { 485 Handle<String> Shell::ReadFromStdin(Isolate* isolate) {
489 static const int kBufferSize = 256; 486 static const int kBufferSize = 256;
490 char buffer[kBufferSize]; 487 char buffer[kBufferSize];
491 Handle<String> accumulator = String::New(""); 488 Handle<String> accumulator = String::NewFromUtf8(isolate, "");
492 int length; 489 int length;
493 while (true) { 490 while (true) {
494 // Continue reading if the line ends with an escape '\\' or the line has 491 // Continue reading if the line ends with an escape '\\' or the line has
495 // not been fully read into the buffer yet (does not end with '\n'). 492 // not been fully read into the buffer yet (does not end with '\n').
496 // If fgets gets an error, just give up. 493 // If fgets gets an error, just give up.
497 char* input = NULL; 494 char* input = NULL;
498 { // Release lock for blocking input. 495 { // Release lock for blocking input.
499 Unlocker unlock(isolate); 496 Unlocker unlock(isolate);
500 input = fgets(buffer, kBufferSize, stdin); 497 input = fgets(buffer, kBufferSize, stdin);
501 } 498 }
502 if (input == NULL) return Handle<String>(); 499 if (input == NULL) return Handle<String>();
503 length = static_cast<int>(strlen(buffer)); 500 length = static_cast<int>(strlen(buffer));
504 if (length == 0) { 501 if (length == 0) {
505 return accumulator; 502 return accumulator;
506 } else if (buffer[length-1] != '\n') { 503 } else if (buffer[length-1] != '\n') {
507 accumulator = String::Concat(accumulator, String::New(buffer, length)); 504 accumulator = String::Concat(
505 accumulator,
506 String::NewFromUtf8(isolate, buffer, String::kNormalString, length));
508 } else if (length > 1 && buffer[length-2] == '\\') { 507 } else if (length > 1 && buffer[length-2] == '\\') {
509 buffer[length-2] = '\n'; 508 buffer[length-2] = '\n';
510 accumulator = String::Concat(accumulator, String::New(buffer, length-1)); 509 accumulator = String::Concat(
510 accumulator, String::NewFromUtf8(isolate, buffer,
511 String::kNormalString, length - 1));
511 } else { 512 } else {
512 return String::Concat(accumulator, String::New(buffer, length-1)); 513 return String::Concat(
514 accumulator, String::NewFromUtf8(isolate, buffer,
515 String::kNormalString, length - 1));
513 } 516 }
514 } 517 }
515 } 518 }
516 519
517 520
518 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) { 521 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
519 for (int i = 0; i < args.Length(); i++) { 522 for (int i = 0; i < args.Length(); i++) {
520 HandleScope handle_scope(args.GetIsolate()); 523 HandleScope handle_scope(args.GetIsolate());
521 String::Utf8Value file(args[i]); 524 String::Utf8Value file(args[i]);
522 if (*file == NULL) { 525 if (*file == NULL) {
523 Throw("Error loading file"); 526 Throw(args.GetIsolate(), "Error loading file");
524 return; 527 return;
525 } 528 }
526 Handle<String> source = ReadFile(args.GetIsolate(), *file); 529 Handle<String> source = ReadFile(args.GetIsolate(), *file);
527 if (source.IsEmpty()) { 530 if (source.IsEmpty()) {
528 Throw("Error loading file"); 531 Throw(args.GetIsolate(), "Error loading file");
529 return; 532 return;
530 } 533 }
531 if (!ExecuteString(args.GetIsolate(), 534 if (!ExecuteString(args.GetIsolate(),
532 source, 535 source,
533 String::New(*file), 536 String::NewFromUtf8(args.GetIsolate(), *file),
534 false, 537 false,
535 true)) { 538 true)) {
536 Throw("Error executing file"); 539 Throw(args.GetIsolate(), "Error executing file");
537 return; 540 return;
538 } 541 }
539 } 542 }
540 } 543 }
541 544
542 545
543 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { 546 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
544 int exit_code = args[0]->Int32Value(); 547 int exit_code = args[0]->Int32Value();
545 OnExit(); 548 OnExit();
546 exit(exit_code); 549 exit(exit_code);
547 } 550 }
548 551
549 552
550 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { 553 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
551 args.GetReturnValue().Set(String::New(V8::GetVersion())); 554 args.GetReturnValue().Set(
555 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion()));
552 } 556 }
553 557
554 558
555 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { 559 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) {
556 HandleScope handle_scope(isolate); 560 HandleScope handle_scope(isolate);
557 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 561 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT)
558 Handle<Context> utility_context; 562 Handle<Context> utility_context;
559 bool enter_context = !Context::InContext(); 563 bool enter_context = !isolate->InContext();
560 if (enter_context) { 564 if (enter_context) {
561 utility_context = Local<Context>::New(isolate, utility_context_); 565 utility_context = Local<Context>::New(isolate, utility_context_);
562 utility_context->Enter(); 566 utility_context->Enter();
563 } 567 }
564 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 568 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT
565 v8::String::Utf8Value exception(try_catch->Exception()); 569 v8::String::Utf8Value exception(try_catch->Exception());
566 const char* exception_string = ToCString(exception); 570 const char* exception_string = ToCString(exception);
567 Handle<Message> message = try_catch->Message(); 571 Handle<Message> message = try_catch->Message();
568 if (message.IsEmpty()) { 572 if (message.IsEmpty()) {
569 // V8 didn't provide any extra information about this error; just 573 // V8 didn't provide any extra information about this error; just
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 608
605 #ifndef V8_SHARED 609 #ifndef V8_SHARED
606 Handle<Array> Shell::GetCompletions(Isolate* isolate, 610 Handle<Array> Shell::GetCompletions(Isolate* isolate,
607 Handle<String> text, 611 Handle<String> text,
608 Handle<String> full) { 612 Handle<String> full) {
609 HandleScope handle_scope(isolate); 613 HandleScope handle_scope(isolate);
610 v8::Local<v8::Context> utility_context = 614 v8::Local<v8::Context> utility_context =
611 v8::Local<v8::Context>::New(isolate, utility_context_); 615 v8::Local<v8::Context>::New(isolate, utility_context_);
612 v8::Context::Scope context_scope(utility_context); 616 v8::Context::Scope context_scope(utility_context);
613 Handle<Object> global = utility_context->Global(); 617 Handle<Object> global = utility_context->Global();
614 Handle<Value> fun = global->Get(String::New("GetCompletions")); 618 Handle<Value> fun =
619 global->Get(String::NewFromUtf8(isolate, "GetCompletions"));
615 static const int kArgc = 3; 620 static const int kArgc = 3;
616 v8::Local<v8::Context> evaluation_context = 621 v8::Local<v8::Context> evaluation_context =
617 v8::Local<v8::Context>::New(isolate, evaluation_context_); 622 v8::Local<v8::Context>::New(isolate, evaluation_context_);
618 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; 623 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full };
619 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 624 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
620 return handle_scope.Close(Handle<Array>::Cast(val)); 625 return handle_scope.Close(Handle<Array>::Cast(val));
621 } 626 }
622 627
623 628
624 #ifdef ENABLE_DEBUGGER_SUPPORT 629 #ifdef ENABLE_DEBUGGER_SUPPORT
625 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate, 630 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate,
626 Handle<String> message) { 631 Handle<String> message) {
627 HandleScope handle_scope(isolate); 632 HandleScope handle_scope(isolate);
628 v8::Local<v8::Context> context = 633 v8::Local<v8::Context> context =
629 v8::Local<v8::Context>::New(isolate, utility_context_); 634 v8::Local<v8::Context>::New(isolate, utility_context_);
630 v8::Context::Scope context_scope(context); 635 v8::Context::Scope context_scope(context);
631 Handle<Object> global = context->Global(); 636 Handle<Object> global = context->Global();
632 Handle<Value> fun = global->Get(String::New("DebugMessageDetails")); 637 Handle<Value> fun =
638 global->Get(String::NewFromUtf8(isolate, "DebugMessageDetails"));
633 static const int kArgc = 1; 639 static const int kArgc = 1;
634 Handle<Value> argv[kArgc] = { message }; 640 Handle<Value> argv[kArgc] = { message };
635 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 641 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
636 return Handle<Object>::Cast(val); 642 return Handle<Object>::Cast(val);
637 } 643 }
638 644
639 645
640 Handle<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, 646 Handle<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate,
641 Handle<String> command) { 647 Handle<String> command) {
642 HandleScope handle_scope(isolate); 648 HandleScope handle_scope(isolate);
643 v8::Local<v8::Context> context = 649 v8::Local<v8::Context> context =
644 v8::Local<v8::Context>::New(isolate, utility_context_); 650 v8::Local<v8::Context>::New(isolate, utility_context_);
645 v8::Context::Scope context_scope(context); 651 v8::Context::Scope context_scope(context);
646 Handle<Object> global = context->Global(); 652 Handle<Object> global = context->Global();
647 Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest")); 653 Handle<Value> fun =
654 global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest"));
648 static const int kArgc = 1; 655 static const int kArgc = 1;
649 Handle<Value> argv[kArgc] = { command }; 656 Handle<Value> argv[kArgc] = { command };
650 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 657 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
651 return val; 658 return val;
652 } 659 }
653 660
654 661
655 void Shell::DispatchDebugMessages() { 662 void Shell::DispatchDebugMessages() {
656 Isolate* isolate = v8::Isolate::GetCurrent(); 663 Isolate* isolate = v8::Isolate::GetCurrent();
657 HandleScope handle_scope(isolate); 664 HandleScope handle_scope(isolate);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 evaluation_context->SetSecurityToken(Undefined(isolate)); 783 evaluation_context->SetSecurityToken(Undefined(isolate));
777 v8::Context::Scope context_scope(utility_context); 784 v8::Context::Scope context_scope(utility_context);
778 785
779 #ifdef ENABLE_DEBUGGER_SUPPORT 786 #ifdef ENABLE_DEBUGGER_SUPPORT
780 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); 787 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n");
781 // Install the debugger object in the utility scope 788 // Install the debugger object in the utility scope
782 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); 789 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug();
783 debug->Load(); 790 debug->Load();
784 i::Handle<i::JSObject> js_debug 791 i::Handle<i::JSObject> js_debug
785 = i::Handle<i::JSObject>(debug->debug_context()->global_object()); 792 = i::Handle<i::JSObject>(debug->debug_context()->global_object());
786 utility_context->Global()->Set(String::New("$debug"), 793 utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"),
787 Utils::ToLocal(js_debug)); 794 Utils::ToLocal(js_debug));
788 debug->debug_context()->set_security_token( 795 debug->debug_context()->set_security_token(
789 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); 796 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value());
790 #endif // ENABLE_DEBUGGER_SUPPORT 797 #endif // ENABLE_DEBUGGER_SUPPORT
791 798
792 // Run the d8 shell utility script in the utility context 799 // Run the d8 shell utility script in the utility context
793 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); 800 int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
794 i::Vector<const char> shell_source = 801 i::Vector<const char> shell_source =
795 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); 802 i::NativesCollection<i::D8>::GetRawScriptSource(source_index);
796 i::Vector<const char> shell_source_name = 803 i::Vector<const char> shell_source_name =
797 i::NativesCollection<i::D8>::GetScriptName(source_index); 804 i::NativesCollection<i::D8>::GetScriptName(source_index);
798 Handle<String> source = String::New(shell_source.start(), 805 Handle<String> source =
799 shell_source.length()); 806 String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString,
800 Handle<String> name = String::New(shell_source_name.start(), 807 shell_source.length());
801 shell_source_name.length()); 808 Handle<String> name =
809 String::NewFromUtf8(isolate, shell_source_name.start(),
810 String::kNormalString, shell_source_name.length());
802 Handle<Script> script = Script::Compile(source, name); 811 Handle<Script> script = Script::Compile(source, name);
803 script->Run(); 812 script->Run();
804 // Mark the d8 shell script as native to avoid it showing up as normal source 813 // Mark the d8 shell script as native to avoid it showing up as normal source
805 // in the debugger. 814 // in the debugger.
806 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); 815 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script);
807 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() 816 i::Handle<i::Script> script_object = compiled_script->IsJSFunction()
808 ? i::Handle<i::Script>(i::Script::cast( 817 ? i::Handle<i::Script>(i::Script::cast(
809 i::JSFunction::cast(*compiled_script)->shared()->script())) 818 i::JSFunction::cast(*compiled_script)->shared()->script()))
810 : i::Handle<i::Script>(i::Script::cast( 819 : i::Handle<i::Script>(i::Script::cast(
811 i::SharedFunctionInfo::cast(*compiled_script)->script())); 820 i::SharedFunctionInfo::cast(*compiled_script)->script()));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 *raw_data_size = decompressed_size; 853 *raw_data_size = decompressed_size;
845 } 854 }
846 return result; 855 return result;
847 } 856 }
848 }; 857 };
849 #endif 858 #endif
850 859
851 860
852 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { 861 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
853 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 862 Handle<ObjectTemplate> global_template = ObjectTemplate::New();
854 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); 863 global_template->Set(String::NewFromUtf8(isolate, "print"),
855 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); 864 FunctionTemplate::New(Print));
856 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); 865 global_template->Set(String::NewFromUtf8(isolate, "write"),
857 global_template->Set(String::New("readbuffer"), 866 FunctionTemplate::New(Write));
867 global_template->Set(String::NewFromUtf8(isolate, "read"),
868 FunctionTemplate::New(Read));
869 global_template->Set(String::NewFromUtf8(isolate, "readbuffer"),
858 FunctionTemplate::New(ReadBuffer)); 870 FunctionTemplate::New(ReadBuffer));
859 global_template->Set(String::New("readline"), 871 global_template->Set(String::NewFromUtf8(isolate, "readline"),
860 FunctionTemplate::New(ReadLine)); 872 FunctionTemplate::New(ReadLine));
861 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); 873 global_template->Set(String::NewFromUtf8(isolate, "load"),
862 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); 874 FunctionTemplate::New(Load));
863 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); 875 global_template->Set(String::NewFromUtf8(isolate, "quit"),
876 FunctionTemplate::New(Quit));
877 global_template->Set(String::NewFromUtf8(isolate, "version"),
878 FunctionTemplate::New(Version));
864 879
865 // Bind the Realm object. 880 // Bind the Realm object.
866 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(); 881 Handle<ObjectTemplate> realm_template = ObjectTemplate::New();
867 realm_template->Set(String::New("current"), 882 realm_template->Set(String::NewFromUtf8(isolate, "current"),
868 FunctionTemplate::New(RealmCurrent)); 883 FunctionTemplate::New(RealmCurrent));
869 realm_template->Set(String::New("owner"), 884 realm_template->Set(String::NewFromUtf8(isolate, "owner"),
870 FunctionTemplate::New(RealmOwner)); 885 FunctionTemplate::New(RealmOwner));
871 realm_template->Set(String::New("global"), 886 realm_template->Set(String::NewFromUtf8(isolate, "global"),
872 FunctionTemplate::New(RealmGlobal)); 887 FunctionTemplate::New(RealmGlobal));
873 realm_template->Set(String::New("create"), 888 realm_template->Set(String::NewFromUtf8(isolate, "create"),
874 FunctionTemplate::New(RealmCreate)); 889 FunctionTemplate::New(RealmCreate));
875 realm_template->Set(String::New("dispose"), 890 realm_template->Set(String::NewFromUtf8(isolate, "dispose"),
876 FunctionTemplate::New(RealmDispose)); 891 FunctionTemplate::New(RealmDispose));
877 realm_template->Set(String::New("switch"), 892 realm_template->Set(String::NewFromUtf8(isolate, "switch"),
878 FunctionTemplate::New(RealmSwitch)); 893 FunctionTemplate::New(RealmSwitch));
879 realm_template->Set(String::New("eval"), 894 realm_template->Set(String::NewFromUtf8(isolate, "eval"),
880 FunctionTemplate::New(RealmEval)); 895 FunctionTemplate::New(RealmEval));
881 realm_template->SetAccessor(String::New("shared"), 896 realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"),
882 RealmSharedGet, RealmSharedSet); 897 RealmSharedGet, RealmSharedSet);
883 global_template->Set(String::New("Realm"), realm_template); 898 global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template);
884 899
885 #ifndef V8_SHARED 900 #ifndef V8_SHARED
886 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(); 901 Handle<ObjectTemplate> performance_template = ObjectTemplate::New();
887 performance_template->Set(String::New("now"), 902 performance_template->Set(String::NewFromUtf8(isolate, "now"),
888 FunctionTemplate::New(PerformanceNow)); 903 FunctionTemplate::New(PerformanceNow));
889 global_template->Set(String::New("performance"), performance_template); 904 global_template->Set(String::NewFromUtf8(isolate, "performance"),
905 performance_template);
890 #endif // V8_SHARED 906 #endif // V8_SHARED
891 907
892 #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64) 908 #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64)
893 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); 909 Handle<ObjectTemplate> os_templ = ObjectTemplate::New();
894 AddOSMethods(os_templ); 910 AddOSMethods(isolate, os_templ);
895 global_template->Set(String::New("os"), os_templ); 911 global_template->Set(String::NewFromUtf8(isolate, "os"), os_templ);
896 #endif // V8_SHARED 912 #endif // V8_SHARED
897 913
898 return global_template; 914 return global_template;
899 } 915 }
900 916
901 917
902 void Shell::Initialize(Isolate* isolate) { 918 void Shell::Initialize(Isolate* isolate) {
903 #ifdef COMPRESS_STARTUP_DATA_BZ2 919 #ifdef COMPRESS_STARTUP_DATA_BZ2
904 BZip2Decompressor startup_data_decompressor; 920 BZip2Decompressor startup_data_decompressor;
905 int bz2_result = startup_data_decompressor.Decompress(); 921 int bz2_result = startup_data_decompressor.Decompress();
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
960 i::JSArguments js_args = i::FLAG_js_arguments; 976 i::JSArguments js_args = i::FLAG_js_arguments;
961 i::Handle<i::FixedArray> arguments_array = 977 i::Handle<i::FixedArray> arguments_array =
962 factory->NewFixedArray(js_args.argc); 978 factory->NewFixedArray(js_args.argc);
963 for (int j = 0; j < js_args.argc; j++) { 979 for (int j = 0; j < js_args.argc; j++) {
964 i::Handle<i::String> arg = 980 i::Handle<i::String> arg =
965 factory->NewStringFromUtf8(i::CStrVector(js_args[j])); 981 factory->NewStringFromUtf8(i::CStrVector(js_args[j]));
966 arguments_array->set(j, *arg); 982 arguments_array->set(j, *arg);
967 } 983 }
968 i::Handle<i::JSArray> arguments_jsarray = 984 i::Handle<i::JSArray> arguments_jsarray =
969 factory->NewJSArrayWithElements(arguments_array); 985 factory->NewJSArrayWithElements(arguments_array);
970 context->Global()->Set(String::New("arguments"), 986 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"),
971 Utils::ToLocal(arguments_jsarray)); 987 Utils::ToLocal(arguments_jsarray));
972 #endif // V8_SHARED 988 #endif // V8_SHARED
973 return handle_scope.Close(context); 989 return handle_scope.Close(context);
974 } 990 }
975 991
976 992
977 void Shell::Exit(int exit_code) { 993 void Shell::Exit(int exit_code) {
978 // Use _exit instead of exit to avoid races between isolate 994 // Use _exit instead of exit to avoid races between isolate
979 // threads and static destructors. 995 // threads and static destructors.
980 fflush(stdout); 996 fflush(stdout);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 1099
1084 static void ReadBufferWeakCallback(v8::Isolate* isolate, 1100 static void ReadBufferWeakCallback(v8::Isolate* isolate,
1085 Persistent<ArrayBuffer>* array_buffer, 1101 Persistent<ArrayBuffer>* array_buffer,
1086 uint8_t* data) { 1102 uint8_t* data) {
1087 size_t byte_length = 1103 size_t byte_length =
1088 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength(); 1104 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength();
1089 isolate->AdjustAmountOfExternalAllocatedMemory( 1105 isolate->AdjustAmountOfExternalAllocatedMemory(
1090 -static_cast<intptr_t>(byte_length)); 1106 -static_cast<intptr_t>(byte_length));
1091 1107
1092 delete[] data; 1108 delete[] data;
1093 array_buffer->Dispose(); 1109 array_buffer->Reset();
1094 } 1110 }
1095 1111
1096 1112
1097 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { 1113 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
1098 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT 1114 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT
1099 String::Utf8Value filename(args[0]); 1115 String::Utf8Value filename(args[0]);
1100 int length; 1116 int length;
1101 if (*filename == NULL) { 1117 if (*filename == NULL) {
1102 Throw("Error loading file"); 1118 Throw(args.GetIsolate(), "Error loading file");
1103 return; 1119 return;
1104 } 1120 }
1105 1121
1106 Isolate* isolate = args.GetIsolate(); 1122 Isolate* isolate = args.GetIsolate();
1107 uint8_t* data = reinterpret_cast<uint8_t*>( 1123 uint8_t* data = reinterpret_cast<uint8_t*>(
1108 ReadChars(args.GetIsolate(), *filename, &length)); 1124 ReadChars(args.GetIsolate(), *filename, &length));
1109 if (data == NULL) { 1125 if (data == NULL) {
1110 Throw("Error reading file"); 1126 Throw(args.GetIsolate(), "Error reading file");
1111 return; 1127 return;
1112 } 1128 }
1113 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length); 1129 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length);
1114 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer); 1130 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer);
1115 weak_handle.MakeWeak(data, ReadBufferWeakCallback); 1131 weak_handle.MakeWeak(data, ReadBufferWeakCallback);
1116 weak_handle.MarkIndependent(); 1132 weak_handle.MarkIndependent();
1117 isolate->AdjustAmountOfExternalAllocatedMemory(length); 1133 isolate->AdjustAmountOfExternalAllocatedMemory(length);
1118 1134
1119 args.GetReturnValue().Set(buffer); 1135 args.GetReturnValue().Set(buffer);
1120 } 1136 }
(...skipping 20 matching lines...) Expand all
1141 return ReadToken(data, ' '); 1157 return ReadToken(data, ' ');
1142 } 1158 }
1143 #endif // V8_SHARED 1159 #endif // V8_SHARED
1144 1160
1145 1161
1146 // Reads a file into a v8 string. 1162 // Reads a file into a v8 string.
1147 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { 1163 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) {
1148 int size = 0; 1164 int size = 0;
1149 char* chars = ReadChars(isolate, name, &size); 1165 char* chars = ReadChars(isolate, name, &size);
1150 if (chars == NULL) return Handle<String>(); 1166 if (chars == NULL) return Handle<String>();
1151 Handle<String> result = String::New(chars, size); 1167 Handle<String> result =
1168 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
1152 delete[] chars; 1169 delete[] chars;
1153 return result; 1170 return result;
1154 } 1171 }
1155 1172
1156 1173
1157 void Shell::RunShell(Isolate* isolate) { 1174 void Shell::RunShell(Isolate* isolate) {
1158 Locker locker(isolate); 1175 Locker locker(isolate);
1159 HandleScope outer_scope(isolate); 1176 HandleScope outer_scope(isolate);
1160 v8::Local<v8::Context> context = 1177 v8::Local<v8::Context> context =
1161 v8::Local<v8::Context>::New(isolate, evaluation_context_); 1178 v8::Local<v8::Context>::New(isolate, evaluation_context_);
1162 v8::Context::Scope context_scope(context); 1179 v8::Context::Scope context_scope(context);
1163 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 1180 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1164 Handle<String> name = String::New("(d8)"); 1181 Handle<String> name = String::NewFromUtf8(isolate, "(d8)");
1165 LineEditor* console = LineEditor::Get(); 1182 LineEditor* console = LineEditor::Get();
1166 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); 1183 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name());
1167 console->Open(isolate); 1184 console->Open(isolate);
1168 while (true) { 1185 while (true) {
1169 HandleScope inner_scope(isolate); 1186 HandleScope inner_scope(isolate);
1170 Handle<String> input = console->Prompt(Shell::kPrompt); 1187 Handle<String> input = console->Prompt(Shell::kPrompt);
1171 if (input.IsEmpty()) break; 1188 if (input.IsEmpty()) break;
1172 ExecuteString(isolate, input, name, true, true); 1189 ExecuteString(isolate, input, name, true, true);
1173 } 1190 }
1174 printf("\n"); 1191 printf("\n");
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1223 if (strlen(filename) == 0) { 1240 if (strlen(filename) == 0) {
1224 continue; 1241 continue;
1225 } 1242 }
1226 1243
1227 Handle<String> str = Shell::ReadFile(isolate_, filename); 1244 Handle<String> str = Shell::ReadFile(isolate_, filename);
1228 if (str.IsEmpty()) { 1245 if (str.IsEmpty()) {
1229 printf("File '%s' not found\n", filename); 1246 printf("File '%s' not found\n", filename);
1230 Shell::Exit(1); 1247 Shell::Exit(1);
1231 } 1248 }
1232 1249
1233 Shell::ExecuteString(isolate_, str, String::New(filename), false, false); 1250 Shell::ExecuteString(
1251 isolate_, str, String::NewFromUtf8(isolate_, filename), false, false);
1234 } 1252 }
1235 1253
1236 ptr = next_line; 1254 ptr = next_line;
1237 } 1255 }
1238 } 1256 }
1239 #endif // V8_SHARED 1257 #endif // V8_SHARED
1240 1258
1241 1259
1242 SourceGroup::~SourceGroup() { 1260 SourceGroup::~SourceGroup() {
1243 #ifndef V8_SHARED 1261 #ifndef V8_SHARED
1244 delete thread_; 1262 delete thread_;
1245 thread_ = NULL; 1263 thread_ = NULL;
1246 #endif // V8_SHARED 1264 #endif // V8_SHARED
1247 } 1265 }
1248 1266
1249 1267
1250 void SourceGroup::Execute(Isolate* isolate) { 1268 void SourceGroup::Execute(Isolate* isolate) {
1251 bool exception_was_thrown = false; 1269 bool exception_was_thrown = false;
1252 for (int i = begin_offset_; i < end_offset_; ++i) { 1270 for (int i = begin_offset_; i < end_offset_; ++i) {
1253 const char* arg = argv_[i]; 1271 const char* arg = argv_[i];
1254 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { 1272 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
1255 // Execute argument given to -e option directly. 1273 // Execute argument given to -e option directly.
1256 HandleScope handle_scope(isolate); 1274 HandleScope handle_scope(isolate);
1257 Handle<String> file_name = String::New("unnamed"); 1275 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed");
1258 Handle<String> source = String::New(argv_[i + 1]); 1276 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]);
1259 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { 1277 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1260 exception_was_thrown = true; 1278 exception_was_thrown = true;
1261 break; 1279 break;
1262 } 1280 }
1263 ++i; 1281 ++i;
1264 } else if (arg[0] == '-') { 1282 } else if (arg[0] == '-') {
1265 // Ignore other options. They have been parsed already. 1283 // Ignore other options. They have been parsed already.
1266 } else { 1284 } else {
1267 // Use all other arguments as names of files to load and run. 1285 // Use all other arguments as names of files to load and run.
1268 HandleScope handle_scope(isolate); 1286 HandleScope handle_scope(isolate);
1269 Handle<String> file_name = String::New(arg); 1287 Handle<String> file_name = String::NewFromUtf8(isolate, arg);
1270 Handle<String> source = ReadFile(isolate, arg); 1288 Handle<String> source = ReadFile(isolate, arg);
1271 if (source.IsEmpty()) { 1289 if (source.IsEmpty()) {
1272 printf("Error reading '%s'\n", arg); 1290 printf("Error reading '%s'\n", arg);
1273 Shell::Exit(1); 1291 Shell::Exit(1);
1274 } 1292 }
1275 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { 1293 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1276 exception_was_thrown = true; 1294 exception_was_thrown = true;
1277 break; 1295 break;
1278 } 1296 }
1279 } 1297 }
1280 } 1298 }
1281 if (exception_was_thrown != Shell::options.expected_to_throw) { 1299 if (exception_was_thrown != Shell::options.expected_to_throw) {
1282 Shell::Exit(1); 1300 Shell::Exit(1);
1283 } 1301 }
1284 } 1302 }
1285 1303
1286 1304
1287 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { 1305 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) {
1288 int size; 1306 int size;
1289 char* chars = ReadChars(isolate, name, &size); 1307 char* chars = ReadChars(isolate, name, &size);
1290 if (chars == NULL) return Handle<String>(); 1308 if (chars == NULL) return Handle<String>();
1291 Handle<String> result = String::New(chars, size); 1309 Handle<String> result =
1310 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
1292 delete[] chars; 1311 delete[] chars;
1293 return result; 1312 return result;
1294 } 1313 }
1295 1314
1296 1315
1297 #ifndef V8_SHARED 1316 #ifndef V8_SHARED
1298 i::Thread::Options SourceGroup::GetThreadOptions() { 1317 i::Thread::Options SourceGroup::GetThreadOptions() {
1299 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less 1318 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
1300 // which is not enough to parse the big literal expressions used in tests. 1319 // which is not enough to parse the big literal expressions used in tests.
1301 // The stack size should be at least StackGuard::kLimitSize + some 1320 // The stack size should be at least StackGuard::kLimitSize + some
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1351 } 1370 }
1352 } 1371 }
1353 #endif // V8_SHARED 1372 #endif // V8_SHARED
1354 1373
1355 1374
1356 bool Shell::SetOptions(int argc, char* argv[]) { 1375 bool Shell::SetOptions(int argc, char* argv[]) {
1357 for (int i = 0; i < argc; i++) { 1376 for (int i = 0; i < argc; i++) {
1358 if (strcmp(argv[i], "--stress-opt") == 0) { 1377 if (strcmp(argv[i], "--stress-opt") == 0) {
1359 options.stress_opt = true; 1378 options.stress_opt = true;
1360 argv[i] = NULL; 1379 argv[i] = NULL;
1380 } else if (strcmp(argv[i], "--nostress-opt") == 0) {
1381 options.stress_opt = false;
1382 argv[i] = NULL;
1361 } else if (strcmp(argv[i], "--stress-deopt") == 0) { 1383 } else if (strcmp(argv[i], "--stress-deopt") == 0) {
1362 options.stress_deopt = true; 1384 options.stress_deopt = true;
1363 argv[i] = NULL; 1385 argv[i] = NULL;
1386 } else if (strcmp(argv[i], "--mock-arraybuffer-allocator") == 0) {
1387 options.mock_arraybuffer_allocator = true;
1388 argv[i] = NULL;
1364 } else if (strcmp(argv[i], "--noalways-opt") == 0) { 1389 } else if (strcmp(argv[i], "--noalways-opt") == 0) {
1365 // No support for stressing if we can't use --always-opt. 1390 // No support for stressing if we can't use --always-opt.
1366 options.stress_opt = false; 1391 options.stress_opt = false;
1367 options.stress_deopt = false; 1392 options.stress_deopt = false;
1368 } else if (strcmp(argv[i], "--shell") == 0) { 1393 } else if (strcmp(argv[i], "--shell") == 0) {
1369 options.interactive_shell = true; 1394 options.interactive_shell = true;
1370 argv[i] = NULL; 1395 argv[i] = NULL;
1371 } else if (strcmp(argv[i], "--test") == 0) { 1396 } else if (strcmp(argv[i], "--test") == 0) {
1372 options.test_shell = true; 1397 options.test_shell = true;
1373 argv[i] = NULL; 1398 argv[i] = NULL;
1374 } else if (strcmp(argv[i], "--send-idle-notification") == 0) { 1399 } else if (strcmp(argv[i], "--send-idle-notification") == 0) {
1375 options.send_idle_notification = true; 1400 options.send_idle_notification = true;
1376 argv[i] = NULL; 1401 argv[i] = NULL;
1377 } else if (strcmp(argv[i], "--preemption") == 0) {
1378 #ifdef V8_SHARED
1379 printf("D8 with shared library does not support multi-threading\n");
1380 return false;
1381 #else
1382 options.use_preemption = true;
1383 argv[i] = NULL;
1384 #endif // V8_SHARED
1385 } else if (strcmp(argv[i], "--nopreemption") == 0) {
1386 #ifdef V8_SHARED
1387 printf("D8 with shared library does not support multi-threading\n");
1388 return false;
1389 #else
1390 options.use_preemption = false;
1391 argv[i] = NULL;
1392 #endif // V8_SHARED
1393 } else if (strcmp(argv[i], "--preemption-interval") == 0) {
1394 #ifdef V8_SHARED
1395 printf("D8 with shared library does not support multi-threading\n");
1396 return false;
1397 #else
1398 if (++i < argc) {
1399 argv[i-1] = NULL;
1400 char* end = NULL;
1401 options.preemption_interval = strtol(argv[i], &end, 10); // NOLINT
1402 if (options.preemption_interval <= 0
1403 || *end != '\0'
1404 || errno == ERANGE) {
1405 printf("Invalid value for --preemption-interval '%s'\n", argv[i]);
1406 return false;
1407 }
1408 argv[i] = NULL;
1409 } else {
1410 printf("Missing value for --preemption-interval\n");
1411 return false;
1412 }
1413 #endif // V8_SHARED
1414 } else if (strcmp(argv[i], "-f") == 0) { 1402 } else if (strcmp(argv[i], "-f") == 0) {
1415 // Ignore any -f flags for compatibility with other stand-alone 1403 // Ignore any -f flags for compatibility with other stand-alone
1416 // JavaScript engines. 1404 // JavaScript engines.
1417 continue; 1405 continue;
1418 } else if (strcmp(argv[i], "--isolate") == 0) { 1406 } else if (strcmp(argv[i], "--isolate") == 0) {
1419 #ifdef V8_SHARED 1407 #ifdef V8_SHARED
1420 printf("D8 with shared library does not support multi-threading\n"); 1408 printf("D8 with shared library does not support multi-threading\n");
1421 return false; 1409 return false;
1422 #endif // V8_SHARED 1410 #endif // V8_SHARED
1423 options.num_isolates++; 1411 options.num_isolates++;
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
1542 options.isolate_sources[0].Execute(isolate); 1530 options.isolate_sources[0].Execute(isolate);
1543 } 1531 }
1544 } 1532 }
1545 if (!options.last_run) { 1533 if (!options.last_run) {
1546 if (options.send_idle_notification) { 1534 if (options.send_idle_notification) {
1547 const int kLongIdlePauseInMs = 1000; 1535 const int kLongIdlePauseInMs = 1000;
1548 V8::ContextDisposedNotification(); 1536 V8::ContextDisposedNotification();
1549 V8::IdleNotification(kLongIdlePauseInMs); 1537 V8::IdleNotification(kLongIdlePauseInMs);
1550 } 1538 }
1551 } 1539 }
1552
1553 #ifndef V8_SHARED
1554 // Start preemption if threads have been created and preemption is enabled.
1555 if (threads.length() > 0
1556 && options.use_preemption) {
1557 Locker::StartPreemption(isolate, options.preemption_interval);
1558 }
1559 #endif // V8_SHARED
1560 } 1540 }
1561 1541
1562 #ifndef V8_SHARED 1542 #ifndef V8_SHARED
1563 for (int i = 1; i < options.num_isolates; ++i) { 1543 for (int i = 1; i < options.num_isolates; ++i) {
1564 options.isolate_sources[i].WaitForThread(); 1544 options.isolate_sources[i].WaitForThread();
1565 } 1545 }
1566 1546
1567 for (int i = 0; i < threads.length(); i++) { 1547 for (int i = 0; i < threads.length(); i++) {
1568 i::Thread* thread = threads[i]; 1548 i::Thread* thread = threads[i];
1569 thread->Join(); 1549 thread->Join();
1570 delete thread; 1550 delete thread;
1571 } 1551 }
1572
1573 if (threads.length() > 0 && options.use_preemption) {
1574 Locker lock(isolate);
1575 Locker::StopPreemption(isolate);
1576 }
1577 #endif // V8_SHARED 1552 #endif // V8_SHARED
1578 return 0; 1553 return 0;
1579 } 1554 }
1580 1555
1581 1556
1582 #ifdef V8_SHARED 1557 #ifdef V8_SHARED
1583 static void SetStandaloneFlagsViaCommandLine() { 1558 static void SetStandaloneFlagsViaCommandLine() {
1584 int fake_argc = 2; 1559 int fake_argc = 2;
1585 char **fake_argv = new char*[2]; 1560 char **fake_argv = new char*[2];
1586 fake_argv[0] = NULL; 1561 fake_argv[0] = NULL;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1663 virtual void Free(void* data, size_t) { free(data); } 1638 virtual void Free(void* data, size_t) { free(data); }
1664 // TODO(dslomov): Remove when v8:2823 is fixed. 1639 // TODO(dslomov): Remove when v8:2823 is fixed.
1665 virtual void Free(void* data) { 1640 virtual void Free(void* data) {
1666 #ifndef V8_SHARED 1641 #ifndef V8_SHARED
1667 UNREACHABLE(); 1642 UNREACHABLE();
1668 #endif 1643 #endif
1669 } 1644 }
1670 }; 1645 };
1671 1646
1672 1647
1648 class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
1649 public:
1650 virtual void* Allocate(size_t) V8_OVERRIDE {
1651 return malloc(0);
1652 }
1653 virtual void* AllocateUninitialized(size_t length) V8_OVERRIDE {
1654 return malloc(0);
1655 }
1656 virtual void Free(void*, size_t) V8_OVERRIDE {
1657 }
1658 };
1659
1660
1673 int Shell::Main(int argc, char* argv[]) { 1661 int Shell::Main(int argc, char* argv[]) {
1674 if (!SetOptions(argc, argv)) return 1; 1662 if (!SetOptions(argc, argv)) return 1;
1675 v8::V8::InitializeICU(); 1663 v8::V8::InitializeICU();
1676 #ifndef V8_SHARED 1664 #ifndef V8_SHARED
1677 i::FLAG_trace_hydrogen_file = "hydrogen.cfg"; 1665 i::FLAG_trace_hydrogen_file = "hydrogen.cfg";
1678 i::FLAG_redirect_code_traces_to = "code.asm"; 1666 i::FLAG_redirect_code_traces_to = "code.asm";
1679 #else 1667 #else
1680 SetStandaloneFlagsViaCommandLine(); 1668 SetStandaloneFlagsViaCommandLine();
1681 #endif 1669 #endif
1682 v8::SetDefaultResourceConstraintsForCurrentPlatform();
1683 ShellArrayBufferAllocator array_buffer_allocator; 1670 ShellArrayBufferAllocator array_buffer_allocator;
1684 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator); 1671 MockArrayBufferAllocator mock_arraybuffer_allocator;
1672 if (options.mock_arraybuffer_allocator) {
1673 v8::V8::SetArrayBufferAllocator(&mock_arraybuffer_allocator);
1674 } else {
1675 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
1676 }
1685 int result = 0; 1677 int result = 0;
1686 Isolate* isolate = Isolate::GetCurrent(); 1678 Isolate* isolate = Isolate::GetCurrent();
1679 #ifndef V8_SHARED
1680 v8::ResourceConstraints constraints;
1681 constraints.ConfigureDefaults(i::OS::TotalPhysicalMemory(),
1682 i::CPU::NumberOfProcessorsOnline());
1683 v8::SetResourceConstraints(isolate, &constraints);
1684 #endif
1687 DumbLineEditor dumb_line_editor(isolate); 1685 DumbLineEditor dumb_line_editor(isolate);
1688 { 1686 {
1689 Initialize(isolate); 1687 Initialize(isolate);
1690 #ifdef ENABLE_VTUNE_JIT_INTERFACE 1688 #ifdef ENABLE_VTUNE_JIT_INTERFACE
1691 vTune::InitializeVtuneForV8(); 1689 vTune::InitializeVtuneForV8();
1692 #endif 1690 #endif
1693 PerIsolateData data(isolate); 1691 PerIsolateData data(isolate);
1694 InitializeDebugger(isolate); 1692 InitializeDebugger(isolate);
1695 1693
1696 #ifndef V8_SHARED 1694 #ifndef V8_SHARED
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1757 } 1755 }
1758 1756
1759 } // namespace v8 1757 } // namespace v8
1760 1758
1761 1759
1762 #ifndef GOOGLE3 1760 #ifndef GOOGLE3
1763 int main(int argc, char* argv[]) { 1761 int main(int argc, char* argv[]) {
1764 return v8::Shell::Main(argc, argv); 1762 return v8::Shell::Main(argc, argv);
1765 } 1763 }
1766 #endif 1764 #endif
OLDNEW
« include/v8-platform.h ('K') | « src/d8.h ('k') | src/d8-debug.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698