Skip to content

Commit 103a218

Browse files
authored
Move JS version of strlen/printf/puts to library_bootstrap (emscripten-core#16961)
These functions are only needed/used when generating struct info. Contrary to the comment we have not tests that use these functions.
1 parent 56ba041 commit 103a218

File tree

4 files changed

+42
-37
lines changed

4 files changed

+42
-37
lines changed

src/library_bootstrap.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,34 @@ assert(false, "library_bootstrap.js only designed for use with BOOTSTRAPPING_STR
1414
assert(Object.keys(LibraryManager.library).length === 0);
1515
mergeInto(LibraryManager.library, {
1616
$callRuntimeCallbacks: function() {},
17-
$handleException: function(e) { if (!e instanceof ExitStatus && !e == 'unwind') throw e; },
17+
18+
$handleException: function(e) {
19+
if (!(e instanceof ExitStatus) && e !== 'unwind') {
20+
throw e;
21+
}
22+
},
23+
24+
// printf/puts implementations for when musl is not pulled in - very
25+
// partial, but enough for bootstrapping structInfo
26+
27+
printf__deps: ['$formatString'],
28+
printf: function(format, varargs) {
29+
// int printf(const char *restrict format, ...);
30+
// http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html
31+
// extra effort to support printf, even without a filesystem. very partial, very hackish
32+
var result = formatString(format, varargs);
33+
var string = intArrayToString(result);
34+
if (string[string.length-1] === '\n') string = string.substr(0, string.length-1); // remove a final \n, as Module.print will do that
35+
out(string);
36+
return result.length;
37+
},
38+
39+
puts: function(s) {
40+
// extra effort to support puts, even without a filesystem. very partial, very hackish
41+
var result = UTF8ToString(s);
42+
var string = result.substr(0);
43+
if (string[string.length-1] === '\n') string = string.substr(0, string.length-1); // remove a final \n, as Module.print will do that
44+
out(string);
45+
return result.length;
46+
},
1847
});

src/library_formatString.js

+8-34
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ mergeInto(LibraryManager.library, {
2121
: Math.pow(2, bits) + value;
2222
},
2323

24+
$strLen: function(ptr) {
25+
var end = ptr;
26+
while (HEAPU8[end]) ++end;
27+
return end - ptr;
28+
},
29+
2430
// Converts a value we have as unsigned, into a signed value. For
2531
// example, 200 in a uint8 would be a negative number.
2632
$reSign: function(value, bits) {
@@ -46,7 +52,7 @@ mergeInto(LibraryManager.library, {
4652
// varargs: A pointer to the start of the arguments list.
4753
// Returns the resulting string string as a character array.
4854
$formatString__deps: ['$reallyNegative', '$convertI32PairToI53', '$convertU32PairToI53',
49-
'$reSign', '$unSign', 'strlen'
55+
'$reSign', '$unSign', '$strLen',
5056
#if MINIMAL_RUNTIME
5157
, '$intArrayFromString'
5258
#endif
@@ -409,7 +415,7 @@ mergeInto(LibraryManager.library, {
409415
case 's': {
410416
// String.
411417
var arg = getNextArg('i8*');
412-
var argLength = arg ? _strlen(arg) : '(null)'.length;
418+
var argLength = arg ? strLen(arg) : '(null)'.length;
413419
if (precisionSet) argLength = Math.min(argLength, precision);
414420
if (!flagLeftAlign) {
415421
while (argLength < width--) {
@@ -467,37 +473,5 @@ mergeInto(LibraryManager.library, {
467473
}
468474
return ret;
469475
},
470-
471-
// printf/puts/strlen implementations for when musl is not pulled in - very
472-
// partial. useful for tests, and when bootstrapping structInfo
473-
strlen: function(ptr) {
474-
{{{ from64('ptr') }}};
475-
var end = ptr;
476-
while (HEAPU8[end]) ++end;
477-
return end - ptr;
478-
},
479-
printf__deps: ['$formatString'
480-
#if MINIMAL_RUNTIME
481-
, '$intArrayToString'
482-
#endif
483-
],
484-
printf: function(format, varargs) {
485-
// int printf(const char *restrict format, ...);
486-
// http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html
487-
// extra effort to support printf, even without a filesystem. very partial, very hackish
488-
var result = formatString(format, varargs);
489-
var string = intArrayToString(result);
490-
if (string[string.length-1] === '\n') string = string.substr(0, string.length-1); // remove a final \n, as Module.print will do that
491-
out(string);
492-
return result.length;
493-
},
494-
puts: function(s) {
495-
// extra effort to support puts, even without a filesystem. very partial, very hackish
496-
var result = UTF8ToString(s);
497-
var string = result.substr(0);
498-
if (string[string.length-1] === '\n') string = string.substr(0, string.length-1); // remove a final \n, as Module.print will do that
499-
out(string);
500-
return result.length;
501-
},
502476
});
503477

tests/test_other.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3298,7 +3298,10 @@ def test_js_lib_quoted_key(self):
32983298
'<' : 0,
32993299
'white space' : 1
33003300
},
3301-
printf__deps: ['__internal_data', 'fprintf']
3301+
foo__deps: ['__internal_data'],
3302+
foo: function() {
3303+
return 0;
3304+
}
33023305
});
33033306
''')
33043307

tools/deps_info.py

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
'emscripten_idb_async_load': ['malloc', 'free'],
9595
'emscripten_idb_load': ['malloc', 'free'],
9696
'emscripten_init_websocket_to_posix_socket_bridge': ['malloc', 'free'],
97-
'emscripten_log': ['strlen'],
9897
# This list is the same as setjmp's dependencies. In non-LTO builds, setjmp
9998
# does not exist in the object files; it is converted into a code sequence
10099
# that includes several functions, one of which is emscripten_longjmp. This is

0 commit comments

Comments
 (0)