Hello,
Sorry for (maybe) stupid question, I'm just beginning to play with nim.
I'm trying to use a small AJAX library https://github.com/flouthoc/minAjax.js in nim-created browser JavaScript. I've created a small wrapper:
type
minAjaxConf* = object {.exportc.}
url* : cstring
rtype* : cstring
data* : cstring
success* : cstring
rmethod* : bool
debugLog* : cstring
proc minAjax*(conf : minAjaxConf) {.importc.}
In my client.nim I use this wrapper as follows:
var conf : minAjaxConf
conf.url = "/data"
conf.rtype = "GET"
conf.data = ""
conf.success = "function(data) { printData(data); }"
conf.debugLog = "true"
minAjax(conf)
And finally I get in compiled client.js:
var conf_33016 = {url_32005: null, rtype_32006: null, data_32007: null, success_32008: null, rmethod_32009: false, debuglog_32010: null};
conf_33016.url_32005 = "/data";
conf_33016.rtype_32006 = "GET";
conf_33016.data_32007 = "";
conf_33016.success_32008 = "function(data) { printData(data); }";
conf_33016.debuglog_32010 = "true";
minAjax(conf_33016);
The problem is that during the compilation to JS, the object member's names are changed, for example url became url_32005! So the library (searching for url, rtype, etc.) does not work.
How can I fix the names of nim objects compiled to JS objects?
If you want nim bindings for foreign JS objects, you might want to try this approach:
type
minAjaxConf* = ref minAjaxConfObj
minAjaxConfObj {.importc.} = object
url* : cstring
rtype* : cstring
data* : cstring
success* : cstring
rmethod* : bool
debugLog* : cstring
let conf = minAjaxConf.new()
conf.url = "/data"
# ...
When writing bindings to pure JS types, IMHO it's a good practice to give them ref remantics from the start.Why not, but it does not work at all:
client.nim(21, 25) Error: internal error: genVarInit Traceback (most recent call last) nim.nim(94) nim nim.nim(56) handleCmdLine main.nim(270) mainCommand main.nim(113) commandCompileToJS modules.nim(203) compileProject modules.nim(151) compileModule passes.nim(197) processModule passes.nim(137) processTopLevelStmt jsgen.nim(1714) myProcess jsgen.nim(1704) genModule jsgen.nim(1565) genStmt jsgen.nim(1646) gen jsgen.nim(1565) genStmt jsgen.nim(1676) gen jsgen.nim(1022) genSym jsgen.nim(1555) genProc jsgen.nim(1565) genStmt jsgen.nim(1646) gen jsgen.nim(1565) genStmt jsgen.nim(1652) gen jsgen.nim(1231) genVarStmt jsgen.nim(1202) genVarInit msgs.nim(842) internalError msgs.nim(816) liMessage msgs.nim(715) handleError FAILURE
on line: let conf = minAjaxConf.new()
The bindings modified:
type
minAjaxConf* = ref minAjaxConfObj
minAjaxConfObj {.importc.} = object
url* : cstring
rtype* : cstring
data* : cstring
success* : cstring
rmethod* : bool
debugLog* : cstring