UNPKG

111 kBPlain TextView Raw
1/* Copyright 2016 Mozilla Foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// See https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md
17const WASM_MAGIC_NUMBER = 0x6d736100;
18const WASM_SUPPORTED_EXPERIMENTAL_VERSION = 0xd;
19const WASM_SUPPORTED_VERSION = 0x1;
20export const enum SectionCode {
21 Unknown = -1,
22 Custom = 0,
23 Type = 1, // Function signature declarations
24 Import = 2, // Import declarations
25 Function = 3, // Function declarations
26 Table = 4, // Indirect function table and other tables
27 Memory = 5, // Memory attributes
28 Global = 6, // Global declarations
29 Export = 7, //Exports
30 Start = 8, // Start function declaration
31 Element = 9, // Elements section
32 Code = 10, // Function bodies (code)
33 Data = 11, // Data segments
34 Event = 13, // Events
35}
36export const enum OperatorCode {
37 unreachable = 0x00,
38 nop = 0x01,
39 block = 0x02,
40 loop = 0x03,
41 if = 0x04,
42 else = 0x05,
43 try = 0x06,
44 catch = 0x07,
45 throw = 0x08,
46 rethrow = 0x09,
47 unwind = 0x0a,
48 end = 0x0b,
49 br = 0x0c,
50 br_if = 0x0d,
51 br_table = 0x0e,
52 return = 0x0f,
53 call = 0x10,
54 call_indirect = 0x11,
55 return_call = 0x12,
56 return_call_indirect = 0x13,
57 call_ref = 0x14,
58 return_call_ref = 0x15,
59 let = 0x17,
60 delegate = 0x18,
61 catch_all = 0x19,
62 drop = 0x1a,
63 select = 0x1b,
64 select_with_type = 0x1c,
65 local_get = 0x20,
66 local_set = 0x21,
67 local_tee = 0x22,
68 global_get = 0x23,
69 global_set = 0x24,
70 i32_load = 0x28,
71 i64_load = 0x29,
72 f32_load = 0x2a,
73 f64_load = 0x2b,
74 i32_load8_s = 0x2c,
75 i32_load8_u = 0x2d,
76 i32_load16_s = 0x2e,
77 i32_load16_u = 0x2f,
78 i64_load8_s = 0x30,
79 i64_load8_u = 0x31,
80 i64_load16_s = 0x32,
81 i64_load16_u = 0x33,
82 i64_load32_s = 0x34,
83 i64_load32_u = 0x35,
84 i32_store = 0x36,
85 i64_store = 0x37,
86 f32_store = 0x38,
87 f64_store = 0x39,
88 i32_store8 = 0x3a,
89 i32_store16 = 0x3b,
90 i64_store8 = 0x3c,
91 i64_store16 = 0x3d,
92 i64_store32 = 0x3e,
93 current_memory = 0x3f,
94 grow_memory = 0x40,
95 i32_const = 0x41,
96 i64_const = 0x42,
97 f32_const = 0x43,
98 f64_const = 0x44,
99 i32_eqz = 0x45,
100 i32_eq = 0x46,
101 i32_ne = 0x47,
102 i32_lt_s = 0x48,
103 i32_lt_u = 0x49,
104 i32_gt_s = 0x4a,
105 i32_gt_u = 0x4b,
106 i32_le_s = 0x4c,
107 i32_le_u = 0x4d,
108 i32_ge_s = 0x4e,
109 i32_ge_u = 0x4f,
110 i64_eqz = 0x50,
111 i64_eq = 0x51,
112 i64_ne = 0x52,
113 i64_lt_s = 0x53,
114 i64_lt_u = 0x54,
115 i64_gt_s = 0x55,
116 i64_gt_u = 0x56,
117 i64_le_s = 0x57,
118 i64_le_u = 0x58,
119 i64_ge_s = 0x59,
120 i64_ge_u = 0x5a,
121 f32_eq = 0x5b,
122 f32_ne = 0x5c,
123 f32_lt = 0x5d,
124 f32_gt = 0x5e,
125 f32_le = 0x5f,
126 f32_ge = 0x60,
127 f64_eq = 0x61,
128 f64_ne = 0x62,
129 f64_lt = 0x63,
130 f64_gt = 0x64,
131 f64_le = 0x65,
132 f64_ge = 0x66,
133 i32_clz = 0x67,
134 i32_ctz = 0x68,
135 i32_popcnt = 0x69,
136 i32_add = 0x6a,
137 i32_sub = 0x6b,
138 i32_mul = 0x6c,
139 i32_div_s = 0x6d,
140 i32_div_u = 0x6e,
141 i32_rem_s = 0x6f,
142 i32_rem_u = 0x70,
143 i32_and = 0x71,
144 i32_or = 0x72,
145 i32_xor = 0x73,
146 i32_shl = 0x74,
147 i32_shr_s = 0x75,
148 i32_shr_u = 0x76,
149 i32_rotl = 0x77,
150 i32_rotr = 0x78,
151 i64_clz = 0x79,
152 i64_ctz = 0x7a,
153 i64_popcnt = 0x7b,
154 i64_add = 0x7c,
155 i64_sub = 0x7d,
156 i64_mul = 0x7e,
157 i64_div_s = 0x7f,
158 i64_div_u = 0x80,
159 i64_rem_s = 0x81,
160 i64_rem_u = 0x82,
161 i64_and = 0x83,
162 i64_or = 0x84,
163 i64_xor = 0x85,
164 i64_shl = 0x86,
165 i64_shr_s = 0x87,
166 i64_shr_u = 0x88,
167 i64_rotl = 0x89,
168 i64_rotr = 0x8a,
169 f32_abs = 0x8b,
170 f32_neg = 0x8c,
171 f32_ceil = 0x8d,
172 f32_floor = 0x8e,
173 f32_trunc = 0x8f,
174 f32_nearest = 0x90,
175 f32_sqrt = 0x91,
176 f32_add = 0x92,
177 f32_sub = 0x93,
178 f32_mul = 0x94,
179 f32_div = 0x95,
180 f32_min = 0x96,
181 f32_max = 0x97,
182 f32_copysign = 0x98,
183 f64_abs = 0x99,
184 f64_neg = 0x9a,
185 f64_ceil = 0x9b,
186 f64_floor = 0x9c,
187 f64_trunc = 0x9d,
188 f64_nearest = 0x9e,
189 f64_sqrt = 0x9f,
190 f64_add = 0xa0,
191 f64_sub = 0xa1,
192 f64_mul = 0xa2,
193 f64_div = 0xa3,
194 f64_min = 0xa4,
195 f64_max = 0xa5,
196 f64_copysign = 0xa6,
197 i32_wrap_i64 = 0xa7,
198 i32_trunc_f32_s = 0xa8,
199 i32_trunc_f32_u = 0xa9,
200 i32_trunc_f64_s = 0xaa,
201 i32_trunc_f64_u = 0xab,
202 i64_extend_i32_s = 0xac,
203 i64_extend_i32_u = 0xad,
204 i64_trunc_f32_s = 0xae,
205 i64_trunc_f32_u = 0xaf,
206 i64_trunc_f64_s = 0xb0,
207 i64_trunc_f64_u = 0xb1,
208 f32_convert_i32_s = 0xb2,
209 f32_convert_i32_u = 0xb3,
210 f32_convert_i64_s = 0xb4,
211 f32_convert_i64_u = 0xb5,
212 f32_demote_f64 = 0xb6,
213 f64_convert_i32_s = 0xb7,
214 f64_convert_i32_u = 0xb8,
215 f64_convert_i64_s = 0xb9,
216 f64_convert_i64_u = 0xba,
217 f64_promote_f32 = 0xbb,
218 i32_reinterpret_f32 = 0xbc,
219 i64_reinterpret_f64 = 0xbd,
220 f32_reinterpret_i32 = 0xbe,
221 f64_reinterpret_i64 = 0xbf,
222
223 i32_extend8_s = 0xc0,
224 i32_extend16_s = 0xc1,
225 i64_extend8_s = 0xc2,
226 i64_extend16_s = 0xc3,
227 i64_extend32_s = 0xc4,
228
229 prefix_0xfb = 0xfb,
230 prefix_0xfc = 0xfc,
231 prefix_0xfd = 0xfd,
232 prefix_0xfe = 0xfe,
233
234 i32_trunc_sat_f32_s = 0xfc00,
235 i32_trunc_sat_f32_u = 0xfc01,
236 i32_trunc_sat_f64_s = 0xfc02,
237 i32_trunc_sat_f64_u = 0xfc03,
238 i64_trunc_sat_f32_s = 0xfc04,
239 i64_trunc_sat_f32_u = 0xfc05,
240 i64_trunc_sat_f64_s = 0xfc06,
241 i64_trunc_sat_f64_u = 0xfc07,
242
243 memory_init = 0xfc08,
244 data_drop = 0xfc09,
245 memory_copy = 0xfc0a,
246 memory_fill = 0xfc0b,
247 table_init = 0xfc0c,
248 elem_drop = 0xfc0d,
249 table_copy = 0xfc0e,
250
251 table_grow = 0xfc0f,
252 table_size = 0xfc10,
253 table_fill = 0xfc11,
254
255 table_get = 0x25,
256 table_set = 0x26,
257
258 ref_null = 0xd0,
259 ref_is_null = 0xd1,
260 ref_func = 0xd2,
261 ref_as_non_null = 0xd3,
262 br_on_null = 0xd4,
263 ref_eq = 0xd5,
264 br_on_non_null = 0xd6,
265
266 atomic_notify = 0xfe00,
267 i32_atomic_wait = 0xfe01,
268 i64_atomic_wait = 0xfe02,
269 atomic_fence = 0xfe03,
270 i32_atomic_load = 0xfe10,
271 i64_atomic_load = 0xfe11,
272 i32_atomic_load8_u = 0xfe12,
273 i32_atomic_load16_u = 0xfe13,
274 i64_atomic_load8_u = 0xfe14,
275 i64_atomic_load16_u = 0xfe15,
276 i64_atomic_load32_u = 0xfe16,
277 i32_atomic_store = 0xfe17,
278 i64_atomic_store = 0xfe18,
279 i32_atomic_store8 = 0xfe19,
280 i32_atomic_store16 = 0xfe1a,
281 i64_atomic_store8 = 0xfe1b,
282 i64_atomic_store16 = 0xfe1c,
283 i64_atomic_store32 = 0xfe1d,
284 i32_atomic_rmw_add = 0xfe1e,
285 i64_atomic_rmw_add = 0xfe1f,
286 i32_atomic_rmw8_add_u = 0xfe20,
287 i32_atomic_rmw16_add_u = 0xfe21,
288 i64_atomic_rmw8_add_u = 0xfe22,
289 i64_atomic_rmw16_add_u = 0xfe23,
290 i64_atomic_rmw32_add_u = 0xfe24,
291 i32_atomic_rmw_sub = 0xfe25,
292 i64_atomic_rmw_sub = 0xfe26,
293 i32_atomic_rmw8_sub_u = 0xfe27,
294 i32_atomic_rmw16_sub_u = 0xfe28,
295 i64_atomic_rmw8_sub_u = 0xfe29,
296 i64_atomic_rmw16_sub_u = 0xfe2a,
297 i64_atomic_rmw32_sub_u = 0xfe2b,
298 i32_atomic_rmw_and = 0xfe2c,
299 i64_atomic_rmw_and = 0xfe2d,
300 i32_atomic_rmw8_and_u = 0xfe2e,
301 i32_atomic_rmw16_and_u = 0xfe2f,
302 i64_atomic_rmw8_and_u = 0xfe30,
303 i64_atomic_rmw16_and_u = 0xfe31,
304 i64_atomic_rmw32_and_u = 0xfe32,
305 i32_atomic_rmw_or = 0xfe33,
306 i64_atomic_rmw_or = 0xfe34,
307 i32_atomic_rmw8_or_u = 0xfe35,
308 i32_atomic_rmw16_or_u = 0xfe36,
309 i64_atomic_rmw8_or_u = 0xfe37,
310 i64_atomic_rmw16_or_u = 0xfe38,
311 i64_atomic_rmw32_or_u = 0xfe39,
312 i32_atomic_rmw_xor = 0xfe3a,
313 i64_atomic_rmw_xor = 0xfe3b,
314 i32_atomic_rmw8_xor_u = 0xfe3c,
315 i32_atomic_rmw16_xor_u = 0xfe3d,
316 i64_atomic_rmw8_xor_u = 0xfe3e,
317 i64_atomic_rmw16_xor_u = 0xfe3f,
318 i64_atomic_rmw32_xor_u = 0xfe40,
319 i32_atomic_rmw_xchg = 0xfe41,
320 i64_atomic_rmw_xchg = 0xfe42,
321 i32_atomic_rmw8_xchg_u = 0xfe43,
322 i32_atomic_rmw16_xchg_u = 0xfe44,
323 i64_atomic_rmw8_xchg_u = 0xfe45,
324 i64_atomic_rmw16_xchg_u = 0xfe46,
325 i64_atomic_rmw32_xchg_u = 0xfe47,
326 i32_atomic_rmw_cmpxchg = 0xfe48,
327 i64_atomic_rmw_cmpxchg = 0xfe49,
328 i32_atomic_rmw8_cmpxchg_u = 0xfe4a,
329 i32_atomic_rmw16_cmpxchg_u = 0xfe4b,
330 i64_atomic_rmw8_cmpxchg_u = 0xfe4c,
331 i64_atomic_rmw16_cmpxchg_u = 0xfe4d,
332 i64_atomic_rmw32_cmpxchg_u = 0xfe4e,
333
334 v128_load = 0xfd00,
335 i16x8_load8x8_s = 0xfd01,
336 i16x8_load8x8_u = 0xfd02,
337 i32x4_load16x4_s = 0xfd03,
338 i32x4_load16x4_u = 0xfd04,
339 i64x2_load32x2_s = 0xfd05,
340 i64x2_load32x2_u = 0xfd06,
341 v8x16_load_splat = 0xfd07,
342 v16x8_load_splat = 0xfd08,
343 v32x4_load_splat = 0xfd09,
344 v64x2_load_splat = 0xfd0a,
345 v128_store = 0xfd0b,
346 v128_const = 0xfd0c,
347 i8x16_shuffle = 0xfd0d,
348 i8x16_swizzle = 0xfd0e,
349 i8x16_splat = 0xfd0f,
350 i16x8_splat = 0xfd10,
351 i32x4_splat = 0xfd11,
352 i64x2_splat = 0xfd12,
353 f32x4_splat = 0xfd13,
354 f64x2_splat = 0xfd14,
355 i8x16_extract_lane_s = 0xfd15,
356 i8x16_extract_lane_u = 0xfd16,
357 i8x16_replace_lane = 0xfd17,
358 i16x8_extract_lane_s = 0xfd18,
359 i16x8_extract_lane_u = 0xfd19,
360 i16x8_replace_lane = 0xfd1a,
361 i32x4_extract_lane = 0xfd1b,
362 i32x4_replace_lane = 0xfd1c,
363 i64x2_extract_lane = 0xfd1d,
364 i64x2_replace_lane = 0xfd1e,
365 f32x4_extract_lane = 0xfd1f,
366 f32x4_replace_lane = 0xfd20,
367 f64x2_extract_lane = 0xfd21,
368 f64x2_replace_lane = 0xfd22,
369 i8x16_eq = 0xfd23,
370 i8x16_ne = 0xfd24,
371 i8x16_lt_s = 0xfd25,
372 i8x16_lt_u = 0xfd26,
373 i8x16_gt_s = 0xfd27,
374 i8x16_gt_u = 0xfd28,
375 i8x16_le_s = 0xfd29,
376 i8x16_le_u = 0xfd2a,
377 i8x16_ge_s = 0xfd2b,
378 i8x16_ge_u = 0xfd2c,
379 i16x8_eq = 0xfd2d,
380 i16x8_ne = 0xfd2e,
381 i16x8_lt_s = 0xfd2f,
382 i16x8_lt_u = 0xfd30,
383 i16x8_gt_s = 0xfd31,
384 i16x8_gt_u = 0xfd32,
385 i16x8_le_s = 0xfd33,
386 i16x8_le_u = 0xfd34,
387 i16x8_ge_s = 0xfd35,
388 i16x8_ge_u = 0xfd36,
389 i32x4_eq = 0xfd37,
390 i32x4_ne = 0xfd38,
391 i32x4_lt_s = 0xfd39,
392 i32x4_lt_u = 0xfd3a,
393 i32x4_gt_s = 0xfd3b,
394 i32x4_gt_u = 0xfd3c,
395 i32x4_le_s = 0xfd3d,
396 i32x4_le_u = 0xfd3e,
397 i32x4_ge_s = 0xfd3f,
398 i32x4_ge_u = 0xfd40,
399 f32x4_eq = 0xfd41,
400 f32x4_ne = 0xfd42,
401 f32x4_lt = 0xfd43,
402 f32x4_gt = 0xfd44,
403 f32x4_le = 0xfd45,
404 f32x4_ge = 0xfd46,
405 f64x2_eq = 0xfd47,
406 f64x2_ne = 0xfd48,
407 f64x2_lt = 0xfd49,
408 f64x2_gt = 0xfd4a,
409 f64x2_le = 0xfd4b,
410 f64x2_ge = 0xfd4c,
411 v128_not = 0xfd4d,
412 v128_and = 0xfd4e,
413 v128_andnot = 0xfd4f,
414 v128_or = 0xfd50,
415 v128_xor = 0xfd51,
416 v128_bitselect = 0xfd52,
417 v128_any_true = 0xfd53,
418 v128_load8_lane = 0xfd54,
419 v128_load16_lane = 0xfd55,
420 v128_load32_lane = 0xfd56,
421 v128_load64_lane = 0xfd57,
422 v128_store8_lane = 0xfd58,
423 v128_store16_lane = 0xfd59,
424 v128_store32_lane = 0xfd5a,
425 v128_store64_lane = 0xfd5b,
426 v128_load32_zero = 0xfd5c,
427 v128_load64_zero = 0xfd5d,
428 f32x4_demote_f64x2_zero = 0xfd5e,
429 f64x2_promote_low_f32x4 = 0xfd5f,
430 i8x16_abs = 0xfd60,
431 i8x16_neg = 0xfd61,
432 i8x16_popcnt = 0xfd62,
433 i8x16_all_true = 0xfd63,
434 i8x16_bitmask = 0xfd64,
435 i8x16_narrow_i16x8_s = 0xfd65,
436 i8x16_narrow_i16x8_u = 0xfd66,
437 f32x4_ceil = 0xfd67,
438 f32x4_floor = 0xfd68,
439 f32x4_trunc = 0xfd69,
440 f32x4_nearest = 0xfd6a,
441 i8x16_shl = 0xfd6b,
442 i8x16_shr_s = 0xfd6c,
443 i8x16_shr_u = 0xfd6d,
444 i8x16_add = 0xfd6e,
445 i8x16_add_sat_s = 0xfd6f,
446 i8x16_add_sat_u = 0xfd70,
447 i8x16_sub = 0xfd71,
448 i8x16_sub_sat_s = 0xfd72,
449 i8x16_sub_sat_u = 0xfd73,
450 f64x2_ceil = 0xfd74,
451 f64x2_floor = 0xfd75,
452 i8x16_min_s = 0xfd76,
453 i8x16_min_u = 0xfd77,
454 i8x16_max_s = 0xfd78,
455 i8x16_max_u = 0xfd79,
456 f64x2_trunc = 0xfd7a,
457 i8x16_avgr_u = 0xfd7b,
458 i16x8_extadd_pairwise_i8x16_s = 0xfd7c,
459 i16x8_extadd_pairwise_i8x16_u = 0xfd7d,
460 i32x4_extadd_pairwise_i16x8_s = 0xfd7e,
461 i32x4_extadd_pairwise_i16x8_u = 0xfd7f,
462 i16x8_abs = 0xfd80,
463 i16x8_neg = 0xfd81,
464 i16x8_q15mulr_sat_s = 0xfd82,
465 i16x8_all_true = 0xfd83,
466 i16x8_bitmask = 0xfd84,
467 i16x8_narrow_i32x4_s = 0xfd85,
468 i16x8_narrow_i32x4_u = 0xfd86,
469 i16x8_extend_low_i8x16_s = 0xfd87,
470 i16x8_extend_high_i8x16_s = 0xfd88,
471 i16x8_extend_low_i8x16_u = 0xfd89,
472 i16x8_extend_high_i8x16_u = 0xfd8a,
473 i16x8_shl = 0xfd8b,
474 i16x8_shr_s = 0xfd8c,
475 i16x8_shr_u = 0xfd8d,
476 i16x8_add = 0xfd8e,
477 i16x8_add_sat_s = 0xfd8f,
478 i16x8_add_sat_u = 0xfd90,
479 i16x8_sub = 0xfd91,
480 i16x8_sub_sat_s = 0xfd92,
481 i16x8_sub_sat_u = 0xfd93,
482 f64x2_nearest = 0xfd94,
483 i16x8_mul = 0xfd95,
484 i16x8_min_s = 0xfd96,
485 i16x8_min_u = 0xfd97,
486 i16x8_max_s = 0xfd98,
487 i16x8_max_u = 0xfd99,
488 i16x8_avgr_u = 0xfd9b,
489 i16x8_extmul_low_i8x16_s = 0xfd9c,
490 i16x8_extmul_high_i8x16_s = 0xfd9d,
491 i16x8_extmul_low_i8x16_u = 0xfd9e,
492 i16x8_extmul_high_i8x16_u = 0xfd9f,
493 i32x4_abs = 0xfda0,
494 i32x4_neg = 0xfda1,
495 i32x4_all_true = 0xfda3,
496 i32x4_bitmask = 0xfda4,
497 i32x4_extend_low_i16x8_s = 0xfda7,
498 i32x4_extend_high_i16x8_s = 0xfda8,
499 i32x4_extend_low_i16x8_u = 0xfda9,
500 i32x4_extend_high_i16x8_u = 0xfdaa,
501 i32x4_shl = 0xfdab,
502 i32x4_shr_s = 0xfdac,
503 i32x4_shr_u = 0xfdad,
504 i32x4_add = 0xfdae,
505 i32x4_sub = 0xfdb1,
506 i32x4_mul = 0xfdb5,
507 i32x4_min_s = 0xfdb6,
508 i32x4_min_u = 0xfdb7,
509 i32x4_max_s = 0xfdb8,
510 i32x4_max_u = 0xfdb9,
511 i32x4_dot_i16x8_s = 0xfdba,
512 i32x4_extmul_low_i16x8_s = 0xfdbc,
513 i32x4_extmul_high_i16x8_s = 0xfdbd,
514 i32x4_extmul_low_i16x8_u = 0xfdbe,
515 i32x4_extmul_high_i16x8_u = 0xfdbf,
516 i64x2_abs = 0xfdc0,
517 i64x2_neg = 0xfdc1,
518 i64x2_all_true = 0xfdc3,
519 i64x2_bitmask = 0xfdc4,
520 i64x2_extend_low_i32x4_s = 0xfdc7,
521 i64x2_extend_high_i32x4_s = 0xfdc8,
522 i64x2_extend_low_i32x4_u = 0xfdc9,
523 i64x2_extend_high_i32x4_u = 0xfdca,
524 i64x2_shl = 0xfdcb,
525 i64x2_shr_s = 0xfdcc,
526 i64x2_shr_u = 0xfdcd,
527 i64x2_add = 0xfdce,
528 i64x2_sub = 0xfdd1,
529 i64x2_mul = 0xfdd5,
530 i64x2_eq = 0xfdd6,
531 i64x2_ne = 0xfdd7,
532 i64x2_lt_s = 0xfdd8,
533 i64x2_gt_s = 0xfdd9,
534 i64x2_le_s = 0xfdda,
535 i64x2_ge_s = 0xfddb,
536 i64x2_extmul_low_i32x4_s = 0xfddc,
537 i64x2_extmul_high_i32x4_s = 0xfddd,
538 i64x2_extmul_low_i32x4_u = 0xfdde,
539 i64x2_extmul_high_i32x4_u = 0xfddf,
540 f32x4_abs = 0xfde0,
541 f32x4_neg = 0xfde1,
542 f32x4_sqrt = 0xfde3,
543 f32x4_add = 0xfde4,
544 f32x4_sub = 0xfde5,
545 f32x4_mul = 0xfde6,
546 f32x4_div = 0xfde7,
547 f32x4_min = 0xfde8,
548 f32x4_max = 0xfde9,
549 f32x4_pmin = 0xfdea,
550 f32x4_pmax = 0xfdeb,
551 f64x2_abs = 0xfdec,
552 f64x2_neg = 0xfded,
553 f64x2_sqrt = 0xfdef,
554 f64x2_add = 0xfdf0,
555 f64x2_sub = 0xfdf1,
556 f64x2_mul = 0xfdf2,
557 f64x2_div = 0xfdf3,
558 f64x2_min = 0xfdf4,
559 f64x2_max = 0xfdf5,
560 f64x2_pmin = 0xfdf6,
561 f64x2_pmax = 0xfdf7,
562 i32x4_trunc_sat_f32x4_s = 0xfdf8,
563 i32x4_trunc_sat_f32x4_u = 0xfdf9,
564 f32x4_convert_i32x4_s = 0xfdfa,
565 f32x4_convert_i32x4_u = 0xfdfb,
566 i32x4_trunc_sat_f64x2_s_zero = 0xfdfc,
567 i32x4_trunc_sat_f64x2_u_zero = 0xfdfd,
568 f64x2_convert_low_i32x4_s = 0xfdfe,
569 f64x2_convert_low_i32x4_u = 0xfdff,
570
571 // GC proposal.
572 struct_new_with_rtt = 0xfb01,
573 struct_new_default_with_rtt = 0xfb02,
574 struct_get = 0xfb03,
575 struct_get_s = 0xfb04,
576 struct_get_u = 0xfb05,
577 struct_set = 0xfb06,
578 struct_new = 0xfb07,
579 struct_new_default = 0xfb08,
580 array_new_with_rtt = 0xfb11,
581 array_new_default_with_rtt = 0xfb12,
582 array_get = 0xfb13,
583 array_get_s = 0xfb14,
584 array_get_u = 0xfb15,
585 array_set = 0xfb16,
586 array_len = 0xfb17,
587 array_copy = 0xfb18, // Non-standard experiment in V8.
588 array_init = 0xfb19,
589 array_init_static = 0xfb1a,
590 array_new = 0xfb1b,
591 array_new_default = 0xfb1c,
592 i31_new = 0xfb20,
593 i31_get_s = 0xfb21,
594 i31_get_u = 0xfb22,
595 rtt_canon = 0xfb30,
596 rtt_sub = 0xfb31,
597 rtt_fresh_sub = 0xfb32, // Non-standard experiment in V8.
598 ref_test = 0xfb40,
599 ref_test_static = 0xfb44,
600 ref_cast = 0xfb41,
601 ref_cast_static = 0xfb45,
602 br_on_cast = 0xfb42,
603 br_on_cast_static = 0xfb46,
604 br_on_cast_fail = 0xfb43,
605 br_on_cast_static_fail = 0xfb47,
606 ref_is_func = 0xfb50,
607 ref_is_data = 0xfb51,
608 ref_is_i31 = 0xfb52,
609 ref_as_func = 0xfb58,
610 ref_as_data = 0xfb59,
611 ref_as_i31 = 0xfb5a,
612 br_on_func = 0xfb60,
613 br_on_data = 0xfb61,
614 br_on_i31 = 0xfb62,
615 br_on_non_func = 0xfb63,
616 br_on_non_data = 0xfb64,
617 br_on_non_i31 = 0xfb65,
618}
619
620export const OperatorCodeNames = [
621 "unreachable",
622 "nop",
623 "block",
624 "loop",
625 "if",
626 "else",
627 "try",
628 "catch",
629 "throw",
630 "rethrow",
631 "unwind",
632 "end",
633 "br",
634 "br_if",
635 "br_table",
636 "return",
637 "call",
638 "call_indirect",
639 "return_call",
640 "return_call_indirect",
641 "call_ref",
642 "return_call_ref",
643 undefined,
644 "let",
645 "delegate",
646 "catch_all",
647 "drop",
648 "select",
649 "select", // with types.
650 undefined,
651 undefined,
652 undefined,
653 "local.get",
654 "local.set",
655 "local.tee",
656 "global.get",
657 "global.set",
658 "table.get",
659 "table.set",
660 undefined,
661 "i32.load",
662 "i64.load",
663 "f32.load",
664 "f64.load",
665 "i32.load8_s",
666 "i32.load8_u",
667 "i32.load16_s",
668 "i32.load16_u",
669 "i64.load8_s",
670 "i64.load8_u",
671 "i64.load16_s",
672 "i64.load16_u",
673 "i64.load32_s",
674 "i64.load32_u",
675 "i32.store",
676 "i64.store",
677 "f32.store",
678 "f64.store",
679 "i32.store8",
680 "i32.store16",
681 "i64.store8",
682 "i64.store16",
683 "i64.store32",
684 "current_memory",
685 "memory.grow",
686 "i32.const",
687 "i64.const",
688 "f32.const",
689 "f64.const",
690 "i32.eqz",
691 "i32.eq",
692 "i32.ne",
693 "i32.lt_s",
694 "i32.lt_u",
695 "i32.gt_s",
696 "i32.gt_u",
697 "i32.le_s",
698 "i32.le_u",
699 "i32.ge_s",
700 "i32.ge_u",
701 "i64.eqz",
702 "i64.eq",
703 "i64.ne",
704 "i64.lt_s",
705 "i64.lt_u",
706 "i64.gt_s",
707 "i64.gt_u",
708 "i64.le_s",
709 "i64.le_u",
710 "i64.ge_s",
711 "i64.ge_u",
712 "f32.eq",
713 "f32.ne",
714 "f32.lt",
715 "f32.gt",
716 "f32.le",
717 "f32.ge",
718 "f64.eq",
719 "f64.ne",
720 "f64.lt",
721 "f64.gt",
722 "f64.le",
723 "f64.ge",
724 "i32.clz",
725 "i32.ctz",
726 "i32.popcnt",
727 "i32.add",
728 "i32.sub",
729 "i32.mul",
730 "i32.div_s",
731 "i32.div_u",
732 "i32.rem_s",
733 "i32.rem_u",
734 "i32.and",
735 "i32.or",
736 "i32.xor",
737 "i32.shl",
738 "i32.shr_s",
739 "i32.shr_u",
740 "i32.rotl",
741 "i32.rotr",
742 "i64.clz",
743 "i64.ctz",
744 "i64.popcnt",
745 "i64.add",
746 "i64.sub",
747 "i64.mul",
748 "i64.div_s",
749 "i64.div_u",
750 "i64.rem_s",
751 "i64.rem_u",
752 "i64.and",
753 "i64.or",
754 "i64.xor",
755 "i64.shl",
756 "i64.shr_s",
757 "i64.shr_u",
758 "i64.rotl",
759 "i64.rotr",
760 "f32.abs",
761 "f32.neg",
762 "f32.ceil",
763 "f32.floor",
764 "f32.trunc",
765 "f32.nearest",
766 "f32.sqrt",
767 "f32.add",
768 "f32.sub",
769 "f32.mul",
770 "f32.div",
771 "f32.min",
772 "f32.max",
773 "f32.copysign",
774 "f64.abs",
775 "f64.neg",
776 "f64.ceil",
777 "f64.floor",
778 "f64.trunc",
779 "f64.nearest",
780 "f64.sqrt",
781 "f64.add",
782 "f64.sub",
783 "f64.mul",
784 "f64.div",
785 "f64.min",
786 "f64.max",
787 "f64.copysign",
788 "i32.wrap_i64",
789 "i32.trunc_f32_s",
790 "i32.trunc_f32_u",
791 "i32.trunc_f64_s",
792 "i32.trunc_f64_u",
793 "i64.extend_i32_s",
794 "i64.extend_i32_u",
795 "i64.trunc_f32_s",
796 "i64.trunc_f32_u",
797 "i64.trunc_f64_s",
798 "i64.trunc_f64_u",
799 "f32.convert_i32_s",
800 "f32.convert_i32_u",
801 "f32.convert_i64_s",
802 "f32.convert_i64_u",
803 "f32.demote_f64",
804 "f64.convert_i32_s",
805 "f64.convert_i32_u",
806 "f64.convert_i64_s",
807 "f64.convert_i64_u",
808 "f64.promote_f32",
809 "i32.reinterpret_f32",
810 "i64.reinterpret_f64",
811 "f32.reinterpret_i32",
812 "f64.reinterpret_i64",
813 "i32.extend8_s",
814 "i32.extend16_s",
815 "i64.extend8_s",
816 "i64.extend16_s",
817 "i64.extend32_s",
818 undefined,
819 undefined,
820 undefined,
821 undefined,
822 undefined,
823 undefined,
824 undefined,
825 undefined,
826 undefined,
827 undefined,
828 undefined,
829 "ref.null",
830 "ref.is_null",
831 "ref.func",
832 "ref.as_non_null",
833 "br_on_null",
834 "ref.eq",
835 "br_on_non_null",
836 undefined,
837 undefined,
838 undefined,
839 undefined,
840 undefined,
841 undefined,
842 undefined,
843 undefined,
844 undefined,
845 undefined,
846 undefined,
847 undefined,
848 undefined,
849 undefined,
850 undefined,
851 undefined,
852 undefined,
853 undefined,
854 undefined,
855 undefined,
856 undefined,
857 undefined,
858 undefined,
859 undefined,
860 undefined,
861 undefined,
862 undefined,
863 undefined,
864 undefined,
865 undefined,
866 undefined,
867 undefined,
868 undefined,
869 undefined,
870 undefined,
871 undefined,
872 undefined,
873 undefined,
874 undefined,
875 undefined,
876 undefined,
877];
878
879[
880 "i32.trunc_sat_f32_s",
881 "i32.trunc_sat_f32_u",
882 "i32.trunc_sat_f64_s",
883 "i32.trunc_sat_f64_u",
884 "i64.trunc_sat_f32_s",
885 "i64.trunc_sat_f32_u",
886 "i64.trunc_sat_f64_s",
887 "i64.trunc_sat_f64_u",
888 "memory.init",
889 "data.drop",
890 "memory.copy",
891 "memory.fill",
892 "table.init",
893 "elem.drop",
894 "table.copy",
895 "table.grow",
896 "table.size",
897 "table.fill",
898].forEach((s, i) => {
899 OperatorCodeNames[0xfc00 | i] = s;
900});
901
902[
903 "v128.load",
904 "i16x8.load8x8_s",
905 "i16x8.load8x8_u",
906 "i32x4.load16x4_s",
907 "i32x4.load16x4_u",
908 "i64x2.load32x2_s",
909 "i64x2.load32x2_u",
910 "v8x16.load_splat",
911 "v16x8.load_splat",
912 "v32x4.load_splat",
913 "v64x2.load_splat",
914 "v128.store",
915 "v128.const",
916 "i8x16.shuffle",
917 "i8x16.swizzle",
918 "i8x16.splat",
919 "i16x8.splat",
920 "i32x4.splat",
921 "i64x2.splat",
922 "f32x4.splat",
923 "f64x2.splat",
924 "i8x16.extract_lane_s",
925 "i8x16.extract_lane_u",
926 "i8x16.replace_lane",
927 "i16x8.extract_lane_s",
928 "i16x8.extract_lane_u",
929 "i16x8.replace_lane",
930 "i32x4.extract_lane",
931 "i32x4.replace_lane",
932 "i64x2.extract_lane",
933 "i64x2.replace_lane",
934 "f32x4.extract_lane",
935 "f32x4.replace_lane",
936 "f64x2.extract_lane",
937 "f64x2.replace_lane",
938 "i8x16.eq",
939 "i8x16.ne",
940 "i8x16.lt_s",
941 "i8x16.lt_u",
942 "i8x16.gt_s",
943 "i8x16.gt_u",
944 "i8x16.le_s",
945 "i8x16.le_u",
946 "i8x16.ge_s",
947 "i8x16.ge_u",
948 "i16x8.eq",
949 "i16x8.ne",
950 "i16x8.lt_s",
951 "i16x8.lt_u",
952 "i16x8.gt_s",
953 "i16x8.gt_u",
954 "i16x8.le_s",
955 "i16x8.le_u",
956 "i16x8.ge_s",
957 "i16x8.ge_u",
958 "i32x4.eq",
959 "i32x4.ne",
960 "i32x4.lt_s",
961 "i32x4.lt_u",
962 "i32x4.gt_s",
963 "i32x4.gt_u",
964 "i32x4.le_s",
965 "i32x4.le_u",
966 "i32x4.ge_s",
967 "i32x4.ge_u",
968 "f32x4.eq",
969 "f32x4.ne",
970 "f32x4.lt",
971 "f32x4.gt",
972 "f32x4.le",
973 "f32x4.ge",
974 "f64x2.eq",
975 "f64x2.ne",
976 "f64x2.lt",
977 "f64x2.gt",
978 "f64x2.le",
979 "f64x2.ge",
980 "v128.not",
981 "v128.and",
982 "v128.andnot",
983 "v128.or",
984 "v128.xor",
985 "v128.bitselect",
986 "v128.any_true",
987 "v128.load8_lane",
988 "v128.load16_lane",
989 "v128.load32_lane",
990 "v128.load64_lane",
991 "v128.store8_lane",
992 "v128.store16_lane",
993 "v128.store32_lane",
994 "v128.store64_lane",
995 "v128.load32_zero",
996 "v128.load64_zero",
997 "f32x4.demote_f64x2_zero",
998 "f64x2.promote_low_f32x4",
999 "i8x16.abs",
1000 "i8x16.neg",
1001 "i8x16_popcnt",
1002 "i8x16.all_true",
1003 "i8x16.bitmask",
1004 "i8x16.narrow_i16x8_s",
1005 "i8x16.narrow_i16x8_u",
1006 "f32x4.ceil",
1007 "f32x4.floor",
1008 "f32x4.trunc",
1009 "f32x4.nearest",
1010 "i8x16.shl",
1011 "i8x16.shr_s",
1012 "i8x16.shr_u",
1013 "i8x16.add",
1014 "i8x16.add_sat_s",
1015 "i8x16.add_sat_u",
1016 "i8x16.sub",
1017 "i8x16.sub_sat_s",
1018 "i8x16.sub_sat_u",
1019 "f64x2.ceil",
1020 "f64x2.floor",
1021 "i8x16.min_s",
1022 "i8x16.min_u",
1023 "i8x16.max_s",
1024 "i8x16.max_u",
1025 "f64x2.trunc",
1026 "i8x16.avgr_u",
1027 "i16x8.extadd_pairwise_i8x16_s",
1028 "i16x8.extadd_pairwise_i8x16_u",
1029 "i32x4.extadd_pairwise_i16x8_s",
1030 "i32x4.extadd_pairwise_i16x8_u",
1031 "i16x8.abs",
1032 "i16x8.neg",
1033 "i16x8.q15mulr_sat_s",
1034 "i16x8.all_true",
1035 "i16x8.bitmask",
1036 "i16x8.narrow_i32x4_s",
1037 "i16x8.narrow_i32x4_u",
1038 "i16x8.extend_low_i8x16_s",
1039 "i16x8.extend_high_i8x16_s",
1040 "i16x8.extend_low_i8x16_u",
1041 "i16x8.extend_high_i8x16_u",
1042 "i16x8.shl",
1043 "i16x8.shr_s",
1044 "i16x8.shr_u",
1045 "i16x8.add",
1046 "i16x8.add_sat_s",
1047 "i16x8.add_sat_u",
1048 "i16x8.sub",
1049 "i16x8.sub_sat_s",
1050 "i16x8.sub_sat_u",
1051 "f64x2.nearest",
1052 "i16x8.mul",
1053 "i16x8.min_s",
1054 "i16x8.min_u",
1055 "i16x8.max_s",
1056 "i16x8.max_u",
1057 undefined,
1058 "i16x8.avgr_u",
1059 "i16x8.extmul_low_i8x16_s",
1060 "i16x8.extmul_high_i8x16_s",
1061 "i16x8.extmul_low_i8x16_u",
1062 "i16x8.extmul_high_i8x16_u",
1063 "i32x4.abs",
1064 "i32x4.neg",
1065 undefined,
1066 "i32x4.all_true",
1067 "i32x4.bitmask",
1068 undefined,
1069 undefined,
1070 "i32x4.extend_low_i16x8_s",
1071 "i32x4.extend_high_i16x8_s",
1072 "i32x4.extend_low_i16x8_u",
1073 "i32x4.extend_high_i16x8_u",
1074 "i32x4.shl",
1075 "i32x4.shr_s",
1076 "i32x4.shr_u",
1077 "i32x4.add",
1078 undefined,
1079 undefined,
1080 "i32x4.sub",
1081 undefined,
1082 undefined,
1083 undefined,
1084 "i32x4.mul",
1085 "i32x4.min_s",
1086 "i32x4.min_u",
1087 "i32x4.max_s",
1088 "i32x4.max_u",
1089 "i32x4.dot_i16x8_s",
1090 undefined,
1091 "i32x4.extmul_low_i16x8_s",
1092 "i32x4.extmul_high_i16x8_s",
1093 "i32x4.extmul_low_i16x8_u",
1094 "i32x4.extmul_high_i16x8_u",
1095 "i64x2.abs",
1096 "i64x2.neg",
1097 undefined,
1098 "i64x2.all_true",
1099 "i64x2.bitmask",
1100 undefined,
1101 undefined,
1102 "i64x2.extend_low_i32x4_s",
1103 "i64x2.extend_high_i32x4_s",
1104 "i64x2.extend_low_i32x4_u",
1105 "i64x2.extend_high_i32x4_u",
1106 "i64x2.shl",
1107 "i64x2.shr_s",
1108 "i64x2.shr_u",
1109 "i64x2.add",
1110 undefined,
1111 undefined,
1112 "i64x2.sub",
1113 undefined,
1114 undefined,
1115 undefined,
1116 "i64x2.mul",
1117 "i64x2.eq",
1118 "i64x2.ne",
1119 "i64x2.lt_s",
1120 "i64x2.gt_s",
1121 "i64x2.le_s",
1122 "i64x2.ge_s",
1123 "i64x2.extmul_low_i32x4_s",
1124 "i64x2.extmul_high_i32x4_s",
1125 "i64x2.extmul_low_i32x4_u",
1126 "i64x2.extmul_high_i32x4_u",
1127 "f32x4.abs",
1128 "f32x4.neg",
1129 undefined,
1130 "f32x4.sqrt",
1131 "f32x4.add",
1132 "f32x4.sub",
1133 "f32x4.mul",
1134 "f32x4.div",
1135 "f32x4.min",
1136 "f32x4.max",
1137 "f32x4.pmin",
1138 "f32x4.pmax",
1139 "f64x2.abs",
1140 "f64x2.neg",
1141 undefined,
1142 "f64x2.sqrt",
1143 "f64x2.add",
1144 "f64x2.sub",
1145 "f64x2.mul",
1146 "f64x2.div",
1147 "f64x2.min",
1148 "f64x2.max",
1149 "f64x2.pmin",
1150 "f64x2.pmax",
1151 "i32x4.trunc_sat_f32x4_s",
1152 "i32x4.trunc_sat_f32x4_u",
1153 "f32x4.convert_i32x4_s",
1154 "f32x4.convert_i32x4_u",
1155 "i32x4.trunc_sat_f64x2_s_zero",
1156 "i32x4.trunc_sat_f64x2_u_zero",
1157 "f64x2.convert_low_i32x4_s",
1158 "f64x2.convert_low_i32x4_u",
1159].forEach((s, i) => {
1160 OperatorCodeNames[0xfd00 | i] = s;
1161});
1162
1163[
1164 "atomic.notify",
1165 "i32.atomic.wait",
1166 "i64.atomic.wait",
1167 "atomic.fence",
1168 undefined,
1169 undefined,
1170 undefined,
1171 undefined,
1172 undefined,
1173 undefined,
1174 undefined,
1175 undefined,
1176 undefined,
1177 undefined,
1178 undefined,
1179 undefined,
1180 "i32.atomic.load",
1181 "i64.atomic.load",
1182 "i32.atomic.load8_u",
1183 "i32.atomic.load16_u",
1184 "i64.atomic.load8_u",
1185 "i64.atomic.load16_u",
1186 "i64.atomic.load32_u",
1187 "i32.atomic.store",
1188 "i64.atomic.store",
1189 "i32.atomic.store8",
1190 "i32.atomic.store16",
1191 "i64.atomic.store8",
1192 "i64.atomic.store16",
1193 "i64.atomic.store32",
1194 "i32.atomic.rmw.add",
1195 "i64.atomic.rmw.add",
1196 "i32.atomic.rmw8.add_u",
1197 "i32.atomic.rmw16.add_u",
1198 "i64.atomic.rmw8.add_u",
1199 "i64.atomic.rmw16.add_u",
1200 "i64.atomic.rmw32.add_u",
1201 "i32.atomic.rmw.sub",
1202 "i64.atomic.rmw.sub",
1203 "i32.atomic.rmw8.sub_u",
1204 "i32.atomic.rmw16.sub_u",
1205 "i64.atomic.rmw8.sub_u",
1206 "i64.atomic.rmw16.sub_u",
1207 "i64.atomic.rmw32.sub_u",
1208 "i32.atomic.rmw.and",
1209 "i64.atomic.rmw.and",
1210 "i32.atomic.rmw8.and_u",
1211 "i32.atomic.rmw16.and_u",
1212 "i64.atomic.rmw8.and_u",
1213 "i64.atomic.rmw16.and_u",
1214 "i64.atomic.rmw32.and_u",
1215 "i32.atomic.rmw.or",
1216 "i64.atomic.rmw.or",
1217 "i32.atomic.rmw8.or_u",
1218 "i32.atomic.rmw16.or_u",
1219 "i64.atomic.rmw8.or_u",
1220 "i64.atomic.rmw16.or_u",
1221 "i64.atomic.rmw32.or_u",
1222 "i32.atomic.rmw.xor",
1223 "i64.atomic.rmw.xor",
1224 "i32.atomic.rmw8.xor_u",
1225 "i32.atomic.rmw16.xor_u",
1226 "i64.atomic.rmw8.xor_u",
1227 "i64.atomic.rmw16.xor_u",
1228 "i64.atomic.rmw32.xor_u",
1229 "i32.atomic.rmw.xchg",
1230 "i64.atomic.rmw.xchg",
1231 "i32.atomic.rmw8.xchg_u",
1232 "i32.atomic.rmw16.xchg_u",
1233 "i64.atomic.rmw8.xchg_u",
1234 "i64.atomic.rmw16.xchg_u",
1235 "i64.atomic.rmw32.xchg_u",
1236 "i32.atomic.rmw.cmpxchg",
1237 "i64.atomic.rmw.cmpxchg",
1238 "i32.atomic.rmw8.cmpxchg_u",
1239 "i32.atomic.rmw16.cmpxchg_u",
1240 "i64.atomic.rmw8.cmpxchg_u",
1241 "i64.atomic.rmw16.cmpxchg_u",
1242 "i64.atomic.rmw32.cmpxchg_u",
1243].forEach((s, i) => {
1244 OperatorCodeNames[0xfe00 | i] = s;
1245});
1246
1247OperatorCodeNames[0xfb01] = "struct.new_with_rtt";
1248OperatorCodeNames[0xfb02] = "struct.new_default_with_rtt";
1249OperatorCodeNames[0xfb03] = "struct.get";
1250OperatorCodeNames[0xfb04] = "struct.get_s";
1251OperatorCodeNames[0xfb05] = "struct.get_u";
1252OperatorCodeNames[0xfb06] = "struct.set";
1253OperatorCodeNames[0xfb07] = "struct.new";
1254OperatorCodeNames[0xfb08] = "struct.new_default";
1255OperatorCodeNames[0xfb11] = "array.new_with_rtt";
1256OperatorCodeNames[0xfb12] = "array.new_default_with_rtt";
1257OperatorCodeNames[0xfb13] = "array.get";
1258OperatorCodeNames[0xfb14] = "array.get_s";
1259OperatorCodeNames[0xfb15] = "array.get_u";
1260OperatorCodeNames[0xfb16] = "array.set";
1261OperatorCodeNames[0xfb17] = "array.len";
1262OperatorCodeNames[0xfb18] = "array.copy";
1263OperatorCodeNames[0xfb19] = "array.init";
1264OperatorCodeNames[0xfb1a] = "array.init_static";
1265OperatorCodeNames[0xfb1b] = "array.new";
1266OperatorCodeNames[0xfb1c] = "array.new_default";
1267OperatorCodeNames[0xfb20] = "i31.new";
1268OperatorCodeNames[0xfb21] = "i31.get_s";
1269OperatorCodeNames[0xfb22] = "i31.get_u";
1270OperatorCodeNames[0xfb30] = "rtt.canon";
1271OperatorCodeNames[0xfb31] = "rtt.sub";
1272OperatorCodeNames[0xfb32] = "rtt.fresh_sub";
1273OperatorCodeNames[0xfb40] = "ref.test";
1274OperatorCodeNames[0xfb41] = "ref.cast";
1275OperatorCodeNames[0xfb42] = "br_on_cast";
1276OperatorCodeNames[0xfb43] = "br_on_cast_fail";
1277OperatorCodeNames[0xfb44] = "ref.test_static";
1278OperatorCodeNames[0xfb45] = "ref.cast_static";
1279OperatorCodeNames[0xfb46] = "br_on_cast_static";
1280OperatorCodeNames[0xfb47] = "br_on_cast_static_fail";
1281OperatorCodeNames[0xfb50] = "ref.is_func";
1282OperatorCodeNames[0xfb51] = "ref.is_data";
1283OperatorCodeNames[0xfb52] = "ref.is_i31";
1284OperatorCodeNames[0xfb58] = "ref.as_func";
1285OperatorCodeNames[0xfb59] = "ref.as_data";
1286OperatorCodeNames[0xfb5a] = "ref.as_i31";
1287OperatorCodeNames[0xfb60] = "br_on_func";
1288OperatorCodeNames[0xfb61] = "br_on_data";
1289OperatorCodeNames[0xfb62] = "br_on_i31";
1290OperatorCodeNames[0xfb63] = "br_on_non_func";
1291OperatorCodeNames[0xfb64] = "br_on_non_data";
1292OperatorCodeNames[0xfb65] = "br_on_non_i31";
1293
1294export const enum ExternalKind {
1295 Function = 0,
1296 Table = 1,
1297 Memory = 2,
1298 Global = 3,
1299 Event = 4,
1300}
1301export const enum TypeKind {
1302 unspecified = 0,
1303 i32 = -0x01,
1304 i64 = -0x02,
1305 f32 = -0x03,
1306 f64 = -0x04,
1307 v128 = -0x05,
1308 i8 = -0x06,
1309 i16 = -0x07,
1310 funcref = -0x10,
1311 externref = -0x11,
1312 anyref = -0x12,
1313 eqref = -0x13,
1314 optref = -0x14,
1315 ref = -0x15,
1316 i31ref = -0x16,
1317 rtt_d = -0x17, // RTT with depth.
1318 rtt = -0x18,
1319 dataref = -0x19,
1320 func = -0x20,
1321 struct = -0x21,
1322 array = -0x22,
1323 func_subtype = -0x23,
1324 struct_subtype = -0x24,
1325 array_subtype = -0x25,
1326 empty_block_type = -0x40,
1327}
1328export class Type {
1329 kind: TypeKind; // Always present.
1330 index: number; // Only for reference types: ref, optref, rtt, rtt_d.
1331 depth: number; // Only for RTTs with depth.
1332 constructor(kind: TypeKind, index = -1, depth = -1) {
1333 if (kind < 0 || (kind === 0 && index >= 0)) {
1334 // all good
1335 } else {
1336 throw new Error(`invalid type: ${kind}/${index}/${depth}`);
1337 }
1338 this.kind = kind;
1339 this.index = index;
1340 this.depth = depth;
1341 // Canonicalize (ref any) to (anyref) etc.
1342 if (
1343 (index === TypeKind.funcref && kind === TypeKind.optref) ||
1344 (index === TypeKind.externref && kind === TypeKind.optref) ||
1345 (index === TypeKind.anyref && kind === TypeKind.optref) ||
1346 (index === TypeKind.eqref && kind === TypeKind.optref) ||
1347 (index === TypeKind.i31ref && kind === TypeKind.ref) ||
1348 (index === TypeKind.dataref && kind === TypeKind.ref)
1349 ) {
1350 this.kind = index;
1351 this.index = -1;
1352 }
1353 }
1354 // Convenience singletons.
1355 static funcref: Type = new Type(TypeKind.funcref);
1356 static externref: Type = new Type(TypeKind.externref);
1357}
1358export const enum RelocType {
1359 FunctionIndex_LEB = 0,
1360 TableIndex_SLEB = 1,
1361 TableIndex_I32 = 2,
1362 GlobalAddr_LEB = 3,
1363 GlobalAddr_SLEB = 4,
1364 GlobalAddr_I32 = 5,
1365 TypeIndex_LEB = 6,
1366 GlobalIndex_LEB = 7,
1367}
1368export const enum LinkingType {
1369 StackPointer = 1,
1370}
1371export const enum NameType {
1372 Module = 0,
1373 Function = 1,
1374 Local = 2,
1375 Event = 3,
1376 Type = 4,
1377 Table = 5,
1378 Memory = 6,
1379 Global = 7,
1380 Field = 10,
1381}
1382export const enum BinaryReaderState {
1383 ERROR = -1,
1384 INITIAL = 0,
1385 BEGIN_WASM = 1,
1386 END_WASM = 2,
1387 BEGIN_SECTION = 3,
1388 END_SECTION = 4,
1389 SKIPPING_SECTION = 5,
1390 READING_SECTION_RAW_DATA = 6,
1391 SECTION_RAW_DATA = 7,
1392
1393 TYPE_SECTION_ENTRY = 11,
1394 IMPORT_SECTION_ENTRY = 12,
1395 FUNCTION_SECTION_ENTRY = 13,
1396 TABLE_SECTION_ENTRY = 14,
1397 MEMORY_SECTION_ENTRY = 15,
1398 GLOBAL_SECTION_ENTRY = 16,
1399 EXPORT_SECTION_ENTRY = 17,
1400 DATA_SECTION_ENTRY = 18,
1401 NAME_SECTION_ENTRY = 19,
1402 ELEMENT_SECTION_ENTRY = 20,
1403 LINKING_SECTION_ENTRY = 21,
1404 START_SECTION_ENTRY = 22,
1405 EVENT_SECTION_ENTRY = 23,
1406
1407 BEGIN_INIT_EXPRESSION_BODY = 25,
1408 INIT_EXPRESSION_OPERATOR = 26,
1409 END_INIT_EXPRESSION_BODY = 27,
1410
1411 BEGIN_FUNCTION_BODY = 28,
1412 READING_FUNCTION_HEADER = 29,
1413 CODE_OPERATOR = 30,
1414 END_FUNCTION_BODY = 31,
1415 SKIPPING_FUNCTION_BODY = 32,
1416
1417 BEGIN_ELEMENT_SECTION_ENTRY = 33,
1418 ELEMENT_SECTION_ENTRY_BODY = 34,
1419 END_ELEMENT_SECTION_ENTRY = 35,
1420
1421 BEGIN_DATA_SECTION_ENTRY = 36,
1422 DATA_SECTION_ENTRY_BODY = 37,
1423 END_DATA_SECTION_ENTRY = 38,
1424
1425 BEGIN_GLOBAL_SECTION_ENTRY = 39,
1426 END_GLOBAL_SECTION_ENTRY = 40,
1427
1428 RELOC_SECTION_HEADER = 41,
1429 RELOC_SECTION_ENTRY = 42,
1430
1431 SOURCE_MAPPING_URL = 43,
1432
1433 BEGIN_OFFSET_EXPRESSION_BODY = 44,
1434 OFFSET_EXPRESSION_OPERATOR = 45,
1435 END_OFFSET_EXPRESSION_BODY = 46,
1436}
1437
1438const enum DataSegmentType {
1439 Active = 0x00,
1440 Passive = 0x01,
1441 ActiveWithMemoryIndex = 0x02,
1442}
1443
1444function isActiveDataSegmentType(segmentType: number): boolean {
1445 switch (segmentType) {
1446 case DataSegmentType.Active:
1447 case DataSegmentType.ActiveWithMemoryIndex:
1448 return true;
1449 default:
1450 return false;
1451 }
1452}
1453
1454export const enum DataMode {
1455 Active,
1456 Passive,
1457}
1458
1459const enum ElementSegmentType {
1460 LegacyActiveFuncrefExternval = 0x00,
1461 PassiveExternval = 0x01,
1462 ActiveExternval = 0x02,
1463 DeclaredExternval = 0x03,
1464 LegacyActiveFuncrefElemexpr = 0x04,
1465 PassiveElemexpr = 0x05,
1466 ActiveElemexpr = 0x06,
1467 DeclaredElemexpr = 0x07,
1468}
1469
1470function isActiveElementSegmentType(segmentType: number): boolean {
1471 switch (segmentType) {
1472 case ElementSegmentType.LegacyActiveFuncrefExternval:
1473 case ElementSegmentType.ActiveExternval:
1474 case ElementSegmentType.LegacyActiveFuncrefElemexpr:
1475 case ElementSegmentType.ActiveElemexpr:
1476 return true;
1477 default:
1478 return false;
1479 }
1480}
1481
1482function isExternvalElementSegmentType(segmentType: number): boolean {
1483 switch (segmentType) {
1484 case ElementSegmentType.LegacyActiveFuncrefExternval:
1485 case ElementSegmentType.PassiveExternval:
1486 case ElementSegmentType.ActiveExternval:
1487 case ElementSegmentType.DeclaredExternval:
1488 return true;
1489 default:
1490 return false;
1491 }
1492}
1493
1494export const enum ElementMode {
1495 Active,
1496 Passive,
1497 Declarative,
1498}
1499
1500class DataRange {
1501 start: number;
1502 end: number;
1503 constructor(start: number, end: number) {
1504 this.start = start;
1505 this.end = end;
1506 }
1507 offset(delta: number) {
1508 this.start += delta;
1509 this.end += delta;
1510 }
1511}
1512export interface IModuleHeader {
1513 magicNumber: number;
1514 version: number;
1515}
1516export interface IResizableLimits {
1517 initial: number;
1518 maximum?: number;
1519}
1520export interface ITableType {
1521 elementType: Type;
1522 limits: IResizableLimits;
1523}
1524export interface IMemoryType {
1525 limits: IResizableLimits;
1526 shared: boolean;
1527}
1528export interface IGlobalType {
1529 contentType: Type;
1530 mutability: number;
1531}
1532export interface IEventType {
1533 attribute: number;
1534 typeIndex: number;
1535}
1536export interface IGlobalVariable {
1537 type: IGlobalType;
1538}
1539export interface IElementSegment {
1540 mode: ElementMode;
1541 tableIndex?: number;
1542}
1543export interface IElementSegmentBody {
1544 elementType: Type;
1545}
1546export interface IDataSegment {
1547 mode: DataMode;
1548 memoryIndex?: number;
1549}
1550export interface IDataSegmentBody {
1551 data: Uint8Array;
1552}
1553export type ImportEntryType =
1554 | ITableType
1555 | IMemoryType
1556 | IGlobalType
1557 | IEventType;
1558export interface IImportEntry {
1559 module: Uint8Array;
1560 field: Uint8Array;
1561 kind: ExternalKind;
1562 funcTypeIndex?: number;
1563 type?: ImportEntryType;
1564}
1565export interface IExportEntry {
1566 field: Uint8Array;
1567 kind: ExternalKind;
1568 index: number;
1569}
1570export interface INameEntry {
1571 type: NameType;
1572}
1573export interface INaming {
1574 index: number;
1575 name: Uint8Array;
1576}
1577export interface IModuleNameEntry extends INameEntry {
1578 moduleName: Uint8Array;
1579}
1580export interface IFunctionNameEntry extends INameEntry {
1581 names: INaming[];
1582}
1583export interface ILocalName {
1584 index: number;
1585 locals: INaming[];
1586}
1587export interface ILocalNameEntry extends INameEntry {
1588 funcs: ILocalName[];
1589}
1590export interface IEventNameEntry extends INameEntry {
1591 names: INaming[];
1592}
1593export interface ITypeNameEntry extends INameEntry {
1594 names: INaming[];
1595}
1596export interface ITableNameEntry extends INameEntry {
1597 names: INaming[];
1598}
1599export interface IMemoryNameEntry extends INameEntry {
1600 names: INaming[];
1601}
1602export interface IGlobalNameEntry extends INameEntry {
1603 names: INaming[];
1604}
1605export interface IFieldName {
1606 index: number;
1607 fields: INaming[];
1608}
1609export interface IFieldNameEntry extends INameEntry {
1610 types: IFieldName[];
1611}
1612export interface ILinkingEntry {
1613 type: LinkingType;
1614 index?: number;
1615}
1616export interface IRelocHeader {
1617 id: SectionCode;
1618 name: Uint8Array;
1619}
1620export interface IRelocEntry {
1621 type: RelocType;
1622 offset: number;
1623 index: number;
1624 addend?: number;
1625}
1626export interface ISourceMappingURL {
1627 url: Uint8Array;
1628}
1629export interface IStartEntry {
1630 index: number;
1631}
1632export interface IFunctionEntry {
1633 typeIndex: number;
1634}
1635export interface ITypeEntry {
1636 // func | struct | array | func_subtype | struct_subtype | array_subtype
1637 form: number;
1638 params?: Type[]; // For function types.
1639 returns?: Type[]; // For function types.
1640 fields?: Type[]; // For struct types.
1641 mutabilities?: boolean[]; // For struct types.
1642 elementType?: Type; // For array types.
1643 mutability?: boolean; // For array types.
1644 supertype?: number; // For *_subtype types.
1645}
1646export interface ISectionInformation {
1647 id: SectionCode;
1648 name: Uint8Array;
1649}
1650export interface ILocals {
1651 count: number;
1652 type: Type;
1653}
1654export interface IFunctionInformation {
1655 locals: Array<ILocals>;
1656}
1657export interface IMemoryAddress {
1658 flags: number;
1659 offset: number;
1660}
1661export interface IOperatorInformation {
1662 code: OperatorCode;
1663 blockType?: Type;
1664 selectType?: Type;
1665 refType?: number; // "HeapType" format, a.k.a. s33
1666 srcType?: number; // "HeapType" format, a.k.a. s33
1667 brDepth?: number;
1668 brTable?: Array<number>;
1669 relativeDepth?: number;
1670 funcIndex?: number;
1671 typeIndex?: number;
1672 tableIndex?: number;
1673 localIndex?: number;
1674 fieldIndex?: number;
1675 globalIndex?: number;
1676 segmentIndex?: number;
1677 eventIndex?: number;
1678 destinationIndex?: number;
1679 memoryAddress?: IMemoryAddress;
1680 literal?: number | Int64 | Uint8Array;
1681 lines: Uint8Array;
1682 lineIndex: number;
1683}
1684export class Int64 {
1685 private _data: Uint8Array;
1686 constructor(data: Uint8Array) {
1687 this._data = data || new Uint8Array(8);
1688 }
1689 public toInt32(): number {
1690 return (
1691 this._data[0] |
1692 (this._data[1] << 8) |
1693 (this._data[2] << 16) |
1694 (this._data[3] << 24)
1695 );
1696 }
1697 public toDouble(): number {
1698 var power = 1;
1699 var sum;
1700 if (this._data[7] & 0x80) {
1701 sum = -1;
1702 for (var i = 0; i < 8; i++, power *= 256)
1703 sum -= power * (0xff ^ this._data[i]);
1704 } else {
1705 sum = 0;
1706 for (var i = 0; i < 8; i++, power *= 256) sum += power * this._data[i];
1707 }
1708 return sum;
1709 }
1710 public toString(): string {
1711 var low =
1712 (this._data[0] |
1713 (this._data[1] << 8) |
1714 (this._data[2] << 16) |
1715 (this._data[3] << 24)) >>>
1716 0;
1717 var high =
1718 (this._data[4] |
1719 (this._data[5] << 8) |
1720 (this._data[6] << 16) |
1721 (this._data[7] << 24)) >>>
1722 0;
1723 if (low === 0 && high === 0) {
1724 return "0";
1725 }
1726 var sign = false;
1727 if (high >> 31) {
1728 high = 4294967296 - high;
1729 if (low > 0) {
1730 high--;
1731 low = 4294967296 - low;
1732 }
1733 sign = true;
1734 }
1735 var buf = [];
1736 while (high > 0) {
1737 var t = (high % 10) * 4294967296 + low;
1738 high = Math.floor(high / 10);
1739 buf.unshift((t % 10).toString());
1740 low = Math.floor(t / 10);
1741 }
1742 while (low > 0) {
1743 buf.unshift((low % 10).toString());
1744 low = Math.floor(low / 10);
1745 }
1746 if (sign) buf.unshift("-");
1747 return buf.join("");
1748 }
1749 public get data(): Uint8Array {
1750 return this._data;
1751 }
1752}
1753export type BinaryReaderResult =
1754 | IImportEntry
1755 | IExportEntry
1756 | IFunctionEntry
1757 | ITypeEntry
1758 | IModuleHeader
1759 | IOperatorInformation
1760 | IMemoryType
1761 | ITableType
1762 | IGlobalVariable
1763 | INameEntry
1764 | IElementSegment
1765 | IElementSegmentBody
1766 | IDataSegment
1767 | IDataSegmentBody
1768 | ISectionInformation
1769 | IFunctionInformation
1770 | ISectionInformation
1771 | IFunctionInformation
1772 | IRelocHeader
1773 | IRelocEntry
1774 | ILinkingEntry
1775 | ISourceMappingURL
1776 | IModuleNameEntry
1777 | IStartEntry
1778 | Uint8Array;
1779export class BinaryReader {
1780 private _data: Uint8Array = null;
1781 private _pos = 0;
1782 private _length = 0;
1783 private _eof = false;
1784 public state: BinaryReaderState = BinaryReaderState.INITIAL;
1785 public result: BinaryReaderResult = null;
1786 public error: Error = null;
1787 private _sectionEntriesLeft = 0;
1788 private _sectionId: SectionCode = SectionCode.Unknown;
1789 private _sectionRange: DataRange = null;
1790 private _functionRange: DataRange = null;
1791 private _segmentType: DataSegmentType | ElementSegmentType = 0;
1792 private _segmentEntriesLeft = 0;
1793 public get data(): Uint8Array {
1794 return this._data;
1795 }
1796 public get position(): number {
1797 return this._pos;
1798 }
1799 public get length(): number {
1800 return this._length;
1801 }
1802 public setData(
1803 buffer: ArrayBuffer,
1804 pos: number,
1805 length: number,
1806 eof?: boolean
1807 ): void {
1808 var posDelta = pos - this._pos;
1809 this._data = new Uint8Array(buffer);
1810 this._pos = pos;
1811 this._length = length;
1812 this._eof = eof === undefined ? true : eof;
1813 if (this._sectionRange) this._sectionRange.offset(posDelta);
1814 if (this._functionRange) this._functionRange.offset(posDelta);
1815 }
1816 private hasBytes(n: number): boolean {
1817 return this._pos + n <= this._length;
1818 }
1819 public hasMoreBytes(): boolean {
1820 return this.hasBytes(1);
1821 }
1822 private readUint8(): number {
1823 return this._data[this._pos++];
1824 }
1825 private readInt32(): number {
1826 var b1 = this._data[this._pos++];
1827 var b2 = this._data[this._pos++];
1828 var b3 = this._data[this._pos++];
1829 var b4 = this._data[this._pos++];
1830 return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
1831 }
1832 private readUint32(): number {
1833 return this.readInt32();
1834 }
1835 private peekInt32(): number {
1836 var b1 = this._data[this._pos];
1837 var b2 = this._data[this._pos + 1];
1838 var b3 = this._data[this._pos + 2];
1839 var b4 = this._data[this._pos + 3];
1840 return b1 | (b2 << 8) | (b3 << 16) | (b4 << 24);
1841 }
1842 private hasVarIntBytes(): boolean {
1843 var pos = this._pos;
1844 while (pos < this._length) {
1845 if ((this._data[pos++] & 0x80) == 0) return true;
1846 }
1847 return false;
1848 }
1849 private readVarUint1(): number {
1850 return this.readUint8();
1851 }
1852 private readVarInt7(): number {
1853 return (this.readUint8() << 25) >> 25;
1854 }
1855 private readVarUint7(): number {
1856 return this.readUint8();
1857 }
1858 private readVarInt32(): number {
1859 var result = 0;
1860 var shift = 0;
1861 while (true) {
1862 var byte = this.readUint8();
1863 result |= (byte & 0x7f) << shift;
1864 shift += 7;
1865 if ((byte & 0x80) === 0) break;
1866 }
1867 if (shift >= 32) return result;
1868 var ashift = 32 - shift;
1869 return (result << ashift) >> ashift;
1870 }
1871 private readVarUint32(): number {
1872 var result = 0;
1873 var shift = 0;
1874 while (true) {
1875 var byte = this.readUint8();
1876 result |= (byte & 0x7f) << shift;
1877 shift += 7;
1878 if ((byte & 0x80) === 0) break;
1879 }
1880 return result >>> 0;
1881 }
1882 private readVarInt64(): Int64 {
1883 var result = new Uint8Array(8);
1884 var i = 0;
1885 var c = 0;
1886 var shift = 0;
1887 while (true) {
1888 var byte = this.readUint8();
1889 c |= (byte & 0x7f) << shift;
1890 shift += 7;
1891 if (shift > 8) {
1892 result[i++] = c & 0xff;
1893 c >>= 8;
1894 shift -= 8;
1895 }
1896 if ((byte & 0x80) === 0) break;
1897 }
1898 var ashift = 32 - shift;
1899 c = (c << ashift) >> ashift;
1900 while (i < 8) {
1901 result[i++] = c & 0xff;
1902 c >>= 8;
1903 }
1904 return new Int64(result);
1905 }
1906 // Reads any "s33" (signed 33-bit integer) value correctly; no guarantees
1907 // outside that range.
1908 private readHeapType(): number {
1909 var result = 0;
1910 var shift = 0;
1911 var byte: number;
1912 while (true) {
1913 byte = this.readUint8();
1914 if (shift === 28) {
1915 var signed = (byte << 25) >> 25;
1916 return signed * Math.pow(2, 28) + result;
1917 }
1918 result |= (byte & 0x7f) << shift;
1919 shift += 7;
1920 if ((byte & 0x80) === 0) break;
1921 }
1922 shift = 32 - shift;
1923 return (result << shift) >> shift;
1924 }
1925 private readTypeInternal(kind: TypeKind): Type {
1926 if (
1927 kind === TypeKind.ref ||
1928 kind === TypeKind.optref ||
1929 kind === TypeKind.rtt
1930 ) {
1931 var index = this.readHeapType();
1932 return new Type(kind, index);
1933 }
1934 if (kind === TypeKind.rtt_d) {
1935 var index = this.readHeapType();
1936 var depth = this.readVarUint32();
1937 return new Type(kind, index, depth);
1938 }
1939 return new Type(kind);
1940 }
1941 private readType(): Type {
1942 var kind = this.readVarInt7();
1943 return this.readTypeInternal(kind);
1944 }
1945 private readBlockType(): Type {
1946 var block_type = this.readHeapType();
1947 if (block_type < 0) {
1948 return this.readTypeInternal(block_type);
1949 }
1950 var func_index = block_type;
1951 return new Type(TypeKind.unspecified, func_index);
1952 }
1953 private readStringBytes(): Uint8Array {
1954 var length = this.readVarUint32();
1955 return this.readBytes(length);
1956 }
1957 private readBytes(length: number): Uint8Array {
1958 var result = this._data.subarray(this._pos, this._pos + length);
1959 this._pos += length;
1960 return new Uint8Array(result); // making a clone of the data
1961 }
1962 private skipBytes(length: number) {
1963 this._pos += length;
1964 }
1965 private hasStringBytes(): boolean {
1966 if (!this.hasVarIntBytes()) return false;
1967 var pos = this._pos;
1968 var length = this.readVarUint32();
1969 var result = this.hasBytes(length);
1970 this._pos = pos;
1971 return result;
1972 }
1973 private hasSectionPayload(): boolean {
1974 return this.hasBytes(this._sectionRange.end - this._pos);
1975 }
1976 private readFuncType(): ITypeEntry {
1977 var paramCount = this.readVarUint32();
1978 var paramTypes = new Array(paramCount);
1979 for (var i = 0; i < paramCount; i++) paramTypes[i] = this.readType();
1980 var returnCount = this.readVarUint1();
1981 var returnTypes = new Array(returnCount);
1982 for (var i = 0; i < returnCount; i++) returnTypes[i] = this.readType();
1983 return {
1984 form: TypeKind.func,
1985 params: paramTypes,
1986 returns: returnTypes,
1987 };
1988 }
1989 private readFuncSubtype(): ITypeEntry {
1990 var result = this.readFuncType();
1991 result.form = TypeKind.func_subtype;
1992 result.supertype = this.readHeapType();
1993 return result;
1994 }
1995 private readStructType(): ITypeEntry {
1996 var fieldCount = this.readVarUint32();
1997 var fieldTypes = new Array(fieldCount);
1998 var fieldMutabilities = new Array(fieldCount);
1999 for (var i = 0; i < fieldCount; i++) {
2000 fieldTypes[i] = this.readType();
2001 fieldMutabilities[i] = !!this.readVarUint1();
2002 }
2003 return {
2004 form: TypeKind.struct,
2005 fields: fieldTypes,
2006 mutabilities: fieldMutabilities,
2007 };
2008 }
2009 private readStructSubtype(): ITypeEntry {
2010 var result = this.readStructType();
2011 result.form = TypeKind.struct_subtype;
2012 result.supertype = this.readHeapType();
2013 return result;
2014 }
2015 private readArrayType(): ITypeEntry {
2016 var elementType = this.readType();
2017 var mutability = !!this.readVarUint1();
2018 return {
2019 form: TypeKind.array,
2020 elementType: elementType,
2021 mutability: mutability,
2022 };
2023 }
2024 private readArraySubtype(): ITypeEntry {
2025 var result = this.readArrayType();
2026 result.form = TypeKind.array_subtype;
2027 result.supertype = this.readHeapType();
2028 return result;
2029 }
2030 private readResizableLimits(maxPresent: boolean): IResizableLimits {
2031 var initial = this.readVarUint32();
2032 var maximum;
2033 if (maxPresent) {
2034 maximum = this.readVarUint32();
2035 }
2036 return { initial: initial, maximum: maximum };
2037 }
2038 private readTableType(): ITableType {
2039 var elementType = this.readType();
2040 var flags = this.readVarUint32();
2041 var limits = this.readResizableLimits(!!(flags & 0x01));
2042 return { elementType: elementType, limits: limits };
2043 }
2044 private readMemoryType(): IMemoryType {
2045 var flags = this.readVarUint32();
2046 var shared = !!(flags & 0x02);
2047 return {
2048 limits: this.readResizableLimits(!!(flags & 0x01)),
2049 shared: shared,
2050 };
2051 }
2052 private readGlobalType(): IGlobalType {
2053 if (!this.hasVarIntBytes()) {
2054 return null;
2055 }
2056 var pos = this._pos;
2057 var contentType = this.readType();
2058 if (!this.hasVarIntBytes()) {
2059 this._pos = pos;
2060 return null;
2061 }
2062 var mutability = this.readVarUint1();
2063 return { contentType: contentType, mutability: mutability };
2064 }
2065 private readEventType(): IEventType {
2066 var attribute = this.readVarUint32();
2067 var typeIndex = this.readVarUint32();
2068 return {
2069 attribute: attribute,
2070 typeIndex: typeIndex,
2071 };
2072 }
2073 private readTypeEntry() {
2074 if (this._sectionEntriesLeft === 0) {
2075 this.skipSection();
2076 return this.read();
2077 }
2078 this.state = BinaryReaderState.TYPE_SECTION_ENTRY;
2079 var form = this.readVarInt7();
2080 switch (form) {
2081 case TypeKind.func:
2082 this.result = this.readFuncType();
2083 break;
2084 case TypeKind.func_subtype:
2085 this.result = this.readFuncSubtype();
2086 break;
2087 case TypeKind.struct:
2088 this.result = this.readStructType();
2089 break;
2090 case TypeKind.struct_subtype:
2091 this.result = this.readStructSubtype();
2092 break;
2093 case TypeKind.array:
2094 this.result = this.readArrayType();
2095 break;
2096 case TypeKind.array_subtype:
2097 this.result = this.readArraySubtype();
2098 break;
2099 default:
2100 throw new Error(`Unknown type kind: ${form}`);
2101 }
2102 this._sectionEntriesLeft--;
2103 return true;
2104 }
2105 private readImportEntry() {
2106 if (this._sectionEntriesLeft === 0) {
2107 this.skipSection();
2108 return this.read();
2109 }
2110 this.state = BinaryReaderState.IMPORT_SECTION_ENTRY;
2111 var module = this.readStringBytes();
2112 var field = this.readStringBytes();
2113 var kind = this.readUint8();
2114 var funcTypeIndex: number;
2115 var type: ITableType | IMemoryType | IGlobalType | IEventType;
2116 switch (kind) {
2117 case ExternalKind.Function:
2118 funcTypeIndex = this.readVarUint32();
2119 break;
2120 case ExternalKind.Table:
2121 type = this.readTableType();
2122 break;
2123 case ExternalKind.Memory:
2124 type = this.readMemoryType();
2125 break;
2126 case ExternalKind.Global:
2127 type = this.readGlobalType();
2128 break;
2129 case ExternalKind.Event:
2130 type = this.readEventType();
2131 break;
2132 }
2133 this.result = {
2134 module: module,
2135 field: field,
2136 kind: kind,
2137 funcTypeIndex: funcTypeIndex,
2138 type: type,
2139 };
2140 this._sectionEntriesLeft--;
2141 return true;
2142 }
2143 private readExportEntry(): boolean {
2144 if (this._sectionEntriesLeft === 0) {
2145 this.skipSection();
2146 return this.read();
2147 }
2148 var field = this.readStringBytes();
2149 var kind = this.readUint8();
2150 var index = this.readVarUint32();
2151 this.state = BinaryReaderState.EXPORT_SECTION_ENTRY;
2152 this.result = { field: field, kind: kind, index: index };
2153 this._sectionEntriesLeft--;
2154 return true;
2155 }
2156 private readFunctionEntry(): boolean {
2157 if (this._sectionEntriesLeft === 0) {
2158 this.skipSection();
2159 return this.read();
2160 }
2161 var typeIndex = this.readVarUint32();
2162 this.state = BinaryReaderState.FUNCTION_SECTION_ENTRY;
2163 this.result = { typeIndex: typeIndex };
2164 this._sectionEntriesLeft--;
2165 return true;
2166 }
2167 private readTableEntry(): boolean {
2168 if (this._sectionEntriesLeft === 0) {
2169 this.skipSection();
2170 return this.read();
2171 }
2172 this.state = BinaryReaderState.TABLE_SECTION_ENTRY;
2173 this.result = this.readTableType();
2174 this._sectionEntriesLeft--;
2175 return true;
2176 }
2177 private readMemoryEntry(): boolean {
2178 if (this._sectionEntriesLeft === 0) {
2179 this.skipSection();
2180 return this.read();
2181 }
2182 this.state = BinaryReaderState.MEMORY_SECTION_ENTRY;
2183 this.result = this.readMemoryType();
2184 this._sectionEntriesLeft--;
2185 return true;
2186 }
2187 private readEventEntry(): boolean {
2188 if (this._sectionEntriesLeft === 0) {
2189 this.skipSection();
2190 return this.read();
2191 }
2192 this.state = BinaryReaderState.EVENT_SECTION_ENTRY;
2193 this.result = this.readEventType();
2194 this._sectionEntriesLeft--;
2195 return true;
2196 }
2197 private readGlobalEntry(): boolean {
2198 if (this._sectionEntriesLeft === 0) {
2199 this.skipSection();
2200 return this.read();
2201 }
2202 var globalType = this.readGlobalType();
2203 if (!globalType) {
2204 this.state = BinaryReaderState.GLOBAL_SECTION_ENTRY;
2205 return false;
2206 }
2207 this.state = BinaryReaderState.BEGIN_GLOBAL_SECTION_ENTRY;
2208 this.result = {
2209 type: globalType,
2210 };
2211 this._sectionEntriesLeft--;
2212 return true;
2213 }
2214 private readElementEntry(): boolean {
2215 if (this._sectionEntriesLeft === 0) {
2216 this.skipSection();
2217 return this.read();
2218 }
2219 const pos = this._pos;
2220 if (!this.hasMoreBytes()) {
2221 this.state = BinaryReaderState.ELEMENT_SECTION_ENTRY;
2222 return false;
2223 }
2224 const segmentType = this.readUint8();
2225 let mode, tableIndex;
2226 switch (segmentType) {
2227 case ElementSegmentType.LegacyActiveFuncrefExternval:
2228 case ElementSegmentType.LegacyActiveFuncrefElemexpr:
2229 mode = ElementMode.Active;
2230 tableIndex = 0;
2231 break;
2232 case ElementSegmentType.PassiveExternval:
2233 case ElementSegmentType.PassiveElemexpr:
2234 mode = ElementMode.Passive;
2235 break;
2236 case ElementSegmentType.ActiveExternval:
2237 case ElementSegmentType.ActiveElemexpr:
2238 mode = ElementMode.Active;
2239 if (!this.hasVarIntBytes()) {
2240 this.state = BinaryReaderState.ELEMENT_SECTION_ENTRY;
2241 this._pos = pos;
2242 return false;
2243 }
2244 tableIndex = this.readVarUint32();
2245 break;
2246 case ElementSegmentType.DeclaredExternval:
2247 case ElementSegmentType.DeclaredElemexpr:
2248 mode = ElementMode.Declarative;
2249 break;
2250 default:
2251 throw new Error(`Unsupported element segment type ${segmentType}`);
2252 }
2253 this.state = BinaryReaderState.BEGIN_ELEMENT_SECTION_ENTRY;
2254 this.result = { mode, tableIndex };
2255 this._sectionEntriesLeft--;
2256 this._segmentType = segmentType;
2257 return true;
2258 }
2259 private readElementEntryBody(): boolean {
2260 let elementType = Type.funcref;
2261 switch (this._segmentType) {
2262 case ElementSegmentType.PassiveExternval:
2263 case ElementSegmentType.ActiveExternval:
2264 case ElementSegmentType.DeclaredExternval:
2265 if (!this.hasMoreBytes()) return false;
2266 // We just skip the 0x00 byte, the `elemkind` byte
2267 // is reserved for future versions of WebAssembly.
2268 this.skipBytes(1);
2269 break;
2270 case ElementSegmentType.PassiveElemexpr:
2271 case ElementSegmentType.ActiveElemexpr:
2272 case ElementSegmentType.DeclaredElemexpr:
2273 if (!this.hasMoreBytes()) return false;
2274 elementType = this.readType();
2275 break;
2276 case ElementSegmentType.LegacyActiveFuncrefExternval:
2277 case ElementSegmentType.LegacyActiveFuncrefElemexpr:
2278 // The element type is implicitly `funcref`.
2279 break;
2280 default:
2281 throw new Error(
2282 `Unsupported element segment type ${this._segmentType}`
2283 );
2284 }
2285 this.state = BinaryReaderState.ELEMENT_SECTION_ENTRY_BODY;
2286 this.result = { elementType };
2287 return true;
2288 }
2289 private readDataEntry(): boolean {
2290 if (this._sectionEntriesLeft === 0) {
2291 this.skipSection();
2292 return this.read();
2293 }
2294 const pos = this._pos;
2295 if (!this.hasVarIntBytes()) {
2296 this.state = BinaryReaderState.DATA_SECTION_ENTRY;
2297 return false;
2298 }
2299 const segmentType = this.readVarUint32();
2300 let mode, memoryIndex;
2301 switch (segmentType) {
2302 case DataSegmentType.Active:
2303 mode = DataMode.Active;
2304 memoryIndex = 0;
2305 break;
2306 case DataSegmentType.Passive:
2307 mode = DataMode.Passive;
2308 break;
2309 case DataSegmentType.ActiveWithMemoryIndex:
2310 mode = DataMode.Active;
2311 if (!this.hasVarIntBytes()) {
2312 this._pos = pos;
2313 this.state = BinaryReaderState.DATA_SECTION_ENTRY;
2314 return false;
2315 }
2316 memoryIndex = this.readVarUint32();
2317 break;
2318 default:
2319 throw new Error(`Unsupported data segment type ${segmentType}`);
2320 }
2321 this.state = BinaryReaderState.BEGIN_DATA_SECTION_ENTRY;
2322 this.result = { mode, memoryIndex };
2323 this._sectionEntriesLeft--;
2324 this._segmentType = segmentType;
2325 return true;
2326 }
2327 private readDataEntryBody(): boolean {
2328 if (!this.hasStringBytes()) {
2329 return false;
2330 }
2331 this.state = BinaryReaderState.DATA_SECTION_ENTRY_BODY;
2332 this.result = {
2333 data: this.readStringBytes(),
2334 };
2335 return true;
2336 }
2337 private readInitExpressionBody(): boolean {
2338 this.state = BinaryReaderState.BEGIN_INIT_EXPRESSION_BODY;
2339 this.result = null;
2340 return true;
2341 }
2342 private readOffsetExpressionBody(): boolean {
2343 this.state = BinaryReaderState.BEGIN_OFFSET_EXPRESSION_BODY;
2344 this.result = null;
2345 return true;
2346 }
2347 private readMemoryImmediate(): IMemoryAddress {
2348 var flags = this.readVarUint32();
2349 var offset = this.readVarUint32();
2350 return { flags: flags, offset: offset };
2351 }
2352 private readNameMap(): INaming[] {
2353 var count = this.readVarUint32();
2354 var result: INaming[] = [];
2355 for (var i = 0; i < count; i++) {
2356 var index = this.readVarUint32();
2357 var name = this.readStringBytes();
2358 result.push({ index: index, name: name });
2359 }
2360 return result;
2361 }
2362 private readNameEntry(): boolean {
2363 var pos = this._pos;
2364 if (pos >= this._sectionRange.end) {
2365 this.skipSection();
2366 return this.read();
2367 }
2368 if (!this.hasVarIntBytes()) return false;
2369 var type: NameType = this.readVarUint7();
2370 if (!this.hasVarIntBytes()) {
2371 this._pos = pos;
2372 return false;
2373 }
2374 var payloadLength = this.readVarUint32();
2375 if (!this.hasBytes(payloadLength)) {
2376 this._pos = pos;
2377 return false;
2378 }
2379 var result:
2380 | IModuleNameEntry
2381 | IFunctionNameEntry
2382 | ILocalNameEntry
2383 | IEventNameEntry
2384 | ITypeNameEntry
2385 | ITableNameEntry
2386 | IMemoryNameEntry
2387 | IGlobalNameEntry
2388 | IFieldNameEntry;
2389 switch (type) {
2390 case NameType.Module:
2391 result = {
2392 type,
2393 moduleName: this.readStringBytes(),
2394 };
2395 break;
2396 case NameType.Function:
2397 case NameType.Event:
2398 case NameType.Type:
2399 case NameType.Table:
2400 case NameType.Memory:
2401 case NameType.Global:
2402 result = {
2403 type,
2404 names: this.readNameMap(),
2405 };
2406 break;
2407 case NameType.Local:
2408 var funcsLength = this.readVarUint32();
2409 var funcs: ILocalName[] = [];
2410 for (var i = 0; i < funcsLength; i++) {
2411 var funcIndex = this.readVarUint32();
2412 funcs.push({
2413 index: funcIndex,
2414 locals: this.readNameMap(),
2415 });
2416 }
2417 result = {
2418 type,
2419 funcs: funcs,
2420 };
2421 break;
2422 case NameType.Field:
2423 var typesLength = this.readVarUint32();
2424 var types: IFieldName[] = [];
2425 for (var i = 0; i < typesLength; i++) {
2426 var fieldIndex = this.readVarUint32();
2427 types.push({
2428 index: fieldIndex,
2429 fields: this.readNameMap(),
2430 });
2431 }
2432 result = {
2433 type,
2434 types: types,
2435 };
2436 break;
2437 default:
2438 // Skip this unknown name subsection (as per specification,
2439 // custom section errors shouldn't cause Wasm parsing to fail).
2440 this.skipBytes(payloadLength);
2441 return this.read();
2442 }
2443 this.state = BinaryReaderState.NAME_SECTION_ENTRY;
2444 this.result = result;
2445 return true;
2446 }
2447 private readRelocHeader(): boolean {
2448 // See https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
2449 if (!this.hasVarIntBytes()) {
2450 return false;
2451 }
2452 var pos = this._pos;
2453 var sectionId: SectionCode = this.readVarUint7();
2454 var sectionName;
2455 if (sectionId === SectionCode.Custom) {
2456 if (!this.hasStringBytes()) {
2457 this._pos = pos;
2458 return false;
2459 }
2460 sectionName = this.readStringBytes();
2461 }
2462 this.state = BinaryReaderState.RELOC_SECTION_HEADER;
2463 this.result = {
2464 id: sectionId,
2465 name: sectionName,
2466 };
2467 return true;
2468 }
2469 private readLinkingEntry(): boolean {
2470 if (this._sectionEntriesLeft === 0) {
2471 this.skipSection();
2472 return this.read();
2473 }
2474 if (!this.hasVarIntBytes()) return false;
2475 var pos = this._pos;
2476 var type: LinkingType = this.readVarUint32();
2477 var index;
2478 switch (type) {
2479 case LinkingType.StackPointer:
2480 if (!this.hasVarIntBytes()) {
2481 this._pos = pos;
2482 return false;
2483 }
2484 index = this.readVarUint32();
2485 break;
2486 default:
2487 this.error = new Error(`Bad linking type: ${type}`);
2488 this.state = BinaryReaderState.ERROR;
2489 return true;
2490 }
2491 this.state = BinaryReaderState.LINKING_SECTION_ENTRY;
2492 this.result = { type: type, index: index };
2493 this._sectionEntriesLeft--;
2494 return true;
2495 }
2496 private readSourceMappingURL(): boolean {
2497 if (!this.hasStringBytes()) return false;
2498 var url = this.readStringBytes();
2499 this.state = BinaryReaderState.SOURCE_MAPPING_URL;
2500 this.result = { url: url };
2501 return true;
2502 }
2503 private readRelocEntry(): boolean {
2504 if (this._sectionEntriesLeft === 0) {
2505 this.skipSection();
2506 return this.read();
2507 }
2508 if (!this.hasVarIntBytes()) return false;
2509 var pos = this._pos;
2510 var type: RelocType = this.readVarUint7();
2511 if (!this.hasVarIntBytes()) {
2512 this._pos = pos;
2513 return false;
2514 }
2515 var offset = this.readVarUint32();
2516 if (!this.hasVarIntBytes()) {
2517 this._pos = pos;
2518 return false;
2519 }
2520 var index = this.readVarUint32();
2521 var addend;
2522 switch (type) {
2523 case RelocType.FunctionIndex_LEB:
2524 case RelocType.TableIndex_SLEB:
2525 case RelocType.TableIndex_I32:
2526 case RelocType.TypeIndex_LEB:
2527 case RelocType.GlobalIndex_LEB:
2528 break;
2529 case RelocType.GlobalAddr_LEB:
2530 case RelocType.GlobalAddr_SLEB:
2531 case RelocType.GlobalAddr_I32:
2532 if (!this.hasVarIntBytes()) {
2533 this._pos = pos;
2534 return false;
2535 }
2536 addend = this.readVarUint32();
2537 break;
2538 default:
2539 this.error = new Error(`Bad relocation type: ${type}`);
2540 this.state = BinaryReaderState.ERROR;
2541 return true;
2542 }
2543 this.state = BinaryReaderState.RELOC_SECTION_ENTRY;
2544 this.result = {
2545 type: type,
2546 offset: offset,
2547 index: index,
2548 addend: addend,
2549 };
2550 this._sectionEntriesLeft--;
2551 return true;
2552 }
2553
2554 private readCodeOperator_0xfb(): boolean {
2555 // The longest instructions have: 2 bytes opcode, 5 bytes type index,
2556 // 5 bytes field index.
2557 const MAX_CODE_OPERATOR_0XFB_SIZE = 12;
2558 if (!this._eof && !this.hasBytes(MAX_CODE_OPERATOR_0XFB_SIZE)) {
2559 return false;
2560 }
2561 var code, brDepth, refType, srcType, fieldIndex;
2562
2563 code = this._data[this._pos++] | 0xfb00;
2564 switch (code) {
2565 case OperatorCode.br_on_cast:
2566 case OperatorCode.br_on_cast_fail:
2567 case OperatorCode.br_on_func:
2568 case OperatorCode.br_on_non_func:
2569 case OperatorCode.br_on_data:
2570 case OperatorCode.br_on_non_data:
2571 case OperatorCode.br_on_i31:
2572 case OperatorCode.br_on_non_i31:
2573 brDepth = this.readVarUint32();
2574 break;
2575 case OperatorCode.br_on_cast_static:
2576 case OperatorCode.br_on_cast_static_fail:
2577 brDepth = this.readVarUint32();
2578 refType = this.readHeapType();
2579 break;
2580 case OperatorCode.array_get:
2581 case OperatorCode.array_get_s:
2582 case OperatorCode.array_get_u:
2583 case OperatorCode.array_len:
2584 case OperatorCode.array_set:
2585 case OperatorCode.array_new:
2586 case OperatorCode.array_new_with_rtt:
2587 case OperatorCode.array_new_default:
2588 case OperatorCode.array_new_default_with_rtt:
2589 case OperatorCode.struct_new:
2590 case OperatorCode.struct_new_with_rtt:
2591 case OperatorCode.struct_new_default:
2592 case OperatorCode.struct_new_default_with_rtt:
2593 case OperatorCode.rtt_canon:
2594 case OperatorCode.rtt_sub:
2595 case OperatorCode.rtt_fresh_sub:
2596 case OperatorCode.ref_test_static:
2597 case OperatorCode.ref_cast_static:
2598 refType = this.readHeapType();
2599 break;
2600 case OperatorCode.array_copy:
2601 refType = this.readHeapType();
2602 srcType = this.readHeapType();
2603 break;
2604 case OperatorCode.struct_get:
2605 case OperatorCode.struct_get_s:
2606 case OperatorCode.struct_get_u:
2607 case OperatorCode.struct_set:
2608 refType = this.readHeapType();
2609 fieldIndex = this.readVarUint32();
2610 break;
2611 case OperatorCode.array_init:
2612 case OperatorCode.array_init_static:
2613 refType = this.readHeapType();
2614 // This really is the "length" value. Overload "brDepth" to keep the
2615 // IOperatorInformation interface a little leaner.
2616 brDepth = this.readVarUint32();
2617 break;
2618 case OperatorCode.ref_is_func:
2619 case OperatorCode.ref_is_data:
2620 case OperatorCode.ref_is_i31:
2621 case OperatorCode.ref_as_func:
2622 case OperatorCode.ref_as_data:
2623 case OperatorCode.ref_as_i31:
2624 case OperatorCode.ref_test:
2625 case OperatorCode.ref_cast:
2626 case OperatorCode.i31_new:
2627 case OperatorCode.i31_get_s:
2628 case OperatorCode.i31_get_u:
2629 break;
2630 default:
2631 this.error = new Error(
2632 `Unknown operator: 0x${code.toString(16).padStart(4, "0")}`
2633 );
2634 this.state = BinaryReaderState.ERROR;
2635 return true;
2636 }
2637 this.result = {
2638 code,
2639 blockType: undefined,
2640 refType,
2641 srcType,
2642 brDepth,
2643 brTable: undefined,
2644 tableIndex: undefined,
2645 funcIndex: undefined,
2646 typeIndex: undefined,
2647 localIndex: undefined,
2648 globalIndex: undefined,
2649 fieldIndex,
2650 memoryAddress: undefined,
2651 literal: undefined,
2652 segmentIndex: undefined,
2653 destinationIndex: undefined,
2654 lines: undefined,
2655 lineIndex: undefined,
2656 };
2657 return true;
2658 }
2659
2660 private readCodeOperator_0xfc(): boolean {
2661 if (!this.hasVarIntBytes()) {
2662 return false;
2663 }
2664 var code = this.readVarUint32() | 0xfc00;
2665 var reserved, segmentIndex, destinationIndex, tableIndex;
2666 switch (code) {
2667 case OperatorCode.i32_trunc_sat_f32_s:
2668 case OperatorCode.i32_trunc_sat_f32_u:
2669 case OperatorCode.i32_trunc_sat_f64_s:
2670 case OperatorCode.i32_trunc_sat_f64_u:
2671 case OperatorCode.i64_trunc_sat_f32_s:
2672 case OperatorCode.i64_trunc_sat_f32_u:
2673 case OperatorCode.i64_trunc_sat_f64_s:
2674 case OperatorCode.i64_trunc_sat_f64_u:
2675 break;
2676 case OperatorCode.memory_copy:
2677 // Currently memory index must be zero.
2678 reserved = this.readVarUint1();
2679 reserved = this.readVarUint1();
2680 break;
2681 case OperatorCode.memory_fill:
2682 reserved = this.readVarUint1();
2683 break;
2684 case OperatorCode.table_init:
2685 segmentIndex = this.readVarUint32();
2686 tableIndex = this.readVarUint32();
2687 break;
2688 case OperatorCode.table_copy:
2689 tableIndex = this.readVarUint32();
2690 destinationIndex = this.readVarUint32();
2691 break;
2692 case OperatorCode.table_grow:
2693 case OperatorCode.table_size:
2694 case OperatorCode.table_fill:
2695 tableIndex = this.readVarUint32();
2696 break;
2697 case OperatorCode.memory_init:
2698 segmentIndex = this.readVarUint32();
2699 reserved = this.readVarUint1();
2700 break;
2701 case OperatorCode.data_drop:
2702 case OperatorCode.elem_drop:
2703 segmentIndex = this.readVarUint32();
2704 break;
2705 default:
2706 this.error = new Error(
2707 `Unknown operator: 0x${code.toString(16).padStart(4, "0")}`
2708 );
2709 this.state = BinaryReaderState.ERROR;
2710 return true;
2711 }
2712 this.result = {
2713 code: code,
2714 blockType: undefined,
2715 selectType: undefined,
2716 refType: undefined,
2717 srcType: undefined,
2718 brDepth: undefined,
2719 brTable: undefined,
2720 funcIndex: undefined,
2721 typeIndex: undefined,
2722 tableIndex: tableIndex,
2723 localIndex: undefined,
2724 globalIndex: undefined,
2725 fieldIndex: undefined,
2726 memoryAddress: undefined,
2727 literal: undefined,
2728 segmentIndex: segmentIndex,
2729 destinationIndex: destinationIndex,
2730 lines: undefined,
2731 lineIndex: undefined,
2732 };
2733 return true;
2734 }
2735
2736 private readCodeOperator_0xfd(): boolean {
2737 const MAX_CODE_OPERATOR_0XFD_SIZE = 17;
2738 var pos = this._pos;
2739 if (!this._eof && pos + MAX_CODE_OPERATOR_0XFD_SIZE > this._length) {
2740 return false;
2741 }
2742 if (!this.hasVarIntBytes()) {
2743 return false;
2744 }
2745 var code = this.readVarUint32() | 0xfd00;
2746 var memoryAddress;
2747 var literal;
2748 var lineIndex;
2749 var lines;
2750 switch (code) {
2751 case OperatorCode.v128_load:
2752 case OperatorCode.i16x8_load8x8_s:
2753 case OperatorCode.i16x8_load8x8_u:
2754 case OperatorCode.i32x4_load16x4_s:
2755 case OperatorCode.i32x4_load16x4_u:
2756 case OperatorCode.i64x2_load32x2_s:
2757 case OperatorCode.i64x2_load32x2_u:
2758 case OperatorCode.v8x16_load_splat:
2759 case OperatorCode.v16x8_load_splat:
2760 case OperatorCode.v32x4_load_splat:
2761 case OperatorCode.v64x2_load_splat:
2762 case OperatorCode.v128_store:
2763 case OperatorCode.v128_load32_zero:
2764 case OperatorCode.v128_load64_zero:
2765 memoryAddress = this.readMemoryImmediate();
2766 break;
2767 case OperatorCode.v128_const:
2768 literal = this.readBytes(16);
2769 break;
2770 case OperatorCode.i8x16_shuffle:
2771 lines = new Uint8Array(16);
2772 for (var i = 0; i < lines.length; i++) {
2773 lines[i] = this.readUint8();
2774 }
2775 break;
2776 case OperatorCode.i8x16_extract_lane_s:
2777 case OperatorCode.i8x16_extract_lane_u:
2778 case OperatorCode.i8x16_replace_lane:
2779 case OperatorCode.i16x8_extract_lane_s:
2780 case OperatorCode.i16x8_extract_lane_u:
2781 case OperatorCode.i16x8_replace_lane:
2782 case OperatorCode.i32x4_extract_lane:
2783 case OperatorCode.i32x4_replace_lane:
2784 case OperatorCode.i64x2_extract_lane:
2785 case OperatorCode.i64x2_replace_lane:
2786 case OperatorCode.f32x4_extract_lane:
2787 case OperatorCode.f32x4_replace_lane:
2788 case OperatorCode.f64x2_extract_lane:
2789 case OperatorCode.f64x2_replace_lane:
2790 lineIndex = this.readUint8();
2791 break;
2792 case OperatorCode.i8x16_swizzle:
2793 case OperatorCode.i8x16_splat:
2794 case OperatorCode.i16x8_splat:
2795 case OperatorCode.i32x4_splat:
2796 case OperatorCode.i64x2_splat:
2797 case OperatorCode.f32x4_splat:
2798 case OperatorCode.f64x2_splat:
2799 case OperatorCode.i8x16_eq:
2800 case OperatorCode.i8x16_ne:
2801 case OperatorCode.i8x16_lt_s:
2802 case OperatorCode.i8x16_lt_u:
2803 case OperatorCode.i8x16_gt_s:
2804 case OperatorCode.i8x16_gt_u:
2805 case OperatorCode.i8x16_le_s:
2806 case OperatorCode.i8x16_le_u:
2807 case OperatorCode.i8x16_ge_s:
2808 case OperatorCode.i8x16_ge_u:
2809 case OperatorCode.i16x8_eq:
2810 case OperatorCode.i16x8_ne:
2811 case OperatorCode.i16x8_lt_s:
2812 case OperatorCode.i16x8_lt_u:
2813 case OperatorCode.i16x8_gt_s:
2814 case OperatorCode.i16x8_gt_u:
2815 case OperatorCode.i16x8_le_s:
2816 case OperatorCode.i16x8_le_u:
2817 case OperatorCode.i16x8_ge_s:
2818 case OperatorCode.i16x8_ge_u:
2819 case OperatorCode.i32x4_eq:
2820 case OperatorCode.i32x4_ne:
2821 case OperatorCode.i32x4_lt_s:
2822 case OperatorCode.i32x4_lt_u:
2823 case OperatorCode.i32x4_gt_s:
2824 case OperatorCode.i32x4_gt_u:
2825 case OperatorCode.i32x4_le_s:
2826 case OperatorCode.i32x4_le_u:
2827 case OperatorCode.i32x4_ge_s:
2828 case OperatorCode.i32x4_ge_u:
2829 case OperatorCode.f32x4_eq:
2830 case OperatorCode.f32x4_ne:
2831 case OperatorCode.f32x4_lt:
2832 case OperatorCode.f32x4_gt:
2833 case OperatorCode.f32x4_le:
2834 case OperatorCode.f32x4_ge:
2835 case OperatorCode.f64x2_eq:
2836 case OperatorCode.f64x2_ne:
2837 case OperatorCode.f64x2_lt:
2838 case OperatorCode.f64x2_gt:
2839 case OperatorCode.f64x2_le:
2840 case OperatorCode.f64x2_ge:
2841 case OperatorCode.v128_not:
2842 case OperatorCode.v128_and:
2843 case OperatorCode.v128_andnot:
2844 case OperatorCode.v128_or:
2845 case OperatorCode.v128_xor:
2846 case OperatorCode.v128_bitselect:
2847 case OperatorCode.v128_any_true:
2848 case OperatorCode.f32x4_demote_f64x2_zero:
2849 case OperatorCode.f64x2_promote_low_f32x4:
2850 case OperatorCode.i8x16_abs:
2851 case OperatorCode.i8x16_neg:
2852 case OperatorCode.i8x16_popcnt:
2853 case OperatorCode.i8x16_all_true:
2854 case OperatorCode.i8x16_bitmask:
2855 case OperatorCode.i8x16_narrow_i16x8_s:
2856 case OperatorCode.i8x16_narrow_i16x8_u:
2857 case OperatorCode.f32x4_ceil:
2858 case OperatorCode.f32x4_floor:
2859 case OperatorCode.f32x4_trunc:
2860 case OperatorCode.f32x4_nearest:
2861 case OperatorCode.i8x16_shl:
2862 case OperatorCode.i8x16_shr_s:
2863 case OperatorCode.i8x16_shr_u:
2864 case OperatorCode.i8x16_add:
2865 case OperatorCode.i8x16_add_sat_s:
2866 case OperatorCode.i8x16_add_sat_u:
2867 case OperatorCode.i8x16_sub:
2868 case OperatorCode.i8x16_sub_sat_s:
2869 case OperatorCode.i8x16_sub_sat_u:
2870 case OperatorCode.f64x2_ceil:
2871 case OperatorCode.f64x2_floor:
2872 case OperatorCode.i8x16_min_s:
2873 case OperatorCode.i8x16_min_u:
2874 case OperatorCode.i8x16_max_s:
2875 case OperatorCode.i8x16_max_u:
2876 case OperatorCode.f64x2_trunc:
2877 case OperatorCode.i8x16_avgr_u:
2878 case OperatorCode.i16x8_extadd_pairwise_i8x16_s:
2879 case OperatorCode.i16x8_extadd_pairwise_i8x16_u:
2880 case OperatorCode.i32x4_extadd_pairwise_i16x8_s:
2881 case OperatorCode.i32x4_extadd_pairwise_i16x8_u:
2882 case OperatorCode.i16x8_abs:
2883 case OperatorCode.i16x8_neg:
2884 case OperatorCode.i16x8_q15mulr_sat_s:
2885 case OperatorCode.i16x8_all_true:
2886 case OperatorCode.i16x8_bitmask:
2887 case OperatorCode.i16x8_narrow_i32x4_s:
2888 case OperatorCode.i16x8_narrow_i32x4_u:
2889 case OperatorCode.i16x8_extend_low_i8x16_s:
2890 case OperatorCode.i16x8_extend_high_i8x16_s:
2891 case OperatorCode.i16x8_extend_low_i8x16_u:
2892 case OperatorCode.i16x8_extend_high_i8x16_u:
2893 case OperatorCode.i16x8_shl:
2894 case OperatorCode.i16x8_shr_s:
2895 case OperatorCode.i16x8_shr_u:
2896 case OperatorCode.i16x8_add:
2897 case OperatorCode.i16x8_add_sat_s:
2898 case OperatorCode.i16x8_add_sat_u:
2899 case OperatorCode.i16x8_sub:
2900 case OperatorCode.i16x8_sub_sat_s:
2901 case OperatorCode.i16x8_sub_sat_u:
2902 case OperatorCode.f64x2_nearest:
2903 case OperatorCode.i16x8_mul:
2904 case OperatorCode.i16x8_min_s:
2905 case OperatorCode.i16x8_min_u:
2906 case OperatorCode.i16x8_max_s:
2907 case OperatorCode.i16x8_max_u:
2908 case OperatorCode.i16x8_avgr_u:
2909 case OperatorCode.i16x8_extmul_low_i8x16_s:
2910 case OperatorCode.i16x8_extmul_high_i8x16_s:
2911 case OperatorCode.i16x8_extmul_low_i8x16_u:
2912 case OperatorCode.i16x8_extmul_high_i8x16_u:
2913 case OperatorCode.i32x4_abs:
2914 case OperatorCode.i32x4_neg:
2915 case OperatorCode.i32x4_all_true:
2916 case OperatorCode.i32x4_bitmask:
2917 case OperatorCode.i32x4_extend_low_i16x8_s:
2918 case OperatorCode.i32x4_extend_high_i16x8_s:
2919 case OperatorCode.i32x4_extend_low_i16x8_u:
2920 case OperatorCode.i32x4_extend_high_i16x8_u:
2921 case OperatorCode.i32x4_shl:
2922 case OperatorCode.i32x4_shr_s:
2923 case OperatorCode.i32x4_shr_u:
2924 case OperatorCode.i32x4_add:
2925 case OperatorCode.i32x4_sub:
2926 case OperatorCode.i32x4_mul:
2927 case OperatorCode.i32x4_min_s:
2928 case OperatorCode.i32x4_min_u:
2929 case OperatorCode.i32x4_max_s:
2930 case OperatorCode.i32x4_max_u:
2931 case OperatorCode.i32x4_dot_i16x8_s:
2932 case OperatorCode.i32x4_extmul_low_i16x8_s:
2933 case OperatorCode.i32x4_extmul_high_i16x8_s:
2934 case OperatorCode.i32x4_extmul_low_i16x8_u:
2935 case OperatorCode.i32x4_extmul_high_i16x8_u:
2936 case OperatorCode.i64x2_abs:
2937 case OperatorCode.i64x2_neg:
2938 case OperatorCode.i64x2_all_true:
2939 case OperatorCode.i64x2_bitmask:
2940 case OperatorCode.i64x2_extend_low_i32x4_s:
2941 case OperatorCode.i64x2_extend_high_i32x4_s:
2942 case OperatorCode.i64x2_extend_low_i32x4_u:
2943 case OperatorCode.i64x2_extend_high_i32x4_u:
2944 case OperatorCode.i64x2_shl:
2945 case OperatorCode.i64x2_shr_s:
2946 case OperatorCode.i64x2_shr_u:
2947 case OperatorCode.i64x2_add:
2948 case OperatorCode.i64x2_sub:
2949 case OperatorCode.i64x2_mul:
2950 case OperatorCode.i64x2_eq:
2951 case OperatorCode.i64x2_ne:
2952 case OperatorCode.i64x2_lt_s:
2953 case OperatorCode.i64x2_gt_s:
2954 case OperatorCode.i64x2_le_s:
2955 case OperatorCode.i64x2_ge_s:
2956 case OperatorCode.i64x2_extmul_low_i32x4_s:
2957 case OperatorCode.i64x2_extmul_high_i32x4_s:
2958 case OperatorCode.i64x2_extmul_low_i32x4_s:
2959 case OperatorCode.i64x2_extmul_high_i32x4_s:
2960 case OperatorCode.f32x4_abs:
2961 case OperatorCode.f32x4_abs:
2962 case OperatorCode.f32x4_neg:
2963 case OperatorCode.f32x4_sqrt:
2964 case OperatorCode.f32x4_add:
2965 case OperatorCode.f32x4_sub:
2966 case OperatorCode.f32x4_mul:
2967 case OperatorCode.f32x4_div:
2968 case OperatorCode.f32x4_min:
2969 case OperatorCode.f32x4_max:
2970 case OperatorCode.f32x4_pmin:
2971 case OperatorCode.f32x4_pmax:
2972 case OperatorCode.f64x2_abs:
2973 case OperatorCode.f64x2_neg:
2974 case OperatorCode.f64x2_sqrt:
2975 case OperatorCode.f64x2_add:
2976 case OperatorCode.f64x2_sub:
2977 case OperatorCode.f64x2_mul:
2978 case OperatorCode.f64x2_div:
2979 case OperatorCode.f64x2_min:
2980 case OperatorCode.f64x2_max:
2981 case OperatorCode.f64x2_pmin:
2982 case OperatorCode.f64x2_pmax:
2983 case OperatorCode.i32x4_trunc_sat_f32x4_s:
2984 case OperatorCode.i32x4_trunc_sat_f32x4_u:
2985 case OperatorCode.f32x4_convert_i32x4_s:
2986 case OperatorCode.f32x4_convert_i32x4_u:
2987 case OperatorCode.i32x4_trunc_sat_f64x2_s_zero:
2988 case OperatorCode.i32x4_trunc_sat_f64x2_u_zero:
2989 case OperatorCode.f64x2_convert_low_i32x4_s:
2990 case OperatorCode.f64x2_convert_low_i32x4_u:
2991 break;
2992 default:
2993 this.error = new Error(
2994 `Unknown operator: 0x${code.toString(16).padStart(4, "0")}`
2995 );
2996 this.state = BinaryReaderState.ERROR;
2997 return true;
2998 }
2999 this.result = {
3000 code: code,
3001 blockType: undefined,
3002 selectType: undefined,
3003 refType: undefined,
3004 srcType: undefined,
3005 brDepth: undefined,
3006 brTable: undefined,
3007 funcIndex: undefined,
3008 typeIndex: undefined,
3009 localIndex: undefined,
3010 globalIndex: undefined,
3011 fieldIndex: undefined,
3012 memoryAddress: memoryAddress,
3013 literal: literal,
3014 segmentIndex: undefined,
3015 destinationIndex: undefined,
3016 lines: lines,
3017 lineIndex: lineIndex,
3018 };
3019 return true;
3020 }
3021
3022 private readCodeOperator_0xfe(): boolean {
3023 const MAX_CODE_OPERATOR_0XFE_SIZE = 11;
3024 var pos = this._pos;
3025 if (!this._eof && pos + MAX_CODE_OPERATOR_0XFE_SIZE > this._length) {
3026 return false;
3027 }
3028 if (!this.hasVarIntBytes()) {
3029 return false;
3030 }
3031 var code = this.readVarUint32() | 0xfe00;
3032 var memoryAddress;
3033 switch (code) {
3034 case OperatorCode.atomic_notify:
3035 case OperatorCode.i32_atomic_wait:
3036 case OperatorCode.i64_atomic_wait:
3037 case OperatorCode.i32_atomic_load:
3038 case OperatorCode.i64_atomic_load:
3039 case OperatorCode.i32_atomic_load8_u:
3040 case OperatorCode.i32_atomic_load16_u:
3041 case OperatorCode.i64_atomic_load8_u:
3042 case OperatorCode.i64_atomic_load16_u:
3043 case OperatorCode.i64_atomic_load32_u:
3044 case OperatorCode.i32_atomic_store:
3045 case OperatorCode.i64_atomic_store:
3046 case OperatorCode.i32_atomic_store8:
3047 case OperatorCode.i32_atomic_store16:
3048 case OperatorCode.i64_atomic_store8:
3049 case OperatorCode.i64_atomic_store16:
3050 case OperatorCode.i64_atomic_store32:
3051 case OperatorCode.i32_atomic_rmw_add:
3052 case OperatorCode.i64_atomic_rmw_add:
3053 case OperatorCode.i32_atomic_rmw8_add_u:
3054 case OperatorCode.i32_atomic_rmw16_add_u:
3055 case OperatorCode.i64_atomic_rmw8_add_u:
3056 case OperatorCode.i64_atomic_rmw16_add_u:
3057 case OperatorCode.i64_atomic_rmw32_add_u:
3058 case OperatorCode.i32_atomic_rmw_sub:
3059 case OperatorCode.i64_atomic_rmw_sub:
3060 case OperatorCode.i32_atomic_rmw8_sub_u:
3061 case OperatorCode.i32_atomic_rmw16_sub_u:
3062 case OperatorCode.i64_atomic_rmw8_sub_u:
3063 case OperatorCode.i64_atomic_rmw16_sub_u:
3064 case OperatorCode.i64_atomic_rmw32_sub_u:
3065 case OperatorCode.i32_atomic_rmw_and:
3066 case OperatorCode.i64_atomic_rmw_and:
3067 case OperatorCode.i32_atomic_rmw8_and_u:
3068 case OperatorCode.i32_atomic_rmw16_and_u:
3069 case OperatorCode.i64_atomic_rmw8_and_u:
3070 case OperatorCode.i64_atomic_rmw16_and_u:
3071 case OperatorCode.i64_atomic_rmw32_and_u:
3072 case OperatorCode.i32_atomic_rmw_or:
3073 case OperatorCode.i64_atomic_rmw_or:
3074 case OperatorCode.i32_atomic_rmw8_or_u:
3075 case OperatorCode.i32_atomic_rmw16_or_u:
3076 case OperatorCode.i64_atomic_rmw8_or_u:
3077 case OperatorCode.i64_atomic_rmw16_or_u:
3078 case OperatorCode.i64_atomic_rmw32_or_u:
3079 case OperatorCode.i32_atomic_rmw_xor:
3080 case OperatorCode.i64_atomic_rmw_xor:
3081 case OperatorCode.i32_atomic_rmw8_xor_u:
3082 case OperatorCode.i32_atomic_rmw16_xor_u:
3083 case OperatorCode.i64_atomic_rmw8_xor_u:
3084 case OperatorCode.i64_atomic_rmw16_xor_u:
3085 case OperatorCode.i64_atomic_rmw32_xor_u:
3086 case OperatorCode.i32_atomic_rmw_xchg:
3087 case OperatorCode.i64_atomic_rmw_xchg:
3088 case OperatorCode.i32_atomic_rmw8_xchg_u:
3089 case OperatorCode.i32_atomic_rmw16_xchg_u:
3090 case OperatorCode.i64_atomic_rmw8_xchg_u:
3091 case OperatorCode.i64_atomic_rmw16_xchg_u:
3092 case OperatorCode.i64_atomic_rmw32_xchg_u:
3093 case OperatorCode.i32_atomic_rmw_cmpxchg:
3094 case OperatorCode.i64_atomic_rmw_cmpxchg:
3095 case OperatorCode.i32_atomic_rmw8_cmpxchg_u:
3096 case OperatorCode.i32_atomic_rmw16_cmpxchg_u:
3097 case OperatorCode.i64_atomic_rmw8_cmpxchg_u:
3098 case OperatorCode.i64_atomic_rmw16_cmpxchg_u:
3099 case OperatorCode.i64_atomic_rmw32_cmpxchg_u:
3100 memoryAddress = this.readMemoryImmediate();
3101 break;
3102 case OperatorCode.atomic_fence: {
3103 var consistency_model = this.readUint8();
3104 if (consistency_model != 0) {
3105 this.error = new Error("atomic.fence consistency model must be 0");
3106 this.state = BinaryReaderState.ERROR;
3107 return true;
3108 }
3109 break;
3110 }
3111 default:
3112 this.error = new Error(
3113 `Unknown operator: 0x${code.toString(16).padStart(4, "0")}`
3114 );
3115 this.state = BinaryReaderState.ERROR;
3116 return true;
3117 }
3118 this.result = {
3119 code: code,
3120 blockType: undefined,
3121 selectType: undefined,
3122 refType: undefined,
3123 srcType: undefined,
3124 brDepth: undefined,
3125 brTable: undefined,
3126 funcIndex: undefined,
3127 typeIndex: undefined,
3128 localIndex: undefined,
3129 globalIndex: undefined,
3130 fieldIndex: undefined,
3131 memoryAddress: memoryAddress,
3132 literal: undefined,
3133 segmentIndex: undefined,
3134 destinationIndex: undefined,
3135 lines: undefined,
3136 lineIndex: undefined,
3137 };
3138 return true;
3139 }
3140
3141 private readCodeOperator(): boolean {
3142 switch (this.state) {
3143 case BinaryReaderState.CODE_OPERATOR:
3144 if (this._pos >= this._functionRange.end) {
3145 this.skipFunctionBody();
3146 return this.read();
3147 }
3148 break;
3149 case BinaryReaderState.INIT_EXPRESSION_OPERATOR:
3150 if (
3151 this.result &&
3152 (<IOperatorInformation>this.result).code === OperatorCode.end
3153 ) {
3154 this.state = BinaryReaderState.END_INIT_EXPRESSION_BODY;
3155 this.result = null;
3156 return true;
3157 }
3158 break;
3159 case BinaryReaderState.OFFSET_EXPRESSION_OPERATOR:
3160 if (
3161 this.result &&
3162 (<IOperatorInformation>this.result).code === OperatorCode.end
3163 ) {
3164 this.state = BinaryReaderState.END_OFFSET_EXPRESSION_BODY;
3165 this.result = null;
3166 return true;
3167 }
3168 break;
3169 }
3170 var code,
3171 blockType,
3172 selectType,
3173 refType,
3174 brDepth,
3175 brTable,
3176 relativeDepth,
3177 funcIndex,
3178 typeIndex,
3179 tableIndex,
3180 localIndex,
3181 globalIndex,
3182 eventIndex,
3183 memoryAddress,
3184 literal,
3185 reserved;
3186 if (
3187 this.state === BinaryReaderState.INIT_EXPRESSION_OPERATOR &&
3188 this._sectionId === SectionCode.Element &&
3189 isExternvalElementSegmentType(this._segmentType)
3190 ) {
3191 // We are reading a `vec(funcidx)` here, which is a dense encoding
3192 // for a sequence of `((ref.func y) end)` instructions.
3193 if (
3194 this.result &&
3195 (<IOperatorInformation>this.result).code === OperatorCode.ref_func
3196 ) {
3197 code = OperatorCode.end;
3198 } else {
3199 if (!this.hasVarIntBytes()) return false;
3200 code = OperatorCode.ref_func;
3201 funcIndex = this.readVarUint32();
3202 }
3203 } else {
3204 const MAX_CODE_OPERATOR_SIZE = 11; // i64.const or load/store
3205 var pos = this._pos;
3206 if (!this._eof && pos + MAX_CODE_OPERATOR_SIZE > this._length) {
3207 return false;
3208 }
3209 code = this._data[this._pos++];
3210 switch (code) {
3211 case OperatorCode.block:
3212 case OperatorCode.loop:
3213 case OperatorCode.if:
3214 case OperatorCode.try:
3215 blockType = this.readBlockType();
3216 break;
3217 case OperatorCode.br:
3218 case OperatorCode.br_if:
3219 case OperatorCode.br_on_null:
3220 case OperatorCode.br_on_non_null:
3221 brDepth = this.readVarUint32();
3222 break;
3223 case OperatorCode.br_table:
3224 var tableCount = this.readVarUint32();
3225 if (!this.hasBytes(tableCount + 1)) {
3226 // We need at least (tableCount + 1) bytes
3227 this._pos = pos;
3228 return false;
3229 }
3230 brTable = [];
3231 for (var i = 0; i <= tableCount; i++) {
3232 // including default
3233 if (!this.hasVarIntBytes()) {
3234 this._pos = pos;
3235 return false;
3236 }
3237 brTable.push(this.readVarUint32());
3238 }
3239 break;
3240 case OperatorCode.rethrow:
3241 case OperatorCode.delegate:
3242 relativeDepth = this.readVarUint32();
3243 break;
3244 case OperatorCode.catch:
3245 case OperatorCode.throw:
3246 eventIndex = this.readVarInt32();
3247 break;
3248 case OperatorCode.ref_null:
3249 refType = this.readHeapType();
3250 break;
3251 case OperatorCode.call:
3252 case OperatorCode.return_call:
3253 case OperatorCode.ref_func:
3254 funcIndex = this.readVarUint32();
3255 break;
3256 case OperatorCode.call_indirect:
3257 case OperatorCode.return_call_indirect:
3258 typeIndex = this.readVarUint32();
3259 reserved = this.readVarUint1();
3260 break;
3261 case OperatorCode.local_get:
3262 case OperatorCode.local_set:
3263 case OperatorCode.local_tee:
3264 localIndex = this.readVarUint32();
3265 break;
3266 case OperatorCode.global_get:
3267 case OperatorCode.global_set:
3268 globalIndex = this.readVarUint32();
3269 break;
3270 case OperatorCode.table_get:
3271 case OperatorCode.table_set:
3272 tableIndex = this.readVarUint32();
3273 break;
3274 case OperatorCode.i32_load:
3275 case OperatorCode.i64_load:
3276 case OperatorCode.f32_load:
3277 case OperatorCode.f64_load:
3278 case OperatorCode.i32_load8_s:
3279 case OperatorCode.i32_load8_u:
3280 case OperatorCode.i32_load16_s:
3281 case OperatorCode.i32_load16_u:
3282 case OperatorCode.i64_load8_s:
3283 case OperatorCode.i64_load8_u:
3284 case OperatorCode.i64_load16_s:
3285 case OperatorCode.i64_load16_u:
3286 case OperatorCode.i64_load32_s:
3287 case OperatorCode.i64_load32_u:
3288 case OperatorCode.i32_store:
3289 case OperatorCode.i64_store:
3290 case OperatorCode.f32_store:
3291 case OperatorCode.f64_store:
3292 case OperatorCode.i32_store8:
3293 case OperatorCode.i32_store16:
3294 case OperatorCode.i64_store8:
3295 case OperatorCode.i64_store16:
3296 case OperatorCode.i64_store32:
3297 memoryAddress = this.readMemoryImmediate();
3298 break;
3299 case OperatorCode.current_memory:
3300 case OperatorCode.grow_memory:
3301 reserved = this.readVarUint1();
3302 break;
3303 case OperatorCode.i32_const:
3304 literal = this.readVarInt32();
3305 break;
3306 case OperatorCode.i64_const:
3307 literal = this.readVarInt64();
3308 break;
3309 case OperatorCode.f32_const:
3310 literal = new DataView(
3311 this._data.buffer,
3312 this._data.byteOffset
3313 ).getFloat32(this._pos, true);
3314 this._pos += 4;
3315 break;
3316 case OperatorCode.f64_const:
3317 literal = new DataView(
3318 this._data.buffer,
3319 this._data.byteOffset
3320 ).getFloat64(this._pos, true);
3321 this._pos += 8;
3322 break;
3323 case OperatorCode.select_with_type:
3324 const num_types = this.readVarInt32();
3325 // Only 1 is a valid value currently.
3326 if (num_types == 1) {
3327 selectType = this.readType();
3328 }
3329 break;
3330 case OperatorCode.prefix_0xfb:
3331 if (this.readCodeOperator_0xfb()) {
3332 return true;
3333 }
3334 this._pos = pos;
3335 return false;
3336 case OperatorCode.prefix_0xfc:
3337 if (this.readCodeOperator_0xfc()) {
3338 return true;
3339 }
3340 this._pos = pos;
3341 return false;
3342 case OperatorCode.prefix_0xfd:
3343 if (this.readCodeOperator_0xfd()) {
3344 return true;
3345 }
3346 this._pos = pos;
3347 return false;
3348 case OperatorCode.prefix_0xfe:
3349 if (this.readCodeOperator_0xfe()) {
3350 return true;
3351 }
3352 this._pos = pos;
3353 return false;
3354 case OperatorCode.unreachable:
3355 case OperatorCode.nop:
3356 case OperatorCode.else:
3357 case OperatorCode.unwind:
3358 case OperatorCode.end:
3359 case OperatorCode.return:
3360 case OperatorCode.catch_all:
3361 case OperatorCode.drop:
3362 case OperatorCode.select:
3363 case OperatorCode.i32_eqz:
3364 case OperatorCode.i32_eq:
3365 case OperatorCode.i32_ne:
3366 case OperatorCode.i32_lt_s:
3367 case OperatorCode.i32_lt_u:
3368 case OperatorCode.i32_gt_s:
3369 case OperatorCode.i32_gt_u:
3370 case OperatorCode.i32_le_s:
3371 case OperatorCode.i32_le_u:
3372 case OperatorCode.i32_ge_s:
3373 case OperatorCode.i32_ge_u:
3374 case OperatorCode.i64_eqz:
3375 case OperatorCode.i64_eq:
3376 case OperatorCode.i64_ne:
3377 case OperatorCode.i64_lt_s:
3378 case OperatorCode.i64_lt_u:
3379 case OperatorCode.i64_gt_s:
3380 case OperatorCode.i64_gt_u:
3381 case OperatorCode.i64_le_s:
3382 case OperatorCode.i64_le_u:
3383 case OperatorCode.i64_ge_s:
3384 case OperatorCode.i64_ge_u:
3385 case OperatorCode.f32_eq:
3386 case OperatorCode.f32_ne:
3387 case OperatorCode.f32_lt:
3388 case OperatorCode.f32_gt:
3389 case OperatorCode.f32_le:
3390 case OperatorCode.f32_ge:
3391 case OperatorCode.f64_eq:
3392 case OperatorCode.f64_ne:
3393 case OperatorCode.f64_lt:
3394 case OperatorCode.f64_gt:
3395 case OperatorCode.f64_le:
3396 case OperatorCode.f64_ge:
3397 case OperatorCode.i32_clz:
3398 case OperatorCode.i32_ctz:
3399 case OperatorCode.i32_popcnt:
3400 case OperatorCode.i32_add:
3401 case OperatorCode.i32_sub:
3402 case OperatorCode.i32_mul:
3403 case OperatorCode.i32_div_s:
3404 case OperatorCode.i32_div_u:
3405 case OperatorCode.i32_rem_s:
3406 case OperatorCode.i32_rem_u:
3407 case OperatorCode.i32_and:
3408 case OperatorCode.i32_or:
3409 case OperatorCode.i32_xor:
3410 case OperatorCode.i32_shl:
3411 case OperatorCode.i32_shr_s:
3412 case OperatorCode.i32_shr_u:
3413 case OperatorCode.i32_rotl:
3414 case OperatorCode.i32_rotr:
3415 case OperatorCode.i64_clz:
3416 case OperatorCode.i64_ctz:
3417 case OperatorCode.i64_popcnt:
3418 case OperatorCode.i64_add:
3419 case OperatorCode.i64_sub:
3420 case OperatorCode.i64_mul:
3421 case OperatorCode.i64_div_s:
3422 case OperatorCode.i64_div_u:
3423 case OperatorCode.i64_rem_s:
3424 case OperatorCode.i64_rem_u:
3425 case OperatorCode.i64_and:
3426 case OperatorCode.i64_or:
3427 case OperatorCode.i64_xor:
3428 case OperatorCode.i64_shl:
3429 case OperatorCode.i64_shr_s:
3430 case OperatorCode.i64_shr_u:
3431 case OperatorCode.i64_rotl:
3432 case OperatorCode.i64_rotr:
3433 case OperatorCode.f32_abs:
3434 case OperatorCode.f32_neg:
3435 case OperatorCode.f32_ceil:
3436 case OperatorCode.f32_floor:
3437 case OperatorCode.f32_trunc:
3438 case OperatorCode.f32_nearest:
3439 case OperatorCode.f32_sqrt:
3440 case OperatorCode.f32_add:
3441 case OperatorCode.f32_sub:
3442 case OperatorCode.f32_mul:
3443 case OperatorCode.f32_div:
3444 case OperatorCode.f32_min:
3445 case OperatorCode.f32_max:
3446 case OperatorCode.f32_copysign:
3447 case OperatorCode.f64_abs:
3448 case OperatorCode.f64_neg:
3449 case OperatorCode.f64_ceil:
3450 case OperatorCode.f64_floor:
3451 case OperatorCode.f64_trunc:
3452 case OperatorCode.f64_nearest:
3453 case OperatorCode.f64_sqrt:
3454 case OperatorCode.f64_add:
3455 case OperatorCode.f64_sub:
3456 case OperatorCode.f64_mul:
3457 case OperatorCode.f64_div:
3458 case OperatorCode.f64_min:
3459 case OperatorCode.f64_max:
3460 case OperatorCode.f64_copysign:
3461 case OperatorCode.i32_wrap_i64:
3462 case OperatorCode.i32_trunc_f32_s:
3463 case OperatorCode.i32_trunc_f32_u:
3464 case OperatorCode.i32_trunc_f64_s:
3465 case OperatorCode.i32_trunc_f64_u:
3466 case OperatorCode.i64_extend_i32_s:
3467 case OperatorCode.i64_extend_i32_u:
3468 case OperatorCode.i64_trunc_f32_s:
3469 case OperatorCode.i64_trunc_f32_u:
3470 case OperatorCode.i64_trunc_f64_s:
3471 case OperatorCode.i64_trunc_f64_u:
3472 case OperatorCode.f32_convert_i32_s:
3473 case OperatorCode.f32_convert_i32_u:
3474 case OperatorCode.f32_convert_i64_s:
3475 case OperatorCode.f32_convert_i64_u:
3476 case OperatorCode.f32_demote_f64:
3477 case OperatorCode.f64_convert_i32_s:
3478 case OperatorCode.f64_convert_i32_u:
3479 case OperatorCode.f64_convert_i64_s:
3480 case OperatorCode.f64_convert_i64_u:
3481 case OperatorCode.f64_promote_f32:
3482 case OperatorCode.i32_reinterpret_f32:
3483 case OperatorCode.i64_reinterpret_f64:
3484 case OperatorCode.f32_reinterpret_i32:
3485 case OperatorCode.f64_reinterpret_i64:
3486 case OperatorCode.i32_extend8_s:
3487 case OperatorCode.i32_extend16_s:
3488 case OperatorCode.i64_extend8_s:
3489 case OperatorCode.i64_extend16_s:
3490 case OperatorCode.i64_extend32_s:
3491 case OperatorCode.call_ref:
3492 case OperatorCode.return_call_ref:
3493 case OperatorCode.ref_is_null:
3494 case OperatorCode.ref_as_non_null:
3495 case OperatorCode.ref_eq:
3496 break;
3497 default:
3498 this.error = new Error(`Unknown operator: ${code}`);
3499 this.state = BinaryReaderState.ERROR;
3500 return true;
3501 }
3502 }
3503 this.result = {
3504 code,
3505 blockType,
3506 selectType,
3507 refType,
3508 srcType: undefined,
3509 brDepth,
3510 brTable,
3511 relativeDepth,
3512 tableIndex,
3513 funcIndex,
3514 typeIndex,
3515 localIndex,
3516 globalIndex,
3517 fieldIndex: undefined,
3518 eventIndex,
3519 memoryAddress,
3520 literal,
3521 segmentIndex: undefined,
3522 destinationIndex: undefined,
3523 lines: undefined,
3524 lineIndex: undefined,
3525 };
3526 return true;
3527 }
3528 private readFunctionBody(): boolean {
3529 if (this._sectionEntriesLeft === 0) {
3530 this.skipSection();
3531 return this.read();
3532 }
3533 if (!this.hasVarIntBytes()) return false;
3534 var pos = this._pos;
3535 var size = this.readVarUint32();
3536 var bodyEnd = this._pos + size;
3537 if (!this.hasVarIntBytes()) {
3538 this._pos = pos;
3539 return false;
3540 }
3541 var localCount = this.readVarUint32();
3542 var locals: Array<ILocals> = [];
3543 for (var i = 0; i < localCount; i++) {
3544 if (!this.hasVarIntBytes()) {
3545 this._pos = pos;
3546 return false;
3547 }
3548 var count = this.readVarUint32();
3549 if (!this.hasVarIntBytes()) {
3550 this._pos = pos;
3551 return false;
3552 }
3553 var type = this.readType();
3554 locals.push({ count: count, type: type });
3555 }
3556 var bodyStart = this._pos;
3557 this.state = BinaryReaderState.BEGIN_FUNCTION_BODY;
3558 this.result = {
3559 locals: locals,
3560 };
3561 this._functionRange = new DataRange(bodyStart, bodyEnd);
3562 this._sectionEntriesLeft--;
3563 return true;
3564 }
3565 private readSectionHeader(): boolean {
3566 if (this._pos >= this._length && this._eof) {
3567 this._sectionId = SectionCode.Unknown;
3568 this._sectionRange = null;
3569 this.result = null;
3570 this.state = BinaryReaderState.END_WASM;
3571 return true;
3572 }
3573 // TODO: Handle _eof.
3574 if (this._pos < this._length - 4) {
3575 var magicNumber = this.peekInt32();
3576 if (magicNumber === WASM_MAGIC_NUMBER) {
3577 this._sectionId = SectionCode.Unknown;
3578 this._sectionRange = null;
3579 this.result = null;
3580 this.state = BinaryReaderState.END_WASM;
3581 return true;
3582 }
3583 }
3584 if (!this.hasVarIntBytes()) return false;
3585 var sectionStart = this._pos;
3586 var id = this.readVarUint7();
3587 if (!this.hasVarIntBytes()) {
3588 this._pos = sectionStart;
3589 return false;
3590 }
3591 var payloadLength = this.readVarUint32();
3592 var name = null;
3593 var payloadEnd = this._pos + payloadLength;
3594 if (id == 0) {
3595 if (!this.hasStringBytes()) {
3596 this._pos = sectionStart;
3597 return false;
3598 }
3599 name = this.readStringBytes();
3600 }
3601 this.result = { id: id, name: name };
3602 this._sectionId = id;
3603 this._sectionRange = new DataRange(this._pos, payloadEnd);
3604 this.state = BinaryReaderState.BEGIN_SECTION;
3605 return true;
3606 }
3607 private readSectionRawData(): boolean {
3608 var payloadLength = this._sectionRange.end - this._sectionRange.start;
3609 if (!this.hasBytes(payloadLength)) {
3610 return false;
3611 }
3612 this.state = BinaryReaderState.SECTION_RAW_DATA;
3613 this.result = this.readBytes(payloadLength);
3614 return true;
3615 }
3616 private readSectionBody(): boolean {
3617 if (this._pos >= this._sectionRange.end) {
3618 this.result = null;
3619 this.state = BinaryReaderState.END_SECTION;
3620 this._sectionId = SectionCode.Unknown;
3621 this._sectionRange = null;
3622 return true;
3623 }
3624 var currentSection = <ISectionInformation>this.result;
3625 switch (currentSection.id) {
3626 case SectionCode.Type:
3627 if (!this.hasSectionPayload()) return false;
3628 this._sectionEntriesLeft = this.readVarUint32();
3629 return this.readTypeEntry();
3630 case SectionCode.Import:
3631 if (!this.hasSectionPayload()) return false;
3632 this._sectionEntriesLeft = this.readVarUint32();
3633 return this.readImportEntry();
3634 case SectionCode.Export:
3635 if (!this.hasSectionPayload()) return false;
3636 this._sectionEntriesLeft = this.readVarUint32();
3637 return this.readExportEntry();
3638 case SectionCode.Function:
3639 if (!this.hasSectionPayload()) return false;
3640 this._sectionEntriesLeft = this.readVarUint32();
3641 return this.readFunctionEntry();
3642 case SectionCode.Table:
3643 if (!this.hasSectionPayload()) return false;
3644 this._sectionEntriesLeft = this.readVarUint32();
3645 return this.readTableEntry();
3646 case SectionCode.Memory:
3647 if (!this.hasSectionPayload()) return false;
3648 this._sectionEntriesLeft = this.readVarUint32();
3649 return this.readMemoryEntry();
3650 case SectionCode.Global:
3651 if (!this.hasVarIntBytes()) return false;
3652 this._sectionEntriesLeft = this.readVarUint32();
3653 return this.readGlobalEntry();
3654 case SectionCode.Start:
3655 if (!this.hasVarIntBytes()) return false;
3656 this.state = BinaryReaderState.START_SECTION_ENTRY;
3657 this.result = { index: this.readVarUint32() };
3658 return true;
3659 case SectionCode.Code:
3660 if (!this.hasVarIntBytes()) return false;
3661 this._sectionEntriesLeft = this.readVarUint32();
3662 this.state = BinaryReaderState.READING_FUNCTION_HEADER;
3663 return this.readFunctionBody();
3664 case SectionCode.Element:
3665 if (!this.hasVarIntBytes()) return false;
3666 this._sectionEntriesLeft = this.readVarUint32();
3667 return this.readElementEntry();
3668 case SectionCode.Data:
3669 if (!this.hasVarIntBytes()) return false;
3670 this._sectionEntriesLeft = this.readVarUint32();
3671 return this.readDataEntry();
3672 case SectionCode.Event:
3673 if (!this.hasVarIntBytes()) return false;
3674 this._sectionEntriesLeft = this.readVarUint32();
3675 return this.readEventEntry();
3676 case SectionCode.Custom:
3677 var customSectionName = bytesToString(currentSection.name);
3678 if (customSectionName === "name") {
3679 return this.readNameEntry();
3680 }
3681 if (customSectionName.indexOf("reloc.") === 0) {
3682 return this.readRelocHeader();
3683 }
3684 if (customSectionName === "linking") {
3685 if (!this.hasVarIntBytes()) return false;
3686 this._sectionEntriesLeft = this.readVarUint32();
3687 return this.readLinkingEntry();
3688 }
3689 if (customSectionName === "sourceMappingURL") {
3690 return this.readSourceMappingURL();
3691 }
3692 return this.readSectionRawData();
3693 default:
3694 this.error = new Error(`Unsupported section: ${this._sectionId}`);
3695 this.state = BinaryReaderState.ERROR;
3696 return true;
3697 }
3698 }
3699 public read(): boolean {
3700 switch (this.state) {
3701 case BinaryReaderState.INITIAL:
3702 if (!this.hasBytes(8)) return false;
3703 var magicNumber = this.readUint32();
3704 if (magicNumber != WASM_MAGIC_NUMBER) {
3705 this.error = new Error("Bad magic number");
3706 this.state = BinaryReaderState.ERROR;
3707 return true;
3708 }
3709 var version = this.readUint32();
3710 if (
3711 version != WASM_SUPPORTED_VERSION &&
3712 version != WASM_SUPPORTED_EXPERIMENTAL_VERSION
3713 ) {
3714 this.error = new Error(`Bad version number ${version}`);
3715 this.state = BinaryReaderState.ERROR;
3716 return true;
3717 }
3718 this.result = { magicNumber: magicNumber, version: version };
3719 this.state = BinaryReaderState.BEGIN_WASM;
3720 return true;
3721 case BinaryReaderState.END_WASM:
3722 this.result = null;
3723 this.state = BinaryReaderState.BEGIN_WASM;
3724 if (this.hasMoreBytes()) {
3725 this.state = BinaryReaderState.INITIAL;
3726 return this.read();
3727 }
3728 return false;
3729 case BinaryReaderState.ERROR:
3730 return true;
3731 case BinaryReaderState.BEGIN_WASM:
3732 case BinaryReaderState.END_SECTION:
3733 return this.readSectionHeader();
3734 case BinaryReaderState.BEGIN_SECTION:
3735 return this.readSectionBody();
3736 case BinaryReaderState.SKIPPING_SECTION:
3737 if (!this.hasSectionPayload()) {
3738 return false;
3739 }
3740 this.state = BinaryReaderState.END_SECTION;
3741 this._pos = this._sectionRange.end;
3742 this._sectionId = SectionCode.Unknown;
3743 this._sectionRange = null;
3744 this.result = null;
3745 return true;
3746 case BinaryReaderState.SKIPPING_FUNCTION_BODY:
3747 this.state = BinaryReaderState.END_FUNCTION_BODY;
3748 this._pos = this._functionRange.end;
3749 this._functionRange = null;
3750 this.result = null;
3751 return true;
3752 case BinaryReaderState.TYPE_SECTION_ENTRY:
3753 return this.readTypeEntry();
3754 case BinaryReaderState.IMPORT_SECTION_ENTRY:
3755 return this.readImportEntry();
3756 case BinaryReaderState.EXPORT_SECTION_ENTRY:
3757 return this.readExportEntry();
3758 case BinaryReaderState.FUNCTION_SECTION_ENTRY:
3759 return this.readFunctionEntry();
3760 case BinaryReaderState.TABLE_SECTION_ENTRY:
3761 return this.readTableEntry();
3762 case BinaryReaderState.MEMORY_SECTION_ENTRY:
3763 return this.readMemoryEntry();
3764 case BinaryReaderState.EVENT_SECTION_ENTRY:
3765 return this.readEventEntry();
3766 case BinaryReaderState.GLOBAL_SECTION_ENTRY:
3767 case BinaryReaderState.END_GLOBAL_SECTION_ENTRY:
3768 return this.readGlobalEntry();
3769 case BinaryReaderState.BEGIN_GLOBAL_SECTION_ENTRY:
3770 return this.readInitExpressionBody();
3771 case BinaryReaderState.ELEMENT_SECTION_ENTRY:
3772 case BinaryReaderState.END_ELEMENT_SECTION_ENTRY:
3773 return this.readElementEntry();
3774 case BinaryReaderState.BEGIN_ELEMENT_SECTION_ENTRY:
3775 if (isActiveElementSegmentType(this._segmentType)) {
3776 return this.readOffsetExpressionBody();
3777 } else {
3778 // passive or declared element segment
3779 return this.readElementEntryBody();
3780 }
3781 case BinaryReaderState.ELEMENT_SECTION_ENTRY_BODY:
3782 if (!this.hasVarIntBytes()) return false;
3783 this._segmentEntriesLeft = this.readVarUint32();
3784 if (this._segmentEntriesLeft === 0) {
3785 this.state = BinaryReaderState.END_ELEMENT_SECTION_ENTRY;
3786 this.result = null;
3787 return true;
3788 }
3789 return this.readInitExpressionBody();
3790 case BinaryReaderState.DATA_SECTION_ENTRY:
3791 case BinaryReaderState.END_DATA_SECTION_ENTRY:
3792 return this.readDataEntry();
3793 case BinaryReaderState.BEGIN_DATA_SECTION_ENTRY:
3794 if (isActiveDataSegmentType(this._segmentType)) {
3795 return this.readOffsetExpressionBody();
3796 } else {
3797 // passive data segment
3798 return this.readDataEntryBody();
3799 }
3800 case BinaryReaderState.DATA_SECTION_ENTRY_BODY:
3801 this.state = BinaryReaderState.END_DATA_SECTION_ENTRY;
3802 this.result = null;
3803 return true;
3804 case BinaryReaderState.END_INIT_EXPRESSION_BODY:
3805 switch (this._sectionId) {
3806 case SectionCode.Global:
3807 this.state = BinaryReaderState.END_GLOBAL_SECTION_ENTRY;
3808 return true;
3809 case SectionCode.Element:
3810 if (--this._segmentEntriesLeft > 0) {
3811 return this.readInitExpressionBody();
3812 }
3813 this.state = BinaryReaderState.END_ELEMENT_SECTION_ENTRY;
3814 this.result = null;
3815 return true;
3816 }
3817 this.error = new Error(`Unexpected section type: ${this._sectionId}`);
3818 this.state = BinaryReaderState.ERROR;
3819 return true;
3820 case BinaryReaderState.END_OFFSET_EXPRESSION_BODY:
3821 if (this._sectionId === SectionCode.Data) {
3822 return this.readDataEntryBody();
3823 } else {
3824 return this.readElementEntryBody();
3825 }
3826 case BinaryReaderState.NAME_SECTION_ENTRY:
3827 return this.readNameEntry();
3828 case BinaryReaderState.RELOC_SECTION_HEADER:
3829 if (!this.hasVarIntBytes()) return false;
3830 this._sectionEntriesLeft = this.readVarUint32();
3831 return this.readRelocEntry();
3832 case BinaryReaderState.LINKING_SECTION_ENTRY:
3833 return this.readLinkingEntry();
3834 case BinaryReaderState.SOURCE_MAPPING_URL:
3835 this.state = BinaryReaderState.END_SECTION;
3836 this.result = null;
3837 return true;
3838 case BinaryReaderState.RELOC_SECTION_ENTRY:
3839 return this.readRelocEntry();
3840 case BinaryReaderState.READING_FUNCTION_HEADER:
3841 case BinaryReaderState.END_FUNCTION_BODY:
3842 return this.readFunctionBody();
3843 case BinaryReaderState.BEGIN_FUNCTION_BODY:
3844 this.state = BinaryReaderState.CODE_OPERATOR;
3845 return this.readCodeOperator();
3846 case BinaryReaderState.BEGIN_INIT_EXPRESSION_BODY:
3847 this.state = BinaryReaderState.INIT_EXPRESSION_OPERATOR;
3848 return this.readCodeOperator();
3849 case BinaryReaderState.BEGIN_OFFSET_EXPRESSION_BODY:
3850 this.state = BinaryReaderState.OFFSET_EXPRESSION_OPERATOR;
3851 return this.readCodeOperator();
3852 case BinaryReaderState.CODE_OPERATOR:
3853 case BinaryReaderState.INIT_EXPRESSION_OPERATOR:
3854 case BinaryReaderState.OFFSET_EXPRESSION_OPERATOR:
3855 return this.readCodeOperator();
3856 case BinaryReaderState.READING_SECTION_RAW_DATA:
3857 return this.readSectionRawData();
3858 case BinaryReaderState.START_SECTION_ENTRY:
3859 case BinaryReaderState.SECTION_RAW_DATA:
3860 this.state = BinaryReaderState.END_SECTION;
3861 this.result = null;
3862 return true;
3863 default:
3864 this.error = new Error(`Unsupported state: ${this.state}`);
3865 this.state = BinaryReaderState.ERROR;
3866 return true;
3867 }
3868 }
3869 public skipSection(): void {
3870 if (
3871 this.state === BinaryReaderState.ERROR ||
3872 this.state === BinaryReaderState.INITIAL ||
3873 this.state === BinaryReaderState.END_SECTION ||
3874 this.state === BinaryReaderState.BEGIN_WASM ||
3875 this.state === BinaryReaderState.END_WASM
3876 )
3877 return;
3878 this.state = BinaryReaderState.SKIPPING_SECTION;
3879 }
3880 public skipFunctionBody(): void {
3881 if (
3882 this.state !== BinaryReaderState.BEGIN_FUNCTION_BODY &&
3883 this.state !== BinaryReaderState.CODE_OPERATOR
3884 )
3885 return;
3886 this.state = BinaryReaderState.SKIPPING_FUNCTION_BODY;
3887 }
3888 public skipInitExpression(): void {
3889 while (this.state === BinaryReaderState.INIT_EXPRESSION_OPERATOR)
3890 this.readCodeOperator();
3891 }
3892 public fetchSectionRawData(): void {
3893 if (this.state !== BinaryReaderState.BEGIN_SECTION) {
3894 this.error = new Error(`Unsupported state: ${this.state}`);
3895 this.state = BinaryReaderState.ERROR;
3896 return;
3897 }
3898 this.state = BinaryReaderState.READING_SECTION_RAW_DATA;
3899 }
3900}
3901
3902declare var escape: (string) => string;
3903declare class TextDecoder {
3904 public constructor(encoding: string);
3905 public decode(bytes: Uint8Array): string;
3906}
3907
3908export var bytesToString: (bytes: Uint8Array) => string;
3909if (typeof TextDecoder !== "undefined") {
3910 try {
3911 bytesToString = (function () {
3912 var utf8Decoder = new TextDecoder("utf-8");
3913 utf8Decoder.decode(new Uint8Array([97, 208, 144]));
3914 return (b) => utf8Decoder.decode(b);
3915 })();
3916 } catch (_) {
3917 /* ignore */
3918 }
3919}
3920if (!bytesToString) {
3921 bytesToString = (b) => {
3922 var str = String.fromCharCode.apply(null, b);
3923 return decodeURIComponent(escape(str));
3924 };
3925}
3926
3927export interface IBinaryReaderData {
3928 state: BinaryReaderState;
3929 result?: BinaryReaderResult;
3930}