diff options
Diffstat (limited to 'src/wasm2js.h')
-rw-r--r-- | src/wasm2js.h | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/wasm2js.h b/src/wasm2js.h index 3accfad75..33f70a4fc 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -1146,6 +1146,14 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, } } } + // Ensure the function pointer is a number. In general in wasm2js we are + // ok with true/false being present, as they are immediately cast to a + // number anyhow on their use. However, FUNCTION_TABLE[true] is *not* the + // same as FUNCTION_TABLE[1], so we must cast. This is a rare exception + // because FUNCTION_TABLE is just a normal JS object, not a typed array + // or a mathematical operation (all of which coerce to a number for us). + auto target = visit(curr->target, EXPRESSION_RESULT); + target = makeAsmCoercion(target, ASM_INT); if (mustReorder) { Ref ret; ScopedTemp idx(Type::i32, parent, func); @@ -1155,7 +1163,9 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, IString temp = temps.back()->temp; sequenceAppend(ret, visitAndAssign(operand, temp)); } - sequenceAppend(ret, visitAndAssign(curr->target, idx)); + sequenceAppend(ret, + ValueBuilder::makeBinary( + ValueBuilder::makeName(idx.getName()), SET, target)); Ref theCall = ValueBuilder::makeCall(ValueBuilder::makeSub( ValueBuilder::makeName(FUNCTION_TABLE), idx.getAstName())); for (size_t i = 0; i < temps.size(); i++) { @@ -1172,9 +1182,8 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m, return ret; } else { // Target has no side effects, emit simple code - Ref theCall = ValueBuilder::makeCall( - ValueBuilder::makeSub(ValueBuilder::makeName(FUNCTION_TABLE), - visit(curr->target, EXPRESSION_RESULT))); + Ref theCall = ValueBuilder::makeCall(ValueBuilder::makeSub( + ValueBuilder::makeName(FUNCTION_TABLE), target)); for (auto* operand : curr->operands) { theCall[2]->push_back(visit(operand, EXPRESSION_RESULT)); } |