Skip to content

Commit 794362a

Browse files
authored
Move runtime_legacy.js to library_legacy.js (#17390)
1 parent 1244ea9 commit 794362a

9 files changed

+69
-51
lines changed

ChangeLog.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ See docs/process.md for more on how version tagging works.
2929
`DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`. This change allows us to transition
3030
runtime functions to JS library functions without the need to folks to add
3131
`DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`. (#17369)
32-
- The `addFunction`/`removeFunction` runtime functions were converted into JS
33-
library functions. This means that won't get included in the output unless
34-
explictly required. Exporting them via `EXPORTED_RUNTIME_METHODS` will
35-
continue to work. For internal usage (without exporting them) they can be
36-
added to `DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`. (#17370)
32+
- The following function, which were previously part of the default runtime, are
33+
now JS library functions:
34+
- addFunction
35+
- removeFunction
36+
- allocate
37+
This means they won't get included in the output unless explictly required.
38+
Exporting them via `EXPORTED_RUNTIME_METHODS` will continue to work. For
39+
internal usage (without exporting them) they can be added to
40+
`DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`. (#17370)
3741
- The `run` runtime function is no longer exported by default. It can be added
3842
to `EXPORTED_RUNTIME_METHODS` if needed.
3943
- The getWasmTableEntry/setWasmTableEntry library function are no longer

src/library_legacy.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright 2010 The Emscripten Authors
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
mergeInto(LibraryManager.library, {
8+
$ALLOC_NORMAL: 0, // Tries to use _malloc()
9+
$ALLOC_STACK: 1, // Lives for the duration of the current function call
10+
11+
/**
12+
* allocate(): This function is no longer used by emscripten but is kept around to avoid
13+
* breaking external users.
14+
* You should normally not use allocate(), and instead allocate
15+
* memory using _malloc()/stackAlloc(), initialize it with
16+
* setValue(), and so forth.
17+
* @param {(Uint8Array|Array<number>)} slab: An array of data.
18+
* @param {number=} allocator : How to allocate memory, see ALLOC_*
19+
*/
20+
$allocate__deps: ['$ALLOC_NORMAL', '$ALLOC_STACK'],
21+
$allocate: function(slab, allocator) {
22+
var ret;
23+
#if ASSERTIONS
24+
assert(typeof allocator == 'number', 'allocate no longer takes a type argument')
25+
assert(typeof slab != 'number', 'allocate no longer takes a number as arg0')
26+
#endif
27+
28+
if (allocator == ALLOC_STACK) {
29+
ret = stackAlloc(slab.length);
30+
} else {
31+
ret = {{{ makeMalloc('allocate', 'slab.length') }}};
32+
}
33+
34+
if (!slab.subarray && !slab.slice) {
35+
slab = new Uint8Array(slab);
36+
}
37+
HEAPU8.set(slab, ret);
38+
return ret;
39+
},
40+
});

src/modules.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ global.LibraryManager = {
152152
libraries.push('library_html5_webgpu.js');
153153
}
154154

155+
if (!STRICT) {
156+
libraries.push('library_legacy.js');
157+
}
158+
155159
if (BOOTSTRAPPING_STRUCT_INFO) {
156160
libraries = [
157161
'library_bootstrap.js',
@@ -407,11 +411,8 @@ function exportRuntime() {
407411

408412
// All possible runtime elements that can be exported
409413
let runtimeElements = [
410-
'ALLOC_NORMAL',
411-
'ALLOC_STACK',
412414
'ccall',
413415
'cwrap',
414-
'allocate',
415416
'UTF8ArrayToString',
416417
'UTF8ToString',
417418
'stringToUTF8Array',

src/preamble.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ function _free() {
233233
#endif // free
234234
#endif // ASSERTIONS
235235

236-
#if !STRICT
237-
#include "runtime_legacy.js"
238-
#endif
239236
#include "runtime_strings.js"
240237
#include "runtime_strings_extra.js"
241238

src/runtime_legacy.js

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
92392
1+
91192
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
54045
1+
53126

tests/test_core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7041,6 +7041,7 @@ def test(output_prefix='', args=None, assert_returncode=0):
70417041

70427042
# see that direct usage (not on module) works. we don't export, but the use
70437043
# keeps it alive through JSDCE
7044+
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$ALLOC_STACK'])
70447045
test(args=['-DDIRECT'])
70457046
# see that with assertions, we get a nice error message
70467047
self.set_setting('EXPORTED_RUNTIME_METHODS', [])

tests/test_other.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12072,9 +12072,21 @@ def test_stdint_limits(self):
1207212072

1207312073
def test_legacy_runtime(self):
1207412074
self.set_setting('EXPORTED_FUNCTIONS', ['_malloc', '_main'])
12075+
self.set_setting('EXPORTED_RUNTIME_METHODS', ['ALLOC_NORMAL'])
12076+
12077+
# By default `allocate` is not available (its a JS library function not included by default)
12078+
self.do_runf(test_file('other/test_legacy_runtime.c'),
12079+
'Aborted(Call to `allocate` which is a library function and not included by default',
12080+
assert_returncode=NON_ZERO)
12081+
12082+
# Adding it to EXPORTED_RUNTIME_METHODS makes it available.
12083+
self.set_setting('EXPORTED_RUNTIME_METHODS', ['allocate', 'ALLOC_NORMAL'])
1207512084
self.do_runf(test_file('other/test_legacy_runtime.c'), 'hello from js')
12085+
12086+
# In strict mode the library function is not even available, so we get a build time error
1207612087
self.set_setting('STRICT')
12077-
self.do_runf(test_file('other/test_legacy_runtime.c'), 'ReferenceError: allocate is not defined', assert_returncode=NON_ZERO)
12088+
err = self.expect_fail([EMCC, test_file('other/test_legacy_runtime.c')] + self.get_emcc_args())
12089+
self.assertContained('warning: invalid item in EXPORTED_RUNTIME_METHODS: allocate', err)
1207812090

1207912091
def test_fetch_settings(self):
1208012092
create_file('pre.js', '''

0 commit comments

Comments
 (0)