-
I'm trying to find a way to intercept access to the global object, and I've come up with this code that seems to work: globalObjPtr := vm.GlobalObject()
// Copies the global Object struct
globalObj := *globalObjPtr
globalObjProxy := vm.NewProxy(&globalObj, &goja.ProxyTrapConfig{
Get: func(target *goja.Object, property string, receiver goja.Value) (value goja.Value) {
log.Printf("get %s", property)
return target.Get(property)
},
})
// Overwrites the original global Object with our new Proxy Object
*globalObjPtr = *vm.ToValue(globalObjProxy).(*goja.Object) It works, but it feels very hacky, so I'm wondering if this is safe to do? It seems like copying and overwriting the raw runtime structs could have unintended consequences, but as far as I can tell, there's no other way to replace the global object with your own object. |
Beta Was this translation helpful? Give feedback.
Answered by
dop251
Jan 25, 2025
Replies: 1 comment 1 reply
-
It's better to add |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Hakkin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's better to add
SetGlobalObject()
method to the Runtime. Could you raise a PR?