How to set the prototype of DynamicObject correctly? #488
-
On godoc: The Object's prototype is initially set to Object.prototype, but can be changed using regular mechanisms (Object.SetPrototype() in Go or Object.setPrototypeOf() in JS). The Object cannot have own Symbol properties, however its prototype can. If you need an iterator support for example, you could create a regular object, set Symbol.iterator on that object and then use it as a prototype. See TestDynamicObjectCustomProto for more details. However, I'm having trouble of doing so from Go side. Suppose that I have a class: class FetchEvent {
get [Symbol.toStringTag]() {
return 'FetchEvent'
}
} Calling What's the correct way of doing so? I have tried: class FetchEvent {
get [Symbol.toStringTag]() {
return 'FetchEvent'
}
}
const instance = new FetchEvent() evt.nativeEvt = vm.NewDynamicObject(evt)
class := vm.Get("FetchEvent")
instance := vm.Get("instance")
evt.nativeEvt.SetPrototype(class.ToObject(vm))
fmt.Printf("%+v\n", evt.nativeEvt)
evt.nativeEvt.SetPrototype(instance.ToObject(vm))
fmt.Printf("%+v\n", evt.nativeEvt)
evt.nativeEvt.SetPrototype(class.ToObject(vm).Prototype())
fmt.Printf("%+v\n", evt.nativeEvt)
evt.nativeEvt.SetPrototype(instance.ToObject(vm).Prototype())
fmt.Printf("%+v\n", evt.nativeEvt) But all of them still fails with Maybe I'm just bad at JavaScript 😅 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I figured it out. Even if this is dynamic, it is still trying to access // ...
evt.nativeEvt = evt.vm.NewDynamicObject(evt)
evt.nativeEvt.SetPrototype(evt.nativeEvtInstance)
// ...
func (evt *fetchEvent) Get(key string) goja.Value {
switch key {
case "respondWith":
return evt.vm.ToValue(evt.nativeRespondWith)
case "waitUntil":
return evt.vm.ToValue(evt.nativeWaitUntil)
case "request":
return evt.requestProxy.nativeReq
default:
return evt.nativeEvtInstance.Get(key)
}
} Now it is no longer exploding 😆
|
Beta Was this translation helpful? Give feedback.
-
Yes, I was just writing to suggest that your DynamicObject implementation that either returns a primitive or a function that returns an object when asked for the The following works as expected for me: func TestDynamicObjectProtoSymbol1(t *testing.T) {
vm := New()
_, err := vm.RunString(`
class FetchEvent {
get [Symbol.toStringTag]() {
return 'FetchEvent'
}
}
const instance = new FetchEvent()
`)
if err != nil {
t.Fatal(err)
}
m := make(map[string]Value)
//m["toString"] = vm.ToValue("")
dynObj := &testDynObject{
r: vm,
m: m,
}
o := vm.NewDynamicObject(dynObj)
class := vm.Get("FetchEvent")
instance := vm.Get("instance")
o.SetPrototype(class.ToObject(vm))
t.Logf("%+v\n", o)
o.SetPrototype(instance.ToObject(vm))
t.Logf("%+v\n", o)
o.SetPrototype(class.ToObject(vm).Prototype())
t.Logf("%+v\n", o)
o.SetPrototype(instance.ToObject(vm).Prototype())
t.Logf("%+v\n", o)
} Produces:
(BTW, the second scenario is the right one) However, if I uncomment the
|
Beta Was this translation helpful? Give feedback.
Yes, I was just writing to suggest that your DynamicObject implementation that either returns a primitive or a function that returns an object when asked for the
toString
property.The following works as expected for me: