| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <stdlib.h> | 6 #include <stdlib.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 850 // (Note that properties of global objects cannot be read/written cross-realm.) | 850 // (Note that properties of global objects cannot be read/written cross-realm.) |
| 851 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 851 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 852 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); | 852 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); |
| 853 int index = data->RealmIndexOrThrow(args, 0); | 853 int index = data->RealmIndexOrThrow(args, 0); |
| 854 if (index == -1) return; | 854 if (index == -1) return; |
| 855 args.GetReturnValue().Set( | 855 args.GetReturnValue().Set( |
| 856 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); | 856 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); |
| 857 } | 857 } |
| 858 | 858 |
| 859 MaybeLocal<Context> Shell::CreateRealm( | 859 MaybeLocal<Context> Shell::CreateRealm( |
| 860 const v8::FunctionCallbackInfo<v8::Value>& args) { | 860 const v8::FunctionCallbackInfo<v8::Value>& args, int index, |
| 861 v8::MaybeLocal<Value> global_object) { |
| 861 Isolate* isolate = args.GetIsolate(); | 862 Isolate* isolate = args.GetIsolate(); |
| 862 TryCatch try_catch(isolate); | 863 TryCatch try_catch(isolate); |
| 863 PerIsolateData* data = PerIsolateData::Get(isolate); | 864 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 864 Global<Context>* old_realms = data->realms_; | 865 if (index < 0) { |
| 865 int index = data->realm_count_; | 866 Global<Context>* old_realms = data->realms_; |
| 866 data->realms_ = new Global<Context>[++data->realm_count_]; | 867 index = data->realm_count_; |
| 867 for (int i = 0; i < index; ++i) { | 868 data->realms_ = new Global<Context>[++data->realm_count_]; |
| 868 data->realms_[i].Reset(isolate, old_realms[i]); | 869 for (int i = 0; i < index; ++i) { |
| 869 old_realms[i].Reset(); | 870 data->realms_[i].Reset(isolate, old_realms[i]); |
| 871 old_realms[i].Reset(); |
| 872 } |
| 873 delete[] old_realms; |
| 870 } | 874 } |
| 871 delete[] old_realms; | |
| 872 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 875 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
| 873 Local<Context> context = Context::New(isolate, NULL, global_template); | 876 Local<Context> context = |
| 877 Context::New(isolate, NULL, global_template, global_object); |
| 874 if (context.IsEmpty()) { | 878 if (context.IsEmpty()) { |
| 875 DCHECK(try_catch.HasCaught()); | 879 DCHECK(try_catch.HasCaught()); |
| 876 try_catch.ReThrow(); | 880 try_catch.ReThrow(); |
| 877 return MaybeLocal<Context>(); | 881 return MaybeLocal<Context>(); |
| 878 } | 882 } |
| 879 InitializeModuleEmbedderData(context); | 883 InitializeModuleEmbedderData(context); |
| 880 data->realms_[index].Reset(isolate, context); | 884 data->realms_[index].Reset(isolate, context); |
| 881 args.GetReturnValue().Set(index); | 885 args.GetReturnValue().Set(index); |
| 882 return context; | 886 return context; |
| 883 } | 887 } |
| 884 | 888 |
| 889 void Shell::DisposeRealm(const v8::FunctionCallbackInfo<v8::Value>& args, |
| 890 int index) { |
| 891 Isolate* isolate = args.GetIsolate(); |
| 892 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 893 DisposeModuleEmbedderData(data->realms_[index].Get(isolate)); |
| 894 data->realms_[index].Reset(); |
| 895 isolate->ContextDisposedNotification(); |
| 896 isolate->IdleNotificationDeadline(g_platform->MonotonicallyIncreasingTime()); |
| 897 } |
| 898 |
| 885 // Realm.create() creates a new realm with a distinct security token | 899 // Realm.create() creates a new realm with a distinct security token |
| 886 // and returns its index. | 900 // and returns its index. |
| 887 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 901 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 888 CreateRealm(args); | 902 CreateRealm(args, -1, v8::MaybeLocal<Value>()); |
| 889 } | 903 } |
| 890 | 904 |
| 891 // Realm.createAllowCrossRealmAccess() creates a new realm with the same | 905 // Realm.createAllowCrossRealmAccess() creates a new realm with the same |
| 892 // security token as the current realm. | 906 // security token as the current realm. |
| 893 void Shell::RealmCreateAllowCrossRealmAccess( | 907 void Shell::RealmCreateAllowCrossRealmAccess( |
| 894 const v8::FunctionCallbackInfo<v8::Value>& args) { | 908 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 895 Local<Context> context; | 909 Local<Context> context; |
| 896 if (CreateRealm(args).ToLocal(&context)) { | 910 if (CreateRealm(args, -1, v8::MaybeLocal<Value>()).ToLocal(&context)) { |
| 897 context->SetSecurityToken( | 911 context->SetSecurityToken( |
| 898 args.GetIsolate()->GetEnteredContext()->GetSecurityToken()); | 912 args.GetIsolate()->GetEnteredContext()->GetSecurityToken()); |
| 899 } | 913 } |
| 900 } | 914 } |
| 901 | 915 |
| 916 // Realm.navigate(i) creates a new realm with a distinct security token |
| 917 // in place of realm i. |
| 918 void Shell::RealmNavigate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 919 Isolate* isolate = args.GetIsolate(); |
| 920 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 921 int index = data->RealmIndexOrThrow(args, 0); |
| 922 if (index == -1) return; |
| 923 |
| 924 Local<Context> context = Local<Context>::New(isolate, data->realms_[index]); |
| 925 v8::MaybeLocal<Value> global_object = context->Global(); |
| 926 DisposeRealm(args, index); |
| 927 CreateRealm(args, index, global_object); |
| 928 } |
| 929 |
| 902 // Realm.dispose(i) disposes the reference to the realm i. | 930 // Realm.dispose(i) disposes the reference to the realm i. |
| 903 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { | 931 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 904 Isolate* isolate = args.GetIsolate(); | 932 Isolate* isolate = args.GetIsolate(); |
| 905 PerIsolateData* data = PerIsolateData::Get(isolate); | 933 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 906 int index = data->RealmIndexOrThrow(args, 0); | 934 int index = data->RealmIndexOrThrow(args, 0); |
| 907 if (index == -1) return; | 935 if (index == -1) return; |
| 908 if (index == 0 || | 936 if (index == 0 || |
| 909 index == data->realm_current_ || index == data->realm_switch_) { | 937 index == data->realm_current_ || index == data->realm_switch_) { |
| 910 Throw(args.GetIsolate(), "Invalid realm index"); | 938 Throw(args.GetIsolate(), "Invalid realm index"); |
| 911 return; | 939 return; |
| 912 } | 940 } |
| 913 DisposeModuleEmbedderData(data->realms_[index].Get(isolate)); | 941 DisposeRealm(args, index); |
| 914 data->realms_[index].Reset(); | |
| 915 isolate->ContextDisposedNotification(); | |
| 916 isolate->IdleNotificationDeadline(g_platform->MonotonicallyIncreasingTime()); | |
| 917 } | 942 } |
| 918 | 943 |
| 919 | 944 |
| 920 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. | 945 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. |
| 921 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { | 946 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 922 Isolate* isolate = args.GetIsolate(); | 947 Isolate* isolate = args.GetIsolate(); |
| 923 PerIsolateData* data = PerIsolateData::Get(isolate); | 948 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 924 int index = data->RealmIndexOrThrow(args, 0); | 949 int index = data->RealmIndexOrThrow(args, 0); |
| 925 if (index == -1) return; | 950 if (index == -1) return; |
| 926 data->realm_switch_ = index; | 951 data->realm_switch_ = index; |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1474 realm_template->Set( | 1499 realm_template->Set( |
| 1475 String::NewFromUtf8(isolate, "create", NewStringType::kNormal) | 1500 String::NewFromUtf8(isolate, "create", NewStringType::kNormal) |
| 1476 .ToLocalChecked(), | 1501 .ToLocalChecked(), |
| 1477 FunctionTemplate::New(isolate, RealmCreate)); | 1502 FunctionTemplate::New(isolate, RealmCreate)); |
| 1478 realm_template->Set( | 1503 realm_template->Set( |
| 1479 String::NewFromUtf8(isolate, "createAllowCrossRealmAccess", | 1504 String::NewFromUtf8(isolate, "createAllowCrossRealmAccess", |
| 1480 NewStringType::kNormal) | 1505 NewStringType::kNormal) |
| 1481 .ToLocalChecked(), | 1506 .ToLocalChecked(), |
| 1482 FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess)); | 1507 FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess)); |
| 1483 realm_template->Set( | 1508 realm_template->Set( |
| 1509 String::NewFromUtf8(isolate, "navigate", NewStringType::kNormal) |
| 1510 .ToLocalChecked(), |
| 1511 FunctionTemplate::New(isolate, RealmNavigate)); |
| 1512 realm_template->Set( |
| 1484 String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal) | 1513 String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal) |
| 1485 .ToLocalChecked(), | 1514 .ToLocalChecked(), |
| 1486 FunctionTemplate::New(isolate, RealmDispose)); | 1515 FunctionTemplate::New(isolate, RealmDispose)); |
| 1487 realm_template->Set( | 1516 realm_template->Set( |
| 1488 String::NewFromUtf8(isolate, "switch", NewStringType::kNormal) | 1517 String::NewFromUtf8(isolate, "switch", NewStringType::kNormal) |
| 1489 .ToLocalChecked(), | 1518 .ToLocalChecked(), |
| 1490 FunctionTemplate::New(isolate, RealmSwitch)); | 1519 FunctionTemplate::New(isolate, RealmSwitch)); |
| 1491 realm_template->Set( | 1520 realm_template->Set( |
| 1492 String::NewFromUtf8(isolate, "eval", NewStringType::kNormal) | 1521 String::NewFromUtf8(isolate, "eval", NewStringType::kNormal) |
| 1493 .ToLocalChecked(), | 1522 .ToLocalChecked(), |
| (...skipping 1502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2996 } | 3025 } |
| 2997 | 3026 |
| 2998 } // namespace v8 | 3027 } // namespace v8 |
| 2999 | 3028 |
| 3000 | 3029 |
| 3001 #ifndef GOOGLE3 | 3030 #ifndef GOOGLE3 |
| 3002 int main(int argc, char* argv[]) { | 3031 int main(int argc, char* argv[]) { |
| 3003 return v8::Shell::Main(argc, argv); | 3032 return v8::Shell::Main(argc, argv); |
| 3004 } | 3033 } |
| 3005 #endif | 3034 #endif |
| OLD | NEW |