Hey everyone! I wanted to share a small library (really just a macro) that I wrote to help increase interop with the JavaScript ecosystem. I'm working on some web tooling for Nim and was need to export functions to the JavaScript world. The path of least resistance was utilizing the CommonJS module exports object, and this macro makes that a little easier. Not sure if it's useful to anyone else, but figured I'd put it out there. Feedback is welcome!
Thanks @nepeckman, it is very helpful for me at least.
but... I see some issues if created more export modules then the global variables and functions theoretically could be overwritten from latest imported module. My suggestion is wrap the export code into {. emit: """(function(){""" .} .... {. emit: """})();""" .} See please example:
import jsffi
import ../src/jsExport
{. emit: """(function(){""" .}
type Person* = ref object
name*: string
age*: int
proc newPerson(name: cstring, age: cint): Person =
Person(name: $name, age: int(age))
var name = cstring("Test 3")
var person = Person(name: "Test 4", age: 0)
proc greet(name: cstring): cstring =
"Hello " & name
proc greetPerson(person: Person): cstring =
"Hello " & person.name
jsExport:
"nimGreet" = greet
greetPerson
(name, person)
newPerson
jsExport(newPerson)
{. emit: """})();""" .}