@@ -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+
10901156func ExampleAssertFunction () {
10911157 vm := New ()
10921158 _ , err := vm .RunString (`
0 commit comments