I have a JavaScript library with a function which takes a dictionary object as parameter:
function myfunc(data, opts) {
...
//opts is used like:
opts.type == ...
opts.desc ...
...
}
In the Nim header definition like
func myfunc*(data: cstring, opts: ???) {.importjs: "mylib.$1(#, #)".}
I can map data to cstring, but what do I map opts to?
Cool! Makes sense. Now I'm using:
func myfunc*(data: cstring, opts: JsObject) {.importjs: "mylib.$1(#, #)".}
And I'm trying on the Nim side to provide this` JsObject` aka dictionary:
let opts = toJS({"type": "extended", "desc": "fortified"}.totable)
myFunc(data, opts)
This compiles and runs, but on the JavaScript side opts.type is undefined and not as I hoped/expected "extended".
Any idea how to write the JsObject dictionary in Nim correctly?
I would have expected that to do the right thing…
As it turns out, you can use the macro {}, but then you can't use variables as keys:
let opts = JsObject{`type`: cstring"extended", desc: cstring"fortified"}
Or set the fields explicitly:
var opts = newJsObject()
opts["type"] = cstring"extended"
opts["desc"] = cstring"fortified"
@DomoSokrat @Hlaaftana Thanks a lot for the info. I guess I have to study jsffi in more detail…
(Still, discovery of unknown syntax doesn’t seem to be easy for me just from the type/function reference line on the module page.)