Skip to content

Commit b2161e3

Browse files
authored
wasm2js: fix printing of negated negative constants (#2034)
It is invalid to print --5, we need to add a space - -5 so that it is valid JS to parse.
1 parent b8d93ea commit b2161e3

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/emscripten-optimizer/simple_ast.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,9 @@ struct JSPrinter {
986986
}
987987

988988
void printNum(Ref node) {
989+
if (node->getNumber() < 0 && buffer[used-1] == '-') {
990+
emit(' '); // cannot join - and - to --, looks like the -- operator
991+
}
989992
emit(numToString(node->getNumber(), finalize));
990993
}
991994

test/wasm2js/minus_minus.2asm.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
function asmFunc(global, env, buffer) {
3+
"almost asm";
4+
var HEAP8 = new global.Int8Array(buffer);
5+
var HEAP16 = new global.Int16Array(buffer);
6+
var HEAP32 = new global.Int32Array(buffer);
7+
var HEAPU8 = new global.Uint8Array(buffer);
8+
var HEAPU16 = new global.Uint16Array(buffer);
9+
var HEAPU32 = new global.Uint32Array(buffer);
10+
var HEAPF32 = new global.Float32Array(buffer);
11+
var HEAPF64 = new global.Float64Array(buffer);
12+
var Math_imul = global.Math.imul;
13+
var Math_fround = global.Math.fround;
14+
var Math_abs = global.Math.abs;
15+
var Math_clz32 = global.Math.clz32;
16+
var Math_min = global.Math.min;
17+
var Math_max = global.Math.max;
18+
var Math_floor = global.Math.floor;
19+
var Math_ceil = global.Math.ceil;
20+
var Math_sqrt = global.Math.sqrt;
21+
var abort = env.abort;
22+
var nan = global.NaN;
23+
var infinity = global.Infinity;
24+
function $0() {
25+
return ~~- -7094.0 | 0;
26+
}
27+
28+
function $1() {
29+
$0() | 0;
30+
}
31+
32+
var FUNCTION_TABLE = [];
33+
return {
34+
func_44_invoker: $1
35+
};
36+
}
37+
38+
const memasmFunc = new ArrayBuffer(65536);
39+
const retasmFunc = asmFunc({Math,Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,NaN,Infinity}, {abort:function() { throw new Error('abort'); }},memasmFunc);
40+
export const func_44_invoker = retasmFunc.func_44_invoker;

test/wasm2js/minus_minus.wast

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(module
2+
(type $0 (func (result i32)))
3+
(type $1 (func))
4+
(export "func_44_invoker" (func $1))
5+
(func $0 (; 0 ;) (type $0) (result i32)
6+
(i32.trunc_f64_s
7+
(f64.neg
8+
(f64.const -7094) ;; negation of a negative must not be emitted as "--" in js, that will not parse
9+
)
10+
)
11+
)
12+
(func $1 (; 1 ;) (type $1)
13+
(drop
14+
(call $0)
15+
)
16+
)
17+
)
18+

0 commit comments

Comments
 (0)