| 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 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 // (Note that properties of global objects cannot be read/written cross-realm.) | 855 // (Note that properties of global objects cannot be read/written cross-realm.) |
| 856 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { | 856 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 857 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); | 857 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); |
| 858 int index = data->RealmIndexOrThrow(args, 0); | 858 int index = data->RealmIndexOrThrow(args, 0); |
| 859 if (index == -1) return; | 859 if (index == -1) return; |
| 860 args.GetReturnValue().Set( | 860 args.GetReturnValue().Set( |
| 861 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); | 861 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); |
| 862 } | 862 } |
| 863 | 863 |
| 864 MaybeLocal<Context> Shell::CreateRealm( | 864 MaybeLocal<Context> Shell::CreateRealm( |
| 865 const v8::FunctionCallbackInfo<v8::Value>& args) { | 865 const v8::FunctionCallbackInfo<v8::Value>& args, int index, |
| 866 v8::MaybeLocal<Value> global_object) { |
| 866 Isolate* isolate = args.GetIsolate(); | 867 Isolate* isolate = args.GetIsolate(); |
| 867 TryCatch try_catch(isolate); | 868 TryCatch try_catch(isolate); |
| 868 PerIsolateData* data = PerIsolateData::Get(isolate); | 869 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 869 Global<Context>* old_realms = data->realms_; | 870 if (index < 0) { |
| 870 int index = data->realm_count_; | 871 Global<Context>* old_realms = data->realms_; |
| 871 data->realms_ = new Global<Context>[++data->realm_count_]; | 872 index = data->realm_count_; |
| 872 for (int i = 0; i < index; ++i) { | 873 data->realms_ = new Global<Context>[++data->realm_count_]; |
| 873 data->realms_[i].Reset(isolate, old_realms[i]); | 874 for (int i = 0; i < index; ++i) { |
| 874 old_realms[i].Reset(); | 875 data->realms_[i].Reset(isolate, old_realms[i]); |
| 876 old_realms[i].Reset(); |
| 877 } |
| 878 delete[] old_realms; |
| 875 } | 879 } |
| 876 delete[] old_realms; | |
| 877 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 880 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
| 878 Local<Context> context = Context::New(isolate, NULL, global_template); | 881 Local<Context> context = |
| 882 Context::New(isolate, NULL, global_template, global_object); |
| 879 if (context.IsEmpty()) { | 883 if (context.IsEmpty()) { |
| 880 DCHECK(try_catch.HasCaught()); | 884 DCHECK(try_catch.HasCaught()); |
| 881 try_catch.ReThrow(); | 885 try_catch.ReThrow(); |
| 882 return MaybeLocal<Context>(); | 886 return MaybeLocal<Context>(); |
| 883 } | 887 } |
| 884 InitializeModuleEmbedderData(context); | 888 InitializeModuleEmbedderData(context); |
| 885 data->realms_[index].Reset(isolate, context); | 889 data->realms_[index].Reset(isolate, context); |
| 886 args.GetReturnValue().Set(index); | 890 args.GetReturnValue().Set(index); |
| 887 return context; | 891 return context; |
| 888 } | 892 } |
| 889 | 893 |
| 894 void Shell::DisposeRealm(const v8::FunctionCallbackInfo<v8::Value>& args, |
| 895 int index) { |
| 896 Isolate* isolate = args.GetIsolate(); |
| 897 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 898 DisposeModuleEmbedderData(data->realms_[index].Get(isolate)); |
| 899 data->realms_[index].Reset(); |
| 900 isolate->ContextDisposedNotification(); |
| 901 isolate->IdleNotificationDeadline(g_platform->MonotonicallyIncreasingTime()); |
| 902 } |
| 903 |
| 890 // Realm.create() creates a new realm with a distinct security token | 904 // Realm.create() creates a new realm with a distinct security token |
| 891 // and returns its index. | 905 // and returns its index. |
| 892 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { | 906 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 893 CreateRealm(args); | 907 CreateRealm(args, -1, v8::MaybeLocal<Value>()); |
| 894 } | 908 } |
| 895 | 909 |
| 896 // Realm.createAllowCrossRealmAccess() creates a new realm with the same | 910 // Realm.createAllowCrossRealmAccess() creates a new realm with the same |
| 897 // security token as the current realm. | 911 // security token as the current realm. |
| 898 void Shell::RealmCreateAllowCrossRealmAccess( | 912 void Shell::RealmCreateAllowCrossRealmAccess( |
| 899 const v8::FunctionCallbackInfo<v8::Value>& args) { | 913 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 900 Local<Context> context; | 914 Local<Context> context; |
| 901 if (CreateRealm(args).ToLocal(&context)) { | 915 if (CreateRealm(args, -1, v8::MaybeLocal<Value>()).ToLocal(&context)) { |
| 902 context->SetSecurityToken( | 916 context->SetSecurityToken( |
| 903 args.GetIsolate()->GetEnteredContext()->GetSecurityToken()); | 917 args.GetIsolate()->GetEnteredContext()->GetSecurityToken()); |
| 904 } | 918 } |
| 905 } | 919 } |
| 906 | 920 |
| 921 // Realm.navigate(i) creates a new realm with a distinct security token |
| 922 // in place of realm i. |
| 923 void Shell::RealmNavigate(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 924 Isolate* isolate = args.GetIsolate(); |
| 925 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 926 int index = data->RealmIndexOrThrow(args, 0); |
| 927 if (index == -1) return; |
| 928 |
| 929 Local<Context> context = Local<Context>::New(isolate, data->realms_[index]); |
| 930 v8::MaybeLocal<Value> global_object = context->Global(); |
| 931 DisposeRealm(args, index); |
| 932 CreateRealm(args, index, global_object); |
| 933 } |
| 934 |
| 907 // Realm.dispose(i) disposes the reference to the realm i. | 935 // Realm.dispose(i) disposes the reference to the realm i. |
| 908 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { | 936 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 909 Isolate* isolate = args.GetIsolate(); | 937 Isolate* isolate = args.GetIsolate(); |
| 910 PerIsolateData* data = PerIsolateData::Get(isolate); | 938 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 911 int index = data->RealmIndexOrThrow(args, 0); | 939 int index = data->RealmIndexOrThrow(args, 0); |
| 912 if (index == -1) return; | 940 if (index == -1) return; |
| 913 if (index == 0 || | 941 if (index == 0 || |
| 914 index == data->realm_current_ || index == data->realm_switch_) { | 942 index == data->realm_current_ || index == data->realm_switch_) { |
| 915 Throw(args.GetIsolate(), "Invalid realm index"); | 943 Throw(args.GetIsolate(), "Invalid realm index"); |
| 916 return; | 944 return; |
| 917 } | 945 } |
| 918 DisposeModuleEmbedderData(data->realms_[index].Get(isolate)); | 946 DisposeRealm(args, index); |
| 919 data->realms_[index].Reset(); | |
| 920 isolate->ContextDisposedNotification(); | |
| 921 isolate->IdleNotificationDeadline(g_platform->MonotonicallyIncreasingTime()); | |
| 922 } | 947 } |
| 923 | 948 |
| 924 | 949 |
| 925 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. | 950 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. |
| 926 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { | 951 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 927 Isolate* isolate = args.GetIsolate(); | 952 Isolate* isolate = args.GetIsolate(); |
| 928 PerIsolateData* data = PerIsolateData::Get(isolate); | 953 PerIsolateData* data = PerIsolateData::Get(isolate); |
| 929 int index = data->RealmIndexOrThrow(args, 0); | 954 int index = data->RealmIndexOrThrow(args, 0); |
| 930 if (index == -1) return; | 955 if (index == -1) return; |
| 931 data->realm_switch_ = index; | 956 data->realm_switch_ = index; |
| (...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1505 realm_template->Set( | 1530 realm_template->Set( |
| 1506 String::NewFromUtf8(isolate, "create", NewStringType::kNormal) | 1531 String::NewFromUtf8(isolate, "create", NewStringType::kNormal) |
| 1507 .ToLocalChecked(), | 1532 .ToLocalChecked(), |
| 1508 FunctionTemplate::New(isolate, RealmCreate)); | 1533 FunctionTemplate::New(isolate, RealmCreate)); |
| 1509 realm_template->Set( | 1534 realm_template->Set( |
| 1510 String::NewFromUtf8(isolate, "createAllowCrossRealmAccess", | 1535 String::NewFromUtf8(isolate, "createAllowCrossRealmAccess", |
| 1511 NewStringType::kNormal) | 1536 NewStringType::kNormal) |
| 1512 .ToLocalChecked(), | 1537 .ToLocalChecked(), |
| 1513 FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess)); | 1538 FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess)); |
| 1514 realm_template->Set( | 1539 realm_template->Set( |
| 1540 String::NewFromUtf8(isolate, "navigate", NewStringType::kNormal) |
| 1541 .ToLocalChecked(), |
| 1542 FunctionTemplate::New(isolate, RealmNavigate)); |
| 1543 realm_template->Set( |
| 1515 String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal) | 1544 String::NewFromUtf8(isolate, "dispose", NewStringType::kNormal) |
| 1516 .ToLocalChecked(), | 1545 .ToLocalChecked(), |
| 1517 FunctionTemplate::New(isolate, RealmDispose)); | 1546 FunctionTemplate::New(isolate, RealmDispose)); |
| 1518 realm_template->Set( | 1547 realm_template->Set( |
| 1519 String::NewFromUtf8(isolate, "switch", NewStringType::kNormal) | 1548 String::NewFromUtf8(isolate, "switch", NewStringType::kNormal) |
| 1520 .ToLocalChecked(), | 1549 .ToLocalChecked(), |
| 1521 FunctionTemplate::New(isolate, RealmSwitch)); | 1550 FunctionTemplate::New(isolate, RealmSwitch)); |
| 1522 realm_template->Set( | 1551 realm_template->Set( |
| 1523 String::NewFromUtf8(isolate, "eval", NewStringType::kNormal) | 1552 String::NewFromUtf8(isolate, "eval", NewStringType::kNormal) |
| 1524 .ToLocalChecked(), | 1553 .ToLocalChecked(), |
| (...skipping 1556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3081 } | 3110 } |
| 3082 | 3111 |
| 3083 } // namespace v8 | 3112 } // namespace v8 |
| 3084 | 3113 |
| 3085 | 3114 |
| 3086 #ifndef GOOGLE3 | 3115 #ifndef GOOGLE3 |
| 3087 int main(int argc, char* argv[]) { | 3116 int main(int argc, char* argv[]) { |
| 3088 return v8::Shell::Main(argc, argv); | 3117 return v8::Shell::Main(argc, argv); |
| 3089 } | 3118 } |
| 3090 #endif | 3119 #endif |
| OLD | NEW |