ref_test.wast 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. ;; Abstract Types
  2. (module
  3. (type $ft (func))
  4. (type $st (struct))
  5. (type $at (array i8))
  6. (table $ta 10 anyref)
  7. (table $tf 10 funcref)
  8. (table $te 10 externref)
  9. (elem declare func $f)
  10. (func $f)
  11. (func (export "init") (param $x externref)
  12. (table.set $ta (i32.const 0) (ref.null any))
  13. (table.set $ta (i32.const 1) (ref.null struct))
  14. (table.set $ta (i32.const 2) (ref.null none))
  15. (table.set $ta (i32.const 3) (ref.i31 (i32.const 7)))
  16. (table.set $ta (i32.const 4) (struct.new_default $st))
  17. (table.set $ta (i32.const 5) (array.new_default $at (i32.const 0)))
  18. (table.set $ta (i32.const 6) (any.convert_extern (local.get $x)))
  19. (table.set $ta (i32.const 7) (any.convert_extern (ref.null extern)))
  20. (table.set $tf (i32.const 0) (ref.null nofunc))
  21. (table.set $tf (i32.const 1) (ref.null func))
  22. (table.set $tf (i32.const 2) (ref.func $f))
  23. (table.set $te (i32.const 0) (ref.null noextern))
  24. (table.set $te (i32.const 1) (ref.null extern))
  25. (table.set $te (i32.const 2) (local.get $x))
  26. (table.set $te (i32.const 3) (extern.convert_any (ref.i31 (i32.const 8))))
  27. (table.set $te (i32.const 4) (extern.convert_any (struct.new_default $st)))
  28. (table.set $te (i32.const 5) (extern.convert_any (ref.null any)))
  29. )
  30. (func (export "ref_test_null_data") (param $i i32) (result i32)
  31. (i32.add
  32. (ref.is_null (table.get $ta (local.get $i)))
  33. (ref.test nullref (table.get $ta (local.get $i)))
  34. )
  35. )
  36. (func (export "ref_test_any") (param $i i32) (result i32)
  37. (i32.add
  38. (ref.test (ref any) (table.get $ta (local.get $i)))
  39. (ref.test anyref (table.get $ta (local.get $i)))
  40. )
  41. )
  42. (func (export "ref_test_eq") (param $i i32) (result i32)
  43. (i32.add
  44. (ref.test (ref eq) (table.get $ta (local.get $i)))
  45. (ref.test eqref (table.get $ta (local.get $i)))
  46. )
  47. )
  48. (func (export "ref_test_i31") (param $i i32) (result i32)
  49. (i32.add
  50. (ref.test (ref i31) (table.get $ta (local.get $i)))
  51. (ref.test i31ref (table.get $ta (local.get $i)))
  52. )
  53. )
  54. (func (export "ref_test_struct") (param $i i32) (result i32)
  55. (i32.add
  56. (ref.test (ref struct) (table.get $ta (local.get $i)))
  57. (ref.test structref (table.get $ta (local.get $i)))
  58. )
  59. )
  60. (func (export "ref_test_array") (param $i i32) (result i32)
  61. (i32.add
  62. (ref.test (ref array) (table.get $ta (local.get $i)))
  63. (ref.test arrayref (table.get $ta (local.get $i)))
  64. )
  65. )
  66. (func (export "ref_test_null_func") (param $i i32) (result i32)
  67. (i32.add
  68. (ref.is_null (table.get $tf (local.get $i)))
  69. (ref.test (ref null nofunc) (table.get $tf (local.get $i)))
  70. )
  71. )
  72. (func (export "ref_test_func") (param $i i32) (result i32)
  73. (i32.add
  74. (ref.test (ref func) (table.get $tf (local.get $i)))
  75. (ref.test funcref (table.get $tf (local.get $i)))
  76. )
  77. )
  78. (func (export "ref_test_null_extern") (param $i i32) (result i32)
  79. (i32.add
  80. (ref.is_null (table.get $te (local.get $i)))
  81. (ref.test (ref null noextern) (table.get $te (local.get $i)))
  82. )
  83. )
  84. (func (export "ref_test_extern") (param $i i32) (result i32)
  85. (i32.add
  86. (ref.test (ref extern) (table.get $te (local.get $i)))
  87. (ref.test externref (table.get $te (local.get $i)))
  88. )
  89. )
  90. )
  91. (invoke "init" (ref.extern 0))
  92. (assert_return (invoke "ref_test_null_data" (i32.const 0)) (i32.const 2))
  93. (assert_return (invoke "ref_test_null_data" (i32.const 1)) (i32.const 2))
  94. (assert_return (invoke "ref_test_null_data" (i32.const 2)) (i32.const 2))
  95. (assert_return (invoke "ref_test_null_data" (i32.const 3)) (i32.const 0))
  96. (assert_return (invoke "ref_test_null_data" (i32.const 4)) (i32.const 0))
  97. (assert_return (invoke "ref_test_null_data" (i32.const 5)) (i32.const 0))
  98. (assert_return (invoke "ref_test_null_data" (i32.const 6)) (i32.const 0))
  99. (assert_return (invoke "ref_test_null_data" (i32.const 7)) (i32.const 2))
  100. (assert_return (invoke "ref_test_any" (i32.const 0)) (i32.const 1))
  101. (assert_return (invoke "ref_test_any" (i32.const 1)) (i32.const 1))
  102. (assert_return (invoke "ref_test_any" (i32.const 2)) (i32.const 1))
  103. (assert_return (invoke "ref_test_any" (i32.const 3)) (i32.const 2))
  104. (assert_return (invoke "ref_test_any" (i32.const 4)) (i32.const 2))
  105. (assert_return (invoke "ref_test_any" (i32.const 5)) (i32.const 2))
  106. (assert_return (invoke "ref_test_any" (i32.const 6)) (i32.const 2))
  107. (assert_return (invoke "ref_test_any" (i32.const 7)) (i32.const 1))
  108. (assert_return (invoke "ref_test_eq" (i32.const 0)) (i32.const 1))
  109. (assert_return (invoke "ref_test_eq" (i32.const 1)) (i32.const 1))
  110. (assert_return (invoke "ref_test_eq" (i32.const 2)) (i32.const 1))
  111. (assert_return (invoke "ref_test_eq" (i32.const 3)) (i32.const 2))
  112. (assert_return (invoke "ref_test_eq" (i32.const 4)) (i32.const 2))
  113. (assert_return (invoke "ref_test_eq" (i32.const 5)) (i32.const 2))
  114. (assert_return (invoke "ref_test_eq" (i32.const 6)) (i32.const 0))
  115. (assert_return (invoke "ref_test_eq" (i32.const 7)) (i32.const 1))
  116. (assert_return (invoke "ref_test_i31" (i32.const 0)) (i32.const 1))
  117. (assert_return (invoke "ref_test_i31" (i32.const 1)) (i32.const 1))
  118. (assert_return (invoke "ref_test_i31" (i32.const 2)) (i32.const 1))
  119. (assert_return (invoke "ref_test_i31" (i32.const 3)) (i32.const 2))
  120. (assert_return (invoke "ref_test_i31" (i32.const 4)) (i32.const 0))
  121. (assert_return (invoke "ref_test_i31" (i32.const 5)) (i32.const 0))
  122. (assert_return (invoke "ref_test_i31" (i32.const 6)) (i32.const 0))
  123. (assert_return (invoke "ref_test_i31" (i32.const 7)) (i32.const 1))
  124. (assert_return (invoke "ref_test_struct" (i32.const 0)) (i32.const 1))
  125. (assert_return (invoke "ref_test_struct" (i32.const 1)) (i32.const 1))
  126. (assert_return (invoke "ref_test_struct" (i32.const 2)) (i32.const 1))
  127. (assert_return (invoke "ref_test_struct" (i32.const 3)) (i32.const 0))
  128. (assert_return (invoke "ref_test_struct" (i32.const 4)) (i32.const 2))
  129. (assert_return (invoke "ref_test_struct" (i32.const 5)) (i32.const 0))
  130. (assert_return (invoke "ref_test_struct" (i32.const 6)) (i32.const 0))
  131. (assert_return (invoke "ref_test_struct" (i32.const 7)) (i32.const 1))
  132. (assert_return (invoke "ref_test_array" (i32.const 0)) (i32.const 1))
  133. (assert_return (invoke "ref_test_array" (i32.const 1)) (i32.const 1))
  134. (assert_return (invoke "ref_test_array" (i32.const 2)) (i32.const 1))
  135. (assert_return (invoke "ref_test_array" (i32.const 3)) (i32.const 0))
  136. (assert_return (invoke "ref_test_array" (i32.const 4)) (i32.const 0))
  137. (assert_return (invoke "ref_test_array" (i32.const 5)) (i32.const 2))
  138. (assert_return (invoke "ref_test_array" (i32.const 6)) (i32.const 0))
  139. (assert_return (invoke "ref_test_array" (i32.const 7)) (i32.const 1))
  140. (assert_return (invoke "ref_test_null_func" (i32.const 0)) (i32.const 2))
  141. (assert_return (invoke "ref_test_null_func" (i32.const 1)) (i32.const 2))
  142. (assert_return (invoke "ref_test_null_func" (i32.const 2)) (i32.const 0))
  143. (assert_return (invoke "ref_test_func" (i32.const 0)) (i32.const 1))
  144. (assert_return (invoke "ref_test_func" (i32.const 1)) (i32.const 1))
  145. (assert_return (invoke "ref_test_func" (i32.const 2)) (i32.const 2))
  146. (assert_return (invoke "ref_test_null_extern" (i32.const 0)) (i32.const 2))
  147. (assert_return (invoke "ref_test_null_extern" (i32.const 1)) (i32.const 2))
  148. (assert_return (invoke "ref_test_null_extern" (i32.const 2)) (i32.const 0))
  149. (assert_return (invoke "ref_test_null_extern" (i32.const 3)) (i32.const 0))
  150. (assert_return (invoke "ref_test_null_extern" (i32.const 4)) (i32.const 0))
  151. (assert_return (invoke "ref_test_null_extern" (i32.const 5)) (i32.const 2))
  152. (assert_return (invoke "ref_test_extern" (i32.const 0)) (i32.const 1))
  153. (assert_return (invoke "ref_test_extern" (i32.const 1)) (i32.const 1))
  154. (assert_return (invoke "ref_test_extern" (i32.const 2)) (i32.const 2))
  155. (assert_return (invoke "ref_test_extern" (i32.const 3)) (i32.const 2))
  156. (assert_return (invoke "ref_test_extern" (i32.const 4)) (i32.const 2))
  157. (assert_return (invoke "ref_test_extern" (i32.const 5)) (i32.const 1))
  158. ;; Concrete Types
  159. (module
  160. (type $t0 (sub (struct)))
  161. (type $t1 (sub $t0 (struct (field i32))))
  162. (type $t1' (sub $t0 (struct (field i32))))
  163. (type $t2 (sub $t1 (struct (field i32 i32))))
  164. (type $t2' (sub $t1' (struct (field i32 i32))))
  165. (type $t3 (sub $t0 (struct (field i32 i32))))
  166. (type $t0' (sub $t0 (struct)))
  167. (type $t4 (sub $t0' (struct (field i32 i32))))
  168. (table 20 (ref null struct))
  169. (func $init
  170. (table.set (i32.const 0) (struct.new_default $t0))
  171. (table.set (i32.const 10) (struct.new_default $t0))
  172. (table.set (i32.const 1) (struct.new_default $t1))
  173. (table.set (i32.const 11) (struct.new_default $t1'))
  174. (table.set (i32.const 2) (struct.new_default $t2))
  175. (table.set (i32.const 12) (struct.new_default $t2'))
  176. (table.set (i32.const 3) (struct.new_default $t3))
  177. (table.set (i32.const 4) (struct.new_default $t4))
  178. )
  179. (func (export "test-sub")
  180. (call $init)
  181. (block $l
  182. ;; must hold
  183. (br_if $l (i32.eqz (ref.test (ref null $t0) (ref.null struct))))
  184. (br_if $l (i32.eqz (ref.test (ref null $t0) (ref.null $t0))))
  185. (br_if $l (i32.eqz (ref.test (ref null $t0) (ref.null $t1))))
  186. (br_if $l (i32.eqz (ref.test (ref null $t0) (ref.null $t2))))
  187. (br_if $l (i32.eqz (ref.test (ref null $t0) (ref.null $t3))))
  188. (br_if $l (i32.eqz (ref.test (ref null $t0) (ref.null $t4))))
  189. (br_if $l (i32.eqz (ref.test (ref null $t0) (table.get (i32.const 0)))))
  190. (br_if $l (i32.eqz (ref.test (ref null $t0) (table.get (i32.const 1)))))
  191. (br_if $l (i32.eqz (ref.test (ref null $t0) (table.get (i32.const 2)))))
  192. (br_if $l (i32.eqz (ref.test (ref null $t0) (table.get (i32.const 3)))))
  193. (br_if $l (i32.eqz (ref.test (ref null $t0) (table.get (i32.const 4)))))
  194. (br_if $l (i32.eqz (ref.test (ref null $t1) (ref.null struct))))
  195. (br_if $l (i32.eqz (ref.test (ref null $t1) (ref.null $t0))))
  196. (br_if $l (i32.eqz (ref.test (ref null $t1) (ref.null $t1))))
  197. (br_if $l (i32.eqz (ref.test (ref null $t1) (ref.null $t2))))
  198. (br_if $l (i32.eqz (ref.test (ref null $t1) (ref.null $t3))))
  199. (br_if $l (i32.eqz (ref.test (ref null $t1) (ref.null $t4))))
  200. (br_if $l (i32.eqz (ref.test (ref null $t1) (table.get (i32.const 1)))))
  201. (br_if $l (i32.eqz (ref.test (ref null $t1) (table.get (i32.const 2)))))
  202. (br_if $l (i32.eqz (ref.test (ref null $t2) (ref.null struct))))
  203. (br_if $l (i32.eqz (ref.test (ref null $t2) (ref.null $t0))))
  204. (br_if $l (i32.eqz (ref.test (ref null $t2) (ref.null $t1))))
  205. (br_if $l (i32.eqz (ref.test (ref null $t2) (ref.null $t2))))
  206. (br_if $l (i32.eqz (ref.test (ref null $t2) (ref.null $t3))))
  207. (br_if $l (i32.eqz (ref.test (ref null $t2) (ref.null $t4))))
  208. (br_if $l (i32.eqz (ref.test (ref null $t2) (table.get (i32.const 2)))))
  209. (br_if $l (i32.eqz (ref.test (ref null $t3) (ref.null struct))))
  210. (br_if $l (i32.eqz (ref.test (ref null $t3) (ref.null $t0))))
  211. (br_if $l (i32.eqz (ref.test (ref null $t3) (ref.null $t1))))
  212. (br_if $l (i32.eqz (ref.test (ref null $t3) (ref.null $t2))))
  213. (br_if $l (i32.eqz (ref.test (ref null $t3) (ref.null $t3))))
  214. (br_if $l (i32.eqz (ref.test (ref null $t3) (ref.null $t4))))
  215. (br_if $l (i32.eqz (ref.test (ref null $t3) (table.get (i32.const 3)))))
  216. (br_if $l (i32.eqz (ref.test (ref null $t4) (ref.null struct))))
  217. (br_if $l (i32.eqz (ref.test (ref null $t4) (ref.null $t0))))
  218. (br_if $l (i32.eqz (ref.test (ref null $t4) (ref.null $t1))))
  219. (br_if $l (i32.eqz (ref.test (ref null $t4) (ref.null $t2))))
  220. (br_if $l (i32.eqz (ref.test (ref null $t4) (ref.null $t3))))
  221. (br_if $l (i32.eqz (ref.test (ref null $t4) (ref.null $t4))))
  222. (br_if $l (i32.eqz (ref.test (ref null $t4) (table.get (i32.const 4)))))
  223. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 0)))))
  224. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 1)))))
  225. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 2)))))
  226. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 3)))))
  227. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 4)))))
  228. (br_if $l (i32.eqz (ref.test (ref $t1) (table.get (i32.const 1)))))
  229. (br_if $l (i32.eqz (ref.test (ref $t1) (table.get (i32.const 2)))))
  230. (br_if $l (i32.eqz (ref.test (ref $t2) (table.get (i32.const 2)))))
  231. (br_if $l (i32.eqz (ref.test (ref $t3) (table.get (i32.const 3)))))
  232. (br_if $l (i32.eqz (ref.test (ref $t4) (table.get (i32.const 4)))))
  233. ;; must not hold
  234. (br_if $l (ref.test (ref $t0) (ref.null struct)))
  235. (br_if $l (ref.test (ref $t1) (ref.null struct)))
  236. (br_if $l (ref.test (ref $t2) (ref.null struct)))
  237. (br_if $l (ref.test (ref $t3) (ref.null struct)))
  238. (br_if $l (ref.test (ref $t4) (ref.null struct)))
  239. (br_if $l (ref.test (ref $t1) (table.get (i32.const 0))))
  240. (br_if $l (ref.test (ref $t1) (table.get (i32.const 3))))
  241. (br_if $l (ref.test (ref $t1) (table.get (i32.const 4))))
  242. (br_if $l (ref.test (ref $t2) (table.get (i32.const 0))))
  243. (br_if $l (ref.test (ref $t2) (table.get (i32.const 1))))
  244. (br_if $l (ref.test (ref $t2) (table.get (i32.const 3))))
  245. (br_if $l (ref.test (ref $t2) (table.get (i32.const 4))))
  246. (br_if $l (ref.test (ref $t3) (table.get (i32.const 0))))
  247. (br_if $l (ref.test (ref $t3) (table.get (i32.const 1))))
  248. (br_if $l (ref.test (ref $t3) (table.get (i32.const 2))))
  249. (br_if $l (ref.test (ref $t3) (table.get (i32.const 4))))
  250. (br_if $l (ref.test (ref $t4) (table.get (i32.const 0))))
  251. (br_if $l (ref.test (ref $t4) (table.get (i32.const 1))))
  252. (br_if $l (ref.test (ref $t4) (table.get (i32.const 2))))
  253. (br_if $l (ref.test (ref $t4) (table.get (i32.const 3))))
  254. (return)
  255. )
  256. (unreachable)
  257. )
  258. (func (export "test-canon")
  259. (call $init)
  260. (block $l
  261. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 0)))))
  262. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 1)))))
  263. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 2)))))
  264. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 3)))))
  265. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 4)))))
  266. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 10)))))
  267. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 11)))))
  268. (br_if $l (i32.eqz (ref.test (ref $t0) (table.get (i32.const 12)))))
  269. ;; Must have explicit sub relationship
  270. ;; (br_if $l (i32.eqz (ref.test (ref $t1') (table.get (i32.const 1)))))
  271. ;; (br_if $l (i32.eqz (ref.test (ref $t1') (table.get (i32.const 2)))))
  272. ;; (br_if $l (i32.eqz (ref.test (ref $t1) (table.get (i32.const 11)))))
  273. ;; (br_if $l (i32.eqz (ref.test (ref $t1) (table.get (i32.const 12)))))
  274. ;; (br_if $l (i32.eqz (ref.test (ref $t2') (table.get (i32.const 2)))))
  275. ;; (br_if $l (i32.eqz (ref.test (ref $t2) (table.get (i32.const 12)))))
  276. (return)
  277. )
  278. (unreachable)
  279. )
  280. )
  281. (assert_return (invoke "test-sub"))
  282. (assert_return (invoke "test-canon"))