porffor 0.17.0-f43ba190c → 0.18.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,6 +66,42 @@ export default wasm => {
66
66
  };
67
67
 
68
68
  switch (opcode) {
69
+ case Opcodes.if: {
70
+ if (stack.length < 1) { empty(); break; }
71
+ const cond = bool(pop());
72
+
73
+ // find else split and end
74
+ let j = i + 1;
75
+ let depth = 0, elseStart = 0;
76
+ for (; j < wasm.length; j++) {
77
+ const op = wasm[j][0];
78
+ if (op === Opcodes.if || op === Opcodes.block || op === Opcodes.loop || op === Opcodes.try) depth++;
79
+ if (op === Opcodes.else && depth === 0) elseStart = j;
80
+ if (op === Opcodes.end) {
81
+ depth--;
82
+ if (depth < 0) break;
83
+ }
84
+ if (op === Opcodes.br || op === Opcodes.br_if) wasm[j][1] -= 1;
85
+ }
86
+
87
+ if (cond) {
88
+ // remove else if it exists, or just remove end
89
+ if (elseStart) wasm.splice(elseStart, j - elseStart + 1);
90
+ else wasm.splice(j, 1);
91
+ } else {
92
+ // remove truthy conseq and keep else if it exists, or just remove entire thing
93
+ if (elseStart) {
94
+ wasm.splice(j, 1); // remove end
95
+ wasm.splice(i + 1, elseStart - i + 1); // remove truthy conseq
96
+ } else wasm.splice(i + 1, j - i + 0); // no else, remove entire if
97
+ }
98
+
99
+ // remove this if op
100
+ wasm.splice(i, 1);
101
+
102
+ break;
103
+ }
104
+
69
105
  case Opcodes.i32_const: {
70
106
  const n = read_signedLEB128(op.slice(1));
71
107
  push(n);
@@ -70,7 +70,7 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
70
70
  } else if (inst[0] === Opcodes.i32_load || inst[0] === Opcodes.i64_load || inst[0] === Opcodes.f64_load || inst[0] === Opcodes.i32_store || inst[0] === Opcodes.i64_store || inst[0] === Opcodes.f64_store || inst[0] === Opcodes.i32_store16 || inst[0] === Opcodes.i32_load16_u) {
71
71
  out += ` ${inst[1]} ${read_unsignedLEB128(inst.slice(2))}`;
72
72
  } else for (const operand of inst.slice(1)) {
73
- if (inst[0] === Opcodes.if || inst[0] === Opcodes.loop || inst[0] === Opcodes.block) {
73
+ if (inst[0] === Opcodes.if || inst[0] === Opcodes.loop || inst[0] === Opcodes.block || inst[0] === Opcodes.try) {
74
74
  if (operand === Blocktype.void) continue;
75
75
  out += ` ${invValtype[operand]}`;
76
76
  } else {
@@ -80,7 +80,7 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
80
80
 
81
81
  if (comments.length > 0) out += ` ;; ${comments.join(' ')}`;
82
82
 
83
- if (inst[0] === Opcodes.if || inst[0] === Opcodes.loop || inst[0] === Opcodes.block || inst[0] === Opcodes.else) {
83
+ if (inst[0] === Opcodes.if || inst[0] === Opcodes.loop || inst[0] === Opcodes.block || inst[0] === Opcodes.else || inst[0] === Opcodes.try || inst[0] === Opcodes.catch_all) {
84
84
  out += ` ;; label @${depth}`;
85
85
  }
86
86