Skip to content

Commit

Permalink
Make second argument of UTF8ArrayToString optional. NFC (#22696)
Browse files Browse the repository at this point in the history
This is mostly a wash from a code size POV but makes the call sites more
readable I think.
  • Loading branch information
sbc100 authored Oct 9, 2024
1 parent 2be0e88 commit 94c3f85
Show file tree
Hide file tree
Showing 25 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ addToLibrary({
emscripten_log__deps: ['$formatString', '$emscriptenLog'],
emscripten_log: (flags, format, varargs) => {
var result = formatString(format, varargs);
var str = UTF8ArrayToString(result, 0);
var str = UTF8ArrayToString(result);
emscriptenLog(flags, str);
},
Expand Down
2 changes: 1 addition & 1 deletion src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ FS.staticInit();
var buf = new Uint8Array(length);
FS.read(stream, buf, 0, length, 0);
if (opts.encoding === 'utf8') {
ret = UTF8ArrayToString(buf, 0);
ret = UTF8ArrayToString(buf);
} else if (opts.encoding === 'binary') {
ret = buf;
}
Expand Down
6 changes: 3 additions & 3 deletions src/library_strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ addToLibrary({
* array that contains uint8 values, returns a copy of that string as a
* Javascript String object.
* heapOrArray is either a regular array, or a JavaScript typed array view.
* @param {number} idx
* @param {number=} idx
* @param {number=} maxBytesToRead
* @return {string}
*/`,
#if TEXTDECODER
$UTF8ArrayToString__deps: ['$UTF8Decoder'],
#endif
$UTF8ArrayToString: (heapOrArray, idx, maxBytesToRead) => {
$UTF8ArrayToString: (heapOrArray, idx = 0, maxBytesToRead = NaN) => {
#if CAN_ADDRESS_2GB
idx >>>= 0;
#endif
Expand All @@ -38,7 +38,7 @@ addToLibrary({
// null terminator by itself. Also, use the length info to avoid running tiny
// strings through TextDecoder, since .subarray() allocates garbage.
// (As a tiny code save trick, compare endPtr against endIdx using a negation,
// so that undefined means Infinity)
// so that undefined/NaN means Infinity)
while (heapOrArray[endPtr] && !(endPtr >= endIdx)) ++endPtr;
#endif // TEXTDECODER

Expand Down
8 changes: 4 additions & 4 deletions src/library_tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ addToLibrary({
},
put_char(tty, val) {
if (val === null || val === {{{ charCode('\n') }}}) {
out(UTF8ArrayToString(tty.output, 0));
out(UTF8ArrayToString(tty.output));
tty.output = [];
} else {
if (val != 0) tty.output.push(val); // val == 0 would cut text output off in the middle.
}
},
fsync(tty) {
if (tty.output && tty.output.length > 0) {
out(UTF8ArrayToString(tty.output, 0));
out(UTF8ArrayToString(tty.output));
tty.output = [];
}
},
Expand Down Expand Up @@ -143,15 +143,15 @@ addToLibrary({
default_tty1_ops: {
put_char(tty, val) {
if (val === null || val === {{{ charCode('\n') }}}) {
err(UTF8ArrayToString(tty.output, 0));
err(UTF8ArrayToString(tty.output));
tty.output = [];
} else {
if (val != 0) tty.output.push(val);
}
},
fsync(tty) {
if (tty.output && tty.output.length > 0) {
err(UTF8ArrayToString(tty.output, 0));
err(UTF8ArrayToString(tty.output));
tty.output = [];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/library_wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ var WasiLibrary = {
assert(buffer);
#endif
if (curr === 0 || curr === {{{ charCode('\n') }}}) {
(stream === 1 ? out : err)(UTF8ArrayToString(buffer, 0));
(stream === 1 ? out : err)(UTF8ArrayToString(buffer));
buffer.length = 0;
} else {
buffer.push(curr);
Expand Down
2 changes: 1 addition & 1 deletion src/library_wasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ FS.init();
// The buffer contents exist 8 bytes after the returned pointer.
var ret = new Uint8Array(HEAPU8.subarray(buf + 8, buf + 8 + length));
if (opts.encoding === 'utf8') {
ret = UTF8ArrayToString(ret, 0);
ret = UTF8ArrayToString(ret);
}

return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/source_map_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ if (!isDataURI(wasmSourceMapFile)) {

function getSourceMap() {
var buf = readBinary(wasmSourceMapFile);
return JSON.parse(UTF8ArrayToString(buf, 0, buf.length));
return JSON.parse(UTF8ArrayToString(buf));
}

function getSourceMapPromise() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var SYSCALLS = {
printChar: function(stream, curr) {
var buffer = SYSCALLS.buffers[stream];
if (curr === 0 || curr === 10) {
(stream === 1 ? out : err)(UTF8ArrayToString(buffer, 0));
(stream === 1 ? out : err)(UTF8ArrayToString(buffer));
buffer.length = 0;
} else {
buffer.push(curr);
Expand Down
2 changes: 1 addition & 1 deletion test/js_optimizer/applyImportAndExportNameChanges2.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var SYSCALLS = {
printChar: (function(stream, curr) {
var buffer = SYSCALLS.buffers[stream];
if (curr === 0 || curr === 10) {
(stream === 1 ? out : err)(UTF8ArrayToString(buffer, 0));
(stream === 1 ? out : err)(UTF8ArrayToString(buffer));
buffer.length = 0
} else {
buffer.push(curr)
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3899
3894
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8636
8628
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7594
7595
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18702
18696
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2984
2976
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6272
6265
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6319
6326
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13868
13872
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4948
4951
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10453
10465
4 changes: 2 additions & 2 deletions test/other/test_emit_tsd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ declare namespace RuntimeExports {
* array that contains uint8 values, returns a copy of that string as a
* Javascript String object.
* heapOrArray is either a regular array, or a JavaScript typed array view.
* @param {number} idx
* @param {number=} idx
* @param {number=} maxBytesToRead
* @return {string}
*/
function UTF8ArrayToString(heapOrArray: any, idx: number, maxBytesToRead?: number | undefined): string;
function UTF8ArrayToString(heapOrArray: any, idx?: number | undefined, maxBytesToRead?: number | undefined): string;
let wasmTable: WebAssembly.Table;
let HEAPF32: any;
let HEAPF64: any;
Expand Down
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54846
54854
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
30510
30518
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
53824
53832
2 changes: 1 addition & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ def test_module_stdout_stderr(self):
Module['postRun'] = () => {
assert(stderr.length === 0, 'stderr should be empty. \\n' +
'stderr: \\n' + stderr);
assert(UTF8ArrayToString(stdout, 0).startsWith('hello, world!'), 'stdout should start with the famous greeting. \\n' +
assert(UTF8ArrayToString(stdout).startsWith('hello, world!'), 'stdout should start with the famous greeting. \\n' +
'stdout: \\n' + stdout);
}
''')
Expand Down
2 changes: 1 addition & 1 deletion test/wasmfs/wasmfs_readfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int main() {

EM_ASM({
var output = FS.readFile("/root/test");
out(UTF8ArrayToString(output, 0));
out(UTF8ArrayToString(output));
out("Length: " + output.byteLength);
var err = FS.unlink("/root/test");
out("FS.unlink: " + err);
Expand Down

0 comments on commit 94c3f85

Please sign in to comment.