Skip to content

Commit f08ae1c

Browse files
authored
Convert extended string handling functions into JS library code (#17403)
This was already the case with MINIMAL_RUNTIME. Add a new setting, which is currently enabled by default called `LEGACY_RUNTIME` which enables all of these library functions by default. As a followup we can consider disabling `LEGACY_RUNTIME` by default which will give code size benefits by default.
1 parent b96bcd3 commit f08ae1c

31 files changed

+386
-423
lines changed

ChangeLog.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,28 @@ See docs/process.md for more on how version tagging works.
3434
- addFunction
3535
- removeFunction
3636
- 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)
37+
- AsciiToString
38+
- stringToAscii
39+
- UTF16ToString
40+
- stringToUTF16
41+
- lengthBytesUTF16
42+
- UTF32ToString
43+
- stringToUTF32
44+
- lengthBytesUTF32
45+
- allocateUTF8
46+
- allocateUTF8OnStack
47+
- writeStringToMemory
48+
- writeArrayToMemory
49+
- writeAsciiToMemory
50+
- intArrayFromString
51+
- intArrayToString
52+
However, they all still available by default due to a new setting called
53+
`LEGACY_RUNTIME` which is enabled by default. When `LEGACY_RUNTIME` is
54+
disabled (which it may be in the future) these symbols would only be included
55+
if there were explictly exported via `EXPORTED_RUNTIME_METHODS` or added to
56+
`DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`. `LEGACY_RUNTIME` is disabled by default
57+
in `STRICT` mode so this change only effects users of `STRICT` mode. (#17370,
58+
#17403)
4159
- The `run` runtime function is no longer exported by default. It can be added
4260
to `EXPORTED_RUNTIME_METHODS` if needed.
4361
- The getWasmTableEntry/setWasmTableEntry library function are no longer

emcc.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1788,11 +1788,13 @@ def phase_linker_setup(options, state, newargs, user_settings):
17881788
# errno support by default.
17891789
if settings.MINIMAL_RUNTIME:
17901790
default_setting(user_settings, 'SUPPORT_ERRNO', 0)
1791+
default_setting(user_settings, 'LEGACY_RUNTIME', 0)
17911792
# Require explicit -lfoo.js flags to link with JS libraries.
17921793
default_setting(user_settings, 'AUTO_JS_LIBRARIES', 0)
17931794

17941795
if settings.STRICT:
17951796
default_setting(user_settings, 'STRICT_JS', 1)
1797+
default_setting(user_settings, 'LEGACY_RUNTIME', 0)
17961798
default_setting(user_settings, 'AUTO_JS_LIBRARIES', 0)
17971799
default_setting(user_settings, 'AUTO_NATIVE_LIBRARIES', 0)
17981800
default_setting(user_settings, 'AUTO_ARCHIVE_INDEXES', 0)
@@ -2132,7 +2134,9 @@ def phase_linker_setup(options, state, newargs, user_settings):
21322134
'$jsStackTrace',
21332135
'$stackTrace',
21342136
# Called by `callMain` to handle exceptions
2135-
'$handleException'
2137+
'$handleException',
2138+
# Needed by ccall (remove this once ccall itself is a library function)
2139+
'$writeArrayToMemory',
21362140
]
21372141

21382142
if not settings.STANDALONE_WASM and (settings.EXIT_RUNTIME or settings.ASSERTIONS):
@@ -2873,6 +2877,8 @@ def phase_emscript(options, in_wasm, wasm_target, memfile):
28732877

28742878
if embed_memfile():
28752879
settings.SUPPORT_BASE64_EMBEDDING = 1
2880+
# _read in shell.js depends on intArrayToString when SUPPORT_BASE64_EMBEDDING is set
2881+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$intArrayToString')
28762882

28772883
emscripten.run(in_wasm, wasm_target, final_js, memfile)
28782884
save_intermediate('original')

emscripten.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ def update_settings_glue(wasm_file, metadata):
152152
# When using dynamic linking the main function might be in a side module.
153153
# To be safe assume they do take input parametes.
154154
settings.MAIN_READS_PARAMS = metadata['mainReadsParams'] or bool(settings.MAIN_MODULE)
155+
if settings.MAIN_READS_PARAMS and not settings.STANDALONE_WASM:
156+
# callMain depends on this library function
157+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$allocateUTF8OnStack']
155158

156159
if settings.STACK_OVERFLOW_CHECK and not settings.SIDE_MODULE:
157160
settings.EXPORTED_RUNTIME_METHODS += ['writeStackCookie', 'checkStackCookie']

src/embind/embind.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,8 @@ var LibraryEmbind = {
784784
_embind_register_std_wstring__sig: 'vppp',
785785
_embind_register_std_wstring__deps: [
786786
'$readLatin1String', '$registerType', '$simpleReadValueFromPointer',
787-
#if MINIMAL_RUNTIME
788787
'$UTF16ToString', '$stringToUTF16', '$lengthBytesUTF16',
789788
'$UTF32ToString', '$stringToUTF32', '$lengthBytesUTF32',
790-
#endif
791789
],
792790
_embind_register_std_wstring: function(rawType, charSize, name) {
793791
name = readLatin1String(name);

src/jsifier.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,6 @@ function ${name}(${args}) {
502502

503503
if (!MINIMAL_RUNTIME) {
504504
print('var ASSERTIONS = ' + !!ASSERTIONS + ';\n');
505-
506-
print(preprocess(read('arrayUtils.js')));
507505
}
508506

509507
if ((SUPPORT_BASE64_EMBEDDING || FORCE_FILESYSTEM) && !MINIMAL_RUNTIME) {

src/library.js

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,7 @@ mergeInto(LibraryManager.library, {
565565
tzset_impl__internal: true,
566566
tzset_impl__proxy: 'sync',
567567
tzset_impl__sig: 'viii',
568-
tzset_impl__deps: [
569-
#if MINIMAL_RUNTIME
570-
'$allocateUTF8'
571-
#endif
572-
],
568+
tzset_impl__deps: ['$allocateUTF8'],
573569
tzset_impl: function(timezone, daylight, tzname) {
574570
var currentYear = new Date().getFullYear();
575571
var winter = new Date(currentYear, 0, 1);
@@ -654,10 +650,8 @@ mergeInto(LibraryManager.library, {
654650

655651
// Note: this is not used in STANDALONE_WASM mode, because it is more
656652
// compact to do it in JS.
657-
strftime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP'
658-
#if MINIMAL_RUNTIME
659-
, '$intArrayFromString', '$writeArrayToMemory'
660-
#endif
653+
strftime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP',
654+
'$intArrayFromString', '$writeArrayToMemory'
661655
],
662656
strftime__sig: 'ppppp',
663657
strftime: function(s, maxsize, format, tm) {
@@ -953,11 +947,8 @@ mergeInto(LibraryManager.library, {
953947
return _strftime(s, maxsize, format, tm); // no locale support yet
954948
},
955949

956-
strptime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP', '$jstoi_q'
957-
#if MINIMAL_RUNTIME
958-
, '$intArrayFromString'
959-
#endif
960-
],
950+
strptime__deps: ['_isLeapYear', '_arraySum', '_addDays', '_MONTH_DAYS_REGULAR', '_MONTH_DAYS_LEAP',
951+
'$jstoi_q', '$intArrayFromString' ],
961952
strptime__sig: 'pppp',
962953
strptime: function(buf, format, tm) {
963954
// char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tm);
@@ -2078,11 +2069,7 @@ mergeInto(LibraryManager.library, {
20782069
list: [],
20792070
map: {}
20802071
},
2081-
setprotoent__deps: ['$Protocols'
2082-
#if MINIMAL_RUNTIME
2083-
, '$writeAsciiToMemory'
2084-
#endif
2085-
],
2072+
setprotoent__deps: ['$Protocols', '$writeAsciiToMemory'],
20862073
setprotoent: function(stayopen) {
20872074
// void setprotoent(int stayopen);
20882075

@@ -2771,13 +2758,7 @@ mergeInto(LibraryManager.library, {
27712758

27722759
// Look up the function name from our stack frame cache with our PC representation.
27732760
#if USE_OFFSET_CONVERTER
2774-
emscripten_pc_get_function__deps: [
2775-
'$UNWIND_CACHE',
2776-
'free',
2777-
#if MINIMAL_RUNTIME
2778-
'$allocateUTF8',
2779-
#endif
2780-
],
2761+
emscripten_pc_get_function__deps: ['$UNWIND_CACHE', 'free', '$allocateUTF8'],
27812762
// Don't treat allocation of _emscripten_pc_get_function.ret as a leak
27822763
emscripten_pc_get_function__noleakcheck: true,
27832764
emscripten_pc_get_function__sig: 'pp',
@@ -2838,11 +2819,7 @@ mergeInto(LibraryManager.library, {
28382819
},
28392820

28402821
// Look up the file name from our stack frame cache with our PC representation.
2841-
emscripten_pc_get_file__deps: ['$convertPCtoSourceLocation', 'free',
2842-
#if MINIMAL_RUNTIME
2843-
'$allocateUTF8',
2844-
#endif
2845-
],
2822+
emscripten_pc_get_file__deps: ['$convertPCtoSourceLocation', 'free', '$allocateUTF8'],
28462823
// Don't treat allocation of _emscripten_pc_get_file.ret as a leak
28472824
emscripten_pc_get_file__noleakcheck: true,
28482825
emscripten_pc_get_file__sig: 'pp',
@@ -3651,3 +3628,28 @@ function autoAddDeps(object, name) {
36513628
}
36523629
}
36533630
}
3631+
3632+
#if LEGACY_RUNTIME
3633+
// Library functions that were previously included as runtime functions are
3634+
// automatically included when `LEGACY_RUNTIME` is set.
3635+
DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.push(
3636+
'$addFunction',
3637+
'$removeFunction',
3638+
'$allocate',
3639+
'$AsciiToString',
3640+
'$stringToAscii',
3641+
'$UTF16ToString',
3642+
'$stringToUTF16',
3643+
'$lengthBytesUTF16',
3644+
'$UTF32ToString',
3645+
'$stringToUTF32',
3646+
'$lengthBytesUTF32',
3647+
'$allocateUTF8',
3648+
'$allocateUTF8OnStack',
3649+
'$writeStringToMemory',
3650+
'$writeArrayToMemory',
3651+
'$writeAsciiToMemory',
3652+
'$intArrayFromString',
3653+
'$intArrayToString',
3654+
);
3655+
#endif

src/library_addfunction.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ mergeInto(LibraryManager.library, {
151151

152152
$updateTableMap__deps: ['$getWasmTableEntry'],
153153
$updateTableMap: function(offset, count) {
154-
for (var i = offset; i < offset + count; i++) {
155-
var item = getWasmTableEntry(i);
156-
// Ignore null values.
157-
if (item) {
158-
functionsInTableMap.set(item, i);
154+
if (functionsInTableMap) {
155+
for (var i = offset; i < offset + count; i++) {
156+
var item = getWasmTableEntry(i);
157+
// Ignore null values.
158+
if (item) {
159+
functionsInTableMap.set(item, i);
160+
}
159161
}
160162
}
161163
},

src/library_bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mergeInto(LibraryManager.library, {
2323

2424
// printf/puts implementations for when musl is not pulled in - very
2525
// partial, but enough for bootstrapping structInfo
26-
printf__deps: ['$formatString'],
26+
printf__deps: ['$formatString', '$intArrayToString'],
2727
printf__sig: 'ipp',
2828
printf: function(format, varargs) {
2929
// int printf(const char *restrict format, ...);

src/library_dylink.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,7 @@ var LibraryDylink = {
249249
},
250250

251251
$dlSetError__internal: true,
252-
$dlSetError__deps: ['__dl_seterr',
253-
#if MINIMAL_RUNTIME
254-
'$intArrayFromString'
255-
#endif
256-
],
252+
$dlSetError__deps: ['__dl_seterr', '$allocateUTF8OnStack'],
257253
$dlSetError: function(msg) {
258254
withStackSave(function() {
259255
var cmsg = allocateUTF8OnStack(msg);
@@ -505,6 +501,7 @@ var LibraryDylink = {
505501
'$getDylinkMetadata', '$alignMemory', '$zeroMemory',
506502
'$alignMemory', '$zeroMemory',
507503
'$CurrentModuleWeakSymbols', '$alignMemory', '$zeroMemory',
504+
'$updateTableMap',
508505
],
509506
$loadWebAssemblyModule: function(binary, flags, handle) {
510507
var metadata = getDylinkMetadata(binary);

src/library_egl.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,7 @@ var LibraryEGL = {
522522
},
523523

524524
// EGLAPI const char * EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name);
525-
#if MINIMAL_RUNTIME
526525
eglQueryString__deps: ['$allocateUTF8'],
527-
#endif
528526
eglQueryString__proxy: 'sync',
529527
eglQueryString__sig: 'iii',
530528
eglQueryString: function(display, name) {

0 commit comments

Comments
 (0)