br_on_cast_fail.wast 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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_non_null") (param $i i32) (result i32)
  17. (block $l (result (ref any))
  18. (br_on_non_null $l (table.get (local.get $i)))
  19. (return (i32.const 0))
  20. )
  21. (return (i32.const -1))
  22. )
  23. (func (export "br_on_non_i31") (param $i i32) (result i32)
  24. (block $l (result anyref)
  25. (br_on_cast_fail $l anyref (ref i31) (table.get (local.get $i)))
  26. (return (i31.get_u))
  27. )
  28. (return (i32.const -1))
  29. )
  30. (func (export "br_on_non_struct") (param $i i32) (result i32)
  31. (block $l (result anyref)
  32. (br_on_cast_fail $l anyref (ref struct) (table.get (local.get $i)))
  33. (block $l2 (param structref) (result (ref $st))
  34. (block $l3 (param structref) (result (ref $at))
  35. (br_on_cast $l2 structref (ref $st))
  36. (br_on_cast $l3 anyref (ref $at))
  37. (return (i32.const -2))
  38. )
  39. (return (array.get_u $at (i32.const 0)))
  40. )
  41. (return (struct.get_s $st 0))
  42. )
  43. (return (i32.const -1))
  44. )
  45. (func (export "br_on_non_array") (param $i i32) (result i32)
  46. (block $l (result anyref)
  47. (br_on_cast_fail $l anyref (ref array) (table.get (local.get $i)))
  48. (return (array.len))
  49. )
  50. (return (i32.const -1))
  51. )
  52. (func (export "null-diff") (param $i i32) (result i32)
  53. (block $l (result (ref any))
  54. (block (result (ref null struct))
  55. (br_on_cast_fail $l (ref null any) (ref null struct) (table.get (local.get $i)))
  56. )
  57. (return (i32.const 1))
  58. )
  59. (return (i32.const 0))
  60. )
  61. )
  62. (invoke "init" (ref.extern 0))
  63. (assert_return (invoke "br_on_non_null" (i32.const 0)) (i32.const 0))
  64. (assert_return (invoke "br_on_non_null" (i32.const 1)) (i32.const -1))
  65. (assert_return (invoke "br_on_non_null" (i32.const 2)) (i32.const -1))
  66. (assert_return (invoke "br_on_non_null" (i32.const 3)) (i32.const -1))
  67. (assert_return (invoke "br_on_non_null" (i32.const 4)) (i32.const -1))
  68. (assert_return (invoke "br_on_non_i31" (i32.const 0)) (i32.const -1))
  69. (assert_return (invoke "br_on_non_i31" (i32.const 1)) (i32.const 7))
  70. (assert_return (invoke "br_on_non_i31" (i32.const 2)) (i32.const -1))
  71. (assert_return (invoke "br_on_non_i31" (i32.const 3)) (i32.const -1))
  72. (assert_return (invoke "br_on_non_i31" (i32.const 4)) (i32.const -1))
  73. (assert_return (invoke "br_on_non_struct" (i32.const 0)) (i32.const -1))
  74. (assert_return (invoke "br_on_non_struct" (i32.const 1)) (i32.const -1))
  75. (assert_return (invoke "br_on_non_struct" (i32.const 2)) (i32.const 6))
  76. (assert_return (invoke "br_on_non_struct" (i32.const 3)) (i32.const -1))
  77. (assert_return (invoke "br_on_non_struct" (i32.const 4)) (i32.const -1))
  78. (assert_return (invoke "br_on_non_array" (i32.const 0)) (i32.const -1))
  79. (assert_return (invoke "br_on_non_array" (i32.const 1)) (i32.const -1))
  80. (assert_return (invoke "br_on_non_array" (i32.const 2)) (i32.const -1))
  81. (assert_return (invoke "br_on_non_array" (i32.const 3)) (i32.const 3))
  82. (assert_return (invoke "br_on_non_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 not succeed
  113. (br_on_cast_fail $l structref (ref null $t0) (ref.null struct))
  114. (br_on_cast_fail $l structref (ref null $t0) (table.get (i32.const 0)))
  115. (br_on_cast_fail $l structref (ref null $t0) (table.get (i32.const 1)))
  116. (br_on_cast_fail $l structref (ref null $t0) (table.get (i32.const 2)))
  117. (br_on_cast_fail $l structref (ref null $t0) (table.get (i32.const 3)))
  118. (br_on_cast_fail $l structref (ref null $t0) (table.get (i32.const 4)))
  119. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 0)))
  120. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 1)))
  121. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 2)))
  122. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 3)))
  123. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 4)))
  124. (br_on_cast_fail $l structref (ref null $t1) (ref.null struct))
  125. (br_on_cast_fail $l structref (ref null $t1) (table.get (i32.const 1)))
  126. (br_on_cast_fail $l structref (ref null $t1) (table.get (i32.const 2)))
  127. (br_on_cast_fail $l structref (ref $t1) (table.get (i32.const 1)))
  128. (br_on_cast_fail $l structref (ref $t1) (table.get (i32.const 2)))
  129. (br_on_cast_fail $l structref (ref null $t2) (ref.null struct))
  130. (br_on_cast_fail $l structref (ref null $t2) (table.get (i32.const 2)))
  131. (br_on_cast_fail $l structref (ref $t2) (table.get (i32.const 2)))
  132. (br_on_cast_fail $l structref (ref null $t3) (ref.null struct))
  133. (br_on_cast_fail $l structref (ref null $t3) (table.get (i32.const 3)))
  134. (br_on_cast_fail $l structref (ref $t3) (table.get (i32.const 3)))
  135. (br_on_cast_fail $l structref (ref null $t4) (ref.null struct))
  136. (br_on_cast_fail $l structref (ref null $t4) (table.get (i32.const 4)))
  137. (br_on_cast_fail $l structref (ref $t4) (table.get (i32.const 4)))
  138. ;; must succeed
  139. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t0) (ref.null struct))))
  140. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t1) (ref.null struct))))
  141. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t1) (table.get (i32.const 0)))))
  142. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t1) (table.get (i32.const 3)))))
  143. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t1) (table.get (i32.const 4)))))
  144. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t2) (ref.null struct))))
  145. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t2) (table.get (i32.const 0)))))
  146. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t2) (table.get (i32.const 1)))))
  147. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t2) (table.get (i32.const 3)))))
  148. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t2) (table.get (i32.const 4)))))
  149. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t3) (ref.null struct))))
  150. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t3) (table.get (i32.const 0)))))
  151. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t3) (table.get (i32.const 1)))))
  152. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t3) (table.get (i32.const 2)))))
  153. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t3) (table.get (i32.const 4)))))
  154. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t4) (ref.null struct))))
  155. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t4) (table.get (i32.const 0)))))
  156. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t4) (table.get (i32.const 1)))))
  157. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t4) (table.get (i32.const 2)))))
  158. (drop (block (result structref) (br_on_cast_fail 0 structref (ref $t4) (table.get (i32.const 3)))))
  159. (return)
  160. )
  161. (unreachable)
  162. )
  163. (func (export "test-canon")
  164. (call $init)
  165. (block $l (result structref)
  166. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 0)))
  167. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 1)))
  168. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 2)))
  169. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 3)))
  170. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 4)))
  171. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 10)))
  172. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 11)))
  173. (br_on_cast_fail $l structref (ref $t0) (table.get (i32.const 12)))
  174. (br_on_cast_fail $l structref (ref $t1') (table.get (i32.const 1)))
  175. (br_on_cast_fail $l structref (ref $t1') (table.get (i32.const 2)))
  176. (br_on_cast_fail $l structref (ref $t1) (table.get (i32.const 11)))
  177. (br_on_cast_fail $l structref (ref $t1) (table.get (i32.const 12)))
  178. (br_on_cast_fail $l structref (ref $t2') (table.get (i32.const 2)))
  179. (br_on_cast_fail $l structref (ref $t2) (table.get (i32.const 12)))
  180. (return)
  181. )
  182. (unreachable)
  183. )
  184. )
  185. (invoke "test-sub")
  186. (invoke "test-canon")
  187. ;; Cases of nullability
  188. (module
  189. (type $t (struct))
  190. (func (param (ref any)) (result (ref any))
  191. (block (result (ref $t)) (br_on_cast_fail 1 (ref any) (ref $t) (local.get 0)))
  192. )
  193. (func (param (ref null any)) (result (ref null any))
  194. (block (result (ref $t)) (br_on_cast_fail 1 (ref null any) (ref $t) (local.get 0)))
  195. )
  196. (func (param (ref null any)) (result (ref null any))
  197. (block (result (ref null $t)) (br_on_cast_fail 1 (ref null any) (ref null $t) (local.get 0)))
  198. )
  199. )
  200. (assert_invalid
  201. (module
  202. (type $t (struct))
  203. (func (param (ref any)) (result (ref any))
  204. (block (result (ref $t)) (br_on_cast_fail 1 (ref null any) (ref null $t) (local.get 0)))
  205. )
  206. )
  207. "type mismatch"
  208. )
  209. (assert_invalid
  210. (module
  211. (type $t (struct))
  212. (func (param (ref any)) (result (ref any))
  213. (block (result (ref null $t)) (br_on_cast_fail 1 (ref any) (ref null $t) (local.get 0))) (ref.as_non_null)
  214. )
  215. )
  216. "type mismatch"
  217. )
  218. (assert_invalid
  219. (module
  220. (type $t (struct))
  221. (func (param (ref null any)) (result (ref any))
  222. (block (result (ref $t)) (br_on_cast_fail 1 (ref null any) (ref $t) (local.get 0)))
  223. )
  224. )
  225. "type mismatch"
  226. )
  227. (assert_invalid
  228. (module
  229. (func (result anyref)
  230. (br_on_cast_fail 0 eqref anyref (unreachable))
  231. )
  232. )
  233. "type mismatch"
  234. )
  235. (assert_invalid
  236. (module
  237. (func (result anyref)
  238. (br_on_cast_fail 0 structref arrayref (unreachable))
  239. )
  240. )
  241. "type mismatch"
  242. )