Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Open
42 changes: 42 additions & 0 deletions packages/ash/assembly/__tests__/error.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { stdout, Hello, World, readString, stderr } from './fixtures';
import { main as error } from "../bin/error";
import { main as cat } from "../bin/cat";
import { main as echo } from "../bin/echo";


describe("error", (): void => {

beforeEach((): void => {
stdout.reset();
Console.stdout.erase();
CommandLine.reset();

})

it("cat should cause an error if the file doesn't exist", (): void => {
CommandLine.push("cat");
CommandLine.push("thisshouldn'twork");
cat(CommandLine.all());

let status = error();

expect<i32>(status).toBe(1);




});

it("echo should not cause any error", (): void => {
CommandLine.push("echo");
CommandLine.push("test");
echo(CommandLine.all());

let status = error();

expect<i32>(status).toBe(0);


});

})
2 changes: 2 additions & 0 deletions packages/ash/assembly/bin/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export function main(args: string[]): void {
if (args.length > 1) {
for (let i: i32 = 1; i < args.length; i++) {
let file = fs.openFile(args[i]);

if (file.failed) {
Console.error("cat: " + args[i] + ": No such file or directory");
Process.error(44);
continue;
}
Console.log(file.result.readString().result);
Expand Down
12 changes: 12 additions & 0 deletions packages/ash/assembly/bin/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export function main(): i32 {
let code = Process.error_flag;
if (code != 0) {
Process.error_flag = 0;
return 1;
}
else {

return 0;
}
}
18 changes: 18 additions & 0 deletions packages/assemblyscript/assembly/wasa/mock/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import { Wasi } from "../../wasi";
export class Process {
static _singleton: Process;

// Error status flag
public static error_flag: i32;

constructor(cwd: fd) {
this._cwd = cwd;
Process.error_flag = 0;
Process._singleton = this;

}
_cwd: fd;

/**
* Cleanly terminate the current process
* @param status exit code
Expand All @@ -22,6 +28,18 @@ export class Process {
abort();
}

/**
* Set Process error flag without aborting
* @param status exit code
*/
static error(status: i32): void {
Process.error_flag = status;
}





static get cwd(): fd {
return Process._singleton._cwd;
}
Expand Down
1 change: 1 addition & 0 deletions types/wasa/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ declare class DirectoryEntry {
*/
declare class Process {
static exit(code: number): void;
static error(code: number): void;
}


Expand Down