From bb77de292700ac1fe438ea0b3e5f31cd65d0eb54 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 30 Nov 2026 18:34:55 +0000 Subject: [PATCH 1/2] console.log: null strings and format placeholders; Math.round and parseFloat as JavaScript's Found by comparing programs' output with Node's: - console.log of a null string printed through it (a crash without strict null checks): it is "null" - console.log fills %s %d %i %f %j %o %O %c from the arguments after the first, and %% is "%" - Math.round rounds a half up (Math.round(-2.5) is -2; C's round gave -3), -0 between -0.5 and 0 - parseFloat is NaN where the text has no number (strtod gave 0), reads no hexadecimal, and "Infinity" only spelled so Needs the compiler of the same change (console.log arguments made strings in Node's inspect form). Co-Authored-By: Claude Opus 5.5 --- src/lib.ts | 118 +++++++++++++++++++++++++++++++++---- tests/node_output_forms.ts | 59 +++++++++++++++++++ 2 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 tests/node_output_forms.ts diff --git a/src/lib.ts b/src/lib.ts index 268adaa..acc0f7f 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -8,7 +8,45 @@ export function parseInt(val: string, radix = 10) { return strtol(val, null, radix); } +// The number at the start of the text, after white space, as JavaScript reads it: NaN where there is none +// (`parseFloat("abc")`; strtod gave 0), "Infinity" spelled so only, and no hexadecimal ("0x10" is 0, strtod +// read 16) export function parseFloat(val: string): number { + if (val == null) { + return Number.NaN; + } + + let p = 0; + while (p < val.length) { + const space = val.charCodeAt(p); + if (space != 32 && (space < 9 || space > 13)) { + break; + } + + p++; + } + + const sign = p < val.length ? val.charCodeAt(p) : 0; + const q = sign == 43 /* + */ || sign == 45 /* - */ ? p + 1 : p; + const c = q < val.length ? val.charCodeAt(q) : 0; + const d = q + 1 < val.length ? val.charCodeAt(q + 1) : 0; + if (c == 73 /* I */) { + if (val.substring(q, q + 8) != "Infinity") { + return Number.NaN; + } + + return sign == 45 ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; + } + + const startsDigit = c >= 48 && c <= 57; + if (!startsDigit && !(c == 46 /* . */ && d >= 48 && d <= 57)) { + return Number.NaN; + } + + if (c == 48 && (d == 120 /* x */ || d == 88 /* X */)) { + return sign == 45 ? -0.0 : 0.0; + } + return strtod(val, null); } @@ -1905,7 +1943,11 @@ export static class Math { * @param x The value to be rounded to the nearest integer. */ public round(x: number): number { - return round(x); + // a half goes up, as JavaScript rounds it (Math.round(-2.5) is -2); C's round takes it away from zero (-3). + // Between -0.5 and 0 the result is -0. NaN and the infinities are themselves (x - r is NaN). + const r = floor(x); + const rounded = x - r >= 0.5 ? r + 1 : r; + return rounded == 0 && x < 0 ? -0.0 : rounded; } /** @@ -3521,21 +3563,77 @@ export static class console { } private printData(fileNo: int, data: string[]): void { - switch (data.length) { - case 0: - break; - default: - this.print(fileNo, data[0]); - for (let i = 1; i < data.length; i++) { - this.print(fileNo, " "); - this.print(fileNo, data[i]); - } + // the arguments the first one's placeholders take (`console.log("%s: %d", name, n)`) + let next = 1; + if (data.length > 1 && data[0] != null && data[0].indexOf("%") >= 0) { + next = this.printFormat(fileNo, data); + } else if (data.length > 0) { + this.print(fileNo, data[0]); + } + + for (let i = next; i < data.length; i++) { + this.print(fileNo, " "); + this.print(fileNo, data[i]); } this.print(fileNo, "\n"); } + // The first argument with its placeholders replaced by the arguments after it, as Node's + // util.format: %s %d %i %f %j %o %O %c each take the next argument (%c is CSS, and prints nothing), + // %% is "%". The arguments are strings already: %i is the integer part, the others the text + // itself. A placeholder with no argument left stays as it is. Returns the first argument not taken. + private printFormat(fileNo: int, data: string[]): int { + const format = data[0]; + let next = 1; + let run = 0; + let i = 0; + while (i < format.length - 1) { + if (format.charCodeAt(i) != 37 /* % */) { + i++; + continue; + } + + const c = format.charCodeAt(i + 1); + if (c == 37) { + this.print(fileNo, format.substring(run, i + 1)); + i += 2; + run = i; + continue; + } + + const takes = c == 115 /* s */ || c == 100 /* d */ || c == 105 /* i */ || c == 102 /* f */ + || c == 106 /* j */ || c == 111 /* o */ || c == 79 /* O */ || c == 99 /* c */; + if (!takes || next >= data.length) { + i++; + continue; + } + + this.print(fileNo, format.substring(run, i)); + const value = data[next++]; + if (c == 105) { + const dot = value == null ? -1 : value.indexOf("."); + this.print(fileNo, dot >= 0 ? value.substring(0, dot) : value); + } else if (c != 99) { + this.print(fileNo, value); + } + + i += 2; + run = i; + } + + this.print(fileNo, format.substring(run)); + return next; + } + private print(fileNo: int, data: string): void { + // a null string (`console.log(s)` of a `let s: string = null` without strict null checks) is "null"; it was + // read through + if (data == null) { + stdio_write(fileNo, "null", 4); + return; + } + // stdio_write, not write: going through stdio keeps this in the same buffer // as print()/assert(), which lower to puts(). See src/wrappers/io.cpp. stdio_write(fileNo, data, data.length); diff --git a/tests/node_output_forms.ts b/tests/node_output_forms.ts new file mode 100644 index 0000000..a38f7d5 --- /dev/null +++ b/tests/node_output_forms.ts @@ -0,0 +1,59 @@ +// @strict-null false +// What a comparison of programs' output with Node's found (2026-11-30): +// - Math.round rounded a half away from zero (C's round): Math.round(-2.5) was -3, JavaScript's is -2 +// - parseFloat gave 0 where the text has no number ("abc"; JavaScript's is NaN), and read hexadecimal +// - console.log of a null string read through it (a crash); "%s" and the other placeholders were printed as they are +// - console.log printed a value as String() makes it: a string[] as `a,b`, a tuple as `{ 1, 'a' }` + +function check(condition: boolean, message: string) { + if (!condition) { + console.log("fail:", message); + assert(false, message); + } +} + +function testRound() { + check(Math.round(-2.5) == -2, "Math.round(-2.5) is -2"); + check(Math.round(2.5) == 3, "Math.round(2.5) is 3"); + check(Math.round(-3.7) == -4, "Math.round(-3.7) is -4"); + check(Math.round(0.49999999999999994) == 0, "the largest double below 0.5 rounds to 0"); + const negativeZero = Math.round(-0.4); + check(negativeZero == 0 && 1 / negativeZero < 0, "Math.round(-0.4) is -0"); + check(isNaN(Math.round(Number.NaN)), "NaN is NaN"); + check(Math.round(Number.POSITIVE_INFINITY) == Number.POSITIVE_INFINITY, "Infinity is Infinity"); +} + +function testParseFloat() { + check(isNaN(parseFloat("abc")), "no number is NaN"); + check(isNaN(parseFloat("")), "an empty text is NaN"); + check(parseFloat("12px") == 12, "the number at the start"); + check(parseFloat(" -2.5e1x") == -25, "after white space, with an exponent"); + check(parseFloat(".5") == 0.5, "a fraction"); + check(parseFloat("0x10") == 0, "no hexadecimal"); + check(parseFloat("-Infinity") == Number.NEGATIVE_INFINITY, "-Infinity"); + check(isNaN(parseFloat("inf")), "only Infinity spelled so"); +} + +function testNumber() { + check(isNaN(Number("abc")), "Number('abc') is NaN"); + check(Number("") == 0, "Number('') is 0"); + check(Number(" 42 ") == 42, "white space around it"); + check(isNaN(Number("12px")), "anything after it"); +} + +function testConsole() { + // printed, not checked here: these crashed or printed the placeholders + let nothing: string = null; + console.log(nothing, "after a null string"); + console.log("%s has %d items (%i%%)", "the list", 3, 42.9, "and more"); + const names: string[] = []; + names.push("x"); + console.log(names, [1, "a"], { a: 1, b: [2, 3] }); +} + +testRound(); +testParseFloat(); +testNumber(); +testConsole(); + +console.log("ALL DONE"); From ce5749ad0d1573d7dc1044a25c17710d6acad27e Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Mon, 30 Nov 2026 20:06:39 +0000 Subject: [PATCH 2/2] node_output_forms: no Number() checks (the compiler's conversion; its own test is 00number_conversion_forms) Number("abc") is NaN only with the compiler of TypeScriptCompiler#1080, and this repository's CI tests with the newest compiler release. Co-Authored-By: Claude Opus 5.5 --- tests/node_output_forms.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/node_output_forms.ts b/tests/node_output_forms.ts index a38f7d5..e116751 100644 --- a/tests/node_output_forms.ts +++ b/tests/node_output_forms.ts @@ -34,13 +34,6 @@ function testParseFloat() { check(isNaN(parseFloat("inf")), "only Infinity spelled so"); } -function testNumber() { - check(isNaN(Number("abc")), "Number('abc') is NaN"); - check(Number("") == 0, "Number('') is 0"); - check(Number(" 42 ") == 42, "white space around it"); - check(isNaN(Number("12px")), "anything after it"); -} - function testConsole() { // printed, not checked here: these crashed or printed the placeholders let nothing: string = null; @@ -53,7 +46,6 @@ function testConsole() { testRound(); testParseFloat(); -testNumber(); testConsole(); console.log("ALL DONE");