1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
;; Binding structure
(module
(type $s0 (struct (field (ref 0) (ref 1) (ref $s0) (ref $s1))))
(type $s1 (struct (field (ref 0) (ref 1) (ref $s0) (ref $s1))))
(func (param (ref $forward)))
(type $forward (struct))
)
(assert_invalid
(module (type (struct (field (ref 1)))))
"unknown type"
)
(assert_invalid
(module (type (struct (field (mut (ref 1))))))
"unknown type"
)
;; Basic instructions
(module
(type $vec (struct (field f32) (field $y (mut f32)) (field $z f32)))
(func $get_0 (param $v (ref $vec)) (result f32)
(struct.get $vec 0 (local.get $v))
)
(func (export "get_0") (result f32)
(call $get_0 (struct.new_default_with_rtt $vec (rtt.canon $vec)))
)
(func $set_get_y (param $v (ref $vec)) (param $y f32) (result f32)
(struct.set $vec $y (local.get $v) (local.get $y))
(struct.get $vec $y (local.get $v))
)
(func (export "set_get_y") (param $y f32) (result f32)
(call $set_get_y (struct.new_default_with_rtt $vec (rtt.canon $vec)) (local.get $y))
)
(func $set_get_1 (param $v (ref $vec)) (param $y f32) (result f32)
(struct.set $vec 1 (local.get $v) (local.get $y))
(struct.get $vec $y (local.get $v))
)
(func (export "set_get_1") (param $y f32) (result f32)
(call $set_get_1 (struct.new_default_with_rtt $vec (rtt.canon $vec)) (local.get $y))
)
)
(assert_return (invoke "get_0") (f32.const 0))
(assert_return (invoke "set_get_y" (f32.const 7)) (f32.const 7))
(assert_return (invoke "set_get_1" (f32.const 7)) (f32.const 7))
(assert_invalid
(module
(type $s (struct (field i64)))
(func (export "struct.set-immutable") (param $s (ref $s))
(struct.set $s 0 (local.get $s) (i64.const 1))
)
)
"field is immutable"
)
;; Null dereference
(module
(type $t (struct (field i32) (field (mut i32))))
(func (export "struct.get-null")
(local (ref null $t)) (drop (struct.get $t 1 (local.get 0)))
)
(func (export "struct.set-null")
(local (ref null $t)) (struct.set $t 1 (local.get 0) (i32.const 0))
)
)
(assert_trap (invoke "struct.get-null") "null structure")
(assert_trap (invoke "struct.set-null") "null structure")
(assert_invalid
(module
(type $t (struct (field i32) (field (mut i32))))
(func (export "struct.new-null")
(local (ref null (rtt $t))) (drop (struct.new $t (i32.const 1) (i32.const 2) (local.get 0)))
)
)
"type mismatch"
)
(assert_invalid
(module
(type $t (struct (field i32) (field (mut i32))))
(func (export "struct.new_default-null")
(local (ref null (rtt $t))) (drop (struct.new_default_with_rtt $t (local.get 0)))
)
)
"type mismatch"
)
(assert_invalid
(module
(type $A (struct (field i32)))
(type $B (struct (field i64)))
(global $glob (rtt $A) (rtt.sub $A (rtt.canon $B)))
)
"invalid rtt"
)
(assert_invalid
(module
(type $vec (struct (field i32)))
(func $test
(drop
;; too many arguments
(struct.new_with_rtt $vec (i32.const 1) (i32.const 2) (rtt.canon $vec))
)
)
)
"invalid number of arguments to struct.new"
)
(assert_invalid
(module
(type $vec (struct (field i32) (field i32)))
(func $test
(drop
;; too few arguments
(struct.new_with_rtt $vec (i32.const 1) (rtt.canon $vec))
)
)
)
"invalid number of arguments to struct.new"
)
|