summaryrefslogtreecommitdiff
path: root/test/spec/shared-i31.wast
blob: ef1e2a9f1ca808b9315fb23cc1b5c2da2c8d166a (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(module
  (func (export "new") (param $i i32) (result (ref (shared i31)))
    (ref.i31_shared (local.get $i))
  )

  (func (export "get_u") (param $i i32) (result i32)
    (i31.get_u (ref.i31_shared (local.get $i)))
  )
  (func (export "get_s") (param $i i32) (result i32)
    (i31.get_s (ref.i31_shared (local.get $i)))
  )

  (func (export "get_u-null") (result i32)
    (i31.get_u (ref.null (shared i31)))
  )
  (func (export "get_s-null") (result i32)
    (i31.get_u (ref.null (shared i31)))
  )

  (global $i (ref (shared i31)) (ref.i31_shared (i32.const 2)))
  (global $m (mut (ref (shared i31))) (ref.i31_shared (i32.const 3)))

  (func (export "get_globals") (result i32 i32)
    (i31.get_u (global.get $i))
    (i31.get_u (global.get $m))
  )

  (func (export "set_global") (param i32)
    (global.set $m (ref.i31_shared (local.get 0)))
  )
)

(assert_return (invoke "new" (i32.const 1)) (ref.i31_shared))

(assert_return (invoke "get_u" (i32.const 0)) (i32.const 0))
(assert_return (invoke "get_u" (i32.const 100)) (i32.const 100))
(assert_return (invoke "get_u" (i32.const -1)) (i32.const 0x7fff_ffff))
(assert_return (invoke "get_u" (i32.const 0x3fff_ffff)) (i32.const 0x3fff_ffff))
(assert_return (invoke "get_u" (i32.const 0x4000_0000)) (i32.const 0x4000_0000))
(assert_return (invoke "get_u" (i32.const 0x7fff_ffff)) (i32.const 0x7fff_ffff))
(assert_return (invoke "get_u" (i32.const 0xaaaa_aaaa)) (i32.const 0x2aaa_aaaa))
(assert_return (invoke "get_u" (i32.const 0xcaaa_aaaa)) (i32.const 0x4aaa_aaaa))

(assert_return (invoke "get_s" (i32.const 0)) (i32.const 0))
(assert_return (invoke "get_s" (i32.const 100)) (i32.const 100))
(assert_return (invoke "get_s" (i32.const -1)) (i32.const -1))
(assert_return (invoke "get_s" (i32.const 0x3fff_ffff)) (i32.const 0x3fff_ffff))
(assert_return (invoke "get_s" (i32.const 0x4000_0000)) (i32.const -0x4000_0000))
(assert_return (invoke "get_s" (i32.const 0x7fff_ffff)) (i32.const -1))
(assert_return (invoke "get_s" (i32.const 0xaaaa_aaaa)) (i32.const 0x2aaa_aaaa))
(assert_return (invoke "get_s" (i32.const 0xcaaa_aaaa)) (i32.const 0xcaaa_aaaa))

(assert_trap (invoke "get_u-null") "null i31 reference")
(assert_trap (invoke "get_s-null") "null i31 reference")

(assert_return (invoke "get_globals") (i32.const 2) (i32.const 3))

(invoke "set_global" (i32.const 1234))
(assert_return (invoke "get_globals") (i32.const 2) (i32.const 1234))

(module $tables_of_i31ref
  (table $table 3 10 (ref null (shared i31)))
  (elem (table $table) (i32.const 0) (ref null (shared i31))
      (item (ref.i31_shared (i32.const 999)))
      (item (ref.i31_shared (i32.const 888)))
      (item (ref.i31_shared (i32.const 777))))

  (func (export "size") (result i32)
    table.size $table
  )

  (func (export "get") (param i32) (result i32)
    (i31.get_u (table.get $table (local.get 0)))
  )

  (func (export "grow") (param i32 i32) (result i32)
    (table.grow $table (ref.i31_shared (local.get 1)) (local.get 0))
  )

  (func (export "fill") (param i32 i32 i32)
    (table.fill $table (local.get 0) (ref.i31_shared (local.get 1)) (local.get 2))
  )

  (func (export "copy") (param i32 i32 i32)
    (table.copy $table $table (local.get 0) (local.get 1) (local.get 2))
  )

  (elem $elem (ref null (shared i31)) (item (ref.i31_shared (i32.const 123)))
                                      (item (ref.i31_shared (i32.const 456)))
                                      (item (ref.i31_shared (i32.const 789))))
  ;; (func (export "init") (param i32 i32 i32)
  ;;   (table.init $table $elem (local.get 0) (local.get 1) (local.get 2))
  ;; )
)

;; Initial state.
(assert_return (invoke "size") (i32.const 3))
(assert_return (invoke "get" (i32.const 0)) (i32.const 999))
(assert_return (invoke "get" (i32.const 1)) (i32.const 888))
(assert_return (invoke "get" (i32.const 2)) (i32.const 777))

;; Grow from size 3 to size 5.
(assert_return (invoke "grow" (i32.const 2) (i32.const 333)) (i32.const 3))
(assert_return (invoke "size") (i32.const 5))
(assert_return (invoke "get" (i32.const 3)) (i32.const 333))
(assert_return (invoke "get" (i32.const 4)) (i32.const 333))

;; Fill table[2..4] = 111.
(invoke "fill" (i32.const 2) (i32.const 111) (i32.const 2))
(assert_return (invoke "get" (i32.const 2)) (i32.const 111))
(assert_return (invoke "get" (i32.const 3)) (i32.const 111))

;; Copy from table[0..2] to table[3..5].
(invoke "copy" (i32.const 3) (i32.const 0) (i32.const 2))
(assert_return (invoke "get" (i32.const 3)) (i32.const 999))
(assert_return (invoke "get" (i32.const 4)) (i32.const 888))

;; ;; Initialize the passive element at table[1..4].
;; (invoke "init" (i32.const 1) (i32.const 0) (i32.const 3))
;; (assert_return (invoke "get" (i32.const 1)) (i32.const 123))
;; (assert_return (invoke "get" (i32.const 2)) (i32.const 456))
;; (assert_return (invoke "get" (i32.const 3)) (i32.const 789))

(module $env
  (global (export "g") i32 (i32.const 42))
)
(register "env")

;; (module $i31ref_of_global_table_initializer
;;   (global $g (import "env" "g") i32)
;;   (table $t 3 3 (ref (shared i31)) (ref.i31_shared (global.get $g)))
;;   (func (export "get") (param i32) (result i32)
;;     (i31.get_u (local.get 0) (table.get $t))
;;   )
;; )

;; (assert_return (invoke "get" (i32.const 0)) (i32.const 42))
;; (assert_return (invoke "get" (i32.const 1)) (i32.const 42))
;; (assert_return (invoke "get" (i32.const 2)) (i32.const 42))

(module $i31ref_of_global_global_initializer
  (global $g0 (import "env" "g") i32)
  (global $g1 (ref null (shared i31)) (ref.i31_shared (global.get $g0)))
  (func (export "get") (result i32)
    (i31.get_u (global.get $g1))
  )
)

(assert_return (invoke "get") (i32.const 42))

(module $anyref_global_of_i31ref
  (global $c (ref null (shared any)) (ref.i31_shared (i32.const 1234)))
  (global $m (mut (ref null (shared any))) (ref.i31_shared (i32.const 5678)))

  (func (export "get_globals") (result i32 i32)
    (i31.get_u (ref.cast (ref null (shared i31)) (global.get $c)))
    (i31.get_u (ref.cast (ref null (shared i31)) (global.get $m)))
  )

  (func (export "set_global") (param i32)
    (global.set $m (ref.i31_shared (local.get 0)))
  )
)

(assert_return (invoke "get_globals") (i32.const 1234) (i32.const 5678))
(invoke "set_global" (i32.const 0))
(assert_return (invoke "get_globals") (i32.const 1234) (i32.const 0))

(module $anyref_table_of_i31ref
  (table $table 3 10 (ref null (shared any)))
  (elem (table $table) (i32.const 0) (ref null (shared i31))
      (item (ref.i31_shared (i32.const 999)))
      (item (ref.i31_shared (i32.const 888)))
      (item (ref.i31_shared (i32.const 777))))

  (func (export "size") (result i32)
    table.size $table
  )

  (func (export "get") (param i32) (result i32)
    (i31.get_u (ref.cast (ref null (shared i31)) (table.get $table (local.get 0))))
  )

  (func (export "grow") (param i32 i32) (result i32)
    (table.grow $table (ref.i31_shared (local.get 1)) (local.get 0))
  )

  (func (export "fill") (param i32 i32 i32)
    (table.fill $table (local.get 0) (ref.i31_shared (local.get 1)) (local.get 2))
  )

  (func (export "copy") (param i32 i32 i32)
    (table.copy $table $table (local.get 0) (local.get 1) (local.get 2))
  )

  (elem $elem (ref null (shared i31)) (item (ref.i31_shared (i32.const 123)))
                     (item (ref.i31_shared (i32.const 456)))
                     (item (ref.i31_shared (i32.const 789))))
  ;; (func (export "init") (param i32 i32 i32)
  ;;   (table.init $table $elem (local.get 0) (local.get 1) (local.get 2))
  ;; )
)

;; Initial state.
(assert_return (invoke "size") (i32.const 3))
(assert_return (invoke "get" (i32.const 0)) (i32.const 999))
(assert_return (invoke "get" (i32.const 1)) (i32.const 888))
(assert_return (invoke "get" (i32.const 2)) (i32.const 777))

;; Grow from size 3 to size 5.
(assert_return (invoke "grow" (i32.const 2) (i32.const 333)) (i32.const 3))
(assert_return (invoke "size") (i32.const 5))
(assert_return (invoke "get" (i32.const 3)) (i32.const 333))
(assert_return (invoke "get" (i32.const 4)) (i32.const 333))

;; Fill table[2..4] = 111.
(invoke "fill" (i32.const 2) (i32.const 111) (i32.const 2))
(assert_return (invoke "get" (i32.const 2)) (i32.const 111))
(assert_return (invoke "get" (i32.const 3)) (i32.const 111))

;; Copy from table[0..2] to table[3..5].
(invoke "copy" (i32.const 3) (i32.const 0) (i32.const 2))
(assert_return (invoke "get" (i32.const 3)) (i32.const 999))
(assert_return (invoke "get" (i32.const 4)) (i32.const 888))

;; ;; Initialize the passive element at table[1..4].
;; (invoke "init" (i32.const 1) (i32.const 0) (i32.const 3))
;; (assert_return (invoke "get" (i32.const 1)) (i32.const 123))
;; (assert_return (invoke "get" (i32.const 2)) (i32.const 456))
;; (assert_return (invoke "get" (i32.const 3)) (i32.const 789))