You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using the following function to try and interact with sets in JavaScript:
funcTestSets(t*testing.T) {
runtime:=goja.New()
v, err:=runtime.RunString(`var s = new Set([5]);s`)
// v, err := runtime.RunString(`var s = new Set([5]);s.has(5)`)iferr!=nil {
panic(err)
}
exported:=v.Export()
fmt.Println(exported)
}
While exported is of type map[string]interface{} (looks like the base object export method is used here), I'm getting an empty map when I would expect one that has the element 5. If I change the JavaScript code with the one that is commented out (testing to see if the set contains an element using the has method), then exported is a boolean (with value "true" in this case). This seems to indicate that the code is doing what it is supposed to do, but exporting the set yields an empty map. What am I doing wrong? Is exporting of the set type supported?
The text was updated successfully, but these errors were encountered:
I'm not sure there is a good solution here. There is no set type in Go, but that's ok, we could export into map[interface{}]struct{} for example. But, while this would work for primitives, it wouldn't for objects, because they are unhashable (so you'll get a runtime panic if you try to use one as a key) and even if they were, exporting an Object produces an independent copy so equality match wouldn't work.
Depending on what you're going to do with the exported value, you could either interact with un-exported *Object representing the Set, like this:
constSCRIPT=` const s = new Set(); s.add(1); s; `vm:=New()
s, err:=vm.RunString(SCRIPT)
iferr!=nil {
panic(err)
}
ifsetObj, ok:=s.(*Object); ok {
ifhas, ok:=AssertFunction(setObj.Get("has")); ok {
flag, err:=has(setObj, vm.ToValue(1))
iferr!=nil {
panic(err)
}
t.Log(flag) // prints 'true'
}
}
Or convert it into something that can be exported (like an array) in JS code.
I'm using the following function to try and interact with sets in JavaScript:
While
exported
is of typemap[string]interface{}
(looks like the base objectexport
method is used here), I'm getting an empty map when I would expect one that has the element5
. If I change the JavaScript code with the one that is commented out (testing to see if the set contains an element using thehas
method), thenexported
is a boolean (with value "true" in this case). This seems to indicate that the code is doing what it is supposed to do, but exporting the set yields an empty map. What am I doing wrong? Is exporting of the set type supported?The text was updated successfully, but these errors were encountered: