Skip to content

Commit 116c132

Browse files
committed
MINIMAL_RUNTIME: Always declare wasmExports globally
This simplifies the code and makes MINIMAL_RUNTIME behave more like the regular runtime. It also allows `-sEXPORTED_RUNTIME_METHODS=wasmExports`, which doesn't work without this. This also matches the way we handle other globals such as `wasmTable` and `wasmMemory`. This save a few bytes of code size in some tests and cost a few in others so on average I would say this code size neutral. Fixes: #20145
1 parent e2ad780 commit 116c132

16 files changed

+94
-99
lines changed

src/postamble_minimal.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function run() {
4848
}
4949
#endif
5050

51-
function initRuntime(wasmExports) {
51+
function initRuntime() {
5252
#if ASSERTIONS || SAFE_HEAP || USE_ASAN
5353
runtimeInitialized = true;
5454
#endif
@@ -140,12 +140,6 @@ if (!Module['wasm']) throw 'Must load WebAssembly Module in to variable Module.w
140140
WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
141141
#endif
142142

143-
#if !LibraryManager.has('library_exports.js') && !EMBIND
144-
// If not using the emscripten_get_exported_function() API or embind, keep the
145-
// `wasmExports` variable in local scope to this instantiate function to save
146-
// code size. (otherwise access it without to export it to outer scope)
147-
var
148-
#endif
149143
// WebAssembly instantiation API gotcha: if Module['wasm'] above was a typed
150144
// array, then the output object will have an output.instance and
151145
// output.module objects. But if Module['wasm'] is an already compiled
@@ -226,7 +220,7 @@ WebAssembly.instantiate(Module['wasm'], imports).then((output) => {
226220
HEAPU8.set(new Uint8Array(Module['mem']), {{{ GLOBAL_BASE }}});
227221
#endif
228222

229-
initRuntime(wasmExports);
223+
initRuntime();
230224
#if PTHREADS
231225
// Export Wasm module for pthread creation to access.
232226
wasmModule = output.module || Module['wasm'];

src/preamble_minimal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
6767
#if SUPPORT_BIG_ENDIAN
6868
HEAP_DATA_VIEW,
6969
#endif
70-
wasmMemory, wasmTable;
70+
wasmMemory, wasmTable, wasmExports;
7171

7272
function updateMemoryViews() {
7373
var b = wasmMemory.buffer;
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
var b = Module, c = b.$ww, f, e = b.mem || new WebAssembly.Memory({
1+
var b = Module, c = b.$ww, f, g, e = b.mem || new WebAssembly.Memory({
22
initial: 256,
33
maximum: 256,
44
shared: !0
5-
}), g = e.buffer, h = [], k = a => {
5+
}), h = e.buffer, k = [], l = a => {
66
a = a.data;
77
let d = a._wsc;
88
d && f.get(d)(...a.x);
9-
}, l = a => {
10-
h.push(a);
11-
}, m = {}, n = 1, p, q;
9+
}, m = a => {
10+
k.push(a);
11+
}, n = {}, p = 1, q, r;
1212

13-
c && (m[0] = this, addEventListener("message", l));
13+
c && (n[0] = this, addEventListener("message", m));
1414

1515
WebAssembly.instantiate(b.wasm, {
1616
a: {
1717
b: (a, d) => {
18-
let r = m[n] = new Worker(b.$wb);
19-
r.postMessage({
20-
$ww: n,
18+
let t = n[p] = new Worker(b.$wb);
19+
t.postMessage({
20+
$ww: p,
2121
wasm: b.wasm,
2222
js: b.js,
2323
mem: e,
2424
sb: a,
2525
sz: d
2626
});
27-
r.onmessage = k;
28-
return n++;
27+
t.onmessage = l;
28+
return p++;
2929
},
3030
c: () => !1,
3131
d: (a, d) => {
32-
m[a].postMessage({
32+
n[a].postMessage({
3333
_wsc: d,
3434
x: []
3535
});
@@ -40,11 +40,11 @@ WebAssembly.instantiate(b.wasm, {
4040
a: e
4141
}
4242
}).then((a => {
43-
a = a.instance.exports;
44-
p = a.g;
45-
q = a.i;
46-
f = a.h;
47-
c ? (a = b, q(a.sb, a.sz), removeEventListener("message", l), h = h.forEach(k),
48-
addEventListener("message", k)) : a.f();
49-
c || p();
43+
g = a.instance.exports;
44+
q = g.g;
45+
r = g.i;
46+
f = g.h;
47+
c ? (a = b, r(a.sb, a.sz), removeEventListener("message", m), k = k.forEach(l),
48+
addEventListener("message", l)) : g.f();
49+
c || q();
5050
}));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 737,
33
"a.html.gz": 433,
4-
"a.js": 667,
5-
"a.js.gz": 458,
4+
"a.js": 669,
5+
"a.js.gz": 462,
66
"a.wasm": 1862,
77
"a.wasm.gz": 1040,
8-
"total": 3266,
9-
"total_gz": 1931
8+
"total": 3268,
9+
"total_gz": 1935
1010
}

test/code_size/hello_webgl2_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4700,
5-
"a.js.gz": 2417,
4+
"a.js": 4698,
5+
"a.js.gz": 2426,
66
"a.wasm": 10479,
77
"a.wasm.gz": 6713,
8-
"total": 15748,
9-
"total_gz": 9509
8+
"total": 15746,
9+
"total_gz": 9518
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 17975,
5-
"a.js.gz": 8044,
4+
"a.js": 17982,
5+
"a.js.gz": 8052,
66
"a.mem": 3171,
77
"a.mem.gz": 2713,
8-
"total": 21713,
9-
"total_gz": 11136
8+
"total": 21720,
9+
"total_gz": 11144
1010
}

test/code_size/hello_webgl_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4186,
5-
"a.js.gz": 2244,
4+
"a.js": 4184,
5+
"a.js.gz": 2250,
66
"a.wasm": 10479,
77
"a.wasm.gz": 6713,
8-
"total": 15234,
9-
"total_gz": 9336
8+
"total": 15232,
9+
"total_gz": 9342
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 17454,
5-
"a.js.gz": 7873,
4+
"a.js": 17461,
5+
"a.js.gz": 7878,
66
"a.mem": 3171,
77
"a.mem.gz": 2713,
8-
"total": 21192,
9-
"total_gz": 10965
8+
"total": 21199,
9+
"total_gz": 10970
1010
}

test/code_size/hello_world_wasm.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
var d = Module, e, f, g = new TextDecoder("utf8"), h;
1+
var b = Module, d, e, f, g = new TextDecoder("utf8"), k;
22

3-
WebAssembly.instantiate(d.wasm, {
3+
WebAssembly.instantiate(b.wasm, {
44
a: {
55
a: a => {
6-
var c = console, k = c.log;
6+
var h = console, l = h.log;
77
if (a) {
8-
for (var l = a + void 0, b = a; !(b >= l) && e[b]; ) ++b;
9-
a = g.decode(e.subarray(a, b));
8+
for (var m = a + void 0, c = a; !(c >= m) && d[c]; ) ++c;
9+
a = g.decode(d.subarray(a, c));
1010
} else a = "";
11-
k.call(c, a);
11+
l.call(h, a);
1212
}
1313
}
1414
}).then((a => {
15-
a = a.instance.exports;
16-
h = a.d;
17-
f = a.b;
18-
e = new Uint8Array(f.buffer);
19-
a.c();
20-
h();
15+
f = a.instance.exports;
16+
k = f.d;
17+
e = f.b;
18+
a = e.buffer;
19+
d = new Uint8Array(a);
20+
f.c();
21+
k();
2122
}));

test/code_size/hello_world_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 673,
33
"a.html.gz": 431,
4-
"a.js": 292,
5-
"a.js.gz": 252,
4+
"a.js": 298,
5+
"a.js.gz": 256,
66
"a.wasm": 103,
77
"a.wasm.gz": 112,
8-
"total": 1068,
9-
"total_gz": 795
8+
"total": 1074,
9+
"total_gz": 799
1010
}

test/code_size/hello_world_wasm2js.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
var b = Module, g = new function(a) {
1+
var b = Module, h, g = new function(a) {
22
this.buffer = new ArrayBuffer(65536 * a.initial);
33
}({
44
initial: 256,
55
maximum: 256
6-
}), k = g.buffer, d = new Uint8Array(k), l = new TextDecoder("utf8"), m;
6+
}), l = g.buffer, d = new Uint8Array(l), m = new TextDecoder("utf8"), n;
77

88
function c(a) {
9-
this.exports = function(h) {
10-
function n(f) {
11-
f.set = function(e, p) {
12-
this[e] = p;
9+
this.exports = function(k) {
10+
function p(f) {
11+
f.set = function(e, q) {
12+
this[e] = q;
1313
};
1414
f.get = function(e) {
1515
return this[e];
@@ -20,40 +20,40 @@ function c(a) {
2020
var e = f.a.a;
2121
return {
2222
b: function() {},
23-
c: function(p, q) {
23+
c: function(q, r) {
2424
e(1024);
2525
return 0;
2626
},
27-
d: n([])
27+
d: p([])
2828
};
29-
}(h);
29+
}(k);
3030
}(a);
3131
}
3232

33-
(function(a, h) {
33+
(function(a, k) {
3434
return {
35-
then: function(n) {
36-
n({
37-
instance: new c(h)
35+
then: function(p) {
36+
p({
37+
instance: new c(k)
3838
});
3939
}
4040
};
4141
})(b.wasm, {
4242
a: {
4343
a: a => {
44-
var h = console, n = h.log;
44+
var k = console, p = k.log;
4545
if (a) {
4646
for (var f = a + void 0, e = a; !(e >= f) && d[e]; ) ++e;
47-
a = l.decode(d.subarray(a, e));
47+
a = m.decode(d.subarray(a, e));
4848
} else a = "";
49-
n.call(h, a);
49+
p.call(k, a);
5050
},
5151
memory: g
5252
}
5353
}).then((a => {
54-
a = a.instance.exports;
55-
m = a.c;
54+
h = a.instance.exports;
55+
n = h.c;
5656
d.set(new Uint8Array(b.mem), 1024);
57-
a.b();
58-
m();
57+
h.b();
58+
n();
5959
}));
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 671,
33
"a.html.gz": 430,
4-
"a.js": 694,
5-
"a.js.gz": 433,
4+
"a.js": 696,
5+
"a.js.gz": 436,
66
"a.mem": 6,
77
"a.mem.gz": 32,
8-
"total": 1371,
9-
"total_gz": 895
8+
"total": 1373,
9+
"total_gz": 898
1010
}

test/code_size/math_wasm.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 673,
33
"a.html.gz": 431,
4-
"a.js": 111,
4+
"a.js": 113,
55
"a.js.gz": 127,
66
"a.wasm": 2727,
77
"a.wasm.gz": 1673,
8-
"total": 3511,
8+
"total": 3513,
99
"total_gz": 2231
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 12774,
3-
"a.html.gz": 6965,
4-
"total": 12774,
5-
"total_gz": 6965
2+
"a.html": 12772,
3+
"a.html.gz": 6969,
4+
"total": 12772,
5+
"total_gz": 6969
66
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 17351,
3-
"a.html.gz": 7549,
4-
"total": 17351,
5-
"total_gz": 7549
2+
"a.html": 17353,
3+
"a.html.gz": 7553,
4+
"total": 17353,
5+
"total_gz": 7553
66
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
409
1+
407

0 commit comments

Comments
 (0)