Skip to content

Commit 2bd1fb6

Browse files
committedDec 20, 2024
Enable BigInt for binaryen.js
We previously had a build option for enabling BigInt support when compiling with Emscripten, but the test suite did not actually pass with it enabled. The problem was that the binaryen.js glue code assumed that C API functions that took i64 parameters would be transformed to take a pair of i32 parameters instead, but this assumption was incorrect when BigInt support was enabled. Emscripten has now enabled BigInt by default, so update binaryen.js to use BigInt as well. Fix the JS API glue code to pass i64s as BigInts rather than pairs of numbers and fix the tests accordingly. Also fix some other small problems with the tests that only show up in debug builds. Resolves #7163.
1 parent dcec348 commit 2bd1fb6

File tree

5 files changed

+43
-51
lines changed

5 files changed

+43
-51
lines changed
 

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ full changeset diff at the end of each section.
1515
Current Trunk
1616
-------------
1717

18+
- Binaryen.js now builds with BigInt support. The i64.const and f64.const_bits
19+
functions now take BigInt parameters rather than pairs of numbers.
20+
1821
v121
1922
----
2023

‎CMakeLists.txt

+3-14
Original file line numberDiff line numberDiff line change
@@ -326,20 +326,9 @@ else()
326326
endif()
327327

328328
if(EMSCRIPTEN)
329-
# Note: to debug with DWARF you will usually want to enable BIGINT support, as
330-
# that helps avoid running Binaryen on the wasm after link. Binaryen's DWARF
331-
# rewriting has known limitations, so avoiding it during link is recommended
332-
# where possible (like local debugging).
333-
#
334-
# Note that this is debug info for Binaryen itself, that is, when you are
335-
# debugging Binaryen source code. This flag has no impact on what Binaryen
336-
# does when run on wasm files.
337-
option(ENABLE_BIGINT "Enable wasm BigInt support" OFF)
338-
if(ENABLE_BIGINT)
339-
add_link_flag("-sWASM_BIGINT")
340-
else()
341-
add_link_flag("-sWASM_BIGINT=0")
342-
endif()
329+
# This is now on by default in Emscripten, but set it explicitly to continue
330+
# building correctly on older Emscriptens.
331+
add_link_flag("-sWASM_BIGINT")
343332

344333
if("${CMAKE_BUILD_TYPE}" MATCHES "Release")
345334
# Extra check that cmake has set -O3 in its release flags.

‎src/js/binaryen.js-post.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1049,10 +1049,10 @@ function wrapModule(module, self = {}) {
10491049
'store32'(offset, align, ptr, value, name) {
10501050
return Module['_BinaryenStore'](module, 4, offset, align, ptr, value, Module['i64'], strToStack(name));
10511051
},
1052-
'const'(x, y) {
1052+
'const'(x) {
10531053
return preserveStack(() => {
10541054
const tempLiteral = stackAlloc(sizeOfLiteral);
1055-
Module['_BinaryenLiteralInt64'](tempLiteral, x, y);
1055+
Module['_BinaryenLiteralInt64'](tempLiteral, BigInt(x));
10561056
return Module['_BinaryenConst'](module, tempLiteral);
10571057
});
10581058
},
@@ -1438,10 +1438,10 @@ function wrapModule(module, self = {}) {
14381438
return Module['_BinaryenConst'](module, tempLiteral);
14391439
});
14401440
},
1441-
'const_bits'(x, y) {
1441+
'const_bits'(x) {
14421442
return preserveStack(() => {
14431443
const tempLiteral = stackAlloc(sizeOfLiteral);
1444-
Module['_BinaryenLiteralFloat64Bits'](tempLiteral, x, y);
1444+
Module['_BinaryenLiteralFloat64Bits'](tempLiteral, BigInt(x));
14451445
return Module['_BinaryenConst'](module, tempLiteral);
14461446
});
14471447
},

‎test/binaryen.js/kitchen-sink.js

+31-31
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function makeFloat32(x) {
2424
return module.f32.const(x);
2525
}
2626

27-
function makeInt64(l, h) {
28-
return module.i64.const(l, h);
27+
function makeInt64(x) {
28+
return module.i64.const(x);
2929
}
3030

3131
function makeFloat64(x) {
@@ -214,7 +214,7 @@ function test_core() {
214214
constF32 = module.f32.const(3.14),
215215
constF64 = module.f64.const(2.1828),
216216
constF32Bits = module.f32.const_bits(0xffff1234),
217-
constF64Bits = module.f64.const_bits(0x5678abcd, 0xffff1234);
217+
constF64Bits = module.f64.const_bits(0xffff12345678ABCDn)
218218

219219
var iIfF = binaryen.createType([binaryen.i32, binaryen.i64, binaryen.f32, binaryen.f64])
220220

@@ -229,7 +229,7 @@ function test_core() {
229229
var valueList = [
230230
// Unary
231231
module.i32.clz(module.i32.const(-10)),
232-
module.i64.ctz(module.i64.const(-22, -1)),
232+
module.i64.ctz(module.i64.const(295147905179352825834n)),
233233
module.i32.popcnt(module.i32.const(-10)),
234234
module.f32.neg(module.f32.const(-33.612)),
235235
module.f64.abs(module.f64.const(-9005.841)),
@@ -241,7 +241,7 @@ function test_core() {
241241
module.i32.eqz(module.i32.const(-10)),
242242
module.i64.extend_s(module.i32.const(-10)),
243243
module.i64.extend_u(module.i32.const(-10)),
244-
module.i32.wrap(module.i64.const(-22, -1)),
244+
module.i32.wrap(module.i64.const(295147905179352825834n)),
245245
module.i32.trunc_s.f32(module.f32.const(-33.612)),
246246
module.i64.trunc_s.f32(module.f32.const(-33.612)),
247247
module.i32.trunc_u.f32(module.f32.const(-33.612)),
@@ -264,18 +264,18 @@ function test_core() {
264264
module.f64.convert_s.i32(module.i32.const(-10)),
265265
module.f32.convert_u.i32(module.i32.const(-10)),
266266
module.f64.convert_u.i32(module.i32.const(-10)),
267-
module.f32.convert_s.i64(module.i64.const(-22, -1)),
268-
module.f64.convert_s.i64(module.i64.const(-22, -1)),
269-
module.f32.convert_u.i64(module.i64.const(-22, -1)),
270-
module.f64.convert_u.i64(module.i64.const(-22, -1)),
267+
module.f32.convert_s.i64(module.i64.const(295147905179352825834n)),
268+
module.f64.convert_s.i64(module.i64.const(295147905179352825834n)),
269+
module.f32.convert_u.i64(module.i64.const(295147905179352825834n)),
270+
module.f64.convert_u.i64(module.i64.const(295147905179352825834n)),
271271
module.f64.promote(module.f32.const(-33.612)),
272272
module.f32.demote(module.f64.const(-9005.841)),
273273
module.f32.reinterpret(module.i32.const(-10)),
274-
module.f64.reinterpret(module.i64.const(-22, -1)),
274+
module.f64.reinterpret(module.i64.const(295147905179352825834n)),
275275
module.i8x16.splat(module.i32.const(42)),
276276
module.i16x8.splat(module.i32.const(42)),
277277
module.i32x4.splat(module.i32.const(42)),
278-
module.i64x2.splat(module.i64.const(123, 456)),
278+
module.i64x2.splat(module.i64.const(1958505087099n)),
279279
module.f32x4.splat(module.f32.const(42.0)),
280280
module.f64x2.splat(module.f64.const(42.0)),
281281
module.v128.not(module.v128.const(v128_bytes)),
@@ -333,31 +333,31 @@ function test_core() {
333333
module.i32.add(module.i32.const(-10), module.i32.const(-11)),
334334
module.f64.sub(module.f64.const(-9005.841), module.f64.const(-9007.333)),
335335
module.i32.div_s(module.i32.const(-10), module.i32.const(-11)),
336-
module.i64.div_u(module.i64.const(-22, 0), module.i64.const(-23, 0)),
337-
module.i64.rem_s(module.i64.const(-22, 0), module.i64.const(-23, 0)),
336+
module.i64.div_u(module.i64.const(4294967274n), module.i64.const(4294967273n)),
337+
module.i64.rem_s(module.i64.const(4294967274n), module.i64.const(4294967273n)),
338338
module.i32.rem_u(module.i32.const(-10), module.i32.const(-11)),
339339
module.i32.and(module.i32.const(-10), module.i32.const(-11)),
340-
module.i64.or(module.i64.const(-22, 0), module.i64.const(-23, 0)),
340+
module.i64.or(module.i64.const(4294967274n), module.i64.const(4294967273n)),
341341
module.i32.xor(module.i32.const(-10), module.i32.const(-11)),
342-
module.i64.shl(module.i64.const(-22, 0), module.i64.const(-23, 0)),
343-
module.i64.shr_u(module.i64.const(-22, 0), module.i64.const(-23, 0)),
342+
module.i64.shl(module.i64.const(4294967274n), module.i64.const(4294967273n)),
343+
module.i64.shr_u(module.i64.const(4294967274n), module.i64.const(4294967273n)),
344344
module.i32.shr_s(module.i32.const(-10), module.i32.const(-11)),
345345
module.i32.rotl(module.i32.const(-10), module.i32.const(-11)),
346-
module.i64.rotr(module.i64.const(-22, 0), module.i64.const(-23, 0)),
346+
module.i64.rotr(module.i64.const(4294967274n), module.i64.const(4294967273n)),
347347
module.f32.div(module.f32.const(-33.612), module.f32.const(-62.5)),
348348
module.f64.copysign(module.f64.const(-9005.841), module.f64.const(-9007.333)),
349349
module.f32.min(module.f32.const(-33.612), module.f32.const(-62.5)),
350350
module.f64.max(module.f64.const(-9005.841), module.f64.const(-9007.333)),
351351
module.i32.eq(module.i32.const(-10), module.i32.const(-11)),
352352
module.f32.ne(module.f32.const(-33.612), module.f32.const(-62.5)),
353353
module.i32.lt_s(module.i32.const(-10), module.i32.const(-11)),
354-
module.i64.lt_u(module.i64.const(-22, 0), module.i64.const(-23, 0)),
355-
module.i64.le_s(module.i64.const(-22, 0), module.i64.const(-23, 0)),
354+
module.i64.lt_u(module.i64.const(4294967274n), module.i64.const(4294967273n)),
355+
module.i64.le_s(module.i64.const(4294967274n), module.i64.const(4294967273n)),
356356
module.i32.le_u(module.i32.const(-10), module.i32.const(-11)),
357-
module.i64.gt_s(module.i64.const(-22, 0), module.i64.const(-23, 0)),
357+
module.i64.gt_s(module.i64.const(4294967274n), module.i64.const(4294967273n)),
358358
module.i32.gt_u(module.i32.const(-10), module.i32.const(-11)),
359359
module.i32.ge_s(module.i32.const(-10), module.i32.const(-11)),
360-
module.i64.ge_u(module.i64.const(-22, 0), module.i64.const(-23, 0)),
360+
module.i64.ge_u(module.i64.const(4294967274n), module.i64.const(4294967273n)),
361361
module.f32.lt(module.f32.const(-33.612), module.f32.const(-62.5)),
362362
module.f64.le(module.f64.const(-9005.841), module.f64.const(-9007.333)),
363363
module.f64.gt(module.f64.const(-9005.841), module.f64.const(-9007.333)),
@@ -502,7 +502,7 @@ function test_core() {
502502
module.i16x8.replace_lane(module.v128.const(v128_bytes), 1, module.i32.const(42)),
503503
module.i8x16.replace_lane(module.v128.const(v128_bytes), 1, module.i32.const(42)),
504504
module.i32x4.replace_lane(module.v128.const(v128_bytes), 1, module.i32.const(42)),
505-
module.i64x2.replace_lane(module.v128.const(v128_bytes), 1, module.i64.const(42, 43)),
505+
module.i64x2.replace_lane(module.v128.const(v128_bytes), 1, module.i64.const(184683593770n)),
506506
module.f32x4.replace_lane(module.v128.const(v128_bytes), 1, module.f32.const(42)),
507507
module.f64x2.replace_lane(module.v128.const(v128_bytes), 1, module.f64.const(42)),
508508
// SIMD shift
@@ -569,15 +569,15 @@ function test_core() {
569569
module.switch([ "the-value" ], "the-value", temp8, temp9),
570570
module.switch([ "the-nothing" ], "the-nothing", makeInt32(2)),
571571
module.i32.eqz( // check the output type of the call node
572-
module.call("kitchen()sinker", [ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ], binaryen.i32)
572+
module.call("kitchen()sinker", [ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ], binaryen.i32)
573573
),
574574
module.i32.eqz( // check the output type of the call node
575575
module.i32.trunc_s.f32(
576576
module.call("an-imported", [ makeInt32(13), makeFloat64(3.7) ], binaryen.f32)
577577
)
578578
),
579579
module.i32.eqz( // check the output type of the call node
580-
module.call_indirect("t0", makeInt32(2449), [ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ], iIfF, binaryen.i32)
580+
module.call_indirect("t0", makeInt32(2449), [ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ], iIfF, binaryen.i32)
581581
),
582582
module.drop(module.local.get(0, binaryen.i32)),
583583
module.local.set(0, makeInt32(101)),
@@ -591,8 +591,8 @@ function test_core() {
591591
module.select(temp10, temp11, temp12),
592592
module.return(makeInt32(1337)),
593593
// Tail Call
594-
module.return_call("kitchen()sinker", [ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ], binaryen.i32),
595-
module.return_call_indirect("t0", makeInt32(2449), [ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ], iIfF, binaryen.i32),
594+
module.return_call("kitchen()sinker", [ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ], binaryen.i32),
595+
module.return_call_indirect("t0", makeInt32(2449), [ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ], iIfF, binaryen.i32),
596596

597597
// Reference types
598598
module.ref.is_null(module.ref.null(binaryen.externref)),
@@ -636,11 +636,11 @@ function test_core() {
636636

637637
// Tuples
638638
module.tuple.make(
639-
[ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ]
639+
[ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ]
640640
),
641641
module.tuple.extract(
642642
module.tuple.make(
643-
[ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ]
643+
[ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ]
644644
), 2
645645
),
646646

@@ -695,11 +695,11 @@ function test_core() {
695695
}
696696

697697
console.log("getExpressionInfo(i32.const)=" + JSON.stringify(binaryen.getExpressionInfo(module.i32.const(5))));
698-
console.log("getExpressionInfo(i64.const)=" + JSON.stringify(binaryen.getExpressionInfo(module.i64.const(6, 7))));
698+
console.log("getExpressionInfo(i64.const)=" + JSON.stringify(binaryen.getExpressionInfo(module.i64.const(30064771078n))));
699699
console.log("getExpressionInfo(f32.const)=" + JSON.stringify(binaryen.getExpressionInfo(module.f32.const(8.5))));
700700
console.log("getExpressionInfo(f64.const)=" + JSON.stringify(binaryen.getExpressionInfo(module.f64.const(9.5))));
701701
var elements = binaryen.getExpressionInfo(
702-
module.tuple.make([ makeInt32(13), makeInt64(37, 0), makeFloat32(1.3), makeFloat64(3.7) ])
702+
module.tuple.make([ makeInt32(13), makeInt64(37), makeFloat32(1.3), makeFloat64(3.7) ])
703703
).operands;
704704
for (var i = 0; i < elements.length; i++) {
705705
console.log("getExpressionInfo(tuple[" + i + "])=" + JSON.stringify(binaryen.getExpressionInfo(elements[i])));
@@ -1018,7 +1018,7 @@ function test_nonvalid() {
10181018
module = new binaryen.Module();
10191019

10201020
var func = module.addFunction("func", binaryen.none, binaryen.none, [ binaryen.i32 ],
1021-
module.local.set(0, makeInt64(1234, 0)) // wrong type!
1021+
module.local.set(0, makeInt64(1234)) // wrong type!
10221022
);
10231023

10241024
console.log(module.emitText());

‎test/binaryen.js/validation_errors.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)
88
])
99
);
10-
mod.addExport("test", func);
10+
mod.addExport("test", "test");
1111
console.log(mod.validate())
1212
})();
1313

@@ -20,6 +20,6 @@
2020
)
2121
])
2222
);
23-
mod.addFunctionExport("test", "test", func);
23+
mod.addFunctionExport("test", "test");
2424
console.log(mod.validate())
2525
})();

0 commit comments

Comments
 (0)
Failed to load comments.