tjsffi_old.nim 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. discard """
  2. output: '''
  3. true
  4. true
  5. true
  6. true
  7. true
  8. true
  9. true
  10. true
  11. true
  12. true
  13. true
  14. true
  15. true
  16. true
  17. true
  18. true
  19. 3
  20. 2
  21. 12
  22. Event { name: 'click: test' }
  23. Event { name: 'reloaded: test' }
  24. Event { name: 'updates: test' }
  25. true
  26. true
  27. true
  28. true
  29. true
  30. true
  31. true
  32. '''
  33. """
  34. ## same as tjsffi, but this test uses the old names: importc and
  35. ## importcpp. This test is for backwards compatibility.
  36. # xxx instead of maintaining this near-duplicate test file, just have tests
  37. # that check that importc, importcpp, importjs work and remove this file.
  38. import jsffi, jsconsole
  39. # Tests for JsObject
  40. # Test JsObject []= and []
  41. block:
  42. proc test(): bool =
  43. let obj = newJsObject()
  44. var working = true
  45. obj["a"] = 11
  46. obj["b"] = "test"
  47. obj["c"] = "test".cstring
  48. working = working and obj["a"].to(int) == 11
  49. working = working and obj["c"].to(cstring) == "test".cstring
  50. working
  51. echo test()
  52. # Test JsObject .= and .
  53. block:
  54. proc test(): bool =
  55. let obj = newJsObject()
  56. var working = true
  57. obj.a = 11
  58. obj.b = "test"
  59. obj.c = "test".cstring
  60. obj.`$!&` = 42
  61. obj.`while` = 99
  62. working = working and obj.a.to(int) == 11
  63. working = working and obj.b.to(string) == "test"
  64. working = working and obj.c.to(cstring) == "test".cstring
  65. working = working and obj.`$!&`.to(int) == 42
  66. working = working and obj.`while`.to(int) == 99
  67. working
  68. echo test()
  69. # Test JsObject .()
  70. block:
  71. proc test(): bool =
  72. let obj = newJsObject()
  73. obj.`?!$` = proc(x, y, z: int, t: cstring): cstring = t & $(x + y + z)
  74. obj.`?!$`(1, 2, 3, "Result is: ").to(cstring) == cstring"Result is: 6"
  75. echo test()
  76. # Test JsObject []()
  77. block:
  78. proc test(): bool =
  79. let obj = newJsObject()
  80. obj.a = proc(x, y, z: int, t: string): string = t & $(x + y + z)
  81. let call = obj["a"].to(proc(x, y, z: int, t: string): string)
  82. call(1, 2, 3, "Result is: ") == "Result is: 6"
  83. echo test()
  84. # Test JsObject Iterators
  85. block:
  86. proc testPairs(): bool =
  87. let obj = newJsObject()
  88. var working = true
  89. obj.a = 10
  90. obj.b = 20
  91. obj.c = 30
  92. for k, v in obj.pairs:
  93. case $k
  94. of "a":
  95. working = working and v.to(int) == 10
  96. of "b":
  97. working = working and v.to(int) == 20
  98. of "c":
  99. working = working and v.to(int) == 30
  100. else:
  101. return false
  102. working
  103. proc testItems(): bool =
  104. let obj = newJsObject()
  105. var working = true
  106. obj.a = 10
  107. obj.b = 20
  108. obj.c = 30
  109. for v in obj.items:
  110. working = working and v.to(int) in [10, 20, 30]
  111. working
  112. proc testKeys(): bool =
  113. let obj = newJsObject()
  114. var working = true
  115. obj.a = 10
  116. obj.b = 20
  117. obj.c = 30
  118. for v in obj.keys:
  119. working = working and $v in ["a", "b", "c"]
  120. working
  121. proc test(): bool = testPairs() and testItems() and testKeys()
  122. echo test()
  123. # Test JsObject equality
  124. block:
  125. proc test(): bool =
  126. {. emit: "var comparison = {a: 22, b: 'test'};" .}
  127. var comparison {. importc, nodecl .}: JsObject
  128. let obj = newJsObject()
  129. obj.a = 22
  130. obj.b = "test".cstring
  131. obj.a == comparison.a and obj.b == comparison.b
  132. echo test()
  133. # Test JsObject literal
  134. block:
  135. proc test(): bool =
  136. {. emit: "var comparison = {a: 22, b: 'test'};" .}
  137. var comparison {. importc, nodecl .}: JsObject
  138. let obj = JsObject{ a: 22, b: "test".cstring }
  139. obj.a == comparison.a and obj.b == comparison.b
  140. echo test()
  141. # Tests for JsAssoc
  142. # Test JsAssoc []= and []
  143. block:
  144. proc test(): bool =
  145. let obj = newJsAssoc[int, int]()
  146. var working = true
  147. obj[1] = 11
  148. working = working and not compiles(obj["a"] = 11)
  149. working = working and not compiles(obj["a"])
  150. working = working and not compiles(obj[2] = "test")
  151. working = working and not compiles(obj[3] = "test".cstring)
  152. working = working and obj[1] == 11
  153. working
  154. echo test()
  155. # Test JsAssoc .= and .
  156. block:
  157. proc test(): bool =
  158. let obj = newJsAssoc[cstring, int]()
  159. var working = true
  160. obj.a = 11
  161. obj.`$!&` = 42
  162. working = working and not compiles(obj.b = "test")
  163. working = working and not compiles(obj.c = "test".cstring)
  164. working = working and obj.a == 11
  165. working = working and obj.`$!&` == 42
  166. working
  167. echo test()
  168. # Test JsAssoc .()
  169. block:
  170. proc test(): bool =
  171. let obj = newJsAssoc[cstring, proc(e: int): int]()
  172. obj.a = proc(e: int): int = e * e
  173. obj.a(10) == 100
  174. echo test()
  175. # Test JsAssoc []()
  176. block:
  177. proc test(): bool =
  178. let obj = newJsAssoc[cstring, proc(e: int): int]()
  179. obj.a = proc(e: int): int = e * e
  180. let call = obj["a"]
  181. call(10) == 100
  182. echo test()
  183. # Test JsAssoc Iterators
  184. block:
  185. proc testPairs(): bool =
  186. let obj = newJsAssoc[cstring, int]()
  187. var working = true
  188. obj.a = 10
  189. obj.b = 20
  190. obj.c = 30
  191. for k, v in obj.pairs:
  192. case $k
  193. of "a":
  194. working = working and v == 10
  195. of "b":
  196. working = working and v == 20
  197. of "c":
  198. working = working and v == 30
  199. else:
  200. return false
  201. working
  202. proc testItems(): bool =
  203. let obj = newJsAssoc[cstring, int]()
  204. var working = true
  205. obj.a = 10
  206. obj.b = 20
  207. obj.c = 30
  208. for v in obj.items:
  209. working = working and v in [10, 20, 30]
  210. working
  211. proc testKeys(): bool =
  212. let obj = newJsAssoc[cstring, int]()
  213. var working = true
  214. obj.a = 10
  215. obj.b = 20
  216. obj.c = 30
  217. for v in obj.keys:
  218. working = working and v in [cstring"a", cstring"b", cstring"c"]
  219. working
  220. proc test(): bool = testPairs() and testItems() and testKeys()
  221. echo test()
  222. # Test JsAssoc equality
  223. block:
  224. proc test(): bool =
  225. {. emit: "var comparison = {a: 22, b: 55};" .}
  226. var comparison {. importcpp, nodecl .}: JsAssoc[cstring, int]
  227. let obj = newJsAssoc[cstring, int]()
  228. obj.a = 22
  229. obj.b = 55
  230. obj.a == comparison.a and obj.b == comparison.b
  231. echo test()
  232. # Test JsAssoc literal
  233. block:
  234. proc test(): bool =
  235. {. emit: "var comparison = {a: 22, b: 55};" .}
  236. var comparison {. importcpp, nodecl .}: JsAssoc[cstring, int]
  237. let obj = JsAssoc[cstring, int]{ a: 22, b: 55 }
  238. var working = true
  239. working = working and
  240. compiles(JsAssoc[int, int]{ 1: 22, 2: 55 })
  241. working = working and
  242. comparison.a == obj.a and comparison.b == obj.b
  243. working = working and
  244. not compiles(JsAssoc[cstring, int]{ a: "test" })
  245. working
  246. echo test()
  247. # Tests for macros on non-JsRoot objects
  248. # Test lit
  249. block:
  250. type TestObject = object
  251. a: int
  252. b: cstring
  253. proc test(): bool =
  254. {. emit: "var comparison = {a: 1};" .}
  255. var comparison {. importc, nodecl .}: TestObject
  256. let obj = TestObject{ a: 1 }
  257. obj == comparison
  258. echo test()
  259. # Test bindMethod
  260. block:
  261. type TestObject = object
  262. a: int
  263. onWhatever: proc(e: int): int
  264. proc handleWhatever(this: TestObject, e: int): int =
  265. e + this.a
  266. proc test(): bool =
  267. let obj = TestObject(a: 9, onWhatever: bindMethod(handleWhatever))
  268. obj.onWhatever(1) == 10
  269. echo test()
  270. block:
  271. {.emit: "function jsProc(n) { return n; }" .}
  272. proc jsProc(x: int32): JsObject {.importc: "jsProc".}
  273. proc test() =
  274. var x = jsProc(1)
  275. var y = jsProc(2)
  276. console.log x + y
  277. console.log ++x
  278. x += jsProc(10)
  279. console.log x
  280. test()
  281. block:
  282. {.emit:
  283. """
  284. function Event(name) { this.name = name; }
  285. function on(eventName, eventHandler) { eventHandler(new Event(eventName + ": test")); }
  286. var jslib = { "on": on, "subscribe": on };
  287. """
  288. .}
  289. type Event = object
  290. name: cstring
  291. proc on(event: cstring, handler: proc) {.importc: "on".}
  292. var jslib {.importc: "jslib", nodecl.}: JsObject
  293. on("click") do (e: Event):
  294. console.log e
  295. jslib.on("reloaded") do:
  296. console.log jsarguments[0]
  297. # this test case is different from the above, because
  298. # `subscribe` is not overloaded in the current scope
  299. jslib.subscribe("updates"):
  300. console.log jsarguments[0]
  301. block:
  302. echo jsUndefined == jsNull
  303. echo jsUndefined == nil
  304. echo jsNull == nil
  305. echo jsUndefined.isNil
  306. echo jsNull.isNil
  307. echo jsNull.isNull
  308. echo jsUndefined.isUndefined