Skip to content

Commit 8e75367

Browse files
authored
fix: (wasi) fix some potential issues. Make messages and labels as optional in console (#2047)
1 parent 587c414 commit 8e75367

File tree

9 files changed

+238
-331
lines changed

9 files changed

+238
-331
lines changed

std/assembly/console.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
export namespace console {
99

10-
export function assert<T>(condition: T, message: string): void {
10+
export function assert<T>(condition: T, message: string = ""): void {
1111
if (!condition) {
1212
let stderr = process.stderr;
1313
stderr.write("Assertion failed: ");
@@ -16,41 +16,41 @@ export namespace console {
1616
}
1717
}
1818

19-
export function log(message: string): void {
19+
export function log(message: string = ""): void {
2020
var stdout = process.stdout;
2121
stdout.write(message);
2222
stdout.write("\n");
2323
}
2424

25-
export function debug(message: string): void {
25+
export function debug(message: string = ""): void {
2626
var stdout = process.stdout;
2727
stdout.write("Debug: ");
2828
stdout.write(message);
2929
stdout.write("\n");
3030
}
3131

32-
export function info(message: string): void {
32+
export function info(message: string = ""): void {
3333
var stdout = process.stdout;
3434
stdout.write("Info: ");
3535
stdout.write(message);
3636
stdout.write("\n");
3737
}
3838

39-
export function warn(message: string): void {
39+
export function warn(message: string = ""): void {
4040
var stdout = process.stdout;
4141
stdout.write("Warning: ");
4242
stdout.write(message);
4343
stdout.write("\n");
4444
}
4545

46-
export function error(message: string): void {
46+
export function error(message: string = ""): void {
4747
var stdout = process.stdout;
4848
stdout.write("Error: ");
4949
stdout.write(message);
5050
stdout.write("\n");
5151
}
5252

53-
export function time(label: string): void {
53+
export function time(label: string = "default"): void {
5454
var stdout = process.stdout;
5555
if (timers.has(label)) {
5656
stdout.write("Warning: Label '");
@@ -61,7 +61,7 @@ export namespace console {
6161
timers.set(label, process.hrtime());
6262
}
6363

64-
export function timeLog(label: string): void {
64+
export function timeLog(label: string = "default"): void {
6565
var stdout = process.stdout;
6666
if (!timers.has(label)) {
6767
stdout.write("Warning: No such label '");
@@ -72,7 +72,7 @@ export namespace console {
7272
timeLogImpl(label);
7373
}
7474

75-
export function timeEnd(label: string): void {
75+
export function timeEnd(label: string = "default"): void {
7676
var stdout = process.stdout;
7777
if (!timers.has(label)) {
7878
stdout.write("Warning: No such label '");

std/assembly/index.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,23 +2137,23 @@ declare namespace process {
21372137
/** Browser-like console on top of WASI. */
21382138
declare namespace console {
21392139
/** Logs `message` to console if `assertion` is false-ish. */
2140-
export function assert<T>(assertion: T, message: string): void;
2140+
export function assert<T>(assertion: T, message?: string): void;
21412141
/** Outputs `message` to the console. */
2142-
export function log(message: string): void;
2142+
export function log(message?: string): void;
21432143
/** Outputs `message` to the console, prefixed with "Debug:". */
2144-
export function debug(message: string): void;
2144+
export function debug(message?: string): void;
21452145
/** Outputs `message` to the console, prefixed with "Info:". */
2146-
export function info(message: string): void;
2146+
export function info(message?: string): void;
21472147
/** Outputs `message` to the console, prefixed with "Warning:". */
2148-
export function warn(message: string): void;
2148+
export function warn(message?: string): void;
21492149
/** Outputs `message` to the console, prefixed with "Error:". */
2150-
export function error(message: string): void;
2150+
export function error(message?: string): void;
21512151
/** Starts a new timer using the specified `label`. */
2152-
export function time(label: string): void;
2152+
export function time(label?: string): void;
21532153
/** Logs the current value of a timer previously started with `console.time`. */
2154-
export function timeLog(label: string): void;
2154+
export function timeLog(label?: string): void;
21552155
/** Logs the current value of a timer previously started with `console.time` and discards the timer. */
2156-
export function timeEnd(label: string): void;
2156+
export function timeEnd(label?: string): void;
21572157
}
21582158

21592159
/** Browser-like crypto utilities on top of WASI. */

std/assembly/process.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,29 @@ function writeBuffer(fd: fd, data: ArrayBuffer): void {
152152
}
153153

154154
function writeString(fd: fd, data: string): void {
155-
var char2 = -1;
156-
var char3 = -1;
157-
var char4 = -1;
158-
switch (data.length) {
155+
var len = data.length;
156+
var
157+
char2: u32 = 0,
158+
char3: u32 = 0,
159+
char4: u32 = 0;
160+
switch (len) {
159161
case 4: { // "null"
160-
char4 = <i32>load<u16>(changetype<usize>(data), 6);
162+
char4 = <u32>load<u16>(changetype<usize>(data), 6);
161163
if (char4 >= 0x80) break;
162164
}
163165
case 3: { // "ms\n"
164-
char3 = <i32>load<u16>(changetype<usize>(data), 4);
166+
char3 = <u32>load<u16>(changetype<usize>(data), 4);
165167
if (char3 >= 0x80) break;
166168
}
167169
case 2: { // "\r\n"
168-
char2 = <i32>load<u16>(changetype<usize>(data), 2);
170+
char2 = <u32>load<u16>(changetype<usize>(data), 2);
169171
if (char2 >= 0x80) break;
170172
}
171173
case 1: { // "\n"
172-
let char1 = <i32>load<u16>(changetype<usize>(data));
174+
let char1 = <u32>load<u16>(changetype<usize>(data));
173175
if (char1 >= 0x80) break;
174176
store<usize>(iobuf, iobuf + 2 * sizeof<usize>());
175-
store<usize>(iobuf, <i32>1 + i32(char2 != -1) + i32(char3 != -1) + i32(char4 != -1), sizeof<usize>());
177+
store<usize>(iobuf, len, sizeof<usize>());
176178
store<u32>(iobuf, char1 | char2 << 8 | char3 << 16 | char4 << 24, 2 * sizeof<usize>());
177179
let err = fd_write(<u32>fd, iobuf, 1, iobuf + 3 * sizeof<usize>());
178180
if (err) throw new Error(errnoToString(err));
@@ -181,7 +183,7 @@ function writeString(fd: fd, data: string): void {
181183
}
182184
var utf8len = <usize>String.UTF8.byteLength(data);
183185
var utf8buf = __alloc(utf8len);
184-
assert(String.UTF8.encodeUnsafe(changetype<usize>(data), data.length, utf8buf) == utf8len);
186+
assert(String.UTF8.encodeUnsafe(changetype<usize>(data), len, utf8buf) == utf8len);
185187
store<usize>(iobuf, utf8buf);
186188
store<usize>(iobuf, utf8len, sizeof<usize>());
187189
var err = fd_write(<u32>fd, iobuf, 1, iobuf + 2 * sizeof<usize>());

0 commit comments

Comments
 (0)