Skip to content

fix: [wasi] add new line option for string streams and writeLn. Minor fixes #2033

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

Closed
wants to merge 7 commits into from
Closed
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
69 changes: 31 additions & 38 deletions std/assembly/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,77 +7,70 @@ import {

export namespace console {

export function assert<T>(condition: T, message: string): void {
export function assert<T>(condition: T, message: string | null = null): void {
if (!condition) {
let stderr = process.stderr;
stderr.write("Assertion failed: ");
stderr.write(message);
stderr.write("\n");
stderr.writeLn(message !== null ? message : "console.assert");
}
}

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

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");
stdout.writeLn(message);
}

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");
stdout.writeLn(message);
}

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

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

export function time(label: string): void {
var stdout = process.stdout;
export function time(label: string = "default"): void {
if (timers.has(label)) {
stdout.write("Warning: Label '");
stdout.write(label);
stdout.write("' already exists for console.time()\n");
let stderr = process.stderr;
stderr.write("Warning: Label '");
stderr.write(label);
stderr.write("' already exists for console.time()\n");
return;
}
timers.set(label, process.hrtime());
}

export function timeLog(label: string): void {
var stdout = process.stdout;
export function timeLog(label: string = "default"): void {
if (!timers.has(label)) {
stdout.write("Warning: No such label '");
stdout.write(label);
stdout.write("' for console.timeLog()\n");
let stderr = process.stderr;
stderr.write("Warning: No such label '");
stderr.write(label);
stderr.write("' for console.timeLog()\n");
return;
}
timeLogImpl(label);
}

export function timeEnd(label: string): void {
var stdout = process.stdout;
export function timeEnd(label: string = "default"): void {
if (!timers.has(label)) {
stdout.write("Warning: No such label '");
stdout.write(label);
stdout.write("' for console.timeEnd()\n");
let stderr = process.stderr;
stderr.write("Warning: No such label '");
stderr.write(label);
stderr.write("' for console.timeEnd()\n");
return;
}
timeLogImpl(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
66 changes: 38 additions & 28 deletions std/assembly/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ abstract class WritableStream extends Stream {
ERROR("String or ArrayBuffer expected");
}
}

writeLn(data: string): void {
writeString(<u32>changetype<usize>(this), changetype<string>(data), true);
}
}

@unmanaged
Expand All @@ -151,37 +155,43 @@ function writeBuffer(fd: fd, data: ArrayBuffer): void {
if (err) throw new Error(errnoToString(err));
}

function writeString(fd: fd, data: string): void {
var char2 = -1;
var char3 = -1;
var char4 = -1;
switch (data.length) {
case 4: { // "null"
char4 = <i32>load<u16>(changetype<usize>(data), 6);
if (char4 >= 0x80) break;
}
case 3: { // "ms\n"
char3 = <i32>load<u16>(changetype<usize>(data), 4);
if (char3 >= 0x80) break;
}
case 2: { // "\r\n"
char2 = <i32>load<u16>(changetype<usize>(data), 2);
if (char2 >= 0x80) break;
}
case 1: { // "\n"
let char1 = <i32>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<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));
function writeString(fd: fd, data: string, newLine: bool = false): void {
var len = data.length;
if (!newLine) {
let
char2: u32 = 0,
char3: u32 = 0,
char4: u32 = 0;

switch (len) {
case 4: { // "null"
char4 = <u32>load<u16>(changetype<usize>(data), 6);
if (char4 >= 0x80) break;
}
case 3: { // "ms\n"
char3 = <u32>load<u16>(changetype<usize>(data), 4);
if (char3 >= 0x80) break;
}
case 2: { // "\r\n"
char2 = <u32>load<u16>(changetype<usize>(data), 2);
if (char2 >= 0x80) break;
}
case 1: { // "\n"
let char1 = <u32>load<u16>(changetype<usize>(data));
if (char1 >= 0x80) break;
store<usize>(iobuf, iobuf + 2 * 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));
}
case 0: return;
}
case 0: return;
}
var utf8len = <usize>String.UTF8.byteLength(data);
var utf8buf = __alloc(utf8len);
assert(String.UTF8.encodeUnsafe(changetype<usize>(data), data.length, utf8buf) == utf8len);
var utf8buf = __alloc(utf8len + usize(newLine));
assert(String.UTF8.encodeUnsafe(changetype<usize>(data), len, utf8buf) == utf8len);
if (newLine) store<u8>(utf8buf + utf8len, 0x0A); // \n
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