Skip to content

Commit bff56e2

Browse files
authored
Improve testing of liblegacy.js. NFC (#25515)
We had some testing for `LEGACY_RUNTIME` but not explicitly for the usage of symbols in `liblegacy.js` which are two slightly different things.
1 parent f40f61f commit bff56e2

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

src/lib/liblegacy.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
* @license
33
* Copyright 2010 The Emscripten Authors
44
* SPDX-License-Identifier: MIT
5+
*
6+
* Legacy library symbols that are no longer used by emscripten itself but
7+
* could have external users.
8+
*
9+
* Symbols in this file are not available in `-sSTRICT` mode.
10+
*
11+
* Any usage of symbols in this file will result in a `-Wdeprecated` warning.
12+
*
13+
* Symbol in this file should be removed after "enough time" has passed such
14+
* that all external users have been able to transision away.
515
*/
616

717
legacyFuncs = {
@@ -73,8 +83,10 @@ legacyFuncs = {
7383
if (!dontAddNull) {{{ makeSetValue('buffer', 0, 0, 'i8') }}};
7484
},
7585

76-
$allocateUTF8: '$stringToNewUTF8',
77-
$allocateUTF8OnStack: '$stringToUTF8OnStack',
86+
$allocateUTF8__deps: ['$stringToNewUTF8'],
87+
$allocateUTF8: (...args) => stringToNewUTF8(...args),
88+
$allocateUTF8OnStack__deps: ['$stringToUTF8OnStack'],
89+
$allocateUTF8OnStack: (...args) => stringToUTF8OnStack(...args),
7890

7991
#if LINK_AS_CXX
8092
$demangle__deps: ['$withStackSave', '__cxa_demangle', 'free', '$stringToUTF8OnStack'],
@@ -130,8 +142,8 @@ legacyFuncs = {
130142
if (WARN_DEPRECATED && !INCLUDE_FULL_LIBRARY) {
131143
for (const name of Object.keys(legacyFuncs)) {
132144
if (!isDecorator(name)) {
133-
depsKey = `${name}__deps`;
134-
legacyFuncs[depsKey] = legacyFuncs[depsKey] || [];
145+
const depsKey = `${name}__deps`;
146+
legacyFuncs[depsKey] ??= []
135147
legacyFuncs[depsKey].push(() => {
136148
warn(`JS library symbol '${name}' is deprecated. Please open a bug if you have a continuing need for this symbol [-Wdeprecated]`);
137149
});

test/codesize/test_codesize_hello_O0.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 23198,
3-
"a.out.js.gz": 8524,
3+
"a.out.js.gz": 8525,
44
"a.out.nodebug.wasm": 15127,
55
"a.out.nodebug.wasm.gz": 7450,
66
"total": 38325,
7-
"total_gz": 15974,
7+
"total_gz": 15975,
88
"sent": [
99
"fd_write"
1010
],

test/codesize/test_unoptimized_code_size.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"hello_world.js": 55508,
3-
"hello_world.js.gz": 17498,
3+
"hello_world.js.gz": 17499,
44
"hello_world.wasm": 15127,
55
"hello_world.wasm.gz": 7450,
66
"no_asserts.js": 26614,
@@ -12,5 +12,5 @@
1212
"strict.wasm": 15127,
1313
"strict.wasm.gz": 7447,
1414
"total": 178126,
15-
"total_gz": 64105
15+
"total_gz": 64106
1616
}

test/other/test_legacy_runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
int main () {
55
intptr_t addr = EM_ASM_INT({
6-
return allocate(intArrayFromString("hello from js"), ALLOC_NORMAL);
6+
return stringToNewUTF8("hello from js");
77
});
88
printf("%s\n", (char*)addr);
99
return 0;

test/test_other.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13944,30 +13944,33 @@ def test_stdint_limits(self):
1394413944

1394513945
def test_legacy_runtime(self):
1394613946
self.set_setting('EXPORTED_FUNCTIONS', ['_malloc', '_main'])
13947-
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$intArrayFromString', '$ALLOC_NORMAL'])
1394813947

13949-
# By default `LEGACY_RUNTIME` is disabled and `allocate` is not available.
13950-
self.set_setting('EXPORTED_RUNTIME_METHODS', ['ALLOC_NORMAL'])
13948+
# By default `LEGACY_RUNTIME` is disabled and `stringToNewUTF8` is not available.
1395113949
self.cflags += ['-Wno-deprecated']
1395213950
self.do_runf('other/test_legacy_runtime.c',
13953-
'`allocate` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line',
13951+
'`stringToNewUTF8` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line',
1395413952
assert_returncode=NON_ZERO)
1395513953

1395613954
# When we enable `LEGACY_RUNTIME`, `allocate` should be available.
13957-
self.set_setting('LEGACY_RUNTIME', 1)
13958-
self.do_runf('other/test_legacy_runtime.c')
13955+
self.do_runf('other/test_legacy_runtime.c', 'hello from js', cflags=['-sLEGACY_RUNTIME'])
1395913956

1396013957
# Adding it to EXPORTED_RUNTIME_METHODS should also make it available.
13961-
self.clear_setting('LEGACY_RUNTIME')
13962-
self.set_setting('EXPORTED_RUNTIME_METHODS', ['allocate'])
13963-
self.do_runf('other/test_legacy_runtime.c', 'hello from js')
13958+
self.do_runf('other/test_legacy_runtime.c', 'hello from js', cflags=['-sEXPORTED_RUNTIME_METHODS=stringToNewUTF8'])
1396413959

13965-
# In strict mode the library function is not even available, so we get a build time error
13966-
self.set_setting('STRICT')
13967-
self.clear_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE')
13968-
for opt in ('-O0', '-O3'):
13969-
err = self.expect_fail([EMCC, test_file('other/test_legacy_runtime.c'), opt] + self.get_cflags())
13970-
self.assertContained('undefined exported symbol: "allocate" in EXPORTED_RUNTIME_METHODS', err)
13960+
@parameterized({
13961+
'': (['-O0'],),
13962+
'O3': (['-O3'],),
13963+
})
13964+
def test_legacy_jslib(self, args):
13965+
# Warn about usage of legacy library function
13966+
err = self.run_process([EMCC, test_file('hello_world.c'), '-sEXPORTED_RUNTIME_METHODS=allocateUTF8'] + args, stderr=PIPE).stderr
13967+
self.assertContained("warning: JS library symbol '$allocateUTF8' is deprecated. Please open a bug if you have a continuing need for this symbol", err)
13968+
13969+
self.assertContained('emcc: warning: warnings in JS library compilation [-Wjs-compiler]', err)
13970+
13971+
# In strict mode legacy library functions are not available, so we get a build time error
13972+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sEXPORTED_RUNTIME_METHODS=allocateUTF8', '-sSTRICT'] + args)
13973+
self.assertContained('undefined exported symbol: "allocateUTF8" in EXPORTED_RUNTIME_METHODS', err)
1397113974

1397213975
def test_fetch_settings(self):
1397313976
create_file('pre.js', '''

0 commit comments

Comments
 (0)