Skip to content

Commit d41861d

Browse files
committed
Convert extended string handling functions into JS library code
1 parent fb8375f commit d41861d

29 files changed

+377
-414
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 may be the default soon) these symbol would only be included
55+
if there were explictly exported via `EXPORTED_RUNTIME_METHODS`
56+
or added to `DEFAULT_LIBRARY_FUNCS_TO_INCLUDE`.
57+
`LEGACY_RUNTIME` is disabled bv default in `STRICT` mode so this change
58+
only effects users of `STRICT` mode. (#17370)
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,7 @@ def phase_linker_setup(options, state, newargs, user_settings):
17931793

17941794
if settings.STRICT:
17951795
default_setting(user_settings, 'STRICT_JS', 1)
1796+
default_setting(user_settings, 'LEGACY_RUNTIME', 1)
17961797
default_setting(user_settings, 'AUTO_JS_LIBRARIES', 0)
17971798
default_setting(user_settings, 'AUTO_NATIVE_LIBRARIES', 0)
17981799
default_setting(user_settings, 'AUTO_ARCHIVE_INDEXES', 0)
@@ -2132,7 +2133,9 @@ def phase_linker_setup(options, state, newargs, user_settings):
21322133
'$jsStackTrace',
21332134
'$stackTrace',
21342135
# Called by `callMain` to handle exceptions
2135-
'$handleException'
2136+
'$handleException',
2137+
# Needed by ccall (remove this once ccall itself is a library function)
2138+
'$writeArrayToMemory',
21362139
]
21372140

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

28742877
if embed_memfile():
28752878
settings.SUPPORT_BASE64_EMBEDDING = 1
2879+
# _read in shell.js depends on intArrayToString when SUPPORT_BASE64_EMBEDDING is set
2880+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$intArrayToString')
28762881

28772882
emscripten.run(in_wasm, wasm_target, final_js, memfile)
28782883
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
@@ -506,8 +506,6 @@ function ${name}(${args}) {
506506

507507
if (!MINIMAL_RUNTIME) {
508508
print('var ASSERTIONS = ' + !!ASSERTIONS + ';\n');
509-
510-
print(preprocess(read('arrayUtils.js')));
511509
}
512510

513511
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 function that were previosuly included as runtime functions can be
3634+
// included by setting `LEGACY_RUNTIME`.
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) {

src/library_formatString.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ mergeInto(LibraryManager.library, {
5252
// varargs: A pointer to the start of the arguments list.
5353
// Returns the resulting string string as a character array.
5454
$formatString__deps: ['$reallyNegative', '$convertI32PairToI53', '$convertU32PairToI53',
55-
'$reSign', '$unSign', '$strLen',
56-
#if MINIMAL_RUNTIME
57-
, '$intArrayFromString'
58-
#endif
55+
'$reSign', '$unSign', '$strLen', '$intArrayFromString'
5956
],
6057
$formatString: function(format, varargs) {
6158
#if ASSERTIONS

src/library_fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
mergeInto(LibraryManager.library, {
8-
$FS__deps: ['$getRandomDevice', '$PATH', '$PATH_FS', '$TTY', '$MEMFS', '$asyncLoad',
8+
$FS__deps: ['$getRandomDevice', '$PATH', '$PATH_FS', '$TTY', '$MEMFS', '$asyncLoad', '$intArrayFromString',
99
#if LibraryManager.has('library_idbfs.js')
1010
'$IDBFS',
1111
#endif

src/library_glew.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121

2222
var LibraryGLEW = {
23-
$GLEW__deps: ['glGetString'],
23+
$GLEW__deps: ['glGetString', '$allocateUTF8'],
2424
$GLEW: {
2525
isLinaroFork: 1,
2626
extensions: null,

src/library_glfw.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ var LibraryGLFW = {
8181

8282
$GLFW__deps: ['emscripten_get_now', '$GL', '$Browser', '$GLFW_Window',
8383
'$callUserCallback',
84+
'$allocateUTF8',
8485
#if FILESYSTEM
8586
'$FS',
8687
#endif

src/library_openal.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,7 @@ var LibraryOpenAL = {
24022402

24032403
alcGetString__proxy: 'sync',
24042404
alcGetString__sig: 'iii',
2405+
alcGetString__deps: ['$allocateUTF8'],
24052406
alcGetString: function(deviceId, param) {
24062407
if (AL.alcStringCache[param]) {
24072408
return AL.alcStringCache[param];
@@ -2657,7 +2658,7 @@ var LibraryOpenAL = {
26572658

26582659
emscripten_alcGetStringiSOFT__proxy: 'sync',
26592660
emscripten_alcGetStringiSOFT__sig: 'iiii',
2660-
emscripten_alcGetStringiSOFT__deps: ['alcGetString'],
2661+
emscripten_alcGetStringiSOFT__deps: ['alcGetString', '$allocateUTF8'],
26612662
emscripten_alcGetStringiSOFT: function(deviceId, param, index) {
26622663
if (!(deviceId in AL.deviceRefCounts)) {
26632664
#if OPENAL_DEBUG
@@ -3054,6 +3055,7 @@ var LibraryOpenAL = {
30543055

30553056
alGetString__proxy: 'sync',
30563057
alGetString__sig: 'ii',
3058+
alGetString__deps: ['$allocateUTF8'],
30573059
alGetString: function(param) {
30583060
if (AL.stringCache[param]) {
30593061
return AL.stringCache[param];

src/library_sdl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ var LibrarySDL = {
17791779

17801780
SDL_GetKeyName__proxy: 'sync',
17811781
SDL_GetKeyName__sig: 'ii',
1782+
SDL_GetKeyName__deps: ['$allocateUTF8'],
17821783
SDL_GetKeyName: function(key) {
17831784
if (!SDL.keyName) {
17841785
SDL.keyName = allocateUTF8('unknown key');
@@ -1841,6 +1842,7 @@ var LibrarySDL = {
18411842

18421843
SDL_GetError__proxy: 'sync',
18431844
SDL_GetError__sig: 'i',
1845+
SDL_GetError__deps: ['$allocateUTF8'],
18441846
SDL_GetError: function() {
18451847
if (!SDL.errorMessage) {
18461848
SDL.errorMessage = allocateUTF8("unknown SDL-emscripten error");
@@ -3573,6 +3575,7 @@ var LibrarySDL = {
35733575

35743576
SDL_JoystickName__proxy: 'sync',
35753577
SDL_JoystickName__sig: 'ii',
3578+
SDL_JoystickName__deps: ['$allocateUTF8'],
35763579
SDL_JoystickName: function(deviceIndex) {
35773580
var gamepad = SDL.getGamepad(deviceIndex);
35783581
if (gamepad) {
@@ -3712,6 +3715,7 @@ var LibrarySDL = {
37123715
},
37133716

37143717
SDL_GetNumAudioDrivers: function() { return 1 },
3718+
SDL_GetCurrentAudioDriver__deps: ['$allocateUTF8'],
37153719
SDL_GetCurrentAudioDriver: function() {
37163720
return allocateUTF8('Emscripten Audio');
37173721
},

0 commit comments

Comments
 (0)