a50a1a2b364ead3e128959cfd4c42f803509f19c
[platform/framework/web/lwnode.git] /
1 ;; Test the element section
2
3 ;; Syntax
4 (module
5   (table $t 10 funcref)
6   (func $f)
7   (func $g)
8
9   ;; Passive
10   (elem funcref)
11   (elem funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
12   (elem func)
13   (elem func $f $f $g $g)
14
15   (elem $p1 funcref)
16   (elem $p2 funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
17   (elem $p3 func)
18   (elem $p4 func $f $f $g $g)
19
20   ;; Active
21   (elem (table $t) (i32.const 0) funcref)
22   (elem (table $t) (i32.const 0) funcref (ref.func $f) (ref.null func))
23   (elem (table $t) (i32.const 0) func)
24   (elem (table $t) (i32.const 0) func $f $g)
25   (elem (table $t) (offset (i32.const 0)) funcref)
26   (elem (table $t) (offset (i32.const 0)) func $f $g)
27   (elem (table 0) (i32.const 0) func)
28   (elem (table 0x0) (i32.const 0) func $f $f)
29   (elem (table 0x000) (offset (i32.const 0)) func)
30   (elem (table 0) (offset (i32.const 0)) func $f $f)
31   (elem (table $t) (i32.const 0) func)
32   (elem (table $t) (i32.const 0) func $f $f)
33   (elem (table $t) (offset (i32.const 0)) func)
34   (elem (table $t) (offset (i32.const 0)) func $f $f)
35   (elem (offset (i32.const 0)))
36   (elem (offset (i32.const 0)) funcref (ref.func $f) (ref.null func))
37   (elem (offset (i32.const 0)) func $f $f)
38   (elem (offset (i32.const 0)) $f $f)
39   (elem (i32.const 0))
40   (elem (i32.const 0) funcref (ref.func $f) (ref.null func))
41   (elem (i32.const 0) func $f $f)
42   (elem (i32.const 0) $f $f)
43
44   (elem $a1 (table $t) (i32.const 0) funcref)
45   (elem $a2 (table $t) (i32.const 0) funcref (ref.func $f) (ref.null func))
46   (elem $a3 (table $t) (i32.const 0) func)
47   (elem $a4 (table $t) (i32.const 0) func $f $g)
48   (elem $a9 (table $t) (offset (i32.const 0)) funcref)
49   (elem $a10 (table $t) (offset (i32.const 0)) func $f $g)
50   (elem $a11 (table 0) (i32.const 0) func)
51   (elem $a12 (table 0x0) (i32.const 0) func $f $f)
52   (elem $a13 (table 0x000) (offset (i32.const 0)) func)
53   (elem $a14 (table 0) (offset (i32.const 0)) func $f $f)
54   (elem $a15 (table $t) (i32.const 0) func)
55   (elem $a16 (table $t) (i32.const 0) func $f $f)
56   (elem $a17 (table $t) (offset (i32.const 0)) func)
57   (elem $a18 (table $t) (offset (i32.const 0)) func $f $f)
58   (elem $a19 (offset (i32.const 0)))
59   (elem $a20 (offset (i32.const 0)) funcref (ref.func $f) (ref.null func))
60   (elem $a21 (offset (i32.const 0)) func $f $f)
61   (elem $a22 (offset (i32.const 0)) $f $f)
62   (elem $a23 (i32.const 0))
63   (elem $a24 (i32.const 0) funcref (ref.func $f) (ref.null func))
64   (elem $a25 (i32.const 0) func $f $f)
65   (elem $a26 (i32.const 0) $f $f)
66 )
67
68 (module
69   (func $f)
70   (func $g)
71
72   (table $t funcref (elem (ref.func $f) (ref.null func) (ref.func $g)))
73 )
74 ;; Basic use
75
76 (module
77   (table 10 funcref)
78   (func $f)
79   (elem (i32.const 0) $f)
80 )
81 (module
82   (import "spectest" "table" (table 10 funcref))
83   (func $f)
84   (elem (i32.const 0) $f)
85 )
86
87 (module
88   (table 10 funcref)
89   (func $f)
90   (elem (i32.const 0) $f)
91   (elem (i32.const 3) $f)
92   (elem (i32.const 7) $f)
93   (elem (i32.const 5) $f)
94   (elem (i32.const 3) $f)
95 )
96 (module
97   (import "spectest" "table" (table 10 funcref))
98   (func $f)
99   (elem (i32.const 9) $f)
100   (elem (i32.const 3) $f)
101   (elem (i32.const 7) $f)
102   (elem (i32.const 3) $f)
103   (elem (i32.const 5) $f)
104 )
105
106 (module
107   (global (import "spectest" "global_i32") i32)
108   (table 1000 funcref)
109   (func $f)
110   (elem (global.get 0) $f)
111 )
112
113 (module
114   (global $g (import "spectest" "global_i32") i32)
115   (table 1000 funcref)
116   (func $f)
117   (elem (global.get $g) $f)
118 )
119
120 (module
121   (type $out-i32 (func (result i32)))
122   (table 10 funcref)
123   (elem (i32.const 7) $const-i32-a)
124   (elem (i32.const 9) $const-i32-b)
125   (func $const-i32-a (type $out-i32) (i32.const 65))
126   (func $const-i32-b (type $out-i32) (i32.const 66))
127   (func (export "call-7") (type $out-i32)
128     (call_indirect (type $out-i32) (i32.const 7))
129   )
130   (func (export "call-9") (type $out-i32)
131     (call_indirect (type $out-i32) (i32.const 9))
132   )
133 )
134 (assert_return (invoke "call-7") (i32.const 65))
135 (assert_return (invoke "call-9") (i32.const 66))
136
137 ;; Corner cases
138
139 (module
140   (table 10 funcref)
141   (func $f)
142   (elem (i32.const 9) $f)
143 )
144 (module
145   (import "spectest" "table" (table 10 funcref))
146   (func $f)
147   (elem (i32.const 9) $f)
148 )
149
150 (module
151   (table 0 funcref)
152   (elem (i32.const 0))
153 )
154 (module
155   (import "spectest" "table" (table 0 funcref))
156   (elem (i32.const 0))
157 )
158
159 (module
160   (table 0 0 funcref)
161   (elem (i32.const 0))
162 )
163
164 (module
165   (table 20 funcref)
166   (elem (i32.const 20))
167 )
168
169 (module
170   (import "spectest" "table" (table 0 funcref))
171   (func $f)
172   (elem (i32.const 0) $f)
173 )
174
175 (module
176   (import "spectest" "table" (table 0 100 funcref))
177   (func $f)
178   (elem (i32.const 0) $f)
179 )
180
181 (module
182   (import "spectest" "table" (table 0 funcref))
183   (func $f)
184   (elem (i32.const 1) $f)
185 )
186
187 (module
188   (import "spectest" "table" (table 0 30 funcref))
189   (func $f)
190   (elem (i32.const 1) $f)
191 )
192
193 ;; Invalid bounds for elements
194
195 (assert_trap
196   (module
197     (table 0 funcref)
198     (func $f)
199     (elem (i32.const 0) $f)
200   )
201   "out of bounds"
202 )
203
204 (assert_trap
205   (module
206     (table 0 0 funcref)
207     (func $f)
208     (elem (i32.const 0) $f)
209   )
210   "out of bounds"
211 )
212
213 (assert_trap
214   (module
215     (table 0 1 funcref)
216     (func $f)
217     (elem (i32.const 0) $f)
218   )
219   "out of bounds"
220 )
221
222 (assert_trap
223   (module
224     (table 0 funcref)
225     (elem (i32.const 1))
226   )
227   "out of bounds"
228 )
229 (assert_trap
230   (module
231     (table 10 funcref)
232     (func $f)
233     (elem (i32.const 10) $f)
234   )
235   "out of bounds"
236 )
237 (assert_trap
238   (module
239     (import "spectest" "table" (table 10 funcref))
240     (func $f)
241     (elem (i32.const 10) $f)
242   )
243   "out of bounds"
244 )
245
246 (assert_trap
247   (module
248     (table 10 20 funcref)
249     (func $f)
250     (elem (i32.const 10) $f)
251   )
252   "out of bounds"
253 )
254 (assert_trap
255   (module
256     (import "spectest" "table" (table 10 funcref))
257     (func $f)
258     (elem (i32.const 10) $f)
259   )
260   "out of bounds"
261 )
262
263 (assert_trap
264   (module
265     (table 10 funcref)
266     (func $f)
267     (elem (i32.const -1) $f)
268   )
269   "out of bounds"
270 )
271 (assert_trap
272   (module
273     (import "spectest" "table" (table 10 funcref))
274     (func $f)
275     (elem (i32.const -1) $f)
276   )
277   "out of bounds"
278 )
279
280 (assert_trap
281   (module
282     (table 10 funcref)
283     (func $f)
284     (elem (i32.const -10) $f)
285   )
286   "out of bounds"
287 )
288 (assert_trap
289   (module
290     (import "spectest" "table" (table 10 funcref))
291     (func $f)
292     (elem (i32.const -10) $f)
293   )
294   "out of bounds"
295 )
296
297 ;; Element without table
298
299 (assert_invalid
300   (module
301     (func $f)
302     (elem (i32.const 0) $f)
303   )
304   "unknown table"
305 )
306
307 ;; Invalid offsets
308
309 (assert_invalid
310   (module
311     (table 1 funcref)
312     (elem (i64.const 0))
313   )
314   "type mismatch"
315 )
316
317 (assert_invalid
318   (module
319     (table 1 funcref)
320     (elem (i32.ctz (i32.const 0)))
321   )
322   "constant expression required"
323 )
324
325 (assert_invalid
326   (module
327     (table 1 funcref)
328     (elem (nop))
329   )
330   "constant expression required"
331 )
332
333 (assert_invalid
334   (module
335     (table 1 funcref)
336     (elem (offset (nop) (i32.const 0)))
337   )
338   "constant expression required"
339 )
340
341 (assert_invalid
342   (module
343     (table 1 funcref)
344     (elem (offset (i32.const 0) (nop)))
345   )
346   "constant expression required"
347 )
348
349 ;; Use of internal globals in constant expressions is not allowed in MVP.
350 ;; (assert_invalid
351 ;;   (module (memory 1) (data (global.get $g)) (global $g (mut i32) (i32.const 0)))
352 ;;   "constant expression required"
353 ;; )
354
355 ;; Two elements target the same slot
356
357 (module
358   (type $out-i32 (func (result i32)))
359   (table 10 funcref)
360   (elem (i32.const 9) $const-i32-a)
361   (elem (i32.const 9) $const-i32-b)
362   (func $const-i32-a (type $out-i32) (i32.const 65))
363   (func $const-i32-b (type $out-i32) (i32.const 66))
364   (func (export "call-overwritten") (type $out-i32)
365     (call_indirect (type $out-i32) (i32.const 9))
366   )
367 )
368 (assert_return (invoke "call-overwritten") (i32.const 66))
369
370 (module
371   (type $out-i32 (func (result i32)))
372   (import "spectest" "table" (table 10 funcref))
373   (elem (i32.const 9) $const-i32-a)
374   (elem (i32.const 9) $const-i32-b)
375   (func $const-i32-a (type $out-i32) (i32.const 65))
376   (func $const-i32-b (type $out-i32) (i32.const 66))
377   (func (export "call-overwritten-element") (type $out-i32)
378     (call_indirect (type $out-i32) (i32.const 9))
379   )
380 )
381 (assert_return (invoke "call-overwritten-element") (i32.const 66))
382
383 ;; Element sections across multiple modules change the same table
384
385 (module $module1
386   (type $out-i32 (func (result i32)))
387   (table (export "shared-table") 10 funcref)
388   (elem (i32.const 8) $const-i32-a)
389   (elem (i32.const 9) $const-i32-b)
390   (func $const-i32-a (type $out-i32) (i32.const 65))
391   (func $const-i32-b (type $out-i32) (i32.const 66))
392   (func (export "call-7") (type $out-i32)
393     (call_indirect (type $out-i32) (i32.const 7))
394   )
395   (func (export "call-8") (type $out-i32)
396     (call_indirect (type $out-i32) (i32.const 8))
397   )
398   (func (export "call-9") (type $out-i32)
399     (call_indirect (type $out-i32) (i32.const 9))
400   )
401 )
402
403 (register "module1" $module1)
404
405 (assert_trap (invoke $module1 "call-7") "uninitialized element")
406 (assert_return (invoke $module1 "call-8") (i32.const 65))
407 (assert_return (invoke $module1 "call-9") (i32.const 66))
408
409 (module $module2
410   (type $out-i32 (func (result i32)))
411   (import "module1" "shared-table" (table 10 funcref))
412   (elem (i32.const 7) $const-i32-c)
413   (elem (i32.const 8) $const-i32-d)
414   (func $const-i32-c (type $out-i32) (i32.const 67))
415   (func $const-i32-d (type $out-i32) (i32.const 68))
416 )
417
418 (assert_return (invoke $module1 "call-7") (i32.const 67))
419 (assert_return (invoke $module1 "call-8") (i32.const 68))
420 (assert_return (invoke $module1 "call-9") (i32.const 66))
421
422 (module $module3
423   (type $out-i32 (func (result i32)))
424   (import "module1" "shared-table" (table 10 funcref))
425   (elem (i32.const 8) $const-i32-e)
426   (elem (i32.const 9) $const-i32-f)
427   (func $const-i32-e (type $out-i32) (i32.const 69))
428   (func $const-i32-f (type $out-i32) (i32.const 70))
429 )
430
431 (assert_return (invoke $module1 "call-7") (i32.const 67))
432 (assert_return (invoke $module1 "call-8") (i32.const 69))
433 (assert_return (invoke $module1 "call-9") (i32.const 70))