Skip to content

Commit a0c77bf

Browse files
authored
[wasm2js] Fix memory.size (#2330)
We emitted the __wasm_memory_size function only when memory growth was enabled, but it can be used without that too. In theory we could only emit it if either memory growth or memory.size is used, but I think we can expect JS minifiers to do that later. Also fix a test suite bug - the check/auto_update script didn't run all the wasm2js tests when you run it with argument wasm2js (it used that as the list of tests, instead of the list of files, which confused me here for a while...).
1 parent 3ac5416 commit a0c77bf

20 files changed

Lines changed: 93 additions & 53 deletions

scripts/test/wasm2js.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
from .support import run_command, split_wast, write_wast
2020
from .shared import (
21-
WASM2JS, MOZJS, NODEJS, fail_if_not_identical, options, tests,
21+
WASM2JS, MOZJS, NODEJS, fail_if_not_identical, options,
2222
fail_if_not_identical_to_file, with_pass_debug
2323
)
2424

25-
# tests with i64s, invokes, etc.
25+
tests = sorted(os.listdir(os.path.join(options.binaryen_test)))
2626
spec_dir = os.path.join(options.binaryen_test, 'spec')
2727
spec_tests = [os.path.join(spec_dir, t)
2828
for t in sorted(os.listdir(spec_dir))

src/wasm2js.h

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ class Wasm2JSBuilder {
255255
void addTable(Ref ast, Module* wasm);
256256
void addExports(Ref ast, Module* wasm);
257257
void addGlobal(Ref ast, Global* global);
258+
void addMemoryFuncs(Ref ast, Module* wasm);
258259
void addMemoryGrowthFuncs(Ref ast, Module* wasm);
259260

260261
Wasm2JSBuilder() = delete;
@@ -608,8 +609,8 @@ void Wasm2JSBuilder::addExports(Ref ast, Module* wasm) {
608609
exports, fromName(export_->name, NameScope::Top), memory);
609610
}
610611
}
611-
if (wasm->memory.exists && wasm->memory.max > wasm->memory.initial) {
612-
addMemoryGrowthFuncs(ast, wasm);
612+
if (wasm->memory.exists) {
613+
addMemoryFuncs(ast, wasm);
613614
}
614615
ast->push_back(
615616
ValueBuilder::makeStatement(ValueBuilder::makeReturn(exports)));
@@ -1900,6 +1901,22 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m,
19001901
return ExpressionProcessor(this, m, func, standaloneFunction).process();
19011902
}
19021903

1904+
void Wasm2JSBuilder::addMemoryFuncs(Ref ast, Module* wasm) {
1905+
Ref memorySizeFunc = ValueBuilder::makeFunction(WASM_MEMORY_SIZE);
1906+
memorySizeFunc[3]->push_back(ValueBuilder::makeReturn(
1907+
makeAsmCoercion(ValueBuilder::makeBinary(
1908+
ValueBuilder::makeDot(ValueBuilder::makeName(BUFFER),
1909+
IString("byteLength")),
1910+
DIV,
1911+
ValueBuilder::makeInt(Memory::kPageSize)),
1912+
AsmType::ASM_INT)));
1913+
ast->push_back(memorySizeFunc);
1914+
1915+
if (wasm->memory.max > wasm->memory.initial) {
1916+
addMemoryGrowthFuncs(ast, wasm);
1917+
}
1918+
}
1919+
19031920
void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast, Module* wasm) {
19041921
Ref memoryGrowFunc = ValueBuilder::makeFunction(WASM_MEMORY_GROW);
19051922
ValueBuilder::appendArgumentToFunction(memoryGrowFunc, IString("pagesToAdd"));
@@ -2016,16 +2033,7 @@ void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast, Module* wasm) {
20162033
memoryGrowFunc[3]->push_back(
20172034
ValueBuilder::makeReturn(ValueBuilder::makeName(IString("oldPages"))));
20182035

2019-
Ref memorySizeFunc = ValueBuilder::makeFunction(WASM_MEMORY_SIZE);
2020-
memorySizeFunc[3]->push_back(ValueBuilder::makeReturn(
2021-
makeAsmCoercion(ValueBuilder::makeBinary(
2022-
ValueBuilder::makeDot(ValueBuilder::makeName(BUFFER),
2023-
IString("byteLength")),
2024-
DIV,
2025-
ValueBuilder::makeInt(Memory::kPageSize)),
2026-
AsmType::ASM_INT)));
20272036
ast->push_back(memoryGrowFunc);
2028-
ast->push_back(memorySizeFunc);
20292037
}
20302038

20312039
// Wasm2JSGlue emits the core of the module - the functions etc. that would

test/wasm2js/address.2asm.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ function asmFunc(global, env, buffer) {
4545
}
4646

4747
var FUNCTION_TABLE = [];
48+
function __wasm_memory_size() {
49+
return buffer.byteLength / 65536 | 0;
50+
}
51+
4852
function __wasm_memory_grow(pagesToAdd) {
4953
pagesToAdd = pagesToAdd | 0;
5054
var oldPages = __wasm_memory_size() | 0;
@@ -67,10 +71,6 @@ function asmFunc(global, env, buffer) {
6771
return oldPages;
6872
}
6973

70-
function __wasm_memory_size() {
71-
return buffer.byteLength / 65536 | 0;
72-
}
73-
7474
return {
7575
"good": $0,
7676
"bad": $1

test/wasm2js/atomic_fence.2asm.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function asmFunc(global, env, buffer) {
2525
}
2626

2727
var FUNCTION_TABLE = [];
28+
function __wasm_memory_size() {
29+
return buffer.byteLength / 65536 | 0;
30+
}
31+
2832
function __wasm_memory_grow(pagesToAdd) {
2933
pagesToAdd = pagesToAdd | 0;
3034
var oldPages = __wasm_memory_size() | 0;
@@ -47,10 +51,6 @@ function asmFunc(global, env, buffer) {
4751
return oldPages;
4852
}
4953

50-
function __wasm_memory_size() {
51-
return buffer.byteLength / 65536 | 0;
52-
}
53-
5454
return {
5555
"atomic_fence": $0
5656
};

test/wasm2js/dynamicLibrary.2asm.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ function asmFunc(global, env, buffer) {
4040
var FUNCTION_TABLE = [];
4141
FUNCTION_TABLE[import$tableBase + 0] = foo;
4242
FUNCTION_TABLE[import$tableBase + 1] = bar;
43+
function __wasm_memory_size() {
44+
return buffer.byteLength / 65536 | 0;
45+
}
46+
4347
return {
4448
"baz": baz
4549
};

test/wasm2js/dynamicLibrary.2asm.js.opt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ function asmFunc(global, env, buffer) {
3232
var FUNCTION_TABLE = [];
3333
FUNCTION_TABLE[import$tableBase + 0] = foo;
3434
FUNCTION_TABLE[import$tableBase + 1] = foo;
35+
function __wasm_memory_size() {
36+
return buffer.byteLength / 65536 | 0;
37+
}
38+
3539
return {
3640
"baz": foo
3741
};

test/wasm2js/emscripten-grow-no.2asm.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function asmFunc(global, env, buffer) {
2525
// EMSCRIPTEN_START_FUNCS;
2626
// EMSCRIPTEN_END_FUNCS;
2727
var FUNCTION_TABLE = [];
28+
function __wasm_memory_size() {
29+
return buffer.byteLength / 65536 | 0;
30+
}
31+
2832
return {
2933
"memory": Object.create(Object.prototype, {
3034
"grow": {

test/wasm2js/emscripten-grow-no.2asm.js.opt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function asmFunc(global, env, buffer) {
2525
// EMSCRIPTEN_START_FUNCS;
2626
// EMSCRIPTEN_END_FUNCS;
2727
var FUNCTION_TABLE = [];
28+
function __wasm_memory_size() {
29+
return buffer.byteLength / 65536 | 0;
30+
}
31+
2832
return {
2933
"memory": Object.create(Object.prototype, {
3034
"grow": {

test/wasm2js/emscripten-grow-yes.2asm.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function asmFunc(global, env, buffer) {
2525
// EMSCRIPTEN_START_FUNCS;
2626
// EMSCRIPTEN_END_FUNCS;
2727
var FUNCTION_TABLE = [];
28+
function __wasm_memory_size() {
29+
return buffer.byteLength / 65536 | 0;
30+
}
31+
2832
function __wasm_memory_grow(pagesToAdd) {
2933
pagesToAdd = pagesToAdd | 0;
3034
var oldPages = __wasm_memory_size() | 0;
@@ -48,10 +52,6 @@ function asmFunc(global, env, buffer) {
4852
return oldPages;
4953
}
5054

51-
function __wasm_memory_size() {
52-
return buffer.byteLength / 65536 | 0;
53-
}
54-
5555
return {
5656
"memory": Object.create(Object.prototype, {
5757
"grow": {

test/wasm2js/emscripten-grow-yes.2asm.js.opt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ function asmFunc(global, env, buffer) {
2525
// EMSCRIPTEN_START_FUNCS;
2626
// EMSCRIPTEN_END_FUNCS;
2727
var FUNCTION_TABLE = [];
28+
function __wasm_memory_size() {
29+
return buffer.byteLength / 65536 | 0;
30+
}
31+
2832
function __wasm_memory_grow(pagesToAdd) {
2933
pagesToAdd = pagesToAdd | 0;
3034
var oldPages = __wasm_memory_size() | 0;
@@ -48,10 +52,6 @@ function asmFunc(global, env, buffer) {
4852
return oldPages;
4953
}
5054

51-
function __wasm_memory_size() {
52-
return buffer.byteLength / 65536 | 0;
53-
}
54-
5555
return {
5656
"memory": Object.create(Object.prototype, {
5757
"grow": {

0 commit comments

Comments
 (0)