-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buffered stdin to accept terminal input whilst WASM commands running
- Loading branch information
1 parent
6373b50
commit 58b5e63
Showing
22 changed files
with
233 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Callbacks used by a shell to call functions in the frontend. | ||
*/ | ||
|
||
/** | ||
* Send output string to be displayed in terminal. | ||
*/ | ||
export interface IOutputCallback { (output: string): Promise<void> } | ||
|
||
/** | ||
* Enable/disable buffered stdin in the terminal. | ||
*/ | ||
export interface IEnableBufferedStdinCallback { (enable: boolean): void } | ||
|
||
/** | ||
* Wait for and return a sequence of utf16 code units from stdin, if buffered stdin is enabled. | ||
*/ | ||
export interface IStdinCallback { (): number[] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export { Aliases } from "./aliases" | ||
export { IOutputCallback, IEnableBufferedStdinCallback, IStdinCallback } from "./callback" | ||
export { Context } from "./context" | ||
export { IFileSystem } from "./file_system" | ||
export { IOutputCallback } from "./output_callback" | ||
export { parse } from "./parse" | ||
export { Shell } from "./shell" | ||
export * from "./shell" | ||
export { tokenize, Token } from "./tokenize" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export interface IInput { | ||
/** | ||
* Read and return the entire contents of this input. | ||
* Read and return a single character as a sequence of ASCII character codes. Note this might be | ||
* more than one actual character such as \n or escape code for up arrow, etc. No further input is | ||
* indicated by a single-width character with an ASCII code of 4 (EOT = End Of Transmission). | ||
*/ | ||
read(): string | ||
readChar(): number[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { IInput } from "./input" | ||
|
||
export abstract class InputAll implements IInput { | ||
/** | ||
* Read and return the entire contents of this input. No special character is required to indicate | ||
* the end of the input, it is just the end of the string. Should only be called once per object. | ||
*/ | ||
abstract readAll(): string | ||
|
||
readChar(): number[] { | ||
if (this._buffer === undefined) { | ||
this._buffer = this.readAll() | ||
this._index = 0 | ||
} | ||
|
||
if (this._index < this._buffer.length) { | ||
const char = this._buffer[this._index++] | ||
let ret: number[] = [] | ||
for (let i = 0; i < char.length; i++) { | ||
ret.push(char.charCodeAt(i)) | ||
} | ||
return ret | ||
} else { | ||
return [4] // EOT | ||
} | ||
} | ||
|
||
private _buffer?: string | ||
private _index: number = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { BufferedOutput } from "./buffered_output" | ||
import { IInput } from "./input" | ||
import { PipeInput } from "./pipe_input" | ||
|
||
export class Pipe extends BufferedOutput implements IInput { | ||
/** | ||
* A Pipe provides IOutput and IInput, accepting output and passing it to the input. | ||
* To obtain the input interface PipeInput, call the .input attribute. | ||
*/ | ||
export class Pipe extends BufferedOutput { | ||
override async flush(): Promise<void> { | ||
} | ||
|
||
read(): string { | ||
const ret = this.allContent | ||
this.clear() | ||
return ret | ||
get input(): PipeInput { | ||
// Should restrict this to just one? | ||
return new PipeInput(this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { InputAll } from "./input_all" | ||
import { Pipe } from "./pipe" | ||
|
||
export class PipeInput extends InputAll { | ||
constructor(readonly pipe: Pipe) { | ||
super() | ||
} | ||
|
||
readAll(): string { | ||
const ret = this.pipe.allContent | ||
this.pipe.clear() | ||
return ret | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
import { IInput } from "./input" | ||
import { IStdinCallback } from "../callback" | ||
|
||
export class TerminalInput implements IInput { | ||
read(): string { | ||
return "" | ||
constructor(readonly stdinCallback?: IStdinCallback) {} | ||
|
||
readChar(): number[] { | ||
if (this._finished || this.stdinCallback === undefined) { | ||
return [4] // EOT | ||
} else { | ||
// What to do if more than one character? | ||
const utf16 = this.stdinCallback() | ||
if (utf16[0] == 4) { | ||
this._finished = true | ||
} else if (utf16[0] == 13) { | ||
return [10] | ||
} | ||
return utf16 | ||
} | ||
} | ||
|
||
private _finished = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import { shell_setup_simple } from "../shell_setup" | ||
import { FileInput, Input, SingleCharInput } from "../../src/io" | ||
import { FileInput } from "../../src/io" | ||
|
||
describe("FileInput", () => { | ||
it("should read from file", async () => { | ||
const { fileSystem } = await shell_setup_simple() | ||
const fileInput = new FileInput(fileSystem, "file2") | ||
const read = fileInput.read() | ||
const read = fileInput.readAll() | ||
expect(read).toEqual("Some other file\nSecond line") | ||
}) | ||
}) | ||
|
||
describe("SingleCharInput", () => { | ||
it("should read from file a character at a time", async () => { | ||
const { fileSystem } = await shell_setup_simple() | ||
const fileInput = new SingleCharInput(new FileInput(fileSystem, "file2")) | ||
const fileInput = new FileInput(fileSystem, "file2") | ||
const expected = "Some other file\nSecond line" | ||
for (let i = 0; i < expected.length; i++) { | ||
const charCode = fileInput.readCharCode() | ||
expect(charCode).toEqual(expected.charCodeAt(i)) | ||
const charCodes = fileInput.readChar() | ||
expect(charCodes[0]).toEqual(expected.charCodeAt(i)) | ||
} | ||
for (let i = 0; i < 3; i++) { | ||
const charCode = fileInput.readCharCode() | ||
const charCodes = fileInput.readChar() | ||
// Once end of input file reached, always returns char code 4 (EOT). | ||
expect(charCode).toEqual(4) | ||
expect(charCodes[0]).toEqual(4) | ||
} | ||
}) | ||
}) |
Oops, something went wrong.