Add d8 functionality for switching between realms (a.k.a. contexts)
authorrossberg@chromium.org <rossberg@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 17 Apr 2013 15:07:31 +0000 (15:07 +0000)
committerrossberg@chromium.org <rossberg@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Wed, 17 Apr 2013 15:07:31 +0000 (15:07 +0000)
R=mstarzinger@chromium.org,yangguo@chromium.org
BUG=

Review URL: https://codereview.chromium.org/14295011

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14310 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/d8.cc
src/d8.h
src/debug.cc
src/objects.h
test/mjsunit/bugs/bug-proto.js [new file with mode: 0644]

index 8f6e384..e52e9a5 100644 (file)
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -83,7 +83,7 @@ const char kArrayBufferMarkerPropName[] = "d8::_is_array_buffer_";
 const char kArrayMarkerPropName[] = "d8::_is_typed_array_";
 
 
-#define FOR_EACH_SYMBOL(V) \
+#define FOR_EACH_STRING(V) \
   V(ArrayBuffer, "ArrayBuffer") \
   V(ArrayBufferMarkerPropName, kArrayBufferMarkerPropName) \
   V(ArrayMarkerPropName, kArrayMarkerPropName) \
@@ -94,36 +94,50 @@ const char kArrayMarkerPropName[] = "d8::_is_typed_array_";
   V(length, "length")
 
 
-class Symbols {
+class PerIsolateData {
  public:
-  explicit Symbols(Isolate* isolate) : isolate_(isolate) {
+  explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) {
     HandleScope scope(isolate);
-#define INIT_SYMBOL(name, value) \
-    name##_ = Persistent<String>::New(isolate, String::NewSymbol(value));
-    FOR_EACH_SYMBOL(INIT_SYMBOL)
-#undef INIT_SYMBOL
+#define INIT_STRING(name, value) \
+    name##_string_ = Persistent<String>::New(isolate, String::NewSymbol(value));
+    FOR_EACH_STRING(INIT_STRING)
+#undef INIT_STRING
     isolate->SetData(this);
   }
 
-  ~Symbols() {
-#define DISPOSE_SYMBOL(name, value) name##_.Dispose(isolate_);
-    FOR_EACH_SYMBOL(DISPOSE_SYMBOL)
-#undef DISPOSE_SYMBOL
+  ~PerIsolateData() {
+#define DISPOSE_STRING(name, value) name##_string_.Dispose(isolate_);
+    FOR_EACH_STRING(DISPOSE_STRING)
+#undef DISPOSE_STRING
     isolate_->SetData(NULL);  // Not really needed, just to be sure...
   }
 
-#define DEFINE_SYMBOL_GETTER(name, value) \
-  static Persistent<String> name(Isolate* isolate) { \
-    return reinterpret_cast<Symbols*>(isolate->GetData())->name##_; \
+  inline static PerIsolateData* Get(Isolate* isolate) {
+    return reinterpret_cast<PerIsolateData*>(isolate->GetData());
   }
-  FOR_EACH_SYMBOL(DEFINE_SYMBOL_GETTER)
-#undef DEFINE_SYMBOL_GETTER
+
+#define DEFINE_STRING_GETTER(name, value) \
+  static Persistent<String> name##_string(Isolate* isolate) { \
+    return Get(isolate)->name##_string_; \
+  }
+  FOR_EACH_STRING(DEFINE_STRING_GETTER)
+#undef DEFINE_STRING_GETTER
 
  private:
+  friend class Shell;
   Isolate* isolate_;
-#define DEFINE_MEMBER(name, value) Persistent<String> name##_;
-  FOR_EACH_SYMBOL(DEFINE_MEMBER)
+  int realm_count_;
+  int realm_current_;
+  int realm_switch_;
+  Persistent<Context>* realms_;
+  Persistent<Value> realm_shared_;
+
+#define DEFINE_MEMBER(name, value) Persistent<String> name##_string_;
+  FOR_EACH_STRING(DEFINE_MEMBER)
 #undef DEFINE_MEMBER
+
+  void RealmInit();
+  int RealmFind(Handle<Context> context);
 };
 
 
@@ -207,14 +221,20 @@ bool Shell::ExecuteString(Isolate* isolate,
     // When debugging make exceptions appear to be uncaught.
     try_catch.SetVerbose(true);
   }
-  Handle<Script> script = Script::Compile(source, name);
+  Handle<Script> script = Script::New(source, name);
   if (script.IsEmpty()) {
     // Print errors that happened during compilation.
     if (report_exceptions && !FLAG_debugger)
       ReportException(isolate, &try_catch);
     return false;
   } else {
+    PerIsolateData* data = PerIsolateData::Get(isolate);
+    Local<Context> realm =
+        Local<Context>::New(data->realms_[data->realm_current_]);
+    realm->Enter();
     Handle<Value> result = script->Run();
+    realm->Exit();
+    data->realm_current_ = data->realm_switch_;
     if (result.IsEmpty()) {
       ASSERT(try_catch.HasCaught());
       // Print errors that happened during execution.
@@ -255,6 +275,159 @@ bool Shell::ExecuteString(Isolate* isolate,
 }
 
 
+void PerIsolateData::RealmInit() {
+  if (realms_ != NULL) {
+    // Drop realms from previous run.
+    for (int i = 0; i < realm_count_; ++i) realms_[i].Dispose(isolate_);
+    delete[] realms_;
+    if (!realm_shared_.IsEmpty()) realm_shared_.Dispose(isolate_);
+  }
+  realm_count_ = 1;
+  realm_current_ = 0;
+  realm_switch_ = 0;
+  realms_ = new Persistent<Context>[1];
+  realms_[0] = Persistent<Context>::New(isolate_, Context::GetEntered());
+  realm_shared_.Clear();
+}
+
+
+int PerIsolateData::RealmFind(Handle<Context> context) {
+  for (int i = 0; i < realm_count_; ++i) {
+    if (realms_[i] == context) return i;
+  }
+  return -1;
+}
+
+
+// Realm.current() returns the index of the currently active realm.
+Handle<Value> Shell::RealmCurrent(const Arguments& args) {
+  Isolate* isolate = args.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  int index = data->RealmFind(Context::GetEntered());
+  if (index == -1) return Undefined(isolate);
+  return Number::New(index);
+}
+
+
+// Realm.owner(o) returns the index of the realm that created o.
+Handle<Value> Shell::RealmOwner(const Arguments& args) {
+  Isolate* isolate = args.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  if (args.Length() < 1 || !args[0]->IsObject()) {
+    return Throw("Invalid argument");
+  }
+  int index = data->RealmFind(args[0]->ToObject()->CreationContext());
+  if (index == -1) return Undefined(isolate);
+  return Number::New(index);
+}
+
+
+// Realm.global(i) returns the global object of realm i.
+// (Note that properties of global objects cannot be read/written cross-realm.)
+Handle<Value> Shell::RealmGlobal(const Arguments& args) {
+  PerIsolateData* data = PerIsolateData::Get(args.GetIsolate());
+  if (args.Length() < 1 || !args[0]->IsNumber()) {
+    return Throw("Invalid argument");
+  }
+  int index = args[0]->Uint32Value();
+  if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
+    return Throw("Invalid realm index");
+  }
+  return data->realms_[index]->Global();
+}
+
+
+// Realm.create() creates a new realm and returns its index.
+Handle<Value> Shell::RealmCreate(const Arguments& args) {
+  Isolate* isolate = args.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  Persistent<Context>* old_realms = data->realms_;
+  int index = data->realm_count_;
+  data->realms_ = new Persistent<Context>[++data->realm_count_];
+  for (int i = 0; i < index; ++i) data->realms_[i] = old_realms[i];
+  delete[] old_realms;
+  Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
+  data->realms_[index] = Persistent<Context>::New(
+      isolate, Context::New(isolate, NULL, global_template));
+  return Number::New(index);
+}
+
+
+// Realm.dispose(i) disposes the reference to the realm i.
+Handle<Value> Shell::RealmDispose(const Arguments& args) {
+  Isolate* isolate = args.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  if (args.Length() < 1 || !args[0]->IsNumber()) {
+    return Throw("Invalid argument");
+  }
+  int index = args[0]->Uint32Value();
+  if (index >= data->realm_count_ || data->realms_[index].IsEmpty() ||
+      index == 0 ||
+      index == data->realm_current_ || index == data->realm_switch_) {
+    return Throw("Invalid realm index");
+  }
+  data->realms_[index].Dispose(isolate);
+  data->realms_[index].Clear();
+  return Undefined(isolate);
+}
+
+
+// Realm.switch(i) switches to the realm i for consecutive interactive inputs.
+Handle<Value> Shell::RealmSwitch(const Arguments& args) {
+  Isolate* isolate = args.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  if (args.Length() < 1 || !args[0]->IsNumber()) {
+    return Throw("Invalid argument");
+  }
+  int index = args[0]->Uint32Value();
+  if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
+    return Throw("Invalid realm index");
+  }
+  data->realm_switch_ = index;
+  return Undefined(isolate);
+}
+
+
+// Realm.eval(i, s) evaluates s in realm i and returns the result.
+Handle<Value> Shell::RealmEval(const Arguments& args) {
+  Isolate* isolate = args.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) {
+    return Throw("Invalid argument");
+  }
+  int index = args[0]->Uint32Value();
+  if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) {
+    return Throw("Invalid realm index");
+  }
+  Handle<Script> script = Script::New(args[1]->ToString());
+  if (script.IsEmpty()) return Undefined(isolate);
+  Local<Context> realm = Local<Context>::New(data->realms_[index]);
+  realm->Enter();
+  Handle<Value> result = script->Run();
+  realm->Exit();
+  return result;
+}
+
+
+// Realm.shared is an accessor for a single shared value across realms.
+Handle<Value> Shell::RealmSharedGet(Local<String> property,
+                                    const AccessorInfo& info) {
+  Isolate* isolate = info.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  if (data->realm_shared_.IsEmpty()) return Undefined(isolate);
+  return data->realm_shared_;
+}
+
+void Shell::RealmSharedSet(Local<String> property,
+                           Local<Value> value,
+                           const AccessorInfo& info) {
+  Isolate* isolate = info.GetIsolate();
+  PerIsolateData* data = PerIsolateData::Get(isolate);
+  if (!data->realm_shared_.IsEmpty()) data->realm_shared_.Dispose(isolate);
+  data->realm_shared_ = Persistent<Value>::New(isolate, value);
+}
+
+
 Handle<Value> Shell::Print(const Arguments& args) {
   Handle<Value> val = Write(args);
   printf("\n");
@@ -416,7 +589,8 @@ Handle<Value> Shell::CreateExternalArrayBuffer(Isolate* isolate,
   }
   memset(data, 0, length);
 
-  buffer->SetHiddenValue(Symbols::ArrayBufferMarkerPropName(isolate), True());
+  buffer->SetHiddenValue(
+      PerIsolateData::ArrayBufferMarkerPropName_string(isolate), True());
   Persistent<Object> persistent_array =
       Persistent<Object>::New(isolate, buffer);
   persistent_array.MakeWeak(isolate, data, ExternalArrayWeakCallback);
@@ -425,7 +599,7 @@ Handle<Value> Shell::CreateExternalArrayBuffer(Isolate* isolate,
 
   buffer->SetIndexedPropertiesToExternalArrayData(
       data, v8::kExternalByteArray, length);
-  buffer->Set(Symbols::byteLength(isolate),
+  buffer->Set(PerIsolateData::byteLength_string(isolate),
               Int32::New(length, isolate),
               ReadOnly);
 
@@ -470,20 +644,20 @@ Handle<Object> Shell::CreateExternalArray(Isolate* isolate,
 
   array->SetIndexedPropertiesToExternalArrayData(
       static_cast<uint8_t*>(data) + byteOffset, type, length);
-  array->SetHiddenValue(Symbols::ArrayMarkerPropName(isolate),
+  array->SetHiddenValue(PerIsolateData::ArrayMarkerPropName_string(isolate),
                         Int32::New(type, isolate));
-  array->Set(Symbols::byteLength(isolate),
+  array->Set(PerIsolateData::byteLength_string(isolate),
              Int32::New(byteLength, isolate),
              ReadOnly);
-  array->Set(Symbols::byteOffset(isolate),
+  array->Set(PerIsolateData::byteOffset_string(isolate),
              Int32::New(byteOffset, isolate),
              ReadOnly);
-  array->Set(Symbols::length(isolate),
+  array->Set(PerIsolateData::length_string(isolate),
              Int32::New(length, isolate),
              ReadOnly);
-  array->Set(Symbols::BYTES_PER_ELEMENT(isolate),
+  array->Set(PerIsolateData::BYTES_PER_ELEMENT_string(isolate),
              Int32::New(element_size, isolate));
-  array->Set(Symbols::buffer(isolate),
+  array->Set(PerIsolateData::buffer_string(isolate),
              buffer,
              ReadOnly);
 
@@ -524,11 +698,11 @@ Handle<Value> Shell::CreateExternalArray(const Arguments& args,
   }
   if (args[0]->IsObject() &&
       !args[0]->ToObject()->GetHiddenValue(
-          Symbols::ArrayBufferMarkerPropName(isolate)).IsEmpty()) {
+         PerIsolateData::ArrayBufferMarkerPropName_string(isolate)).IsEmpty()) {
     // Construct from ArrayBuffer.
     buffer = args[0]->ToObject();
-    int32_t bufferLength =
-        convertToUint(buffer->Get(Symbols::byteLength(isolate)), &try_catch);
+    int32_t bufferLength = convertToUint(
+        buffer->Get(PerIsolateData::byteLength_string(isolate)), &try_catch);
     if (try_catch.HasCaught()) return try_catch.ReThrow();
 
     if (args.Length() < 2 || args[1]->IsUndefined()) {
@@ -560,9 +734,10 @@ Handle<Value> Shell::CreateExternalArray(const Arguments& args,
     }
   } else {
     if (args[0]->IsObject() &&
-        args[0]->ToObject()->Has(Symbols::length(isolate))) {
+        args[0]->ToObject()->Has(PerIsolateData::length_string(isolate))) {
       // Construct from array.
-      Local<Value> value = args[0]->ToObject()->Get(Symbols::length(isolate));
+      Local<Value> value =
+          args[0]->ToObject()->Get(PerIsolateData::length_string(isolate));
       if (try_catch.HasCaught()) return try_catch.ReThrow();
       length = convertToUint(value, &try_catch);
       if (try_catch.HasCaught()) return try_catch.ReThrow();
@@ -576,7 +751,8 @@ Handle<Value> Shell::CreateExternalArray(const Arguments& args,
     byteOffset = 0;
 
     Handle<Object> global = Context::GetCurrent()->Global();
-    Handle<Value> array_buffer = global->Get(Symbols::ArrayBuffer(isolate));
+    Handle<Value> array_buffer =
+        global->Get(PerIsolateData::ArrayBuffer_string(isolate));
     ASSERT(!try_catch.HasCaught() && array_buffer->IsFunction());
     Handle<Value> buffer_args[] = { Uint32::New(byteLength, isolate) };
     Handle<Value> result = Handle<Function>::Cast(array_buffer)->NewInstance(
@@ -611,14 +787,14 @@ Handle<Value> Shell::ArrayBufferSlice(const Arguments& args) {
 
   Isolate* isolate = args.GetIsolate();
   Local<Object> self = args.This();
-  Local<Value> marker =
-      self->GetHiddenValue(Symbols::ArrayBufferMarkerPropName(isolate));
+  Local<Value> marker = self->GetHiddenValue(
+      PerIsolateData::ArrayBufferMarkerPropName_string(isolate));
   if (marker.IsEmpty()) {
     return Throw("'slice' invoked on wrong receiver type");
   }
 
-  int32_t length =
-      convertToUint(self->Get(Symbols::byteLength(isolate)), &try_catch);
+  int32_t length = convertToUint(
+      self->Get(PerIsolateData::byteLength_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
 
   if (args.Length() == 0) {
@@ -667,21 +843,22 @@ Handle<Value> Shell::ArraySubArray(const Arguments& args) {
   Isolate* isolate = args.GetIsolate();
   Local<Object> self = args.This();
   Local<Value> marker =
-      self->GetHiddenValue(Symbols::ArrayMarkerPropName(isolate));
+      self->GetHiddenValue(PerIsolateData::ArrayMarkerPropName_string(isolate));
   if (marker.IsEmpty()) {
     return Throw("'subarray' invoked on wrong receiver type");
   }
 
-  Handle<Object> buffer = self->Get(Symbols::buffer(isolate))->ToObject();
+  Handle<Object> buffer =
+      self->Get(PerIsolateData::buffer_string(isolate))->ToObject();
   if (try_catch.HasCaught()) return try_catch.ReThrow();
-  int32_t length =
-      convertToUint(self->Get(Symbols::length(isolate)), &try_catch);
+  int32_t length = convertToUint(
+      self->Get(PerIsolateData::length_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
-  int32_t byteOffset =
-      convertToUint(self->Get(Symbols::byteOffset(isolate)), &try_catch);
+  int32_t byteOffset = convertToUint(
+      self->Get(PerIsolateData::byteOffset_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
-  int32_t element_size =
-      convertToUint(self->Get(Symbols::BYTES_PER_ELEMENT(isolate)), &try_catch);
+  int32_t element_size = convertToUint(
+      self->Get(PerIsolateData::BYTES_PER_ELEMENT_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
 
   if (args.Length() == 0) {
@@ -726,27 +903,27 @@ Handle<Value> Shell::ArraySet(const Arguments& args) {
   Isolate* isolate = args.GetIsolate();
   Local<Object> self = args.This();
   Local<Value> marker =
-      self->GetHiddenValue(Symbols::ArrayMarkerPropName(isolate));
+      self->GetHiddenValue(PerIsolateData::ArrayMarkerPropName_string(isolate));
   if (marker.IsEmpty()) {
     return Throw("'set' invoked on wrong receiver type");
   }
-  int32_t length =
-      convertToUint(self->Get(Symbols::length(isolate)), &try_catch);
+  int32_t length = convertToUint(
+      self->Get(PerIsolateData::length_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
-  int32_t element_size =
-      convertToUint(self->Get(Symbols::BYTES_PER_ELEMENT(isolate)), &try_catch);
+  int32_t element_size = convertToUint(
+      self->Get(PerIsolateData::BYTES_PER_ELEMENT_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
 
   if (args.Length() == 0) {
     return Throw("'set' must have at least one argument");
   }
   if (!args[0]->IsObject() ||
-      !args[0]->ToObject()->Has(Symbols::length(isolate))) {
+      !args[0]->ToObject()->Has(PerIsolateData::length_string(isolate))) {
     return Throw("'set' invoked with non-array argument");
   }
   Handle<Object> source = args[0]->ToObject();
-  int32_t source_length =
-      convertToUint(source->Get(Symbols::length(isolate)), &try_catch);
+  int32_t source_length = convertToUint(
+      source->Get(PerIsolateData::length_string(isolate)), &try_catch);
   if (try_catch.HasCaught()) return try_catch.ReThrow();
 
   int32_t offset;
@@ -761,28 +938,30 @@ Handle<Value> Shell::ArraySet(const Arguments& args) {
   }
 
   int32_t source_element_size;
-  if (source->GetHiddenValue(Symbols::ArrayMarkerPropName(isolate)).IsEmpty()) {
+  if (source->GetHiddenValue(
+          PerIsolateData::ArrayMarkerPropName_string(isolate)).IsEmpty()) {
     source_element_size = 0;
   } else {
-    source_element_size =
-        convertToUint(source->Get(Symbols::BYTES_PER_ELEMENT(isolate)),
-                      &try_catch);
+    source_element_size = convertToUint(
+        source->Get(PerIsolateData::BYTES_PER_ELEMENT_string(isolate)),
+        &try_catch);
     if (try_catch.HasCaught()) return try_catch.ReThrow();
   }
 
   if (element_size == source_element_size &&
       self->GetConstructor()->StrictEquals(source->GetConstructor())) {
     // Use memmove on the array buffers.
-    Handle<Object> buffer = self->Get(Symbols::buffer(isolate))->ToObject();
+    Handle<Object> buffer =
+        self->Get(PerIsolateData::buffer_string(isolate))->ToObject();
     if (try_catch.HasCaught()) return try_catch.ReThrow();
     Handle<Object> source_buffer =
-        source->Get(Symbols::buffer(isolate))->ToObject();
+        source->Get(PerIsolateData::buffer_string(isolate))->ToObject();
     if (try_catch.HasCaught()) return try_catch.ReThrow();
-    int32_t byteOffset =
-        convertToUint(self->Get(Symbols::byteOffset(isolate)), &try_catch);
+    int32_t byteOffset = convertToUint(
+        self->Get(PerIsolateData::byteOffset_string(isolate)), &try_catch);
     if (try_catch.HasCaught()) return try_catch.ReThrow();
-    int32_t source_byteOffset =
-        convertToUint(source->Get(Symbols::byteOffset(isolate)), &try_catch);
+    int32_t source_byteOffset = convertToUint(
+        source->Get(PerIsolateData::byteOffset_string(isolate)), &try_catch);
     if (try_catch.HasCaught()) return try_catch.ReThrow();
 
     uint8_t* dest = byteOffset + offset * element_size + static_cast<uint8_t*>(
@@ -798,21 +977,22 @@ Handle<Value> Shell::ArraySet(const Arguments& args) {
     }
   } else {
     // Need to copy element-wise to make the right conversions.
-    Handle<Object> buffer = self->Get(Symbols::buffer(isolate))->ToObject();
+    Handle<Object> buffer =
+        self->Get(PerIsolateData::buffer_string(isolate))->ToObject();
     if (try_catch.HasCaught()) return try_catch.ReThrow();
     Handle<Object> source_buffer =
-        source->Get(Symbols::buffer(isolate))->ToObject();
+        source->Get(PerIsolateData::buffer_string(isolate))->ToObject();
     if (try_catch.HasCaught()) return try_catch.ReThrow();
 
     if (buffer->StrictEquals(source_buffer)) {
       // Same backing store, need to handle overlap correctly.
       // This gets a bit tricky in the case of different element sizes
       // (which, of course, is extremely unlikely to ever occur in practice).
-      int32_t byteOffset =
-          convertToUint(self->Get(Symbols::byteOffset(isolate)), &try_catch);
+      int32_t byteOffset = convertToUint(
+          self->Get(PerIsolateData::byteOffset_string(isolate)), &try_catch);
       if (try_catch.HasCaught()) return try_catch.ReThrow();
-      int32_t source_byteOffset =
-          convertToUint(source->Get(Symbols::byteOffset(isolate)), &try_catch);
+      int32_t source_byteOffset = convertToUint(
+          source->Get(PerIsolateData::byteOffset_string(isolate)), &try_catch);
       if (try_catch.HasCaught()) return try_catch.ReThrow();
 
       // Copy as much as we can from left to right.
@@ -860,8 +1040,8 @@ void Shell::ExternalArrayWeakCallback(v8::Isolate* isolate,
                                       Persistent<Value> object,
                                       void* data) {
   HandleScope scope(isolate);
-  int32_t length =
-      object->ToObject()->Get(Symbols::byteLength(isolate))->Uint32Value();
+  int32_t length = object->ToObject()->Get(
+      PerIsolateData::byteLength_string(isolate))->Uint32Value();
   isolate->AdjustAmountOfExternalAllocatedMemory(-length);
   delete[] static_cast<uint8_t*>(data);
   object.Dispose(isolate);
@@ -1238,10 +1418,30 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
   global_template->Set(String::New("disableProfiler"),
                        FunctionTemplate::New(DisableProfiler));
 
+  // Bind the Realm object.
+  Handle<ObjectTemplate> realm_template = ObjectTemplate::New();
+  realm_template->Set(String::New("current"),
+                      FunctionTemplate::New(RealmCurrent));
+  realm_template->Set(String::New("owner"),
+                      FunctionTemplate::New(RealmOwner));
+  realm_template->Set(String::New("global"),
+                      FunctionTemplate::New(RealmGlobal));
+  realm_template->Set(String::New("create"),
+                      FunctionTemplate::New(RealmCreate));
+  realm_template->Set(String::New("dispose"),
+                      FunctionTemplate::New(RealmDispose));
+  realm_template->Set(String::New("switch"),
+                      FunctionTemplate::New(RealmSwitch));
+  realm_template->Set(String::New("eval"),
+                      FunctionTemplate::New(RealmEval));
+  realm_template->SetAccessor(String::New("shared"),
+                              RealmSharedGet, RealmSharedSet);
+  global_template->Set(String::New("Realm"), realm_template);
+
   // Bind the handlers for external arrays.
   PropertyAttribute attr =
       static_cast<PropertyAttribute>(ReadOnly | DontDelete);
-  global_template->Set(Symbols::ArrayBuffer(isolate),
+  global_template->Set(PerIsolateData::ArrayBuffer_string(isolate),
                        CreateArrayBufferTemplate(ArrayBuffer), attr);
   global_template->Set(String::New("Int8Array"),
                        CreateArrayTemplate(Int8Array), attr);
@@ -1325,6 +1525,7 @@ Persistent<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
   Persistent<Context> context = Context::New(NULL, global_template);
   ASSERT(!context.IsEmpty());
   Context::Scope scope(context);
+  PerIsolateData::Get(isolate)->RealmInit();
 
 #ifndef V8_SHARED
   i::JSArguments js_args = i::FLAG_js_arguments;
@@ -1469,7 +1670,8 @@ Handle<Value> Shell::ReadBuffer(const Arguments& args) {
   }
   Isolate* isolate = args.GetIsolate();
   Handle<Object> buffer = Object::New();
-  buffer->SetHiddenValue(Symbols::ArrayBufferMarkerPropName(isolate), True());
+  buffer->SetHiddenValue(
+      PerIsolateData::ArrayBufferMarkerPropName_string(isolate), True());
   Persistent<Object> persistent_buffer =
       Persistent<Object>::New(isolate, buffer);
   persistent_buffer.MakeWeak(isolate, data, ExternalArrayWeakCallback);
@@ -1478,7 +1680,7 @@ Handle<Value> Shell::ReadBuffer(const Arguments& args) {
 
   buffer->SetIndexedPropertiesToExternalArrayData(
       data, kExternalUnsignedByteArray, length);
-  buffer->Set(Symbols::byteLength(isolate),
+  buffer->Set(PerIsolateData::byteLength_string(isolate),
       Int32::New(static_cast<int32_t>(length), isolate), ReadOnly);
   return buffer;
 }
@@ -1671,7 +1873,7 @@ void SourceGroup::ExecuteInThread() {
       Isolate::Scope iscope(isolate);
       Locker lock(isolate);
       HandleScope scope(isolate);
-      Symbols symbols(isolate);
+      PerIsolateData data(isolate);
       Persistent<Context> context = Shell::CreateEvaluationContext(isolate);
       {
         Context::Scope cscope(context);
@@ -1933,7 +2135,7 @@ int Shell::Main(int argc, char* argv[]) {
 #ifdef ENABLE_VTUNE_JIT_INTERFACE
     vTune::InitilizeVtuneForV8();
 #endif
-    Symbols symbols(isolate);
+    PerIsolateData data(isolate);
     InitializeDebugger(isolate);
 
     if (options.stress_opt || options.stress_deopt) {
index 2789c07..4d9504f 100644 (file)
--- a/src/d8.h
+++ b/src/d8.h
@@ -298,6 +298,19 @@ class Shell : public i::AllStatic {
 #endif  // ENABLE_DEBUGGER_SUPPORT
 #endif  // V8_SHARED
 
+  static Handle<Value> RealmCurrent(const Arguments& args);
+  static Handle<Value> RealmOwner(const Arguments& args);
+  static Handle<Value> RealmGlobal(const Arguments& args);
+  static Handle<Value> RealmCreate(const Arguments& args);
+  static Handle<Value> RealmDispose(const Arguments& args);
+  static Handle<Value> RealmSwitch(const Arguments& args);
+  static Handle<Value> RealmEval(const Arguments& args);
+  static Handle<Value> RealmSharedGet(Local<String> property,
+                                      const AccessorInfo& info);
+  static void RealmSharedSet(Local<String> property,
+                             Local<Value> value,
+                             const AccessorInfo& info);
+
   static Handle<Value> Print(const Arguments& args);
   static Handle<Value> Write(const Arguments& args);
   static Handle<Value> Quit(const Arguments& args);
index 4af2194..efba8e5 100644 (file)
@@ -612,7 +612,6 @@ void ScriptCache::Add(Handle<Script> script) {
     ASSERT(*script == *reinterpret_cast<Script**>(entry->value));
     return;
   }
-
   // Globalize the script object, make it weak and use the location of the
   // global handle as the value in the hash map.
   Handle<Script> script_ =
index 898b176..3230559 100644 (file)
@@ -8541,7 +8541,7 @@ class JSWeakMap: public JSObject {
 
 class JSArrayBuffer: public JSObject {
  public:
-  // [backing_store]: backing memory for thsi array
+  // [backing_store]: backing memory for this array
   DECL_ACCESSORS(backing_store, void)
 
   // [byte_length]: length in bytes
diff --git a/test/mjsunit/bugs/bug-proto.js b/test/mjsunit/bugs/bug-proto.js
new file mode 100644 (file)
index 0000000..5638336
--- /dev/null
@@ -0,0 +1,61 @@
+// Copyright 2013 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+var realmA = Realm.current();
+var realmB = Realm.create();
+assertEquals(0, realmA);
+assertEquals(1, realmB);
+
+// The global objects match the realms' this binding.
+assertSame(this, Realm.global(realmA));
+assertSame(Realm.eval(realmB, "this"), Realm.global(realmB));
+assertFalse(this === Realm.global(realmB));
+
+// The global object is not accessible cross-realm.
+var x = 3;
+Realm.shared = this;
+assertThrows("Realm.eval(realmB, 'x')");
+assertSame(undefined, Realm.eval(realmB, "this.x"));
+assertSame(undefined, Realm.eval(realmB, "Realm.shared.x"));
+
+Realm.eval(realmB, "Realm.global(0).y = 1");
+assertThrows("y");
+assertSame(undefined, this.y);
+
+// Can get or set other objects' properties cross-realm.
+var p = {a: 1};
+var o = {__proto__: p, b: 2};
+Realm.shared = o;
+assertSame(1, Realm.eval(realmB, "Realm.shared.a"));
+assertSame(2, Realm.eval(realmB, "Realm.shared.b"));
+
+// Cannot get or set a prototype cross-realm.
+assertSame(undefined, Realm.eval(realmB, "Realm.shared.__proto__"));
+
+Realm.eval(realmB, "Realm.shared.__proto__ = {c: 3}");
+assertSame(1, o.a);
+assertSame(undefined, o.c);