-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
210 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,35 +1,70 @@ | ||
import { Arena } from "quickjs-emscripten-sync"; | ||
import * as path from "path"; | ||
import { Worker } from "worker_threads"; | ||
import * as url from "url"; | ||
import { Context } from "./context"; | ||
import { getQuickJS, QuickJSWASMModule } from "quickjs-emscripten"; | ||
|
||
let quickJS: QuickJSWASMModule | undefined; | ||
const worker = new Worker( | ||
url.pathToFileURL(path.join(__dirname, "/worker/quickjs-worker.js")) | ||
); | ||
|
||
export async function safeEval(code: string, $: Context) { | ||
const quickJS = await ensureQuickJS(); | ||
const ctx = quickJS.newContext(); | ||
const area = new Arena(ctx, { isMarshalable: true }); | ||
area.expose({ $: $ }); | ||
const ret = area.evalCode(code); | ||
area.dispose(); | ||
ctx.dispose(); | ||
return ret; | ||
let id = 0; | ||
const idPromises = new Map<number, { resolve: (_: unknown) => void, reject: (_: unknown) => void }>(); | ||
|
||
function setIdPromise(id: number, resolve: (_: unknown) => void, reject: (_: unknown) => void) { | ||
idPromises.set(id, { resolve, reject }); | ||
} | ||
|
||
export async function safeEvalDiagnosticAction(code: string, $: Context, $$: unknown) { | ||
const quickJS = await ensureQuickJS(); | ||
const ctx = quickJS.newContext(); | ||
const area = new Arena(ctx, { isMarshalable: true }); | ||
area.expose({ $: $, $$: $$ }); | ||
const ret = area.evalCode(code); | ||
area.dispose(); | ||
ctx.dispose(); | ||
return ret; | ||
function getIdPromise(id: number): { resolve: (_: unknown) => void, reject: (_: unknown) => void } | undefined { | ||
const promise = idPromises.get(id); | ||
if (!promise) { | ||
return; | ||
} | ||
idPromises.delete(id); | ||
return promise; | ||
} | ||
|
||
async function ensureQuickJS() { | ||
if (quickJS !== undefined) { | ||
return quickJS; | ||
worker.on("message", ((payload: { id: number, result: unknown } | { id: number, error: unknown }) => { | ||
const promise = getIdPromise(payload.id); | ||
if (!promise) { | ||
return; | ||
} | ||
if ("result" in payload) { | ||
promise.resolve(payload.result); | ||
} else if ("error" in payload) { | ||
promise.reject(payload.error); | ||
} | ||
quickJS = await getQuickJS(); | ||
return quickJS; | ||
})); | ||
|
||
|
||
export async function safeEval(code: string, $: Context) { | ||
const currentId = id++; | ||
const promise = new Promise<unknown>((resolve, reject) => { | ||
setIdPromise(currentId, resolve, reject); | ||
}); | ||
worker.postMessage({ | ||
id: currentId, | ||
type: "safeEval", | ||
payload: { | ||
code, | ||
$, | ||
}, | ||
}); | ||
return promise; | ||
} | ||
|
||
export async function safeEvalDiagnosticAction(code: string, $: Context, $$: unknown) { | ||
const currentId = id++; | ||
const promise = new Promise<unknown>((resolve, reject) => { | ||
setIdPromise(currentId, resolve, reject); | ||
}); | ||
worker.postMessage({ | ||
id: currentId, | ||
type: "safeEvalDiagnosticAction", | ||
payload: { | ||
code, | ||
$, | ||
$$, | ||
}, | ||
}); | ||
return promise; | ||
} |
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
Oops, something went wrong.