File: instance.wast

package info (click to toggle)
rust-wasmtime 26.0.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 48,492 kB
  • sloc: ansic: 4,003; sh: 561; javascript: 542; cpp: 254; asm: 175; ml: 96; makefile: 55
file content (327 lines) | stat: -rw-r--r-- 7,481 bytes parent folder | download | duplicates (3)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
(component
  (core module $m)
  (core instance (instantiate $m))
)

(component
  (core module $m
    (func (export ""))
  )
  (core instance $i (instantiate $m))

  (core module $m2
    (func (import "" ""))
  )
  (core instance (instantiate $m2 (with "" (instance $i))))
)

(component
  (core module $m
    (func (export "a"))
  )
  (core instance $i (instantiate $m))

  (core module $m2
    (func (import "" "b"))
  )
  (core instance (instantiate $m2
    (with "" (instance (export "b" (func $i "a"))))
  ))
)

;; all kinds of imports for core wasm modules, and register a start function on
;; one module to ensure that everything is correct
(component
  (core module $m
    (func (export "a"))
    (table (export "b") 1 funcref)
    (memory (export "c") 1)
    (global (export "d") i32 i32.const 1)
  )
  (core instance $i (instantiate $m))

  (core module $m2
    (import "" "a" (func $f))
    (import "" "b" (table 1 funcref))
    (import "" "c" (memory 1))
    (import "" "d" (global $g i32))

    (func $start
      global.get $g
      i32.const 1
      i32.ne
      if
        unreachable
      end

      call $f
    )

    (start $start)

    (data (i32.const 0) "hello")
    (elem (i32.const 0) $start)
  )
  (core instance (instantiate $m2
    (with "" (instance $i))
  ))
)

;; Test to see if a component with a type export can be instantiated.
(component
    (type string)
    (export "a" (type 0))
)

;; double-check the start function runs by ensuring that a trap shows up and it
;; sees the wrong value for the global import
(assert_trap
  (component
    (core module $m
      (global (export "g") i32 i32.const 1)
    )
    (core instance $i (instantiate $m))

    (core module $m2
      (import "" "g" (global $g i32))

      (func $start
        global.get $g
        i32.const 0
        i32.ne
        if
          unreachable
        end
      )

      (start $start)
    )
    (core instance (instantiate $m2 (with "" (instance $i))))
  )
  "unreachable")

;; shuffle around imports to get to what the target core wasm module needs
(component
  (core module $m
    (func (export "1"))
    (table (export "2") 1 funcref)
    (memory (export "3") 1)
    (global (export "4") i32 i32.const 1)
  )
  (core instance $i (instantiate $m))

  (core module $m2
    (import "" "a" (func $f))
    (import "" "b" (table 1 funcref))
    (import "" "c" (memory 1))
    (import "" "d" (global $g i32))
  )
  (core instance (instantiate $m2
    (with "" (instance
      (export "a" (func $i "1"))
      (export "b" (table $i "2"))
      (export "c" (memory $i "3"))
      (export "d" (global $i "4"))
    ))
  ))
)

;; indirect references through a synthetic instance
(component
  (core module $m
    (func (export "a"))
    (table (export "b") 1 funcref)
    (memory (export "c") 1)
    (global (export "d") i32 i32.const 1)
  )
  (core instance $i (instantiate $m))
  (core instance $i2
    (export "a1" (func $i "a"))
    (export "a2" (table $i "b"))
    (export "a3" (memory $i "c"))
    (export "a4" (global $i "d"))
  )

  (core module $m2
    (import "" "1" (func $f))
    (import "" "2" (table 1 funcref))
    (import "" "3" (memory 1))
    (import "" "4" (global $g i32))
  )
  (core instance (instantiate $m2
    (with "" (instance
      (export "1" (func $i2 "a1"))
      (export "2" (table $i2 "a2"))
      (export "3" (memory $i2 "a3"))
      (export "4" (global $i2 "a4"))
    ))
  ))
)

(component
  (import "host" (instance $i (export "return-three" (func (result u32)))))

  (core module $m
    (import "host" "return-three" (func $three (result i32)))
    (func $start
      call $three
      i32.const 3
      i32.ne
      if unreachable end
    )
    (start $start)
  )
  (core func $three_lower
    (canon lower (func $i "return-three"))
  )
  (core instance (instantiate $m
    (with "host" (instance (export "return-three" (func $three_lower))))
  ))
)

(component
  (import "host" (instance $i
    (type $x' (record (field "x" u32)))
    (export $x "x" (type (eq $x')))
    (type $rec' (record (field "x" $x) (field "y" string)))
    (export $rec "rec" (type (eq $rec')))
    (export "some-record" (type (eq $rec)))))
)

(component
  (import "host" (instance $i
    (export "nested" (instance
      (export "return-four" (func (result u32)))
    ))
  ))

  (core module $m
    (import "host" "return-three" (func $three (result i32)))
    (func $start
      call $three
      i32.const 4
      i32.ne
      if unreachable end
    )
    (start $start)
  )
  (core func $three_lower
    (canon lower (func $i "nested" "return-four"))
  )
  (core instance (instantiate $m
    (with "host" (instance (export "return-three" (func $three_lower))))
  ))
)

(component
  (import "host" (instance $i
    (export "simple-module" (core module))
  ))

  (core instance (instantiate (module $i "simple-module")))
)

(component
  (import "host" (instance $i
    (export "simple-module" (core module
      (export "f" (func (result i32)))
      (export "g" (global i32))
    ))
  ))

  (core instance $i (instantiate (module $i "simple-module")))
  (core module $verify
    (import "host" "f" (func $f (result i32)))
    (import "host" "g" (global $g i32))

    (func $start
      call $f
      i32.const 101
      i32.ne
      if unreachable end

      global.get $g
      i32.const 100
      i32.ne
      if unreachable end
    )
    (start $start)
  )

  (core instance (instantiate $verify (with "host" (instance $i))))
)

;; export an instance
(component
  (core module $m)
  (instance $i (export "m" (core module $m)))
  (export "i" (instance $i))
)
(component
  (component $c)
  (instance $i (instantiate $c))
  (export "i" (instance $i))
)
(component
  (import "host" (instance $i))
  (export "i" (instance $i))
)


(component definition $C1
  (type $r1 (resource (rep i32)))
  (export "r" (type $r1))
)
(component definition $C2
  (type $r1 (resource (rep i32)))
  (export "r" (type $r1))
)

(component instance $I1 $C1)
(component instance $I2 $C1)
(component instance $I3 $C2)
(component instance $I4 $C2)

;; all instances have different resource types
(assert_unlinkable
  (component
    (import "I1" (instance $i1 (export "r" (type (sub resource)))))
    (alias export $i1 "r" (type $r))
    (import "I2" (instance $i2 (export "r" (type (eq $r)))))
  )
  "mismatched resource types")
(assert_unlinkable
  (component
    (import "I1" (instance $i1 (export "r" (type (sub resource)))))
    (alias export $i1 "r" (type $r))
    (import "I3" (instance $i2 (export "r" (type (eq $r)))))
  )
  "mismatched resource types")
(assert_unlinkable
  (component
    (import "I1" (instance $i1 (export "r" (type (sub resource)))))
    (alias export $i1 "r" (type $r))
    (import "I4" (instance $i2 (export "r" (type (eq $r)))))
  )
  "mismatched resource types")
(assert_unlinkable
  (component
    (import "I2" (instance $i1 (export "r" (type (sub resource)))))
    (alias export $i1 "r" (type $r))
    (import "I3" (instance $i2 (export "r" (type (eq $r)))))
  )
  "mismatched resource types")
(assert_unlinkable
  (component
    (import "I2" (instance $i1 (export "r" (type (sub resource)))))
    (alias export $i1 "r" (type $r))
    (import "I4" (instance $i2 (export "r" (type (eq $r)))))
  )
  "mismatched resource types")
(assert_unlinkable
  (component
    (import "I3" (instance $i1 (export "r" (type (sub resource)))))
    (alias export $i1 "r" (type $r))
    (import "I4" (instance $i2 (export "r" (type (eq $r)))))
  )
  "mismatched resource types")