Skip to content

Commit 5ca79a7

Browse files
dcodeIOkripken
authored andcommitted
Align binaryen.js with the npm package (WebAssembly#2551)
Binaryen.js now uses binaryen (was Binaryen) as its global name to align with the npm package. Also fixes issues with emitting and testing both the JS and Wasm builds.
1 parent a43b533 commit 5ca79a7

26 files changed

Lines changed: 739 additions & 854 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ v90
4444
Unlike the JS variant, the Wasm variant requires asynchronously awaiting the
4545
Wasm blob's instantiation and initialization before being usable, using the
4646
`binaryen.ready` promise, e.g. `binaryen.ready.then(() => ...)`.
47+
- Binaryen.js now uses `binaryen` (was `Binaryen`) as its global name to align
48+
with the npm package.
4749

4850
v88
4951
---

CMakeLists.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ if(EMSCRIPTEN)
383383
target_link_libraries(binaryen_wasm "-s MODULARIZE_INSTANCE=1")
384384
target_link_libraries(binaryen_wasm "-s NO_FILESYSTEM=0")
385385
target_link_libraries(binaryen_wasm "-s NODERAWFS=0")
386-
target_link_libraries(binaryen_wasm "-s EXPORT_NAME=Binaryen")
386+
target_link_libraries(binaryen_wasm "-s EXPORT_NAME=binaryen")
387387
target_link_libraries(binaryen_wasm "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-post.js")
388388
target_link_libraries(binaryen_wasm optimized "--closure 1")
389389
target_link_libraries(binaryen_wasm optimized "--llvm-lto 1")
@@ -405,7 +405,7 @@ if(EMSCRIPTEN)
405405
target_link_libraries(binaryen_js "-s MODULARIZE_INSTANCE=1")
406406
target_link_libraries(binaryen_js "-s NO_FILESYSTEM=0")
407407
target_link_libraries(binaryen_js "-s NODERAWFS=0")
408-
target_link_libraries(binaryen_js "-s EXPORT_NAME=Binaryen")
408+
target_link_libraries(binaryen_js "-s EXPORT_NAME=binaryen")
409409
target_link_libraries(binaryen_js "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-post.js")
410410
target_link_libraries(binaryen_js optimized "--closure 1")
411411
target_link_libraries(binaryen_js optimized "--llvm-lto 1")
@@ -414,9 +414,6 @@ if(EMSCRIPTEN)
414414
set_property(TARGET binaryen_js PROPERTY CXX_STANDARD 14)
415415
set_property(TARGET binaryen_js PROPERTY CXX_STANDARD_REQUIRED ON)
416416
install(TARGETS binaryen_js DESTINATION ${CMAKE_INSTALL_BINDIR})
417-
418-
# always emit as 'binaryen.js'
419-
set_target_properties(binaryen_wasm binaryen_js PROPERTIES OUTPUT_NAME "binaryen")
420417
endif()
421418

422419
# Testing

auto_update_tests.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,8 @@ def update_binaryen_js_tests():
252252
print(basename)
253253
f = open('a.js', 'w')
254254
f.write(open(shared.BINARYEN_JS).read())
255-
if shared.NODEJS:
256-
f.write(support.node_test_glue())
257255
test_src = open(s).read()
258-
f.write(test_src)
256+
f.write(support.js_test_wrap().replace('%TEST%', test_src))
259257
f.close()
260258
if shared.MOZJS or node_has_wasm or 'WebAssembly.' not in test_src:
261259
cmd = [shared.MOZJS or shared.NODEJS, 'a.js']

scripts/test/binaryenjs.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616

1717
import os
1818
import subprocess
19+
import sys
1920

2021
from . import shared
2122
from . import support
2223

2324

24-
def test_binaryen_js():
25+
def do_test_binaryen_js_with(which):
2526
if not (shared.MOZJS or shared.NODEJS):
2627
print('no vm to run binaryen.js tests')
2728
return
2829

2930
node_has_wasm = shared.NODEJS and support.node_has_webassembly(shared.NODEJS)
3031

31-
if not os.path.exists(shared.BINARYEN_JS):
32-
print('no binaryen.js build to test')
32+
if not os.path.exists(which):
33+
print('no ' + which + ' build to test')
3334
return
3435

35-
print('\n[ checking binaryen.js testcases... ]\n')
36+
print('\n[ checking binaryen.js testcases (' + which + ')... ]\n')
3637

3738
for s in sorted(os.listdir(os.path.join(shared.options.binaryen_test, 'binaryen.js'))):
3839
if not s.endswith('.js'):
@@ -41,15 +42,13 @@ def test_binaryen_js():
4142
f = open('a.js', 'w')
4243
# avoid stdout/stderr ordering issues in some js shells - use just stdout
4344
f.write('''
44-
console.warn = function(x) { console.log(x) };
45+
console.warn = console.error = console.log;
4546
''')
46-
binaryen_js = open(shared.BINARYEN_JS).read()
47+
binaryen_js = open(which).read()
4748
f.write(binaryen_js)
48-
if shared.NODEJS:
49-
f.write(support.node_test_glue())
5049
test_path = os.path.join(shared.options.binaryen_test, 'binaryen.js', s)
5150
test_src = open(test_path).read()
52-
f.write(test_src)
51+
f.write(support.js_test_wrap().replace('%TEST%', test_src))
5352
f.close()
5453

5554
def test(engine):
@@ -73,5 +72,23 @@ def test(engine):
7372
print('Skipping ' + test_path + ' because WebAssembly might not be supported')
7473

7574

76-
if __name__ == "__main__":
75+
def test_binaryen_js():
76+
do_test_binaryen_js_with(shared.BINARYEN_JS)
77+
78+
79+
def test_binaryen_wasm():
80+
do_test_binaryen_js_with(shared.BINARYEN_WASM)
81+
82+
83+
def test_binaryen_js_and_wasm():
7784
test_binaryen_js()
85+
test_binaryen_wasm()
86+
87+
88+
if __name__ == "__main__":
89+
if sys.argv[1] == "js":
90+
test_binaryen_js()
91+
elif sys.argv[1] == "wasm":
92+
test_binaryen_wasm()
93+
else:
94+
test_binaryen_js_and_wasm()

scripts/test/shared.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ def is_exe(fpath):
184184
WASM_METADCE = [os.path.join(options.binaryen_bin, 'wasm-metadce')]
185185
WASM_EMSCRIPTEN_FINALIZE = [os.path.join(options.binaryen_bin,
186186
'wasm-emscripten-finalize')]
187-
BINARYEN_JS = os.path.join(options.binaryen_bin, 'binaryen.js')
187+
BINARYEN_JS = os.path.join(options.binaryen_bin, 'binaryen_js.js')
188+
BINARYEN_WASM = os.path.join(options.binaryen_bin, 'binaryen_wasm.js')
188189

189190

190191
def wrap_with_valgrind(cmd):

scripts/test/support.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ def node_has_webassembly(cmd):
189189
return run_command(cmd) == 'object'
190190

191191

192-
def node_test_glue():
193-
# running concatenated files (a.js) in node interferes with module loading
194-
# because the concatenated file expects a 'var Binaryen' but binaryen.js
195-
# assigned to module.exports. this is correct behavior but tests then need
196-
# a workaround:
197-
return ('if (typeof module === "object" && typeof exports === "object")\n'
198-
' Binaryen = module.exports;\n')
192+
def js_test_wrap():
193+
# common wrapper code for JS tests, waiting for binaryen.js to become ready
194+
# and providing common utility used by all tests:
195+
return '''
196+
binaryen.ready.then(function() {
197+
function assert(x) { if (!x) throw Error('Test assertion failed'); }
198+
%TEST%
199+
});
200+
'''

test/binaryen.js/atomics.js

Lines changed: 75 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,87 @@
1-
function assert(x) {
2-
if (!x) throw 'error!';
3-
}
4-
51
var wast = `
62
(module
73
(memory $0 (shared 1 1))
84
)
95
`;
106

11-
function test() {
12-
var module = Binaryen.parseText(wast);
7+
var module = binaryen.parseText(wast);
138

14-
// i32/i64.atomic.load/store
15-
module.addFunction("main", Binaryen.none, Binaryen.none, [], module.block("", [
16-
// i32
17-
module.i32.atomic.store(0,
18-
module.i32.const(0),
19-
module.i32.atomic.load(0,
20-
module.i32.const(0)
21-
)
22-
),
23-
// i32 as u8
24-
module.i32.atomic.store8(0,
25-
module.i32.const(0),
26-
module.i32.atomic.load8_u(0,
27-
module.i32.const(0)
28-
)
29-
),
30-
// i32 as u16
31-
module.i32.atomic.store16(0,
9+
// i32/i64.atomic.load/store
10+
module.addFunction("main", binaryen.none, binaryen.none, [], module.block("", [
11+
// i32
12+
module.i32.atomic.store(0,
13+
module.i32.const(0),
14+
module.i32.atomic.load(0,
15+
module.i32.const(0)
16+
)
17+
),
18+
// i32 as u8
19+
module.i32.atomic.store8(0,
20+
module.i32.const(0),
21+
module.i32.atomic.load8_u(0,
22+
module.i32.const(0)
23+
)
24+
),
25+
// i32 as u16
26+
module.i32.atomic.store16(0,
27+
module.i32.const(0),
28+
module.i32.atomic.load16_u(0,
29+
module.i32.const(0)
30+
)
31+
),
32+
// i64
33+
module.i64.atomic.store(0,
34+
module.i32.const(0),
35+
module.i64.atomic.load(0,
36+
module.i32.const(0)
37+
)
38+
),
39+
// i64 as u8
40+
module.i64.atomic.store8(0,
41+
module.i32.const(0),
42+
module.i64.atomic.load8_u(0,
43+
module.i32.const(0)
44+
)
45+
),
46+
// i64 as u16
47+
module.i64.atomic.store16(0,
48+
module.i32.const(0),
49+
module.i64.atomic.load16_u(0,
50+
module.i32.const(0)
51+
)
52+
),
53+
// i64 as u32
54+
module.i64.atomic.store32(0,
55+
module.i32.const(0),
56+
module.i64.atomic.load32_u(0,
57+
module.i32.const(0)
58+
)
59+
),
60+
// wait and notify
61+
module.drop(
62+
module.i32.atomic.wait(
3263
module.i32.const(0),
33-
module.i32.atomic.load16_u(0,
34-
module.i32.const(0)
35-
)
36-
),
37-
// i64
38-
module.i64.atomic.store(0,
3964
module.i32.const(0),
40-
module.i64.atomic.load(0,
41-
module.i32.const(0)
42-
)
43-
),
44-
// i64 as u8
45-
module.i64.atomic.store8(0,
65+
module.i64.const(0)
66+
)
67+
),
68+
module.drop(
69+
module.i64.atomic.wait(
4670
module.i32.const(0),
47-
module.i64.atomic.load8_u(0,
48-
module.i32.const(0)
49-
)
50-
),
51-
// i64 as u16
52-
module.i64.atomic.store16(0,
71+
module.i64.const(0),
72+
module.i64.const(0)
73+
)
74+
),
75+
module.drop(
76+
module.atomic.notify(
5377
module.i32.const(0),
54-
module.i64.atomic.load16_u(0,
55-
module.i32.const(0)
56-
)
57-
),
58-
// i64 as u32
59-
module.i64.atomic.store32(0,
60-
module.i32.const(0),
61-
module.i64.atomic.load32_u(0,
62-
module.i32.const(0)
63-
)
64-
),
65-
// wait and notify
66-
module.drop(
67-
module.i32.atomic.wait(
68-
module.i32.const(0),
69-
module.i32.const(0),
70-
module.i64.const(0)
71-
)
72-
),
73-
module.drop(
74-
module.i64.atomic.wait(
75-
module.i32.const(0),
76-
module.i64.const(0),
77-
module.i64.const(0)
78-
)
79-
),
80-
module.drop(
81-
module.atomic.notify(
82-
module.i32.const(0),
83-
module.i32.const(0)
84-
)
85-
),
86-
// fence
87-
module.atomic.fence()
88-
]));
89-
90-
module.setFeatures(Binaryen.Features.Atomics);
91-
assert(module.validate());
92-
console.log(module.emitText());
93-
}
78+
module.i32.const(0)
79+
)
80+
),
81+
// fence
82+
module.atomic.fence()
83+
]));
9484

95-
Binaryen.ready.then(test);
85+
module.setFeatures(binaryen.Features.Atomics);
86+
assert(module.validate());
87+
console.log(module.emitText());

test/binaryen.js/custom-section.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
function assert(x) {
2-
if (!x) throw 'error!';
3-
}
1+
binaryen.setAPITracing(true);
2+
var module = new binaryen.Module();
43

5-
function test() {
6-
Binaryen.setAPITracing(true);
7-
var module = new Binaryen.Module();
4+
module.addCustomSection("hello", [119, 111, 114, 108, 100]);
85

9-
module.addCustomSection("hello", [119, 111, 114, 108, 100]);
10-
11-
assert(module.validate());
12-
console.log(module.emitText());
13-
}
14-
15-
Binaryen.ready.then(test);
6+
assert(module.validate());
7+
console.log(module.emitText());

0 commit comments

Comments
 (0)