improve the usability of the jsffi module

* All JavaScript operators are usable with JsObject
* The dot operators will use native JavaScript strings
* Results returned from dot calls are consired discardable
This commit is contained in:
Zahary Karadjov 2017-04-09 23:04:55 +03:00
commit 48a1a54d1c
2 changed files with 104 additions and 10 deletions

View file

@ -14,10 +14,15 @@ true
true
true
true
true'''
true
3
2
12
Event { name: 'click: test' }
Event { name: 'reloaded: test' }'''
"""
import macros, jsffi
import macros, jsffi, jsconsole
# Tests for JsObject
# Test JsObject []= and []
@ -55,8 +60,8 @@ block:
block:
proc test(): bool =
let obj = newJsObject()
obj.`?!$` = proc(x, y, z: int, t: string): string = t & $(x + y + z)
obj.`?!$`(1, 2, 3, "Result is: ").to(string) == "Result is: 6"
obj.`?!$` = proc(x, y, z: int, t: cstring): cstring = t & $(x + y + z)
obj.`?!$`(1, 2, 3, "Result is: ").to(cstring) == "Result is: 6"
echo test()
# Test JsObject []()
@ -265,3 +270,42 @@ block:
let obj = TestObject(a: 9, onWhatever: bindMethod(handleWhatever))
obj.onWhatever(1) == 10
echo test()
block:
{.emit: "function jsProc(n) { return n; }" .}
proc jsProc(x: int32): JsObject {.importc: "jsProc".}
proc test() =
var x = jsProc(1)
var y = jsProc(2)
console.log x + y
console.log ++x
x += jsProc(10)
console.log x
test()
import macros
block:
{.emit:
"""
function Event(name) { this.name = name; }
function on(eventName, eventHandler) { eventHandler(new Event(eventName + ": test")); }
var jslib = { "on": on };
"""
.}
type Event = object
name: cstring
proc on(event: cstring, handler: proc) {.importc: "on".}
var jslib {.importc: "jslib", nodecl.}: JsObject
on("click") do (e: Event):
console.log e
jslib.on("reloaded") do:
console.log jsarguments[0]