Skip to content

Commit 12bd88c

Browse files
authored
Merge pull request #654 from Hakkin/SetGlobalObject
Add Runtime.SetGlobalObject() method.
2 parents 46d383d + 436e551 commit 12bd88c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,12 @@ func (r *Runtime) GlobalObject() *Object {
23772377
return r.globalObject
23782378
}
23792379

2380+
// SetGlobalObject sets the global object to the given object.
2381+
// Note, any existing references to the previous global object will continue to reference that object.
2382+
func (r *Runtime) SetGlobalObject(object *Object) {
2383+
r.globalObject = object
2384+
}
2385+
23802386
// Set the specified variable in the global context.
23812387
// Equivalent to running "name = value" in non-strict mode.
23822388
// The value is first converted using ToValue().

runtime_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,72 @@ func TestRuntime_ExportToObject(t *testing.T) {
10871087
}
10881088
}
10891089

1090+
func TestRuntime_SetGlobalObject(t *testing.T) {
1091+
vm := New()
1092+
1093+
_, err := vm.RunString(`var myVar = 123;`)
1094+
if err != nil {
1095+
t.Fatal(err)
1096+
}
1097+
1098+
oldGlobalObject := vm.GlobalObject()
1099+
1100+
newGlobalObject, err := vm.RunString(`({oldGlobalReference:globalThis});`)
1101+
if err != nil {
1102+
t.Fatal(err)
1103+
}
1104+
1105+
vm.SetGlobalObject(newGlobalObject.(*Object))
1106+
1107+
if vm.GlobalObject() != newGlobalObject {
1108+
t.Fatal("Expected global object to be new object")
1109+
}
1110+
1111+
if vm.GlobalObject().Get("myVar") != nil {
1112+
t.Fatal("Expected myVar to be undefined")
1113+
}
1114+
1115+
oldGlobalReference := vm.GlobalObject().Get("oldGlobalReference")
1116+
if oldGlobalReference == nil {
1117+
t.Fatal("Expected oldGlobalReference to be defined")
1118+
}
1119+
1120+
if oldGlobalReference != oldGlobalObject {
1121+
t.Fatal("Expected reference to be to old global object")
1122+
}
1123+
}
1124+
1125+
func TestRuntime_SetGlobalObject_Proxy(t *testing.T) {
1126+
vm := New()
1127+
1128+
globalObject := vm.GlobalObject()
1129+
1130+
globalObjectProxy := vm.NewProxy(globalObject, &ProxyTrapConfig{
1131+
Get: func(target *Object, property string, receiver Value) (value Value) {
1132+
if target != globalObject {
1133+
t.Fatal("Expected target to be global object")
1134+
}
1135+
1136+
if property != "testing" {
1137+
t.Fatal("Expected property to be 'testing'")
1138+
}
1139+
1140+
return valueTrue
1141+
},
1142+
})
1143+
1144+
vm.SetGlobalObject(vm.ToValue(globalObjectProxy).(*Object))
1145+
1146+
ret, err := vm.RunString("testing")
1147+
if err != nil {
1148+
t.Fatal(err)
1149+
}
1150+
1151+
if ret != valueTrue {
1152+
t.Fatal("Expected return value to equal true")
1153+
}
1154+
}
1155+
10901156
func ExampleAssertFunction() {
10911157
vm := New()
10921158
_, err := vm.RunString(`

0 commit comments

Comments
 (0)