Skip to content

Commit

Permalink
Add tickybox for unsafe (fix #3)
Browse files Browse the repository at this point in the history
This `twelf.wasm` build was made against an unlanded PR
standardml/twelf#19 . However,
this (`twelf-wasm`) change itself is deployable despite that PR not
having landed yet.

Just for historical interest: the thing I did to build against that PR
was putting
```
RUN git clone -b jcreed/wasi-unsafe https://github.com/standardml/twelf && \
```
in the `Dockerfile`.
  • Loading branch information
jcreedcmu committed Mar 15, 2024
1 parent 0ff2973 commit b395e43
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 6 deletions.
Binary file modified public/assets/twelf.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ label {
user-select: none;
}

label {
user-select: none;
}

.server-status::after {
content: '';
display: block;
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ <h1>Twelf Sandbox</h1>
<div class="wrap" id="twelf-response-wrap">
<textarea readonly id="twelf-response" spellcheck="false"></textarea>
</div>
<div><input id="unsafe" type="checkbox"></input><label for="unsafe">Enable Unsafe</label></div>
</div>
<div style="margin-left: 3em; margin-top: 3em; font-size: 0.8rem;">
<a href="http://twelf.org/wiki/Main_Page">Twelf</a> on Wasm is made possible by <a href="https://twelf-live.onrender.com/">Twelf Live</a> and
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ async function initTwelf(editor: EditorView) {
}

document.getElementById('running-indicator')!.classList.remove('hidden');
const result = await (workerRef.worker).exec(text);
const result = await (workerRef.worker).exec(text, {
unsafe: (document.getElementById('unsafe')! as HTMLInputElement).checked
});
document.getElementById('running-indicator')!.classList.add('hidden');

if (result.status.t == 'timeout') {
Expand Down
6 changes: 6 additions & 0 deletions src/twelf-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type TwelfExports = {
twelf_open(argc: number, argv: number): void;
allocate(size: number): number;
execute(): TwelfStatus;
unsafe(u: boolean): void;
};

function debug(_x: string): void {
Expand Down Expand Up @@ -59,6 +60,11 @@ export class TwelfService {

constructor(public instance: WebAssembly.Instance) { }

unsafe(u: boolean): void {
const exports = this.instance.exports as TwelfExports;
exports.unsafe(u);
}

async exec(input: string): Promise<TwelfStatus> {
const exports = this.instance.exports as TwelfExports;
const mem = exports.memory;
Expand Down
5 changes: 5 additions & 0 deletions src/twelf-worker-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export enum TwelfStatus {
ABORT = 1,
}

export type TwelfOptions = {
unsafe: boolean,
}

export type TwelfExecStatus =
| { t: 'twelfStatus', status: TwelfStatus }
| { t: 'timeout' }
Expand All @@ -16,6 +20,7 @@ export type WithId<T> = { id: number, body: T };

export type TwelfExecRequest = {
input: string,
options: TwelfOptions,
}

export type TwelfSideEffectData = {
Expand Down
10 changes: 5 additions & 5 deletions src/twelf-worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TwelfExecRequest, TwelfExecResponse, WorkerMessage, WithId, TwelfSideEffectData, TwelfError } from "./twelf-worker-types";
import { TwelfExecRequest, TwelfExecResponse, WorkerMessage, WithId, TwelfSideEffectData, TwelfError, TwelfOptions } from "./twelf-worker-types";

export async function mkTwelfWorker(): Promise<TwelfWorker> {
const worker = new TwelfWorker();
Expand Down Expand Up @@ -36,10 +36,10 @@ export class TwelfWorker {
this._readyPromise = readyPromise;
}

mkRequest(input: string): WithId<TwelfExecRequest> {
mkRequest(input: string, options: TwelfOptions): WithId<TwelfExecRequest> {
return {
id: this.requestIdCounter++,
body: { input },
body: { input, options },
};
}

Expand All @@ -64,10 +64,10 @@ export class TwelfWorker {
}
}

async exec(input: string): Promise<TwelfExecResponse> {
async exec(input: string, options: TwelfOptions): Promise<TwelfExecResponse> {
this.output.splice(0); // clear output

const req = this.mkRequest(input);
const req = this.mkRequest(input, options);
const prom = new Promise<WorkerMessage>((res, rej) => {
this.responseMap[req.id] = res;
});
Expand Down
1 change: 1 addition & 0 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async function go() {

self.onmessage = async event => {
const { body, id } = event.data as WithId<TwelfExecRequest>;
await service.unsafe(body.options.unsafe);
post({ t: 'execResponse', id, response: await service.exec(body.input) });
};

Expand Down

0 comments on commit b395e43

Please sign in to comment.