br_on_cast.wast 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. ;; Abstract Types
  2. (module
  3. (type $ft (func (result i32)))
  4. (type $st (struct (field i16)))
  5. (type $at (array i8))
  6. (table 10 anyref)
  7. (elem declare func $f)
  8. (func $f (result i32) (i32.const 9))
  9. (func (export "init") (param $x externref)
  10. (table.set (i32.const 0) (ref.null any))
  11. (table.set (i32.const 1) (ref.i31 (i32.const 7)))
  12. (table.set (i32.const 2) (struct.new $st (i32.const 6)))
  13. (table.set (i32.const 3) (array.new $at (i32.const 5) (i32.const 3)))
  14. (table.set (i32.const 4) (any.convert_extern (local.get $x)))
  15. )
  16. (func (export "br_on_null") (param $i i32) (result i32)
  17. (block $l
  18. (br_on_null $l (table.get (local.get $i)))
  19. (return (i32.const -1))
  20. )
  21. (i32.const 0)
  22. )
  23. (func (export "br_on_i31") (param $i i32) (result i32)
  24. (block $l (result (ref i31))
  25. (br_on_cast $l anyref (ref i31) (table.get (local.get $i)))
  26. (return (i32.const -1))
  27. )
  28. (i31.get_u)
  29. )
  30. (func (export "br_on_struct") (param $i i32) (result i32)
  31. (block $l (result (ref struct))
  32. (br_on_cast $l anyref (ref struct) (table.get (local.get $i)))
  33. (return (i32.const -1))
  34. )
  35. (block $l2 (param structref) (result (ref $st))
  36. (block $l3 (param structref) (result (ref $at))
  37. (br_on_cast $l2 structref (ref $st))
  38. (br_on_cast $l3 anyref (ref $at))
  39. (return (i32.const -2))
  40. )
  41. (return (array.get_u $at (i32.const 0)))
  42. )
  43. (struct.get_s $st 0)
  44. )
  45. (func (export "br_on_array") (param $i i32) (result i32)
  46. (block $l (result (ref array))
  47. (br_on_cast $l anyref (ref array) (table.get (local.get $i)))
  48. (return (i32.const -1))
  49. )
  50. (array.len)
  51. )
  52. (func (export "null-diff") (param $i i32) (result i32)
  53. (block $l (result (ref null struct))
  54. (block (result (ref any))
  55. (br_on_cast $l (ref null any) (ref null struct) (table.get (local.get $i)))
  56. )
  57. (return (i32.const 0))
  58. )
  59. (return (i32.const 1))
  60. )
  61. )
  62. (invoke "init" (ref.extern 0))
  63. (assert_return (invoke "br_on_null" (i32.const 0)) (i32.const 0))
  64. (assert_return (invoke "br_on_null" (i32.const 1)) (i32.const -1))
  65. (assert_return (invoke "br_on_null" (i32.const 2)) (i32.const -1))
  66. (assert_return (invoke "br_on_null" (i32.const 3)) (i32.const -1))
  67. (assert_return (invoke "br_on_null" (i32.const 4)) (i32.const -1))
  68. (assert_return (invoke "br_on_i31" (i32.const 0)) (i32.const -1))
  69. (assert_return (invoke "br_on_i31" (i32.const 1)) (i32.const 7))
  70. (assert_return (invoke "br_on_i31" (i32.const 2)) (i32.const -1))
  71. (assert_return (invoke "br_on_i31" (i32.const 3)) (i32.const -1))
  72. (assert_return (invoke "br_on_i31" (i32.const 4)) (i32.const -1))
  73. (assert_return (invoke "br_on_struct" (i32.const 0)) (i32.const -1))
  74. (assert_return (invoke "br_on_struct" (i32.const 1)) (i32.const -1))
  75. (assert_return (invoke "br_on_struct" (i32.const 2)) (i32.const 6))
  76. (assert_return (invoke "br_on_struct" (i32.const 3)) (i32.const -1))
  77. (assert_return (invoke "br_on_struct" (i32.const 4)) (i32.const -1))
  78. (assert_return (invoke "br_on_array" (i32.const 0)) (i32.const -1))
  79. (assert_return (invoke "br_on_array" (i32.const 1)) (i32.const -1))
  80. (assert_return (invoke "br_on_array" (i32.const 2)) (i32.const -1))
  81. (assert_return (invoke "br_on_array" (i32.const 3)) (i32.const 3))
  82. (assert_return (invoke "br_on_array" (i32.const 4)) (i32.const -1))
  83. (assert_return (invoke "null-diff" (i32.const 0)) (i32.const 1))
  84. (assert_return (invoke "null-diff" (i32.const 1)) (i32.const 0))
  85. (assert_return (invoke "null-diff" (i32.const 2)) (i32.const 1))
  86. (assert_return (invoke "null-diff" (i32.const 3)) (i32.const 0))
  87. (assert_return (invoke "null-diff" (i32.const 4)) (i32.const 0))
  88. ;; Concrete Types
  89. (module
  90. (type $t0 (sub (struct)))
  91. (type $t1 (sub $t0 (struct (field i32))))
  92. (type $t1' (sub $t0 (struct (field i32))))
  93. (type $t2 (sub $t1 (struct (field i32 i32))))
  94. (type $t2' (sub $t1' (struct (field i32 i32))))
  95. (type $t3 (sub $t0 (struct (field i32 i32))))
  96. (type $t0' (sub $t0 (struct)))
  97. (type $t4 (sub $t0' (struct (field i32 i32))))
  98. (table 20 structref)
  99. (func $init
  100. (table.set (i32.const 0) (struct.new_default $t0))
  101. (table.set (i32.const 10) (struct.new_default $t0'))
  102. (table.set (i32.const 1) (struct.new_default $t1))
  103. (table.set (i32.const 11) (struct.new_default $t1'))
  104. (table.set (i32.const 2) (struct.new_default $t2))
  105. (table.set (i32.const 12) (struct.new_default $t2'))
  106. (table.set (i32.const 3) (struct.new_default $t3))
  107. (table.set (i32.const 4) (struct.new_default $t4))
  108. )
  109. (func (export "test-sub")
  110. (call $init)
  111. (block $l (result structref)
  112. ;; must succeed
  113. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (ref.null struct))))
  114. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 0)))))
  115. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 1)))))
  116. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 2)))))
  117. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 3)))))
  118. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 4)))))
  119. (drop (block (result structref) (br_on_cast 0 structref (ref $t1) (ref.null struct))))
  120. (drop (block (result structref) (br_on_cast 0 structref (ref $t1) (table.get (i32.const 1)))))
  121. (drop (block (result structref) (br_on_cast 0 structref (ref $t1) (table.get (i32.const 2)))))
  122. (drop (block (result structref) (br_on_cast 0 structref (ref $t2) (ref.null struct))))
  123. (drop (block (result structref) (br_on_cast 0 structref (ref $t2) (table.get (i32.const 2)))))
  124. (drop (block (result structref) (br_on_cast 0 structref (ref $t3) (ref.null struct))))
  125. (drop (block (result structref) (br_on_cast 0 structref (ref $t3) (table.get (i32.const 3)))))
  126. (drop (block (result structref) (br_on_cast 0 structref (ref $t4) (ref.null struct))))
  127. (drop (block (result structref) (br_on_cast 0 structref (ref $t4) (table.get (i32.const 4)))))
  128. ;; must not succeed
  129. (br_on_cast $l anyref (ref $t1) (table.get (i32.const 0)))
  130. (br_on_cast $l anyref (ref $t1) (table.get (i32.const 3)))
  131. (br_on_cast $l anyref (ref $t1) (table.get (i32.const 4)))
  132. (br_on_cast $l anyref (ref $t2) (table.get (i32.const 0)))
  133. (br_on_cast $l anyref (ref $t2) (table.get (i32.const 1)))
  134. (br_on_cast $l anyref (ref $t2) (table.get (i32.const 3)))
  135. (br_on_cast $l anyref (ref $t2) (table.get (i32.const 4)))
  136. (br_on_cast $l anyref (ref $t3) (table.get (i32.const 0)))
  137. (br_on_cast $l anyref (ref $t3) (table.get (i32.const 1)))
  138. (br_on_cast $l anyref (ref $t3) (table.get (i32.const 2)))
  139. (br_on_cast $l anyref (ref $t3) (table.get (i32.const 4)))
  140. (br_on_cast $l anyref (ref $t4) (table.get (i32.const 0)))
  141. (br_on_cast $l anyref (ref $t4) (table.get (i32.const 1)))
  142. (br_on_cast $l anyref (ref $t4) (table.get (i32.const 2)))
  143. (br_on_cast $l anyref (ref $t4) (table.get (i32.const 3)))
  144. (return)
  145. )
  146. (unreachable)
  147. )
  148. (func (export "test-canon")
  149. (call $init)
  150. (block $l
  151. (drop (block (result structref) (br_on_cast 0 structref (ref $t0') (table.get (i32.const 0)))))
  152. (drop (block (result structref) (br_on_cast 0 structref (ref $t0') (table.get (i32.const 1)))))
  153. (drop (block (result structref) (br_on_cast 0 structref (ref $t0') (table.get (i32.const 2)))))
  154. (drop (block (result structref) (br_on_cast 0 structref (ref $t0') (table.get (i32.const 3)))))
  155. (drop (block (result structref) (br_on_cast 0 structref (ref $t0') (table.get (i32.const 4)))))
  156. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 10)))))
  157. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 11)))))
  158. (drop (block (result structref) (br_on_cast 0 structref (ref $t0) (table.get (i32.const 12)))))
  159. (drop (block (result structref) (br_on_cast 0 structref (ref $t1') (table.get (i32.const 1)))))
  160. (drop (block (result structref) (br_on_cast 0 structref (ref $t1') (table.get (i32.const 2)))))
  161. (drop (block (result structref) (br_on_cast 0 structref (ref $t1) (table.get (i32.const 11)))))
  162. (drop (block (result structref) (br_on_cast 0 structref (ref $t1) (table.get (i32.const 12)))))
  163. (drop (block (result structref) (br_on_cast 0 structref (ref $t2') (table.get (i32.const 2)))))
  164. (drop (block (result structref) (br_on_cast 0 structref (ref $t2) (table.get (i32.const 12)))))
  165. (return)
  166. )
  167. (unreachable)
  168. )
  169. )
  170. (invoke "test-sub")
  171. (invoke "test-canon")
  172. ;; Cases of nullability
  173. (module
  174. (type $t (struct))
  175. (func (param (ref any)) (result (ref $t))
  176. (block (result (ref any)) (br_on_cast 1 (ref any) (ref $t) (local.get 0))) (unreachable)
  177. )
  178. (func (param (ref null any)) (result (ref $t))
  179. (block (result (ref null any)) (br_on_cast 1 (ref null any) (ref $t) (local.get 0))) (unreachable)
  180. )
  181. (func (param (ref null any)) (result (ref null $t))
  182. (block (result (ref null any)) (br_on_cast 1 (ref null any) (ref null $t) (local.get 0))) (unreachable)
  183. )
  184. )
  185. (assert_invalid
  186. (module
  187. (type $t (struct))
  188. (func (param (ref any)) (result (ref $t))
  189. (block (result (ref any)) (br_on_cast 1 (ref null any) (ref null $t) (local.get 0))) (unreachable)
  190. )
  191. )
  192. "type mismatch"
  193. )
  194. (assert_invalid
  195. (module
  196. (type $t (struct))
  197. (func (param (ref any)) (result (ref null $t))
  198. (block (result (ref any)) (br_on_cast 1 (ref any) (ref null $t) (local.get 0))) (unreachable)
  199. )
  200. )
  201. "type mismatch"
  202. )
  203. (assert_invalid
  204. (module
  205. (type $t (struct))
  206. (func (param (ref null any)) (result (ref $t))
  207. (block (result (ref any)) (br_on_cast 1 (ref null any) (ref $t) (local.get 0))) (unreachable)
  208. )
  209. )
  210. "type mismatch"
  211. )
  212. (assert_invalid
  213. (module
  214. (func (result anyref)
  215. (br_on_cast 0 eqref anyref (unreachable))
  216. )
  217. )
  218. "type mismatch"
  219. )
  220. (assert_invalid
  221. (module
  222. (func (result anyref)
  223. (br_on_cast 0 structref arrayref (unreachable))
  224. )
  225. )
  226. "type mismatch"
  227. )