Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
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
10 changes: 4 additions & 6 deletions packages/ash/assembly/__tests__/echo.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Console, IO, fs, CommandLine } from "../../../assemblyscript/assembly/wasa/mock";
import { FileDescriptor, File} from "../../../assemblyscript/assembly/wasa/mock/fs";
import { FileDescriptor, File } from "../../../assemblyscript/assembly/wasa/mock/fs";

import { main } from "../bin/echo";

let Hello = "Hello";
let World = "World";

function openFile(path: string): FileDescriptor{
return fs.get(fs.open(path));
function openFile(path: string): FileDescriptor {
return fs.get(fs.openFile(path));
}

function openStdout(): FileDescriptor {
Expand Down Expand Up @@ -37,9 +37,7 @@ describe("echo", (): void => {
stdout.reset();
expect<string>(stdout.readString()).toBe(Hello + " " + World + "\n")
stdout.reset();
expect<string>(stdout.readString()).toBe(stdout2.readString())


expect<string>(stdout.readString()).toBe(stdout2.readString());
})

it("should print no newline with -n", () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/ash/assembly/bin/cat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


export function main(args: string[]): void {
if (args.length > 1) {
for (let i: usize = 1; i < args.length; i++) {
let fd = fs.open(args[i]);
Console.log(fs.readString(fd))
}
}
}
12 changes: 10 additions & 2 deletions packages/assemblyscript/assembly/__tests__/wasa.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Console, IO, fs } from "../wasa/mock";
import { Console, IO, fs, StringUtils } from "../wasa/mock";
import * as echo from "../bin/echo";
import { JSONDecoder } from "../../node_modules/assemblyscript-json/assembly/decoder";
import { JSONEncoder } from "../../node_modules/assemblyscript-json/assembly/encoder";
Expand Down Expand Up @@ -54,7 +54,7 @@ describe("Console", (): void => {
"Two non-unique file descriptors points to the same object"
);

stdout = fs.get(fs.open("/dev/fd/1"));
stdout = fs.get(fs.openFile("/dev/fd/1"));
expect<usize>(stdout.offset).toBe(
0,
"A fresh file descriptor has a seek (offset) of 0"
Expand All @@ -76,3 +76,11 @@ describe("Json", (): void => {
roundripTest(jsonStr, jsonStr);
});
});

describe("readLine", (): void => {
it("should read until newline", (): void => {
let str = "Hello\nWorld";
let utfStr = str.toUTF8();
expect<string>(StringUtils.fromCStringTilNewLine(utfStr)).toStrictEqual("Hello\n")
})
})
31 changes: 23 additions & 8 deletions packages/assemblyscript/assembly/wasa/mock/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type path = string;
type fd = usize;

export class FileDescriptor {
constructor(public file: File, public id: u32, public offset: u32) {}
constructor(public file: File, public id: u32, public offset: u32) { }

write(bytes: Array<u8>): void {
for (let i = 0; i < bytes.length; i++) {
Expand Down Expand Up @@ -42,11 +42,18 @@ export class FileDescriptor {
this.offset = offset;
}

readString(): string {
let str = StringUtils.fromCString(this.data + this.offset);
this.offset += str.lengthUTF8 + 1; //For new line
readString(max: usize = 4096): string {
let str = StringUtils.fromCString(this.data + this.offset, max);
this.offset += str.lengthUTF8 + 1; //For null character
return str;
}

readLine(max?: usize): string {
let str = StringUtils.fromCStringTilNewLine(this.data + this.offset, max);
this.offset += str.lengthUTF8 + 1; //For null character
return str;
}

/**
* Resets the offset to 0
*/
Expand Down Expand Up @@ -98,16 +105,20 @@ class Directory extends File {
children: Array<File>;
}



export class Filesystem {
files: Map<fd, FileDescriptor> = new Map<fd, FileDescriptor>();
paths: Map<path, File> = new Map<path, File>();
highestFD: usize = 0;
pwd: fd;

static Default(): Filesystem {
let fs = new Filesystem();
fs.open("/dev/fd/0")
fs.open("/dev/fd/1")
fs.open("/dev/fd/2")
fs.pwd = fs.openDirectory("/")
fs.openFile("/dev/fd/0")
fs.openFile("/dev/fd/1")
fs.openFile("/dev/fd/2")
return fs;
}

Expand All @@ -119,7 +130,7 @@ export class Filesystem {
return this.files.get(fd);
}

open(path: path): fd {
openFile(path: path): fd {
let fd = this.highestFD++;
if (!this.paths.has(path)) {
this.paths.set(path, new File(path));
Expand All @@ -146,4 +157,8 @@ export class Filesystem {
close(fd: number): void {
this.files.delete(fd);
}
openDirectory(path: string): fd {
// this.open()
return 0;
}
}
47 changes: 38 additions & 9 deletions packages/assemblyscript/assembly/wasa/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Filesystem {
* @param dirfd Base directory descriptor (will be automatically set soon)
*/
static openForRead(path: string, dirfd: Descriptor = 3): Descriptor {
return fs.open(path);
return fs.openFile(path);
}

/**
Expand All @@ -23,7 +23,11 @@ export class Filesystem {
* @param dirfd Base directory descriptor (will be automatically set soon)
*/
static openForWrite(path: string, dirfd: Descriptor = 3): Descriptor {
return fs.open(path);
return fs.openFile(path);
}

static openDirectory(path: string): Descriptor {
return fs.openDirectory(path)
}
}

Expand Down Expand Up @@ -77,6 +81,7 @@ export class IO {
data: Array<u8> = [],
chunk_size: usize = 4096
): Array<u8> | null {
fs.get(fd).read(data);
return data;
}

Expand All @@ -91,6 +96,7 @@ export class IO {
data: Array<u8> = [],
chunk_size: usize = 4096
): Array<u8> | null {
data.buffer_ = changetype<ArrayBuffer>(fs.get(fd).data);
return data;
}

Expand All @@ -99,9 +105,15 @@ export class IO {
* @param fd file descriptor
* @param chunk_size chunk size (default: 4096)
*/
static readString(fd: Descriptor, chunk_size: usize = 4096): string | null {
let s = "hello world";
return s;
static readString(fd: Descriptor, chunk_size: usize = 4096): string {
return fs.get(fd).readString(chunk_size)
}

/**
* Reach an UTF8 String from a file descriptor until a new line is reached.
*/
static readLine(fd: Descriptor, chunk_size: usize = 4096): string {
return fs.get(fd).readLine(chunk_size)
}

static reset(fd: Descriptor): void {
Expand Down Expand Up @@ -226,7 +238,7 @@ export class Process {
}

export class EnvironEntry {
constructor(readonly key: string, readonly value: string) {}
constructor(readonly key: string, readonly value: string) { }
}

export class Environ {
Expand Down Expand Up @@ -288,12 +300,29 @@ export class CommandLine {
}
}

const newLine: u8 = 10;

export class StringUtils {
static fromCString(cstring: usize): string {
let size = 0;
while (load<u8>(cstring + size) != 0) {
static isNewLine(ptr: usize): boolean {
return load<u8>(ptr) == newLine;
}

static fromCString(cstring: usize, max: usize = 4096): string {
let size: usize = 0;
while (load<u8>(cstring + size) != 0 && size < max) {
size++;
}
return String.fromUTF8(cstring, size);
}

static fromCStringTilNewLine(cstring: usize, max: usize = 4096): string {
let size: usize = 0;
while (load<u8>(cstring + size) != 0 && size < max) {
size++;
if (this.isNewLine(cstring + size - 1)) {
break;
}
}
return String.fromUTF8(cstring, size);
}
}
33 changes: 33 additions & 0 deletions packages/assemblyscript/assembly/wasa/mock/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

const PATH_SEP: string = "/"

export function basename(path: string): string {
return getPath(path)
}


export function dirname(path: string): string {
return getPath(path, 1);
}

export function getPath(path: string, index: usize = 0): string {
let paths = path.split(PATH_SEP);
let path_index = Math.max(paths.length - 1 - index, 0);
return paths[path_index]
}


export function join(paths: string[]): string {
let paths_cleaned = [];
for (let i: usize = 0; i< paths.length; i++){
let path = paths[i];
if (path.endsWith('/')) {
path = path.slice(0, paths.length - 1);
}
if (path.startsWith("./")){
path = path.slice(2)
}
paths_cleaned.push(path)
}
return paths_cleaned.join(PATH_SEP);
}
Loading