Skip to content

fix: (wasi) fix some potential issues. Make messages and labels as optional in console #2047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions std/assembly/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {

export namespace console {

export function assert<T>(condition: T, message: string): void {
export function assert<T>(condition: T, message: string = ""): void {
if (!condition) {
let stderr = process.stderr;
stderr.write("Assertion failed: ");
Expand All @@ -16,41 +16,41 @@ export namespace console {
}
}

export function log(message: string): void {
export function log(message: string = ""): void {
var stdout = process.stdout;
stdout.write(message);
stdout.write("\n");
}

export function debug(message: string): void {
export function debug(message: string = ""): void {
var stdout = process.stdout;
stdout.write("Debug: ");
stdout.write(message);
stdout.write("\n");
}

export function info(message: string): void {
export function info(message: string = ""): void {
var stdout = process.stdout;
stdout.write("Info: ");
stdout.write(message);
stdout.write("\n");
}

export function warn(message: string): void {
export function warn(message: string = ""): void {
var stdout = process.stdout;
stdout.write("Warning: ");
stdout.write(message);
stdout.write("\n");
}

export function error(message: string): void {
export function error(message: string = ""): void {
var stdout = process.stdout;
stdout.write("Error: ");
stdout.write(message);
stdout.write("\n");
}

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

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

export function timeEnd(label: string): void {
export function timeEnd(label: string = "default"): void {
var stdout = process.stdout;
if (!timers.has(label)) {
stdout.write("Warning: No such label '");
Expand Down
18 changes: 9 additions & 9 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2133,23 +2133,23 @@ declare namespace process {
/** Browser-like console on top of WASI. */
declare namespace console {
/** Logs `message` to console if `assertion` is false-ish. */
export function assert<T>(assertion: T, message: string): void;
export function assert<T>(assertion: T, message?: string): void;
/** Outputs `message` to the console. */
export function log(message: string): void;
export function log(message?: string): void;
/** Outputs `message` to the console, prefixed with "Debug:". */
export function debug(message: string): void;
export function debug(message?: string): void;
/** Outputs `message` to the console, prefixed with "Info:". */
export function info(message: string): void;
export function info(message?: string): void;
/** Outputs `message` to the console, prefixed with "Warning:". */
export function warn(message: string): void;
export function warn(message?: string): void;
/** Outputs `message` to the console, prefixed with "Error:". */
export function error(message: string): void;
export function error(message?: string): void;
/** Starts a new timer using the specified `label`. */
export function time(label: string): void;
export function time(label?: string): void;
/** Logs the current value of a timer previously started with `console.time`. */
export function timeLog(label: string): void;
export function timeLog(label?: string): void;
/** Logs the current value of a timer previously started with `console.time` and discards the timer. */
export function timeEnd(label: string): void;
export function timeEnd(label?: string): void;
}

/** Browser-like crypto utilities on top of WASI. */
Expand Down
22 changes: 12 additions & 10 deletions std/assembly/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,29 @@ function writeBuffer(fd: fd, data: ArrayBuffer): void {
}

function writeString(fd: fd, data: string): void {
var char2 = -1;
var char3 = -1;
var char4 = -1;
switch (data.length) {
var len = data.length;
var
char2: u32 = 0,
char3: u32 = 0,
char4: u32 = 0;
switch (len) {
case 4: { // "null"
char4 = <i32>load<u16>(changetype<usize>(data), 6);
char4 = <u32>load<u16>(changetype<usize>(data), 6);
if (char4 >= 0x80) break;
}
case 3: { // "ms\n"
char3 = <i32>load<u16>(changetype<usize>(data), 4);
char3 = <u32>load<u16>(changetype<usize>(data), 4);
if (char3 >= 0x80) break;
}
case 2: { // "\r\n"
char2 = <i32>load<u16>(changetype<usize>(data), 2);
char2 = <u32>load<u16>(changetype<usize>(data), 2);
if (char2 >= 0x80) break;
}
case 1: { // "\n"
let char1 = <i32>load<u16>(changetype<usize>(data));
let char1 = <u32>load<u16>(changetype<usize>(data));
if (char1 >= 0x80) break;
store<usize>(iobuf, iobuf + 2 * sizeof<usize>());
store<usize>(iobuf, <i32>1 + i32(char2 != -1) + i32(char3 != -1) + i32(char4 != -1), sizeof<usize>());
store<usize>(iobuf, len, sizeof<usize>());
store<u32>(iobuf, char1 | char2 << 8 | char3 << 16 | char4 << 24, 2 * sizeof<usize>());
let err = fd_write(<u32>fd, iobuf, 1, iobuf + 3 * sizeof<usize>());
if (err) throw new Error(errnoToString(err));
Expand All @@ -181,7 +183,7 @@ function writeString(fd: fd, data: string): void {
}
var utf8len = <usize>String.UTF8.byteLength(data);
var utf8buf = __alloc(utf8len);
assert(String.UTF8.encodeUnsafe(changetype<usize>(data), data.length, utf8buf) == utf8len);
assert(String.UTF8.encodeUnsafe(changetype<usize>(data), len, utf8buf) == utf8len);
store<usize>(iobuf, utf8buf);
store<usize>(iobuf, utf8len, sizeof<usize>());
var err = fd_write(<u32>fd, iobuf, 1, iobuf + 2 * sizeof<usize>());
Expand Down
Loading