d0537aa2b0b3585392218eb6205a46548a383b2f
[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) (item ref.func $f) (item (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   ;; Declarative
68   (elem declare funcref)
69   (elem declare funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
70   (elem declare func)
71   (elem declare func $f $f $g $g)
72
73   (elem $d1 declare funcref)
74   (elem $d2 declare funcref (ref.func $f) (ref.func $f) (ref.null func) (ref.func $g))
75   (elem $d3 declare func)
76   (elem $d4 declare func $f $f $g $g)
77 )
78
79 (module
80   (func $f)
81   (func $g)
82
83   (table $t funcref (elem (ref.func $f) (ref.null func) (ref.func $g)))
84 )
85
86
87 ;; Basic use
88
89 (module
90   (table 10 funcref)
91   (func $f)
92   (elem (i32.const 0) $f)
93 )
94 (module
95   (import "spectest" "table" (table 10 funcref))
96   (func $f)
97   (elem (i32.const 0) $f)
98 )
99
100 (module
101   (table 10 funcref)
102   (func $f)
103   (elem (i32.const 0) $f)
104   (elem (i32.const 3) $f)
105   (elem (i32.const 7) $f)
106   (elem (i32.const 5) $f)
107   (elem (i32.const 3) $f)
108 )
109 (module
110   (import "spectest" "table" (table 10 funcref))
111   (func $f)
112   (elem (i32.const 9) $f)
113   (elem (i32.const 3) $f)
114   (elem (i32.const 7) $f)
115   (elem (i32.const 3) $f)
116   (elem (i32.const 5) $f)
117 )
118
119 (module
120   (global (import "spectest" "global_i32") i32)
121   (table 1000 funcref)
122   (func $f)
123   (elem (global.get 0) $f)
124 )
125
126 (module
127   (global $g (import "spectest" "global_i32") i32)
128   (table 1000 funcref)
129   (func $f)
130   (elem (global.get $g) $f)
131 )
132
133 (module
134   (type $out-i32 (func (result i32)))
135   (table 10 funcref)
136   (elem (i32.const 7) $const-i32-a)
137   (elem (i32.const 9) $const-i32-b)
138   (func $const-i32-a (type $out-i32) (i32.const 65))
139   (func $const-i32-b (type $out-i32) (i32.const 66))
140   (func (export "call-7") (type $out-i32)
141     (call_indirect (type $out-i32) (i32.const 7))
142   )
143   (func (export "call-9") (type $out-i32)
144     (call_indirect (type $out-i32) (i32.const 9))
145   )
146 )
147 (assert_return (invoke "call-7") (i32.const 65))
148 (assert_return (invoke "call-9") (i32.const 66))
149
150 ;; Corner cases
151
152 (module
153   (table 10 funcref)
154   (func $f)
155   (elem (i32.const 9) $f)
156 )
157 (module
158   (import "spectest" "table" (table 10 funcref))
159   (func $f)
160   (elem (i32.const 9) $f)
161 )
162
163 (module
164   (table 0 funcref)
165   (elem (i32.const 0))
166 )
167 (module
168   (import "spectest" "table" (table 0 funcref))
169   (elem (i32.const 0))
170 )
171
172 (module
173   (table 0 0 funcref)
174   (elem (i32.const 0))
175 )
176
177 (module
178   (table 20 funcref)
179   (elem (i32.const 20))
180 )
181
182 (module
183   (import "spectest" "table" (table 0 funcref))
184   (func $f)
185   (elem (i32.const 0) $f)
186 )
187
188 (module
189   (import "spectest" "table" (table 0 100 funcref))
190   (func $f)
191   (elem (i32.const 0) $f)
192 )
193
194 (module
195   (import "spectest" "table" (table 0 funcref))
196   (func $f)
197   (elem (i32.const 1) $f)
198 )
199
200 (module
201   (import "spectest" "table" (table 0 30 funcref))
202   (func $f)
203   (elem (i32.const 1) $f)
204 )
205
206 ;; Invalid bounds for elements
207
208 (assert_trap
209   (module
210     (table 0 funcref)
211     (func $f)
212     (elem (i32.const 0) $f)
213   )
214   "out of bounds"
215 )
216
217 (assert_trap
218   (module
219     (table 0 0 funcref)
220     (func $f)
221     (elem (i32.const 0) $f)
222   )
223   "out of bounds"
224 )
225
226 (assert_trap
227   (module
228     (table 0 1 funcref)
229     (func $f)
230     (elem (i32.const 0) $f)
231   )
232   "out of bounds"
233 )
234
235 (assert_trap
236   (module
237     (table 0 funcref)
238     (elem (i32.const 1))
239   )
240   "out of bounds"
241 )
242 (assert_trap
243   (module
244     (table 10 funcref)
245     (func $f)
246     (elem (i32.const 10) $f)
247   )
248   "out of bounds"
249 )
250 (assert_trap
251   (module
252     (import "spectest" "table" (table 10 funcref))
253     (func $f)
254     (elem (i32.const 10) $f)
255   )
256   "out of bounds"
257 )
258
259 (assert_trap
260   (module
261     (table 10 20 funcref)
262     (func $f)
263     (elem (i32.const 10) $f)
264   )
265   "out of bounds"
266 )
267 (assert_trap
268   (module
269     (import "spectest" "table" (table 10 funcref))
270     (func $f)
271     (elem (i32.const 10) $f)
272   )
273   "out of bounds"
274 )
275
276 (assert_trap
277   (module
278     (table 10 funcref)
279     (func $f)
280     (elem (i32.const -1) $f)
281   )
282   "out of bounds"
283 )
284 (assert_trap
285   (module
286     (import "spectest" "table" (table 10 funcref))
287     (func $f)
288     (elem (i32.const -1) $f)
289   )
290   "out of bounds"
291 )
292
293 (assert_trap
294   (module
295     (table 10 funcref)
296     (func $f)
297     (elem (i32.const -10) $f)
298   )
299   "out of bounds"
300 )
301 (assert_trap
302   (module
303     (import "spectest" "table" (table 10 funcref))
304     (func $f)
305     (elem (i32.const -10) $f)
306   )
307   "out of bounds"
308 )
309
310 ;; Implicitly dropped elements
311
312 (module
313   (table 10 funcref)
314   (elem $e (i32.const 0) func $f)
315   (func $f)
316   (func (export "init")
317     (table.init $e (i32.const 0) (i32.const 0) (i32.const 1))
318   )
319 )
320 (assert_trap (invoke "init") "out of bounds")
321
322 (module
323   (table 10 funcref)
324   (elem $e declare func $f)
325   (func $f)
326   (func (export "init")
327     (table.init $e (i32.const 0) (i32.const 0) (i32.const 1))
328   )
329 )
330 (assert_trap (invoke "init") "out of bounds")
331
332 ;; Element without table
333
334 (assert_invalid
335   (module
336     (func $f)
337     (elem (i32.const 0) $f)
338   )
339   "unknown table"
340 )
341
342 ;; Invalid offsets
343
344 (assert_invalid
345   (module
346     (table 1 funcref)
347     (elem (i64.const 0))
348   )
349   "type mismatch"
350 )
351
352 (assert_invalid
353   (module
354     (table 1 funcref)
355     (elem (i32.ctz (i32.const 0)))
356   )
357   "constant expression required"
358 )
359
360 (assert_invalid
361   (module
362     (table 1 funcref)
363     (elem (nop))
364   )
365   "constant expression required"
366 )
367
368 (assert_invalid
369   (module
370     (table 1 funcref)
371     (elem (offset (nop) (i32.const 0)))
372   )
373   "constant expression required"
374 )
375
376 (assert_invalid
377   (module
378     (table 1 funcref)
379     (elem (offset (i32.const 0) (nop)))
380   )
381   "constant expression required"
382 )
383
384 ;; Use of internal globals in constant expressions is not allowed in MVP.
385 ;; (assert_invalid
386 ;;   (module (memory 1) (data (global.get $g)) (global $g (mut i32) (i32.const 0)))
387 ;;   "constant expression required"
388 ;; )
389
390 ;; Two elements target the same slot
391
392 (module
393   (type $out-i32 (func (result i32)))
394   (table 10 funcref)
395   (elem (i32.const 9) $const-i32-a)
396   (elem (i32.const 9) $const-i32-b)
397   (func $const-i32-a (type $out-i32) (i32.const 65))
398   (func $const-i32-b (type $out-i32) (i32.const 66))
399   (func (export "call-overwritten") (type $out-i32)
400     (call_indirect (type $out-i32) (i32.const 9))
401   )
402 )
403 (assert_return (invoke "call-overwritten") (i32.const 66))
404
405 (module
406   (type $out-i32 (func (result i32)))
407   (import "spectest" "table" (table 10 funcref))
408   (elem (i32.const 9) $const-i32-a)
409   (elem (i32.const 9) $const-i32-b)
410   (func $const-i32-a (type $out-i32) (i32.const 65))
411   (func $const-i32-b (type $out-i32) (i32.const 66))
412   (func (export "call-overwritten-element") (type $out-i32)
413     (call_indirect (type $out-i32) (i32.const 9))
414   )
415 )
416 (assert_return (invoke "call-overwritten-element") (i32.const 66))
417
418 ;; Element sections across multiple modules change the same table
419
420 (module $module1
421   (type $out-i32 (func (result i32)))
422   (table (export "shared-table") 10 funcref)
423   (elem (i32.const 8) $const-i32-a)
424   (elem (i32.const 9) $const-i32-b)
425   (func $const-i32-a (type $out-i32) (i32.const 65))
426   (func $const-i32-b (type $out-i32) (i32.const 66))
427   (func (export "call-7") (type $out-i32)
428     (call_indirect (type $out-i32) (i32.const 7))
429   )
430   (func (export "call-8") (type $out-i32)
431     (call_indirect (type $out-i32) (i32.const 8))
432   )
433   (func (export "call-9") (type $out-i32)
434     (call_indirect (type $out-i32) (i32.const 9))
435   )
436 )
437
438 (register "module1" $module1)
439
440 (assert_trap (invoke $module1 "call-7") "uninitialized element")
441 (assert_return (invoke $module1 "call-8") (i32.const 65))
442 (assert_return (invoke $module1 "call-9") (i32.const 66))
443
444 (module $module2
445   (type $out-i32 (func (result i32)))
446   (import "module1" "shared-table" (table 10 funcref))
447   (elem (i32.const 7) $const-i32-c)
448   (elem (i32.const 8) $const-i32-d)
449   (func $const-i32-c (type $out-i32) (i32.const 67))
450   (func $const-i32-d (type $out-i32) (i32.const 68))
451 )
452
453 (assert_return (invoke $module1 "call-7") (i32.const 67))
454 (assert_return (invoke $module1 "call-8") (i32.const 68))
455 (assert_return (invoke $module1 "call-9") (i32.const 66))
456
457 (module $module3
458   (type $out-i32 (func (result i32)))
459   (import "module1" "shared-table" (table 10 funcref))
460   (elem (i32.const 8) $const-i32-e)
461   (elem (i32.const 9) $const-i32-f)
462   (func $const-i32-e (type $out-i32) (i32.const 69))
463   (func $const-i32-f (type $out-i32) (i32.const 70))
464 )
465
466 (assert_return (invoke $module1 "call-7") (i32.const 67))
467 (assert_return (invoke $module1 "call-8") (i32.const 69))
468 (assert_return (invoke $module1 "call-9") (i32.const 70))