Browse Source

Add Runtime.SetGlobalObject() method.

Hakkin Lain 8 months ago
parent
commit
436e551de7
2 changed files with 72 additions and 0 deletions
  1. 6 0
      runtime.go
  2. 66 0
      runtime_test.go

+ 6 - 0
runtime.go

@@ -2377,6 +2377,12 @@ func (r *Runtime) GlobalObject() *Object {
 	return r.globalObject
 }
 
+// SetGlobalObject sets the global object to the given object.
+// Note, any existing references to the previous global object will continue to reference that object.
+func (r *Runtime) SetGlobalObject(object *Object) {
+	r.globalObject = object
+}
+
 // Set the specified variable in the global context.
 // Equivalent to running "name = value" in non-strict mode.
 // The value is first converted using ToValue().

+ 66 - 0
runtime_test.go

@@ -1087,6 +1087,72 @@ func TestRuntime_ExportToObject(t *testing.T) {
 	}
 }
 
+func TestRuntime_SetGlobalObject(t *testing.T) {
+	vm := New()
+
+	_, err := vm.RunString(`var myVar = 123;`)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	oldGlobalObject := vm.GlobalObject()
+
+	newGlobalObject, err := vm.RunString(`({oldGlobalReference:globalThis});`)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	vm.SetGlobalObject(newGlobalObject.(*Object))
+
+	if vm.GlobalObject() != newGlobalObject {
+		t.Fatal("Expected global object to be new object")
+	}
+
+	if vm.GlobalObject().Get("myVar") != nil {
+		t.Fatal("Expected myVar to be undefined")
+	}
+
+	oldGlobalReference := vm.GlobalObject().Get("oldGlobalReference")
+	if oldGlobalReference == nil {
+		t.Fatal("Expected oldGlobalReference to be defined")
+	}
+
+	if oldGlobalReference != oldGlobalObject {
+		t.Fatal("Expected reference to be to old global object")
+	}
+}
+
+func TestRuntime_SetGlobalObject_Proxy(t *testing.T) {
+	vm := New()
+
+	globalObject := vm.GlobalObject()
+
+	globalObjectProxy := vm.NewProxy(globalObject, &ProxyTrapConfig{
+		Get: func(target *Object, property string, receiver Value) (value Value) {
+			if target != globalObject {
+				t.Fatal("Expected target to be global object")
+			}
+
+			if property != "testing" {
+				t.Fatal("Expected property to be 'testing'")
+			}
+
+			return valueTrue
+		},
+	})
+
+	vm.SetGlobalObject(vm.ToValue(globalObjectProxy).(*Object))
+
+	ret, err := vm.RunString("testing")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if ret != valueTrue {
+		t.Fatal("Expected return value to equal true")
+	}
+}
+
 func ExampleAssertFunction() {
 	vm := New()
 	_, err := vm.RunString(`