tjsffi.nim 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. discard """
  2. output: '''
  3. 3
  4. 2
  5. 12
  6. Event { name: 'click: test' }
  7. Event { name: 'reloaded: test' }
  8. Event { name: 'updates: test' }
  9. '''
  10. """
  11. import jsffi, jsconsole
  12. # Tests for JsObject
  13. block: # Test JsObject []= and []
  14. let obj = newJsObject()
  15. obj["a"] = 11
  16. obj["b"] = "test"
  17. obj["c"] = "test".cstring
  18. doAssert obj["a"].to(int) == 11
  19. doAssert obj["c"].to(cstring) == "test".cstring
  20. block: # Test JsObject .= and .
  21. let obj = newJsObject()
  22. obj.a = 11
  23. obj.b = "test"
  24. obj.c = "test".cstring
  25. obj.`$!&` = 42
  26. obj.`while` = 99
  27. doAssert obj.a.to(int) == 11
  28. doAssert obj.b.to(string) == "test"
  29. doAssert obj.c.to(cstring) == "test".cstring
  30. doAssert obj.`$!&`.to(int) == 42
  31. doAssert obj.`while`.to(int) == 99
  32. block: # Test JsObject .()
  33. let obj = newJsObject()
  34. obj.`?!$` = proc(x, y, z: int, t: cstring): cstring = t & $(x + y + z)
  35. doAssert obj.`?!$`(1, 2, 3, "Result is: ").to(cstring) == cstring"Result is: 6"
  36. block: # Test JsObject []()
  37. let obj = newJsObject()
  38. obj.a = proc(x, y, z: int, t: string): string = t & $(x + y + z)
  39. let call = obj["a"].to(proc(x, y, z: int, t: string): string)
  40. doAssert call(1, 2, 3, "Result is: ") == "Result is: 6"
  41. # Test JsObject Iterators
  42. block: # testPairs
  43. let obj = newJsObject()
  44. obj.a = 10
  45. obj.b = 20
  46. obj.c = 30
  47. for k, v in obj.pairs:
  48. case $k
  49. of "a":
  50. doAssert v.to(int) == 10
  51. of "b":
  52. doAssert v.to(int) == 20
  53. of "c":
  54. doAssert v.to(int) == 30
  55. else:
  56. doAssert false
  57. block: # testItems
  58. let obj = newJsObject()
  59. obj.a = 10
  60. obj.b = 20
  61. obj.c = 30
  62. for v in obj.items:
  63. doAssert v.to(int) in [10, 20, 30]
  64. block: # testKeys
  65. let obj = newJsObject()
  66. obj.a = 10
  67. obj.b = 20
  68. obj.c = 30
  69. for v in obj.keys:
  70. doAssert $v in ["a", "b", "c"]
  71. block: # Test JsObject equality
  72. {. emit: "var comparison = {a: 22, b: 'test'};" .}
  73. var comparison {. importjs, nodecl .}: JsObject
  74. let obj = newJsObject()
  75. obj.a = 22
  76. obj.b = "test".cstring
  77. doAssert obj.a == comparison.a and obj.b == comparison.b
  78. block: # Test JsObject literal
  79. {. emit: "var comparison = {a: 22, b: 'test'};" .}
  80. var comparison {. importjs, nodecl .}: JsObject
  81. let obj = JsObject{ a: 22, b: "test".cstring }
  82. doAssert obj.a == comparison.a and obj.b == comparison.b
  83. # Tests for JsAssoc
  84. block: # Test JsAssoc []= and []
  85. let obj = newJsAssoc[int, int]()
  86. obj[1] = 11
  87. doAssert not compiles(obj["a"] = 11)
  88. doAssert not compiles(obj["a"])
  89. doAssert not compiles(obj[2] = "test")
  90. doAssert not compiles(obj[3] = "test".cstring)
  91. doAssert obj[1] == 11
  92. block: # Test JsAssoc .= and .
  93. let obj = newJsAssoc[cstring, int]()
  94. var working = true
  95. obj.a = 11
  96. obj.`$!&` = 42
  97. doAssert not compiles(obj.b = "test")
  98. doAssert not compiles(obj.c = "test".cstring)
  99. doAssert obj.a == 11
  100. doAssert obj.`$!&` == 42
  101. block: # Test JsAssoc .()
  102. let obj = newJsAssoc[cstring, proc(e: int): int]()
  103. obj.a = proc(e: int): int = e * e
  104. doAssert obj.a(10) == 100
  105. block: # Test JsAssoc []()
  106. let obj = newJsAssoc[cstring, proc(e: int): int]()
  107. obj.a = proc(e: int): int = e * e
  108. let call = obj["a"]
  109. doAssert call(10) == 100
  110. # Test JsAssoc Iterators
  111. block: # testPairs
  112. let obj = newJsAssoc[cstring, int]()
  113. obj.a = 10
  114. obj.b = 20
  115. obj.c = 30
  116. for k, v in obj.pairs:
  117. case $k
  118. of "a":
  119. doAssert v == 10
  120. of "b":
  121. doAssert v == 20
  122. of "c":
  123. doAssert v == 30
  124. else:
  125. doAssert false
  126. block: # testItems
  127. let obj = newJsAssoc[cstring, int]()
  128. obj.a = 10
  129. obj.b = 20
  130. obj.c = 30
  131. for v in obj.items:
  132. doAssert v in [10, 20, 30]
  133. block: # testKeys
  134. let obj = newJsAssoc[cstring, int]()
  135. obj.a = 10
  136. obj.b = 20
  137. obj.c = 30
  138. for v in obj.keys:
  139. doAssert v in [cstring"a", cstring"b", cstring"c"]
  140. block: # Test JsAssoc equality
  141. {. emit: "var comparison = {a: 22, b: 55};" .}
  142. var comparison {. importjs, nodecl .}: JsAssoc[cstring, int]
  143. let obj = newJsAssoc[cstring, int]()
  144. obj.a = 22
  145. obj.b = 55
  146. doAssert obj.a == comparison.a and obj.b == comparison.b
  147. block: # Test JsAssoc literal
  148. {. emit: "var comparison = {a: 22, b: 55};" .}
  149. var comparison {. importjs, nodecl .}: JsAssoc[cstring, int]
  150. let obj = JsAssoc[cstring, int]{ a: 22, b: 55 }
  151. doAssert compiles(JsAssoc[int, int]{ 1: 22, 2: 55 })
  152. doAssert comparison.a == obj.a and comparison.b == obj.b
  153. doAssert not compiles(JsAssoc[cstring, int]{ a: "test" })
  154. # Tests for macros on non-JsRoot objects
  155. block: # Test lit
  156. type TestObject = object
  157. a: int
  158. b: cstring
  159. {. emit: "var comparison = {a: 1};" .}
  160. var comparison {. importjs, nodecl .}: TestObject
  161. let obj = TestObject{ a: 1 }
  162. doAssert obj == comparison
  163. block: # Test bindMethod
  164. type TestObject = object
  165. a: int
  166. onWhatever: proc(e: int): int
  167. proc handleWhatever(this: TestObject, e: int): int =
  168. e + this.a
  169. block:
  170. let obj = TestObject(a: 9, onWhatever: bindMethod(handleWhatever))
  171. doAssert obj.onWhatever(1) == 10
  172. block:
  173. {.emit: "function jsProc(n) { return n; }" .}
  174. proc jsProc(x: int32): JsObject {.importjs: "jsProc(#)".}
  175. block:
  176. var x = jsProc(1)
  177. var y = jsProc(2)
  178. console.log x + y
  179. console.log ++x
  180. x += jsProc(10)
  181. console.log x
  182. block:
  183. {.emit:
  184. """
  185. function Event(name) { this.name = name; }
  186. function on(eventName, eventHandler) { eventHandler(new Event(eventName + ": test")); }
  187. var jslib = { "on": on, "subscribe": on };
  188. """
  189. .}
  190. type Event = object
  191. name: cstring
  192. proc on(event: cstring, handler: proc) {.importjs: "on(#,#)".}
  193. var jslib {.importjs: "jslib", nodecl.}: JsObject
  194. on("click") do (e: Event):
  195. console.log e
  196. jslib.on("reloaded") do:
  197. console.log jsarguments[0]
  198. # this test case is different from the above, because
  199. # `subscribe` is not overloaded in the current scope
  200. jslib.subscribe("updates"):
  201. console.log jsarguments[0]
  202. block:
  203. doAssert jsUndefined == jsNull
  204. doAssert jsUndefined == nil
  205. doAssert jsNull == nil
  206. doAssert jsUndefined.isNil
  207. doAssert jsNull.isNil
  208. doAssert jsNull.isNull
  209. doAssert jsUndefined.isUndefined
  210. block: # test **
  211. var a = toJs(0)
  212. var b = toJs(0)
  213. doAssert to(a ** b, int) == 1
  214. a = toJs(1)
  215. b = toJs(1)
  216. doAssert to(a ** b, int) == 1
  217. a = toJs(-1)
  218. b = toJs(-1)
  219. doAssert to(a ** b, int) == -1
  220. a = toJs(6)
  221. b = toJs(6)
  222. doAssert to(a ** b, int) == 46656
  223. a = toJs(5.5)
  224. b = toJs(3)
  225. doAssert to(a ** b, float) == 166.375
  226. a = toJs(5)
  227. b = toJs(3.0)
  228. doAssert to(a ** b, float) == 125.0
  229. a = toJs(7.0)
  230. b = toJS(6.0)
  231. doAssert to(a ** b, float) == 117649.0
  232. a = toJs(8)
  233. b = toJS(-2)
  234. doAssert to(a ** b, float) == 0.015625
  235. a = toJs(1)
  236. b = toJs(1)
  237. doAssert to(`**`(a + a, b), int) == 2
  238. doAssert to(`**`(toJs(1) + toJs(1), toJs(2)), int) == 4