| OLD | NEW |
| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 #include <unistd.h> // NOLINT | 73 #include <unistd.h> // NOLINT |
| 74 #endif | 74 #endif |
| 75 | 75 |
| 76 #ifndef ASSERT | 76 #ifndef ASSERT |
| 77 #define ASSERT(condition) assert(condition) | 77 #define ASSERT(condition) assert(condition) |
| 78 #endif | 78 #endif |
| 79 | 79 |
| 80 namespace v8 { | 80 namespace v8 { |
| 81 | 81 |
| 82 | 82 |
| 83 static Handle<Value> Throw(const char* message) { | 83 static Handle<Value> Throw(Isolate* isolate, const char* message) { |
| 84 return ThrowException(String::New(message)); | 84 return isolate->ThrowException(String::NewFromUtf8(isolate, message)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 | 87 |
| 88 | 88 |
| 89 class PerIsolateData { | 89 class PerIsolateData { |
| 90 public: | 90 public: |
| 91 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) { | 91 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) { |
| 92 HandleScope scope(isolate); | 92 HandleScope scope(isolate); |
| 93 isolate->SetData(0, this); | 93 isolate->SetData(0, this); |
| 94 } | 94 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 fwrite(*str, sizeof(**str), str.length(), stdout); | 237 fwrite(*str, sizeof(**str), str.length(), stdout); |
| 238 printf("\n"); | 238 printf("\n"); |
| 239 } | 239 } |
| 240 #if !defined(V8_SHARED) | 240 #if !defined(V8_SHARED) |
| 241 } else { | 241 } else { |
| 242 v8::TryCatch try_catch; | 242 v8::TryCatch try_catch; |
| 243 v8::Local<v8::Context> context = | 243 v8::Local<v8::Context> context = |
| 244 v8::Local<v8::Context>::New(isolate, utility_context_); | 244 v8::Local<v8::Context>::New(isolate, utility_context_); |
| 245 v8::Context::Scope context_scope(context); | 245 v8::Context::Scope context_scope(context); |
| 246 Handle<Object> global = context->Global(); | 246 Handle<Object> global = context->Global(); |
| 247 Handle<Value> fun = global->Get(String::New("Stringify")); | 247 Handle<Value> fun = |
| 248 global->Get(String::NewFromUtf8(isolate, "Stringify")); |
| 248 Handle<Value> argv[1] = { result }; | 249 Handle<Value> argv[1] = { result }; |
| 249 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); | 250 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); |
| 250 if (try_catch.HasCaught()) return true; | 251 if (try_catch.HasCaught()) return true; |
| 251 v8::String::Utf8Value str(s); | 252 v8::String::Utf8Value str(s); |
| 252 fwrite(*str, sizeof(**str), str.length(), stdout); | 253 fwrite(*str, sizeof(**str), str.length(), stdout); |
| 253 printf("\n"); | 254 printf("\n"); |
| 254 } | 255 } |
| 255 #endif | 256 #endif |
| 256 } | 257 } |
| 257 return true; | 258 return true; |
| 258 } | 259 } |
| 259 } | 260 } |
| 260 } | 261 } |
| 261 | 262 |
| 262 | 263 |
| 263 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { | 264 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { |
| 264 data_->realm_count_ = 1; | 265 data_->realm_count_ = 1; |
| 265 data_->realm_current_ = 0; | 266 data_->realm_current_ = 0; |
| 266 data_->realm_switch_ = 0; | 267 data_->realm_switch_ = 0; |
| 267 data_->realms_ = new Persistent<Context>[1]; | 268 data_->realms_ = new Persistent<Context>[1]; |
| 268 data_->realms_[0].Reset(data_->isolate_, | 269 data_->realms_[0].Reset(data_->isolate_, |
| 269 data_->isolate_->GetEnteredContext()); | 270 data_->isolate_->GetEnteredContext()); |
| 270 data_->realm_shared_.Clear(); | |
| 271 } | 271 } |
| 272 | 272 |
| 273 | 273 |
| 274 PerIsolateData::RealmScope::~RealmScope() { | 274 PerIsolateData::RealmScope::~RealmScope() { |
| 275 // Drop realms to avoid keeping them alive. | 275 // Drop realms to avoid keeping them alive. |
| 276 for (int i = 0; i < data_->realm_count_; ++i) | 276 for (int i = 0; i < data_->realm_count_; ++i) |
| 277 data_->realms_[i].Dispose(); | 277 data_->realms_[i].Reset(); |
| 278 delete[] data_->realms_; | 278 delete[] data_->realms_; |
| 279 if (!data_->realm_shared_.IsEmpty()) | 279 if (!data_->realm_shared_.IsEmpty()) |
| 280 data_->realm_shared_.Dispose(); | 280 data_->realm_shared_.Reset(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 | 283 |
| 284 int PerIsolateData::RealmFind(Handle<Context> context) { | 284 int PerIsolateData::RealmFind(Handle<Context> context) { |
| 285 for (int i = 0; i < realm_count_; ++i) { | 285 for (int i = 0; i < realm_count_; ++i) { |
| 286 if (realms_[i] == context) return i; | 286 if (realms_[i] == context) return i; |
| 287 } | 287 } |
| 288 return -1; | 288 return -1; |
| 289 } | 289 } |
| 290 | 290 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 306 if (index == -1) return; | 306 if (index == -1) return; |
| 307 args.GetReturnValue().Set(index); | 307 args.GetReturnValue().Set(index); |
| 308 } | 308 } |
| 309 | 309 |
| 310 | 310 |
| 311 // 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. |
| 312 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) { | 312 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 313 Isolate* isolate = args.GetIsolate(); | 313 Isolate* isolate = args.GetIsolate(); |
| 314 PerIsolateData* data = PerIsolateData::Get(isolate); | 314 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 315 if (args.Length() < 1 || !args[0]->IsObject()) { | 315 if (args.Length() < 1 || !args[0]->IsObject()) { |
| 316 Throw("Invalid argument"); | 316 Throw(args.GetIsolate(), "Invalid argument"); |
| 317 return; | 317 return; |
| 318 } | 318 } |
| 319 int index = data->RealmFind(args[0]->ToObject()->CreationContext()); | 319 int index = data->RealmFind(args[0]->ToObject()->CreationContext()); |
| 320 if (index == -1) return; | 320 if (index == -1) return; |
| 321 args.GetReturnValue().Set(index); | 321 args.GetReturnValue().Set(index); |
| 322 } | 322 } |
| 323 | 323 |
| 324 | 324 |
| 325 // Realm.global(i) returns the global object of realm i. | 325 // Realm.global(i) returns the global object of realm i. |
| 326 // (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.) |
| 327 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 327 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 328 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); | 328 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); |
| 329 if (args.Length() < 1 || !args[0]->IsNumber()) { | 329 if (args.Length() < 1 || !args[0]->IsNumber()) { |
| 330 Throw("Invalid argument"); | 330 Throw(args.GetIsolate(), "Invalid argument"); |
| 331 return; | 331 return; |
| 332 } | 332 } |
| 333 int index = args[0]->Uint32Value(); | 333 int index = args[0]->Uint32Value(); |
| 334 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { | 334 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { |
| 335 Throw("Invalid realm index"); | 335 Throw(args.GetIsolate(), "Invalid realm index"); |
| 336 return; | 336 return; |
| 337 } | 337 } |
| 338 args.GetReturnValue().Set( | 338 args.GetReturnValue().Set( |
| 339 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); | 339 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); |
| 340 } | 340 } |
| 341 | 341 |
| 342 | 342 |
| 343 // Realm.create() creates a new realm and returns its index. | 343 // Realm.create() creates a new realm and returns its index. |
| 344 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 344 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 345 Isolate* isolate = args.GetIsolate(); | 345 Isolate* isolate = args.GetIsolate(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 356 isolate, Context::New(isolate, NULL, global_template)); | 356 isolate, Context::New(isolate, NULL, global_template)); |
| 357 args.GetReturnValue().Set(index); | 357 args.GetReturnValue().Set(index); |
| 358 } | 358 } |
| 359 | 359 |
| 360 | 360 |
| 361 // Realm.dispose(i) disposes the reference to the realm i. | 361 // Realm.dispose(i) disposes the reference to the realm i. |
| 362 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { | 362 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 363 Isolate* isolate = args.GetIsolate(); | 363 Isolate* isolate = args.GetIsolate(); |
| 364 PerIsolateData* data = PerIsolateData::Get(isolate); | 364 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 365 if (args.Length() < 1 || !args[0]->IsNumber()) { | 365 if (args.Length() < 1 || !args[0]->IsNumber()) { |
| 366 Throw("Invalid argument"); | 366 Throw(args.GetIsolate(), "Invalid argument"); |
| 367 return; | 367 return; |
| 368 } | 368 } |
| 369 int index = args[0]->Uint32Value(); | 369 int index = args[0]->Uint32Value(); |
| 370 if (index >= data->realm_count_ || data->realms_[index].IsEmpty() || | 370 if (index >= data->realm_count_ || data->realms_[index].IsEmpty() || |
| 371 index == 0 || | 371 index == 0 || |
| 372 index == data->realm_current_ || index == data->realm_switch_) { | 372 index == data->realm_current_ || index == data->realm_switch_) { |
| 373 Throw("Invalid realm index"); | 373 Throw(args.GetIsolate(), "Invalid realm index"); |
| 374 return; | 374 return; |
| 375 } | 375 } |
| 376 data->realms_[index].Dispose(); | 376 data->realms_[index].Reset(); |
| 377 data->realms_[index].Clear(); | |
| 378 } | 377 } |
| 379 | 378 |
| 380 | 379 |
| 381 // 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. |
| 382 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { | 381 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 383 Isolate* isolate = args.GetIsolate(); | 382 Isolate* isolate = args.GetIsolate(); |
| 384 PerIsolateData* data = PerIsolateData::Get(isolate); | 383 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 385 if (args.Length() < 1 || !args[0]->IsNumber()) { | 384 if (args.Length() < 1 || !args[0]->IsNumber()) { |
| 386 Throw("Invalid argument"); | 385 Throw(args.GetIsolate(), "Invalid argument"); |
| 387 return; | 386 return; |
| 388 } | 387 } |
| 389 int index = args[0]->Uint32Value(); | 388 int index = args[0]->Uint32Value(); |
| 390 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { | 389 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { |
| 391 Throw("Invalid realm index"); | 390 Throw(args.GetIsolate(), "Invalid realm index"); |
| 392 return; | 391 return; |
| 393 } | 392 } |
| 394 data->realm_switch_ = index; | 393 data->realm_switch_ = index; |
| 395 } | 394 } |
| 396 | 395 |
| 397 | 396 |
| 398 // 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. |
| 399 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { | 398 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 400 Isolate* isolate = args.GetIsolate(); | 399 Isolate* isolate = args.GetIsolate(); |
| 401 PerIsolateData* data = PerIsolateData::Get(isolate); | 400 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 402 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) { | 401 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) { |
| 403 Throw("Invalid argument"); | 402 Throw(args.GetIsolate(), "Invalid argument"); |
| 404 return; | 403 return; |
| 405 } | 404 } |
| 406 int index = args[0]->Uint32Value(); | 405 int index = args[0]->Uint32Value(); |
| 407 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { | 406 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { |
| 408 Throw("Invalid realm index"); | 407 Throw(args.GetIsolate(), "Invalid realm index"); |
| 409 return; | 408 return; |
| 410 } | 409 } |
| 411 Handle<Script> script = Script::New(args[1]->ToString()); | 410 Handle<Script> script = Script::New(args[1]->ToString()); |
| 412 if (script.IsEmpty()) return; | 411 if (script.IsEmpty()) return; |
| 413 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); | 412 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); |
| 414 realm->Enter(); | 413 realm->Enter(); |
| 415 Handle<Value> result = script->Run(); | 414 Handle<Value> result = script->Run(); |
| 416 realm->Exit(); | 415 realm->Exit(); |
| 417 args.GetReturnValue().Set(result); | 416 args.GetReturnValue().Set(result); |
| 418 } | 417 } |
| 419 | 418 |
| 420 | 419 |
| 421 // 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. |
| 422 void Shell::RealmSharedGet(Local<String> property, | 421 void Shell::RealmSharedGet(Local<String> property, |
| 423 const PropertyCallbackInfo<Value>& info) { | 422 const PropertyCallbackInfo<Value>& info) { |
| 424 Isolate* isolate = info.GetIsolate(); | 423 Isolate* isolate = info.GetIsolate(); |
| 425 PerIsolateData* data = PerIsolateData::Get(isolate); | 424 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 426 if (data->realm_shared_.IsEmpty()) return; | 425 if (data->realm_shared_.IsEmpty()) return; |
| 427 info.GetReturnValue().Set(data->realm_shared_); | 426 info.GetReturnValue().Set(data->realm_shared_); |
| 428 } | 427 } |
| 429 | 428 |
| 430 void Shell::RealmSharedSet(Local<String> property, | 429 void Shell::RealmSharedSet(Local<String> property, |
| 431 Local<Value> value, | 430 Local<Value> value, |
| 432 const PropertyCallbackInfo<void>& info) { | 431 const PropertyCallbackInfo<void>& info) { |
| 433 Isolate* isolate = info.GetIsolate(); | 432 Isolate* isolate = info.GetIsolate(); |
| 434 PerIsolateData* data = PerIsolateData::Get(isolate); | 433 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 435 if (!data->realm_shared_.IsEmpty()) data->realm_shared_.Dispose(); | |
| 436 data->realm_shared_.Reset(isolate, value); | 434 data->realm_shared_.Reset(isolate, value); |
| 437 } | 435 } |
| 438 | 436 |
| 439 | 437 |
| 440 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { | 438 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 441 Write(args); | 439 Write(args); |
| 442 printf("\n"); | 440 printf("\n"); |
| 443 fflush(stdout); | 441 fflush(stdout); |
| 444 } | 442 } |
| 445 | 443 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 465 printf("Error in fwrite\n"); | 463 printf("Error in fwrite\n"); |
| 466 Exit(1); | 464 Exit(1); |
| 467 } | 465 } |
| 468 } | 466 } |
| 469 } | 467 } |
| 470 | 468 |
| 471 | 469 |
| 472 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { | 470 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 473 String::Utf8Value file(args[0]); | 471 String::Utf8Value file(args[0]); |
| 474 if (*file == NULL) { | 472 if (*file == NULL) { |
| 475 Throw("Error loading file"); | 473 Throw(args.GetIsolate(), "Error loading file"); |
| 476 return; | 474 return; |
| 477 } | 475 } |
| 478 Handle<String> source = ReadFile(args.GetIsolate(), *file); | 476 Handle<String> source = ReadFile(args.GetIsolate(), *file); |
| 479 if (source.IsEmpty()) { | 477 if (source.IsEmpty()) { |
| 480 Throw("Error loading file"); | 478 Throw(args.GetIsolate(), "Error loading file"); |
| 481 return; | 479 return; |
| 482 } | 480 } |
| 483 args.GetReturnValue().Set(source); | 481 args.GetReturnValue().Set(source); |
| 484 } | 482 } |
| 485 | 483 |
| 486 | 484 |
| 487 Handle<String> Shell::ReadFromStdin(Isolate* isolate) { | 485 Handle<String> Shell::ReadFromStdin(Isolate* isolate) { |
| 488 static const int kBufferSize = 256; | 486 static const int kBufferSize = 256; |
| 489 char buffer[kBufferSize]; | 487 char buffer[kBufferSize]; |
| 490 Handle<String> accumulator = String::New(""); | 488 Handle<String> accumulator = String::NewFromUtf8(isolate, ""); |
| 491 int length; | 489 int length; |
| 492 while (true) { | 490 while (true) { |
| 493 // 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 |
| 494 // 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'). |
| 495 // If fgets gets an error, just give up. | 493 // If fgets gets an error, just give up. |
| 496 char* input = NULL; | 494 char* input = NULL; |
| 497 { // Release lock for blocking input. | 495 { // Release lock for blocking input. |
| 498 Unlocker unlock(isolate); | 496 Unlocker unlock(isolate); |
| 499 input = fgets(buffer, kBufferSize, stdin); | 497 input = fgets(buffer, kBufferSize, stdin); |
| 500 } | 498 } |
| 501 if (input == NULL) return Handle<String>(); | 499 if (input == NULL) return Handle<String>(); |
| 502 length = static_cast<int>(strlen(buffer)); | 500 length = static_cast<int>(strlen(buffer)); |
| 503 if (length == 0) { | 501 if (length == 0) { |
| 504 return accumulator; | 502 return accumulator; |
| 505 } else if (buffer[length-1] != '\n') { | 503 } else if (buffer[length-1] != '\n') { |
| 506 accumulator = String::Concat(accumulator, String::New(buffer, length)); | 504 accumulator = String::Concat( |
| 505 accumulator, |
| 506 String::NewFromUtf8(isolate, buffer, String::kNormalString, length)); |
| 507 } else if (length > 1 && buffer[length-2] == '\\') { | 507 } else if (length > 1 && buffer[length-2] == '\\') { |
| 508 buffer[length-2] = '\n'; | 508 buffer[length-2] = '\n'; |
| 509 accumulator = String::Concat(accumulator, String::New(buffer, length-1)); | 509 accumulator = String::Concat( |
| 510 accumulator, String::NewFromUtf8(isolate, buffer, |
| 511 String::kNormalString, length - 1)); |
| 510 } else { | 512 } else { |
| 511 return String::Concat(accumulator, String::New(buffer, length-1)); | 513 return String::Concat( |
| 514 accumulator, String::NewFromUtf8(isolate, buffer, |
| 515 String::kNormalString, length - 1)); |
| 512 } | 516 } |
| 513 } | 517 } |
| 514 } | 518 } |
| 515 | 519 |
| 516 | 520 |
| 517 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) { | 521 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 518 for (int i = 0; i < args.Length(); i++) { | 522 for (int i = 0; i < args.Length(); i++) { |
| 519 HandleScope handle_scope(args.GetIsolate()); | 523 HandleScope handle_scope(args.GetIsolate()); |
| 520 String::Utf8Value file(args[i]); | 524 String::Utf8Value file(args[i]); |
| 521 if (*file == NULL) { | 525 if (*file == NULL) { |
| 522 Throw("Error loading file"); | 526 Throw(args.GetIsolate(), "Error loading file"); |
| 523 return; | 527 return; |
| 524 } | 528 } |
| 525 Handle<String> source = ReadFile(args.GetIsolate(), *file); | 529 Handle<String> source = ReadFile(args.GetIsolate(), *file); |
| 526 if (source.IsEmpty()) { | 530 if (source.IsEmpty()) { |
| 527 Throw("Error loading file"); | 531 Throw(args.GetIsolate(), "Error loading file"); |
| 528 return; | 532 return; |
| 529 } | 533 } |
| 530 if (!ExecuteString(args.GetIsolate(), | 534 if (!ExecuteString(args.GetIsolate(), |
| 531 source, | 535 source, |
| 532 String::New(*file), | 536 String::NewFromUtf8(args.GetIsolate(), *file), |
| 533 false, | 537 false, |
| 534 true)) { | 538 true)) { |
| 535 Throw("Error executing file"); | 539 Throw(args.GetIsolate(), "Error executing file"); |
| 536 return; | 540 return; |
| 537 } | 541 } |
| 538 } | 542 } |
| 539 } | 543 } |
| 540 | 544 |
| 541 | 545 |
| 542 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { | 546 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 543 int exit_code = args[0]->Int32Value(); | 547 int exit_code = args[0]->Int32Value(); |
| 544 OnExit(); | 548 OnExit(); |
| 545 exit(exit_code); | 549 exit(exit_code); |
| 546 } | 550 } |
| 547 | 551 |
| 548 | 552 |
| 549 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { | 553 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 550 args.GetReturnValue().Set(String::New(V8::GetVersion())); | 554 args.GetReturnValue().Set( |
| 555 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion())); |
| 551 } | 556 } |
| 552 | 557 |
| 553 | 558 |
| 554 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { | 559 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { |
| 555 HandleScope handle_scope(isolate); | 560 HandleScope handle_scope(isolate); |
| 556 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) | 561 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
| 557 Handle<Context> utility_context; | 562 Handle<Context> utility_context; |
| 558 bool enter_context = !Context::InContext(); | 563 bool enter_context = !isolate->InContext(); |
| 559 if (enter_context) { | 564 if (enter_context) { |
| 560 utility_context = Local<Context>::New(isolate, utility_context_); | 565 utility_context = Local<Context>::New(isolate, utility_context_); |
| 561 utility_context->Enter(); | 566 utility_context->Enter(); |
| 562 } | 567 } |
| 563 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT | 568 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
| 564 v8::String::Utf8Value exception(try_catch->Exception()); | 569 v8::String::Utf8Value exception(try_catch->Exception()); |
| 565 const char* exception_string = ToCString(exception); | 570 const char* exception_string = ToCString(exception); |
| 566 Handle<Message> message = try_catch->Message(); | 571 Handle<Message> message = try_catch->Message(); |
| 567 if (message.IsEmpty()) { | 572 if (message.IsEmpty()) { |
| 568 // 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 Loading... |
| 603 | 608 |
| 604 #ifndef V8_SHARED | 609 #ifndef V8_SHARED |
| 605 Handle<Array> Shell::GetCompletions(Isolate* isolate, | 610 Handle<Array> Shell::GetCompletions(Isolate* isolate, |
| 606 Handle<String> text, | 611 Handle<String> text, |
| 607 Handle<String> full) { | 612 Handle<String> full) { |
| 608 HandleScope handle_scope(isolate); | 613 HandleScope handle_scope(isolate); |
| 609 v8::Local<v8::Context> utility_context = | 614 v8::Local<v8::Context> utility_context = |
| 610 v8::Local<v8::Context>::New(isolate, utility_context_); | 615 v8::Local<v8::Context>::New(isolate, utility_context_); |
| 611 v8::Context::Scope context_scope(utility_context); | 616 v8::Context::Scope context_scope(utility_context); |
| 612 Handle<Object> global = utility_context->Global(); | 617 Handle<Object> global = utility_context->Global(); |
| 613 Handle<Value> fun = global->Get(String::New("GetCompletions")); | 618 Handle<Value> fun = |
| 619 global->Get(String::NewFromUtf8(isolate, "GetCompletions")); |
| 614 static const int kArgc = 3; | 620 static const int kArgc = 3; |
| 615 v8::Local<v8::Context> evaluation_context = | 621 v8::Local<v8::Context> evaluation_context = |
| 616 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 622 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
| 617 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; | 623 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; |
| 618 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); | 624 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
| 619 return handle_scope.Close(Handle<Array>::Cast(val)); | 625 return handle_scope.Close(Handle<Array>::Cast(val)); |
| 620 } | 626 } |
| 621 | 627 |
| 622 | 628 |
| 623 #ifdef ENABLE_DEBUGGER_SUPPORT | 629 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 624 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate, | 630 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate, |
| 625 Handle<String> message) { | 631 Handle<String> message) { |
| 626 HandleScope handle_scope(isolate); | 632 HandleScope handle_scope(isolate); |
| 627 v8::Local<v8::Context> context = | 633 v8::Local<v8::Context> context = |
| 628 v8::Local<v8::Context>::New(isolate, utility_context_); | 634 v8::Local<v8::Context>::New(isolate, utility_context_); |
| 629 v8::Context::Scope context_scope(context); | 635 v8::Context::Scope context_scope(context); |
| 630 Handle<Object> global = context->Global(); | 636 Handle<Object> global = context->Global(); |
| 631 Handle<Value> fun = global->Get(String::New("DebugMessageDetails")); | 637 Handle<Value> fun = |
| 638 global->Get(String::NewFromUtf8(isolate, "DebugMessageDetails")); |
| 632 static const int kArgc = 1; | 639 static const int kArgc = 1; |
| 633 Handle<Value> argv[kArgc] = { message }; | 640 Handle<Value> argv[kArgc] = { message }; |
| 634 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); | 641 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
| 635 return Handle<Object>::Cast(val); | 642 return Handle<Object>::Cast(val); |
| 636 } | 643 } |
| 637 | 644 |
| 638 | 645 |
| 639 Handle<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, | 646 Handle<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, |
| 640 Handle<String> command) { | 647 Handle<String> command) { |
| 641 HandleScope handle_scope(isolate); | 648 HandleScope handle_scope(isolate); |
| 642 v8::Local<v8::Context> context = | 649 v8::Local<v8::Context> context = |
| 643 v8::Local<v8::Context>::New(isolate, utility_context_); | 650 v8::Local<v8::Context>::New(isolate, utility_context_); |
| 644 v8::Context::Scope context_scope(context); | 651 v8::Context::Scope context_scope(context); |
| 645 Handle<Object> global = context->Global(); | 652 Handle<Object> global = context->Global(); |
| 646 Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest")); | 653 Handle<Value> fun = |
| 654 global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest")); |
| 647 static const int kArgc = 1; | 655 static const int kArgc = 1; |
| 648 Handle<Value> argv[kArgc] = { command }; | 656 Handle<Value> argv[kArgc] = { command }; |
| 649 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); | 657 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); |
| 650 return val; | 658 return val; |
| 651 } | 659 } |
| 652 | 660 |
| 653 | 661 |
| 654 void Shell::DispatchDebugMessages() { | 662 void Shell::DispatchDebugMessages() { |
| 655 Isolate* isolate = v8::Isolate::GetCurrent(); | 663 Isolate* isolate = v8::Isolate::GetCurrent(); |
| 656 HandleScope handle_scope(isolate); | 664 HandleScope handle_scope(isolate); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 evaluation_context->SetSecurityToken(Undefined(isolate)); | 783 evaluation_context->SetSecurityToken(Undefined(isolate)); |
| 776 v8::Context::Scope context_scope(utility_context); | 784 v8::Context::Scope context_scope(utility_context); |
| 777 | 785 |
| 778 #ifdef ENABLE_DEBUGGER_SUPPORT | 786 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 779 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); | 787 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); |
| 780 // Install the debugger object in the utility scope | 788 // Install the debugger object in the utility scope |
| 781 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); | 789 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug(); |
| 782 debug->Load(); | 790 debug->Load(); |
| 783 i::Handle<i::JSObject> js_debug | 791 i::Handle<i::JSObject> js_debug |
| 784 = i::Handle<i::JSObject>(debug->debug_context()->global_object()); | 792 = i::Handle<i::JSObject>(debug->debug_context()->global_object()); |
| 785 utility_context->Global()->Set(String::New("$debug"), | 793 utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"), |
| 786 Utils::ToLocal(js_debug)); | 794 Utils::ToLocal(js_debug)); |
| 787 debug->debug_context()->set_security_token( | 795 debug->debug_context()->set_security_token( |
| 788 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); | 796 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value()); |
| 789 #endif // ENABLE_DEBUGGER_SUPPORT | 797 #endif // ENABLE_DEBUGGER_SUPPORT |
| 790 | 798 |
| 791 // Run the d8 shell utility script in the utility context | 799 // Run the d8 shell utility script in the utility context |
| 792 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); | 800 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
| 793 i::Vector<const char> shell_source = | 801 i::Vector<const char> shell_source = |
| 794 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); | 802 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); |
| 795 i::Vector<const char> shell_source_name = | 803 i::Vector<const char> shell_source_name = |
| 796 i::NativesCollection<i::D8>::GetScriptName(source_index); | 804 i::NativesCollection<i::D8>::GetScriptName(source_index); |
| 797 Handle<String> source = String::New(shell_source.start(), | 805 Handle<String> source = |
| 798 shell_source.length()); | 806 String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString, |
| 799 Handle<String> name = String::New(shell_source_name.start(), | 807 shell_source.length()); |
| 800 shell_source_name.length()); | 808 Handle<String> name = |
| 809 String::NewFromUtf8(isolate, shell_source_name.start(), |
| 810 String::kNormalString, shell_source_name.length()); |
| 801 Handle<Script> script = Script::Compile(source, name); | 811 Handle<Script> script = Script::Compile(source, name); |
| 802 script->Run(); | 812 script->Run(); |
| 803 // 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 |
| 804 // in the debugger. | 814 // in the debugger. |
| 805 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); | 815 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); |
| 806 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() | 816 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() |
| 807 ? i::Handle<i::Script>(i::Script::cast( | 817 ? i::Handle<i::Script>(i::Script::cast( |
| 808 i::JSFunction::cast(*compiled_script)->shared()->script())) | 818 i::JSFunction::cast(*compiled_script)->shared()->script())) |
| 809 : i::Handle<i::Script>(i::Script::cast( | 819 : i::Handle<i::Script>(i::Script::cast( |
| 810 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 Loading... |
| 843 *raw_data_size = decompressed_size; | 853 *raw_data_size = decompressed_size; |
| 844 } | 854 } |
| 845 return result; | 855 return result; |
| 846 } | 856 } |
| 847 }; | 857 }; |
| 848 #endif | 858 #endif |
| 849 | 859 |
| 850 | 860 |
| 851 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { | 861 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { |
| 852 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); | 862 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); |
| 853 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); | 863 global_template->Set(String::NewFromUtf8(isolate, "print"), |
| 854 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); | 864 FunctionTemplate::New(Print)); |
| 855 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); | 865 global_template->Set(String::NewFromUtf8(isolate, "write"), |
| 856 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"), |
| 857 FunctionTemplate::New(ReadBuffer)); | 870 FunctionTemplate::New(ReadBuffer)); |
| 858 global_template->Set(String::New("readline"), | 871 global_template->Set(String::NewFromUtf8(isolate, "readline"), |
| 859 FunctionTemplate::New(ReadLine)); | 872 FunctionTemplate::New(ReadLine)); |
| 860 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); | 873 global_template->Set(String::NewFromUtf8(isolate, "load"), |
| 861 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); | 874 FunctionTemplate::New(Load)); |
| 862 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)); |
| 863 | 879 |
| 864 // Bind the Realm object. | 880 // Bind the Realm object. |
| 865 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(); | 881 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(); |
| 866 realm_template->Set(String::New("current"), | 882 realm_template->Set(String::NewFromUtf8(isolate, "current"), |
| 867 FunctionTemplate::New(RealmCurrent)); | 883 FunctionTemplate::New(RealmCurrent)); |
| 868 realm_template->Set(String::New("owner"), | 884 realm_template->Set(String::NewFromUtf8(isolate, "owner"), |
| 869 FunctionTemplate::New(RealmOwner)); | 885 FunctionTemplate::New(RealmOwner)); |
| 870 realm_template->Set(String::New("global"), | 886 realm_template->Set(String::NewFromUtf8(isolate, "global"), |
| 871 FunctionTemplate::New(RealmGlobal)); | 887 FunctionTemplate::New(RealmGlobal)); |
| 872 realm_template->Set(String::New("create"), | 888 realm_template->Set(String::NewFromUtf8(isolate, "create"), |
| 873 FunctionTemplate::New(RealmCreate)); | 889 FunctionTemplate::New(RealmCreate)); |
| 874 realm_template->Set(String::New("dispose"), | 890 realm_template->Set(String::NewFromUtf8(isolate, "dispose"), |
| 875 FunctionTemplate::New(RealmDispose)); | 891 FunctionTemplate::New(RealmDispose)); |
| 876 realm_template->Set(String::New("switch"), | 892 realm_template->Set(String::NewFromUtf8(isolate, "switch"), |
| 877 FunctionTemplate::New(RealmSwitch)); | 893 FunctionTemplate::New(RealmSwitch)); |
| 878 realm_template->Set(String::New("eval"), | 894 realm_template->Set(String::NewFromUtf8(isolate, "eval"), |
| 879 FunctionTemplate::New(RealmEval)); | 895 FunctionTemplate::New(RealmEval)); |
| 880 realm_template->SetAccessor(String::New("shared"), | 896 realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"), |
| 881 RealmSharedGet, RealmSharedSet); | 897 RealmSharedGet, RealmSharedSet); |
| 882 global_template->Set(String::New("Realm"), realm_template); | 898 global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template); |
| 883 | 899 |
| 884 #ifndef V8_SHARED | 900 #ifndef V8_SHARED |
| 885 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(); | 901 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(); |
| 886 performance_template->Set(String::New("now"), | 902 performance_template->Set(String::NewFromUtf8(isolate, "now"), |
| 887 FunctionTemplate::New(PerformanceNow)); | 903 FunctionTemplate::New(PerformanceNow)); |
| 888 global_template->Set(String::New("performance"), performance_template); | 904 global_template->Set(String::NewFromUtf8(isolate, "performance"), |
| 905 performance_template); |
| 889 #endif // V8_SHARED | 906 #endif // V8_SHARED |
| 890 | 907 |
| 891 #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64) | 908 #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64) |
| 892 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); | 909 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); |
| 893 AddOSMethods(os_templ); | 910 AddOSMethods(isolate, os_templ); |
| 894 global_template->Set(String::New("os"), os_templ); | 911 global_template->Set(String::NewFromUtf8(isolate, "os"), os_templ); |
| 895 #endif // V8_SHARED | 912 #endif // V8_SHARED |
| 896 | 913 |
| 897 return global_template; | 914 return global_template; |
| 898 } | 915 } |
| 899 | 916 |
| 900 | 917 |
| 901 void Shell::Initialize(Isolate* isolate) { | 918 void Shell::Initialize(Isolate* isolate) { |
| 902 #ifdef COMPRESS_STARTUP_DATA_BZ2 | 919 #ifdef COMPRESS_STARTUP_DATA_BZ2 |
| 903 BZip2Decompressor startup_data_decompressor; | 920 BZip2Decompressor startup_data_decompressor; |
| 904 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 Loading... |
| 959 i::JSArguments js_args = i::FLAG_js_arguments; | 976 i::JSArguments js_args = i::FLAG_js_arguments; |
| 960 i::Handle<i::FixedArray> arguments_array = | 977 i::Handle<i::FixedArray> arguments_array = |
| 961 factory->NewFixedArray(js_args.argc); | 978 factory->NewFixedArray(js_args.argc); |
| 962 for (int j = 0; j < js_args.argc; j++) { | 979 for (int j = 0; j < js_args.argc; j++) { |
| 963 i::Handle<i::String> arg = | 980 i::Handle<i::String> arg = |
| 964 factory->NewStringFromUtf8(i::CStrVector(js_args[j])); | 981 factory->NewStringFromUtf8(i::CStrVector(js_args[j])); |
| 965 arguments_array->set(j, *arg); | 982 arguments_array->set(j, *arg); |
| 966 } | 983 } |
| 967 i::Handle<i::JSArray> arguments_jsarray = | 984 i::Handle<i::JSArray> arguments_jsarray = |
| 968 factory->NewJSArrayWithElements(arguments_array); | 985 factory->NewJSArrayWithElements(arguments_array); |
| 969 context->Global()->Set(String::New("arguments"), | 986 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"), |
| 970 Utils::ToLocal(arguments_jsarray)); | 987 Utils::ToLocal(arguments_jsarray)); |
| 971 #endif // V8_SHARED | 988 #endif // V8_SHARED |
| 972 return handle_scope.Close(context); | 989 return handle_scope.Close(context); |
| 973 } | 990 } |
| 974 | 991 |
| 975 | 992 |
| 976 void Shell::Exit(int exit_code) { | 993 void Shell::Exit(int exit_code) { |
| 977 // Use _exit instead of exit to avoid races between isolate | 994 // Use _exit instead of exit to avoid races between isolate |
| 978 // threads and static destructors. | 995 // threads and static destructors. |
| 979 fflush(stdout); | 996 fflush(stdout); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1082 | 1099 |
| 1083 static void ReadBufferWeakCallback(v8::Isolate* isolate, | 1100 static void ReadBufferWeakCallback(v8::Isolate* isolate, |
| 1084 Persistent<ArrayBuffer>* array_buffer, | 1101 Persistent<ArrayBuffer>* array_buffer, |
| 1085 uint8_t* data) { | 1102 uint8_t* data) { |
| 1086 size_t byte_length = | 1103 size_t byte_length = |
| 1087 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength(); | 1104 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength(); |
| 1088 isolate->AdjustAmountOfExternalAllocatedMemory( | 1105 isolate->AdjustAmountOfExternalAllocatedMemory( |
| 1089 -static_cast<intptr_t>(byte_length)); | 1106 -static_cast<intptr_t>(byte_length)); |
| 1090 | 1107 |
| 1091 delete[] data; | 1108 delete[] data; |
| 1092 array_buffer->Dispose(); | 1109 array_buffer->Reset(); |
| 1093 } | 1110 } |
| 1094 | 1111 |
| 1095 | 1112 |
| 1096 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1113 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 1097 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT | 1114 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT |
| 1098 String::Utf8Value filename(args[0]); | 1115 String::Utf8Value filename(args[0]); |
| 1099 int length; | 1116 int length; |
| 1100 if (*filename == NULL) { | 1117 if (*filename == NULL) { |
| 1101 Throw("Error loading file"); | 1118 Throw(args.GetIsolate(), "Error loading file"); |
| 1102 return; | 1119 return; |
| 1103 } | 1120 } |
| 1104 | 1121 |
| 1105 Isolate* isolate = args.GetIsolate(); | 1122 Isolate* isolate = args.GetIsolate(); |
| 1106 uint8_t* data = reinterpret_cast<uint8_t*>( | 1123 uint8_t* data = reinterpret_cast<uint8_t*>( |
| 1107 ReadChars(args.GetIsolate(), *filename, &length)); | 1124 ReadChars(args.GetIsolate(), *filename, &length)); |
| 1108 if (data == NULL) { | 1125 if (data == NULL) { |
| 1109 Throw("Error reading file"); | 1126 Throw(args.GetIsolate(), "Error reading file"); |
| 1110 return; | 1127 return; |
| 1111 } | 1128 } |
| 1112 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length); | 1129 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length); |
| 1113 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer); | 1130 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer); |
| 1114 weak_handle.MakeWeak(data, ReadBufferWeakCallback); | 1131 weak_handle.MakeWeak(data, ReadBufferWeakCallback); |
| 1115 weak_handle.MarkIndependent(); | 1132 weak_handle.MarkIndependent(); |
| 1116 isolate->AdjustAmountOfExternalAllocatedMemory(length); | 1133 isolate->AdjustAmountOfExternalAllocatedMemory(length); |
| 1117 | 1134 |
| 1118 args.GetReturnValue().Set(buffer); | 1135 args.GetReturnValue().Set(buffer); |
| 1119 } | 1136 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1140 return ReadToken(data, ' '); | 1157 return ReadToken(data, ' '); |
| 1141 } | 1158 } |
| 1142 #endif // V8_SHARED | 1159 #endif // V8_SHARED |
| 1143 | 1160 |
| 1144 | 1161 |
| 1145 // Reads a file into a v8 string. | 1162 // Reads a file into a v8 string. |
| 1146 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { | 1163 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { |
| 1147 int size = 0; | 1164 int size = 0; |
| 1148 char* chars = ReadChars(isolate, name, &size); | 1165 char* chars = ReadChars(isolate, name, &size); |
| 1149 if (chars == NULL) return Handle<String>(); | 1166 if (chars == NULL) return Handle<String>(); |
| 1150 Handle<String> result = String::New(chars, size); | 1167 Handle<String> result = |
| 1168 String::NewFromUtf8(isolate, chars, String::kNormalString, size); |
| 1151 delete[] chars; | 1169 delete[] chars; |
| 1152 return result; | 1170 return result; |
| 1153 } | 1171 } |
| 1154 | 1172 |
| 1155 | 1173 |
| 1156 void Shell::RunShell(Isolate* isolate) { | 1174 void Shell::RunShell(Isolate* isolate) { |
| 1157 Locker locker(isolate); | 1175 Locker locker(isolate); |
| 1158 HandleScope outer_scope(isolate); | 1176 HandleScope outer_scope(isolate); |
| 1159 v8::Local<v8::Context> context = | 1177 v8::Local<v8::Context> context = |
| 1160 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 1178 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
| 1161 v8::Context::Scope context_scope(context); | 1179 v8::Context::Scope context_scope(context); |
| 1162 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); | 1180 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); |
| 1163 Handle<String> name = String::New("(d8)"); | 1181 Handle<String> name = String::NewFromUtf8(isolate, "(d8)"); |
| 1164 LineEditor* console = LineEditor::Get(); | 1182 LineEditor* console = LineEditor::Get(); |
| 1165 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); | 1183 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); |
| 1166 console->Open(isolate); | 1184 console->Open(isolate); |
| 1167 while (true) { | 1185 while (true) { |
| 1168 HandleScope inner_scope(isolate); | 1186 HandleScope inner_scope(isolate); |
| 1169 Handle<String> input = console->Prompt(Shell::kPrompt); | 1187 Handle<String> input = console->Prompt(Shell::kPrompt); |
| 1170 if (input.IsEmpty()) break; | 1188 if (input.IsEmpty()) break; |
| 1171 ExecuteString(isolate, input, name, true, true); | 1189 ExecuteString(isolate, input, name, true, true); |
| 1172 } | 1190 } |
| 1173 printf("\n"); | 1191 printf("\n"); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1222 if (strlen(filename) == 0) { | 1240 if (strlen(filename) == 0) { |
| 1223 continue; | 1241 continue; |
| 1224 } | 1242 } |
| 1225 | 1243 |
| 1226 Handle<String> str = Shell::ReadFile(isolate_, filename); | 1244 Handle<String> str = Shell::ReadFile(isolate_, filename); |
| 1227 if (str.IsEmpty()) { | 1245 if (str.IsEmpty()) { |
| 1228 printf("File '%s' not found\n", filename); | 1246 printf("File '%s' not found\n", filename); |
| 1229 Shell::Exit(1); | 1247 Shell::Exit(1); |
| 1230 } | 1248 } |
| 1231 | 1249 |
| 1232 Shell::ExecuteString(isolate_, str, String::New(filename), false, false); | 1250 Shell::ExecuteString( |
| 1251 isolate_, str, String::NewFromUtf8(isolate_, filename), false, false); |
| 1233 } | 1252 } |
| 1234 | 1253 |
| 1235 ptr = next_line; | 1254 ptr = next_line; |
| 1236 } | 1255 } |
| 1237 } | 1256 } |
| 1238 #endif // V8_SHARED | 1257 #endif // V8_SHARED |
| 1239 | 1258 |
| 1240 | 1259 |
| 1241 SourceGroup::~SourceGroup() { | 1260 SourceGroup::~SourceGroup() { |
| 1242 #ifndef V8_SHARED | 1261 #ifndef V8_SHARED |
| 1243 delete thread_; | 1262 delete thread_; |
| 1244 thread_ = NULL; | 1263 thread_ = NULL; |
| 1245 #endif // V8_SHARED | 1264 #endif // V8_SHARED |
| 1246 } | 1265 } |
| 1247 | 1266 |
| 1248 | 1267 |
| 1249 void SourceGroup::Execute(Isolate* isolate) { | 1268 void SourceGroup::Execute(Isolate* isolate) { |
| 1250 bool exception_was_thrown = false; | 1269 bool exception_was_thrown = false; |
| 1251 for (int i = begin_offset_; i < end_offset_; ++i) { | 1270 for (int i = begin_offset_; i < end_offset_; ++i) { |
| 1252 const char* arg = argv_[i]; | 1271 const char* arg = argv_[i]; |
| 1253 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { | 1272 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { |
| 1254 // Execute argument given to -e option directly. | 1273 // Execute argument given to -e option directly. |
| 1255 HandleScope handle_scope(isolate); | 1274 HandleScope handle_scope(isolate); |
| 1256 Handle<String> file_name = String::New("unnamed"); | 1275 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); |
| 1257 Handle<String> source = String::New(argv_[i + 1]); | 1276 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]); |
| 1258 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1277 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { |
| 1259 exception_was_thrown = true; | 1278 exception_was_thrown = true; |
| 1260 break; | 1279 break; |
| 1261 } | 1280 } |
| 1262 ++i; | 1281 ++i; |
| 1263 } else if (arg[0] == '-') { | 1282 } else if (arg[0] == '-') { |
| 1264 // Ignore other options. They have been parsed already. | 1283 // Ignore other options. They have been parsed already. |
| 1265 } else { | 1284 } else { |
| 1266 // 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. |
| 1267 HandleScope handle_scope(isolate); | 1286 HandleScope handle_scope(isolate); |
| 1268 Handle<String> file_name = String::New(arg); | 1287 Handle<String> file_name = String::NewFromUtf8(isolate, arg); |
| 1269 Handle<String> source = ReadFile(isolate, arg); | 1288 Handle<String> source = ReadFile(isolate, arg); |
| 1270 if (source.IsEmpty()) { | 1289 if (source.IsEmpty()) { |
| 1271 printf("Error reading '%s'\n", arg); | 1290 printf("Error reading '%s'\n", arg); |
| 1272 Shell::Exit(1); | 1291 Shell::Exit(1); |
| 1273 } | 1292 } |
| 1274 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1293 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { |
| 1275 exception_was_thrown = true; | 1294 exception_was_thrown = true; |
| 1276 break; | 1295 break; |
| 1277 } | 1296 } |
| 1278 } | 1297 } |
| 1279 } | 1298 } |
| 1280 if (exception_was_thrown != Shell::options.expected_to_throw) { | 1299 if (exception_was_thrown != Shell::options.expected_to_throw) { |
| 1281 Shell::Exit(1); | 1300 Shell::Exit(1); |
| 1282 } | 1301 } |
| 1283 } | 1302 } |
| 1284 | 1303 |
| 1285 | 1304 |
| 1286 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { | 1305 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { |
| 1287 int size; | 1306 int size; |
| 1288 char* chars = ReadChars(isolate, name, &size); | 1307 char* chars = ReadChars(isolate, name, &size); |
| 1289 if (chars == NULL) return Handle<String>(); | 1308 if (chars == NULL) return Handle<String>(); |
| 1290 Handle<String> result = String::New(chars, size); | 1309 Handle<String> result = |
| 1310 String::NewFromUtf8(isolate, chars, String::kNormalString, size); |
| 1291 delete[] chars; | 1311 delete[] chars; |
| 1292 return result; | 1312 return result; |
| 1293 } | 1313 } |
| 1294 | 1314 |
| 1295 | 1315 |
| 1296 #ifndef V8_SHARED | 1316 #ifndef V8_SHARED |
| 1297 i::Thread::Options SourceGroup::GetThreadOptions() { | 1317 i::Thread::Options SourceGroup::GetThreadOptions() { |
| 1298 // 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 |
| 1299 // 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. |
| 1300 // The stack size should be at least StackGuard::kLimitSize + some | 1320 // The stack size should be at least StackGuard::kLimitSize + some |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1735 } | 1755 } |
| 1736 | 1756 |
| 1737 } // namespace v8 | 1757 } // namespace v8 |
| 1738 | 1758 |
| 1739 | 1759 |
| 1740 #ifndef GOOGLE3 | 1760 #ifndef GOOGLE3 |
| 1741 int main(int argc, char* argv[]) { | 1761 int main(int argc, char* argv[]) { |
| 1742 return v8::Shell::Main(argc, argv); | 1762 return v8::Shell::Main(argc, argv); |
| 1743 } | 1763 } |
| 1744 #endif | 1764 #endif |
| OLD | NEW |