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