ref_cast.wast 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ;; Abstract Types
  2. (module
  3. (type $ft (func))
  4. (type $st (struct))
  5. (type $at (array i8))
  6. (table 10 anyref)
  7. (elem declare func $f)
  8. (func $f)
  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_default $st))
  13. (table.set (i32.const 3) (array.new_default $at (i32.const 0)))
  14. (table.set (i32.const 4) (any.convert_extern (local.get $x)))
  15. (table.set (i32.const 5) (ref.null i31))
  16. (table.set (i32.const 6) (ref.null struct))
  17. (table.set (i32.const 7) (ref.null none))
  18. )
  19. (func (export "ref_cast_non_null") (param $i i32)
  20. (drop (ref.as_non_null (table.get (local.get $i))))
  21. (drop (ref.cast (ref null any) (table.get (local.get $i))))
  22. )
  23. (func (export "ref_cast_null") (param $i i32)
  24. (drop (ref.cast anyref (table.get (local.get $i))))
  25. (drop (ref.cast structref (table.get (local.get $i))))
  26. (drop (ref.cast arrayref (table.get (local.get $i))))
  27. (drop (ref.cast i31ref (table.get (local.get $i))))
  28. (drop (ref.cast nullref (table.get (local.get $i))))
  29. )
  30. (func (export "ref_cast_i31") (param $i i32)
  31. (drop (ref.cast (ref i31) (table.get (local.get $i))))
  32. (drop (ref.cast i31ref (table.get (local.get $i))))
  33. )
  34. (func (export "ref_cast_struct") (param $i i32)
  35. (drop (ref.cast (ref struct) (table.get (local.get $i))))
  36. (drop (ref.cast structref (table.get (local.get $i))))
  37. )
  38. (func (export "ref_cast_array") (param $i i32)
  39. (drop (ref.cast (ref array) (table.get (local.get $i))))
  40. (drop (ref.cast arrayref (table.get (local.get $i))))
  41. )
  42. )
  43. (invoke "init" (ref.extern 0))
  44. (assert_trap (invoke "ref_cast_non_null" (i32.const 0)) "null reference")
  45. (assert_return (invoke "ref_cast_non_null" (i32.const 1)))
  46. (assert_return (invoke "ref_cast_non_null" (i32.const 2)))
  47. (assert_return (invoke "ref_cast_non_null" (i32.const 3)))
  48. (assert_return (invoke "ref_cast_non_null" (i32.const 4)))
  49. (assert_trap (invoke "ref_cast_non_null" (i32.const 5)) "null reference")
  50. (assert_trap (invoke "ref_cast_non_null" (i32.const 6)) "null reference")
  51. (assert_trap (invoke "ref_cast_non_null" (i32.const 7)) "null reference")
  52. (assert_return (invoke "ref_cast_null" (i32.const 0)))
  53. (assert_trap (invoke "ref_cast_null" (i32.const 1)) "cast failure")
  54. (assert_trap (invoke "ref_cast_null" (i32.const 2)) "cast failure")
  55. (assert_trap (invoke "ref_cast_null" (i32.const 3)) "cast failure")
  56. (assert_trap (invoke "ref_cast_null" (i32.const 4)) "cast failure")
  57. (assert_return (invoke "ref_cast_null" (i32.const 5)))
  58. (assert_return (invoke "ref_cast_null" (i32.const 6)))
  59. (assert_return (invoke "ref_cast_null" (i32.const 7)))
  60. (assert_trap (invoke "ref_cast_i31" (i32.const 0)) "cast failure")
  61. (assert_return (invoke "ref_cast_i31" (i32.const 1)))
  62. (assert_trap (invoke "ref_cast_i31" (i32.const 2)) "cast failure")
  63. (assert_trap (invoke "ref_cast_i31" (i32.const 3)) "cast failure")
  64. (assert_trap (invoke "ref_cast_i31" (i32.const 4)) "cast failure")
  65. (assert_trap (invoke "ref_cast_i31" (i32.const 5)) "cast failure")
  66. (assert_trap (invoke "ref_cast_i31" (i32.const 6)) "cast failure")
  67. (assert_trap (invoke "ref_cast_i31" (i32.const 7)) "cast failure")
  68. (assert_trap (invoke "ref_cast_struct" (i32.const 0)) "cast failure")
  69. (assert_trap (invoke "ref_cast_struct" (i32.const 1)) "cast failure")
  70. (assert_return (invoke "ref_cast_struct" (i32.const 2)))
  71. (assert_trap (invoke "ref_cast_struct" (i32.const 3)) "cast failure")
  72. (assert_trap (invoke "ref_cast_struct" (i32.const 4)) "cast failure")
  73. (assert_trap (invoke "ref_cast_struct" (i32.const 5)) "cast failure")
  74. (assert_trap (invoke "ref_cast_struct" (i32.const 6)) "cast failure")
  75. (assert_trap (invoke "ref_cast_struct" (i32.const 7)) "cast failure")
  76. (assert_trap (invoke "ref_cast_array" (i32.const 0)) "cast failure")
  77. (assert_trap (invoke "ref_cast_array" (i32.const 1)) "cast failure")
  78. (assert_trap (invoke "ref_cast_array" (i32.const 2)) "cast failure")
  79. (assert_return (invoke "ref_cast_array" (i32.const 3)))
  80. (assert_trap (invoke "ref_cast_array" (i32.const 4)) "cast failure")
  81. (assert_trap (invoke "ref_cast_array" (i32.const 5)) "cast failure")
  82. (assert_trap (invoke "ref_cast_array" (i32.const 6)) "cast failure")
  83. (assert_trap (invoke "ref_cast_array" (i32.const 7)) "cast failure")
  84. ;; Concrete Types
  85. (module
  86. (type $t0 (sub (struct)))
  87. (type $t1 (sub $t0 (struct (field i32))))
  88. (type $t1' (sub $t0 (struct (field i32))))
  89. (type $t2 (sub $t1 (struct (field i32 i32))))
  90. (type $t2' (sub $t1' (struct (field i32 i32))))
  91. (type $t3 (sub $t0 (struct (field i32 i32))))
  92. (type $t0' (sub $t0 (struct)))
  93. (type $t4 (sub $t0' (struct (field i32 i32))))
  94. (table 20 (ref null struct))
  95. (func $init
  96. (table.set (i32.const 0) (struct.new_default $t0))
  97. (table.set (i32.const 10) (struct.new_default $t0))
  98. (table.set (i32.const 1) (struct.new_default $t1))
  99. (table.set (i32.const 11) (struct.new_default $t1'))
  100. (table.set (i32.const 2) (struct.new_default $t2))
  101. (table.set (i32.const 12) (struct.new_default $t2'))
  102. (table.set (i32.const 3) (struct.new_default $t3))
  103. (table.set (i32.const 4) (struct.new_default $t4))
  104. )
  105. (func (export "test-sub")
  106. (call $init)
  107. (drop (ref.cast (ref null $t0) (ref.null struct)))
  108. (drop (ref.cast (ref null $t0) (table.get (i32.const 0))))
  109. (drop (ref.cast (ref null $t0) (table.get (i32.const 1))))
  110. (drop (ref.cast (ref null $t0) (table.get (i32.const 2))))
  111. (drop (ref.cast (ref null $t0) (table.get (i32.const 3))))
  112. (drop (ref.cast (ref null $t0) (table.get (i32.const 4))))
  113. (drop (ref.cast (ref null $t0) (ref.null struct)))
  114. (drop (ref.cast (ref null $t1) (table.get (i32.const 1))))
  115. (drop (ref.cast (ref null $t1) (table.get (i32.const 2))))
  116. (drop (ref.cast (ref null $t0) (ref.null struct)))
  117. (drop (ref.cast (ref null $t2) (table.get (i32.const 2))))
  118. (drop (ref.cast (ref null $t0) (ref.null struct)))
  119. (drop (ref.cast (ref null $t3) (table.get (i32.const 3))))
  120. (drop (ref.cast (ref null $t4) (table.get (i32.const 4))))
  121. (drop (ref.cast (ref $t0) (table.get (i32.const 0))))
  122. (drop (ref.cast (ref $t0) (table.get (i32.const 1))))
  123. (drop (ref.cast (ref $t0) (table.get (i32.const 2))))
  124. (drop (ref.cast (ref $t0) (table.get (i32.const 3))))
  125. (drop (ref.cast (ref $t0) (table.get (i32.const 4))))
  126. (drop (ref.cast (ref $t1) (table.get (i32.const 1))))
  127. (drop (ref.cast (ref $t1) (table.get (i32.const 2))))
  128. (drop (ref.cast (ref $t2) (table.get (i32.const 2))))
  129. (drop (ref.cast (ref $t3) (table.get (i32.const 3))))
  130. (drop (ref.cast (ref $t4) (table.get (i32.const 4))))
  131. )
  132. (func (export "test-canon")
  133. (call $init)
  134. (drop (ref.cast (ref $t0) (table.get (i32.const 0))))
  135. (drop (ref.cast (ref $t0) (table.get (i32.const 1))))
  136. (drop (ref.cast (ref $t0) (table.get (i32.const 2))))
  137. (drop (ref.cast (ref $t0) (table.get (i32.const 3))))
  138. (drop (ref.cast (ref $t0) (table.get (i32.const 4))))
  139. (drop (ref.cast (ref $t0) (table.get (i32.const 10))))
  140. (drop (ref.cast (ref $t0) (table.get (i32.const 11))))
  141. (drop (ref.cast (ref $t0) (table.get (i32.const 12))))
  142. (drop (ref.cast (ref $t1') (table.get (i32.const 1))))
  143. (drop (ref.cast (ref $t1') (table.get (i32.const 2))))
  144. (drop (ref.cast (ref $t1) (table.get (i32.const 11))))
  145. (drop (ref.cast (ref $t1) (table.get (i32.const 12))))
  146. (drop (ref.cast (ref $t2') (table.get (i32.const 2))))
  147. (drop (ref.cast (ref $t2) (table.get (i32.const 12))))
  148. )
  149. )
  150. (invoke "test-sub")
  151. (invoke "test-canon")