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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 private: | 112 private: |
113 friend class Shell; | 113 friend class Shell; |
114 friend class RealmScope; | 114 friend class RealmScope; |
115 Isolate* isolate_; | 115 Isolate* isolate_; |
116 int realm_count_; | 116 int realm_count_; |
117 int realm_current_; | 117 int realm_current_; |
118 int realm_switch_; | 118 int realm_switch_; |
119 Persistent<Context>* realms_; | 119 Persistent<Context>* realms_; |
120 Persistent<Value> realm_shared_; | 120 Persistent<Value> realm_shared_; |
121 | 121 |
| 122 int RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value>& args, |
| 123 int arg_offset); |
122 int RealmFind(Handle<Context> context); | 124 int RealmFind(Handle<Context> context); |
123 }; | 125 }; |
124 | 126 |
125 | 127 |
126 LineEditor *LineEditor::current_ = NULL; | 128 LineEditor *LineEditor::current_ = NULL; |
127 | 129 |
128 | 130 |
129 LineEditor::LineEditor(Type type, const char* name) | 131 LineEditor::LineEditor(Type type, const char* name) |
130 : type_(type), name_(name) { | 132 : type_(type), name_(name) { |
131 if (current_ == NULL || current_->type_ < type) current_ = this; | 133 if (current_ == NULL || current_->type_ < type) current_ = this; |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 283 |
282 | 284 |
283 int PerIsolateData::RealmFind(Handle<Context> context) { | 285 int PerIsolateData::RealmFind(Handle<Context> context) { |
284 for (int i = 0; i < realm_count_; ++i) { | 286 for (int i = 0; i < realm_count_; ++i) { |
285 if (realms_[i] == context) return i; | 287 if (realms_[i] == context) return i; |
286 } | 288 } |
287 return -1; | 289 return -1; |
288 } | 290 } |
289 | 291 |
290 | 292 |
| 293 int PerIsolateData::RealmIndexOrThrow( |
| 294 const v8::FunctionCallbackInfo<v8::Value>& args, |
| 295 int arg_offset) { |
| 296 if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) { |
| 297 Throw(args.GetIsolate(), "Invalid argument"); |
| 298 return -1; |
| 299 } |
| 300 int index = args[arg_offset]->Int32Value(); |
| 301 if (index < 0 || |
| 302 index >= realm_count_ || |
| 303 realms_[index].IsEmpty()) { |
| 304 Throw(args.GetIsolate(), "Invalid realm index"); |
| 305 return -1; |
| 306 } |
| 307 return index; |
| 308 } |
| 309 |
| 310 |
291 #ifndef V8_SHARED | 311 #ifndef V8_SHARED |
292 // performance.now() returns a time stamp as double, measured in milliseconds. | 312 // performance.now() returns a time stamp as double, measured in milliseconds. |
293 void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) { | 313 void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) { |
294 i::TimeDelta delta = i::TimeTicks::HighResolutionNow() - kInitialTicks; | 314 i::TimeDelta delta = i::TimeTicks::HighResolutionNow() - kInitialTicks; |
295 args.GetReturnValue().Set(delta.InMillisecondsF()); | 315 args.GetReturnValue().Set(delta.InMillisecondsF()); |
296 } | 316 } |
297 #endif // V8_SHARED | 317 #endif // V8_SHARED |
298 | 318 |
299 | 319 |
300 // Realm.current() returns the index of the currently active realm. | 320 // Realm.current() returns the index of the currently active realm. |
(...skipping 17 matching lines...) Expand all Loading... |
318 int index = data->RealmFind(args[0]->ToObject()->CreationContext()); | 338 int index = data->RealmFind(args[0]->ToObject()->CreationContext()); |
319 if (index == -1) return; | 339 if (index == -1) return; |
320 args.GetReturnValue().Set(index); | 340 args.GetReturnValue().Set(index); |
321 } | 341 } |
322 | 342 |
323 | 343 |
324 // Realm.global(i) returns the global object of realm i. | 344 // Realm.global(i) returns the global object of realm i. |
325 // (Note that properties of global objects cannot be read/written cross-realm.) | 345 // (Note that properties of global objects cannot be read/written cross-realm.) |
326 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 346 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
327 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); | 347 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); |
328 if (args.Length() < 1 || !args[0]->IsNumber()) { | 348 int index = data->RealmIndexOrThrow(args, 0); |
329 Throw(args.GetIsolate(), "Invalid argument"); | 349 if (index == -1) return; |
330 return; | |
331 } | |
332 int index = args[0]->Uint32Value(); | |
333 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { | |
334 Throw(args.GetIsolate(), "Invalid realm index"); | |
335 return; | |
336 } | |
337 args.GetReturnValue().Set( | 350 args.GetReturnValue().Set( |
338 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); | 351 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); |
339 } | 352 } |
340 | 353 |
341 | 354 |
342 // Realm.create() creates a new realm and returns its index. | 355 // Realm.create() creates a new realm and returns its index. |
343 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 356 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
344 Isolate* isolate = args.GetIsolate(); | 357 Isolate* isolate = args.GetIsolate(); |
345 PerIsolateData* data = PerIsolateData::Get(isolate); | 358 PerIsolateData* data = PerIsolateData::Get(isolate); |
346 Persistent<Context>* old_realms = data->realms_; | 359 Persistent<Context>* old_realms = data->realms_; |
347 int index = data->realm_count_; | 360 int index = data->realm_count_; |
348 data->realms_ = new Persistent<Context>[++data->realm_count_]; | 361 data->realms_ = new Persistent<Context>[++data->realm_count_]; |
349 for (int i = 0; i < index; ++i) { | 362 for (int i = 0; i < index; ++i) { |
350 data->realms_[i].Reset(isolate, old_realms[i]); | 363 data->realms_[i].Reset(isolate, old_realms[i]); |
351 } | 364 } |
352 delete[] old_realms; | 365 delete[] old_realms; |
353 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 366 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
354 data->realms_[index].Reset( | 367 data->realms_[index].Reset( |
355 isolate, Context::New(isolate, NULL, global_template)); | 368 isolate, Context::New(isolate, NULL, global_template)); |
356 args.GetReturnValue().Set(index); | 369 args.GetReturnValue().Set(index); |
357 } | 370 } |
358 | 371 |
359 | 372 |
360 // Realm.dispose(i) disposes the reference to the realm i. | 373 // Realm.dispose(i) disposes the reference to the realm i. |
361 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { | 374 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { |
362 Isolate* isolate = args.GetIsolate(); | 375 Isolate* isolate = args.GetIsolate(); |
363 PerIsolateData* data = PerIsolateData::Get(isolate); | 376 PerIsolateData* data = PerIsolateData::Get(isolate); |
364 if (args.Length() < 1 || !args[0]->IsNumber()) { | 377 int index = data->RealmIndexOrThrow(args, 0); |
365 Throw(args.GetIsolate(), "Invalid argument"); | 378 if (index == -1) return; |
366 return; | 379 if (index == 0 || |
367 } | |
368 int index = args[0]->Uint32Value(); | |
369 if (index >= data->realm_count_ || data->realms_[index].IsEmpty() || | |
370 index == 0 || | |
371 index == data->realm_current_ || index == data->realm_switch_) { | 380 index == data->realm_current_ || index == data->realm_switch_) { |
372 Throw(args.GetIsolate(), "Invalid realm index"); | 381 Throw(args.GetIsolate(), "Invalid realm index"); |
373 return; | 382 return; |
374 } | 383 } |
375 data->realms_[index].Reset(); | 384 data->realms_[index].Reset(); |
376 } | 385 } |
377 | 386 |
378 | 387 |
379 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. | 388 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. |
380 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { | 389 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { |
381 Isolate* isolate = args.GetIsolate(); | 390 Isolate* isolate = args.GetIsolate(); |
382 PerIsolateData* data = PerIsolateData::Get(isolate); | 391 PerIsolateData* data = PerIsolateData::Get(isolate); |
383 if (args.Length() < 1 || !args[0]->IsNumber()) { | 392 int index = data->RealmIndexOrThrow(args, 0); |
384 Throw(args.GetIsolate(), "Invalid argument"); | 393 if (index == -1) return; |
385 return; | |
386 } | |
387 int index = args[0]->Uint32Value(); | |
388 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { | |
389 Throw(args.GetIsolate(), "Invalid realm index"); | |
390 return; | |
391 } | |
392 data->realm_switch_ = index; | 394 data->realm_switch_ = index; |
393 } | 395 } |
394 | 396 |
395 | 397 |
396 // Realm.eval(i, s) evaluates s in realm i and returns the result. | 398 // Realm.eval(i, s) evaluates s in realm i and returns the result. |
397 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { | 399 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { |
398 Isolate* isolate = args.GetIsolate(); | 400 Isolate* isolate = args.GetIsolate(); |
399 PerIsolateData* data = PerIsolateData::Get(isolate); | 401 PerIsolateData* data = PerIsolateData::Get(isolate); |
400 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) { | 402 int index = data->RealmIndexOrThrow(args, 0); |
| 403 if (index == -1) return; |
| 404 if (args.Length() < 2 || !args[1]->IsString()) { |
401 Throw(args.GetIsolate(), "Invalid argument"); | 405 Throw(args.GetIsolate(), "Invalid argument"); |
402 return; | 406 return; |
403 } | 407 } |
404 int index = args[0]->Uint32Value(); | |
405 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { | |
406 Throw(args.GetIsolate(), "Invalid realm index"); | |
407 return; | |
408 } | |
409 Handle<Script> script = Script::New(args[1]->ToString()); | 408 Handle<Script> script = Script::New(args[1]->ToString()); |
410 if (script.IsEmpty()) return; | 409 if (script.IsEmpty()) return; |
411 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); | 410 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); |
412 realm->Enter(); | 411 realm->Enter(); |
413 Handle<Value> result = script->Run(); | 412 Handle<Value> result = script->Run(); |
414 realm->Exit(); | 413 realm->Exit(); |
415 args.GetReturnValue().Set(result); | 414 args.GetReturnValue().Set(result); |
416 } | 415 } |
417 | 416 |
418 | 417 |
(...skipping 1345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1764 } | 1763 } |
1765 | 1764 |
1766 } // namespace v8 | 1765 } // namespace v8 |
1767 | 1766 |
1768 | 1767 |
1769 #ifndef GOOGLE3 | 1768 #ifndef GOOGLE3 |
1770 int main(int argc, char* argv[]) { | 1769 int main(int argc, char* argv[]) { |
1771 return v8::Shell::Main(argc, argv); | 1770 return v8::Shell::Main(argc, argv); |
1772 } | 1771 } |
1773 #endif | 1772 #endif |
OLD | NEW |