-
Notifications
You must be signed in to change notification settings - Fork 12
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
1 parent
e1c0dfb
commit b1b8324
Showing
8 changed files
with
304 additions
and
3 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,74 @@ | ||
let wasmModule, WasmFs, WASI, browserBindings; | ||
const loadWasm = async url => { | ||
if (wasmModule) return wasmModule; | ||
|
||
// todo: use 1.x versions (breaking) | ||
0, { WASI } = await import('https://esm.sh/@wasmer/[email protected]'); | ||
0, browserBindings = (await import('https://esm.sh/@wasmer/[email protected]/lib/bindings/browser')).default; | ||
|
||
// const { WasmFs } = await import('https://esm.sh/@wasmer/[email protected]'); | ||
0, { WasmFs } = await import('https://esm.sh/@wasmer/[email protected]'); | ||
// if (!wasmFs) wasmFs = new WasmFs(); | ||
|
||
const res = fetch(url); | ||
return wasmModule = await WebAssembly.compileStreaming(res); | ||
}; | ||
|
||
export const run = async (js, ipcHandler = () => {}, stdinCallback = () => {}) => { | ||
// const wasmModule = await loadWasm('https://goose-cors.goosemod.workers.dev/?https://files.kiesel.dev/kiesel.wasm'); | ||
const wasmModule = await loadWasm('https://files.kiesel.dev/kiesel.wasm'); | ||
|
||
const wasmFs = new WasmFs(); | ||
|
||
let wasi = new WASI({ | ||
args: ['kiesel', '/input.js'], | ||
preopens: {'/': '/'}, | ||
env: {}, | ||
bindings: { | ||
...browserBindings, | ||
fs: wasmFs.fs, | ||
}, | ||
}); | ||
|
||
let instance = await WebAssembly.instantiate(wasmModule, wasi.getImports(wasmModule)); | ||
|
||
wasmFs.fs.writeFileSync('/input.js', js); | ||
|
||
wasmFs.volume.fds[1].position = 0; | ||
wasmFs.volume.fds[2].position = 0; | ||
|
||
wasmFs.fs.writeFileSync('/dev/stdin', ""); | ||
wasmFs.fs.writeFileSync('/dev/stdout', ""); | ||
wasmFs.fs.writeFileSync('/dev/stderr', ""); | ||
|
||
stdinCallback(wasmFs.fs); | ||
|
||
let lastStdout = ''; | ||
wasmFs.fs.watch('/dev/stdout', () => { | ||
const stdout = wasmFs.fs.readFileSync('/dev/stdout', 'utf8'); | ||
|
||
const newStdout = stdout.slice(lastStdout.length); | ||
|
||
if (newStdout) { | ||
const msgs = newStdout.split('\n'); | ||
for (const x of msgs) { | ||
if (x && x.endsWith('}')) { | ||
ipcHandler(JSON.parse(x)); | ||
lastStdout = stdout; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
try { | ||
wasi.start(instance); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
console.log({ js }); | ||
|
||
/* let stdout = wasmFs.fs.readFileSync('/dev/stdout').toString(); | ||
let stderr = wasmFs.fs.readFileSync('/dev/stderr').toString(); | ||
console.log({stdout, stderr}); */ | ||
}; |
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,72 @@ | ||
let wasmModule, WasmFs, WASI, browserBindings; | ||
const loadWasm = async url => { | ||
if (wasmModule) return wasmModule; | ||
|
||
// todo: use 1.x versions (breaking) | ||
0, { WASI } = await import('https://esm.sh/@wasmer/[email protected]'); | ||
0, browserBindings = (await import('https://esm.sh/@wasmer/[email protected]/lib/bindings/browser')).default; | ||
|
||
// const { WasmFs } = await import('https://esm.sh/@wasmer/[email protected]'); | ||
0, { WasmFs } = await import('https://esm.sh/@wasmer/[email protected]'); | ||
// if (!wasmFs) wasmFs = new WasmFs(); | ||
|
||
const res = fetch(url); | ||
return wasmModule = await WebAssembly.compileStreaming(res); | ||
}; | ||
|
||
let data; | ||
export const run = async (js, ipcHandler = () => {}, stdinCallback = () => {}) => { | ||
if (!data) data = await (await fetch('https://mozilla-spidermonkey.github.io/sm-wasi-demo/data.json')).json(); | ||
|
||
const wasmModule = await loadWasm(data[0].url); | ||
|
||
const wasmFs = new WasmFs(); | ||
|
||
let wasi = new WASI({ | ||
args: ['js.wasm', '-f', '/input.js'], | ||
preopens: {'/': '/'}, | ||
env: {}, | ||
bindings: { | ||
...browserBindings, | ||
fs: wasmFs.fs, | ||
}, | ||
}); | ||
|
||
let instance = await WebAssembly.instantiate(wasmModule, wasi.getImports(wasmModule)); | ||
|
||
wasmFs.fs.writeFileSync('/input.js', js); | ||
|
||
wasmFs.volume.fds[1].position = 0; | ||
wasmFs.volume.fds[2].position = 0; | ||
|
||
wasmFs.fs.writeFileSync('/dev/stdin', ""); | ||
wasmFs.fs.writeFileSync('/dev/stdout', ""); | ||
wasmFs.fs.writeFileSync('/dev/stderr', ""); | ||
|
||
stdinCallback(wasmFs.fs); | ||
|
||
let lastStdout = ''; | ||
wasmFs.fs.watch('/dev/stdout', () => { | ||
const stdout = wasmFs.fs.readFileSync('/dev/stdout', 'utf8'); | ||
|
||
const newStdout = stdout.slice(lastStdout.length); | ||
lastStdout = stdout; | ||
|
||
if (newStdout) { | ||
const msgs = newStdout.split('\n'); | ||
for (const x of msgs) { | ||
if (x) ipcHandler(JSON.parse(x)); | ||
} | ||
} | ||
}); | ||
|
||
try { | ||
wasi.start(instance); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
/* let stdout = wasmFs.fs.readFileSync('/dev/stdout').toString(); | ||
let stderr = wasmFs.fs.readFileSync('/dev/stderr').toString(); | ||
console.log({stdout, stderr}); */ | ||
}; |
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,34 @@ | ||
import * as SpiderMonkey from './backends/spidermonkey.js'; | ||
import * as Kiesel from './backends/kiesel.js'; | ||
|
||
import * as Runner from './ipc/outside.js'; | ||
|
||
const backends = { | ||
kiesel: Kiesel, | ||
spidermonkey: SpiderMonkey | ||
}; | ||
let backend = null; | ||
export let backendName = null; | ||
|
||
export const setBackend = async (name, preload = true) => { | ||
console.log('js backend is now', name); | ||
if (name === null) { | ||
backendName = null; | ||
backend = null; | ||
return; | ||
} | ||
|
||
backendName = name.toLowerCase(); | ||
|
||
backend = backends[backendName]; | ||
|
||
if (preload) await run(null, ''); | ||
}; | ||
|
||
export const run = async (doc, js) => { | ||
if (!backend) return false; | ||
|
||
await Runner.run(backend, doc, js); | ||
|
||
return true; | ||
}; |
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,43 @@ | ||
// JS WORLD | ||
const ipc = { | ||
send: msg => { | ||
msg.id = Math.random(); | ||
if (globalThis.Kiesel) { | ||
Kiesel.print(msg, { pretty: true }); | ||
} else { | ||
print(JSON.stringify(msg)); | ||
} | ||
}, | ||
|
||
recv: () => { | ||
if (globalThis.Kiesel) { | ||
return eval(Kiesel.readLine()); | ||
} else { | ||
return JSON.parse(readline()); | ||
} | ||
} | ||
}; | ||
|
||
class Element { | ||
constructor(data) { | ||
Object.assign(this, data); | ||
} | ||
|
||
get textContent() { | ||
ipc.send({ f: 'Element.getTextContent', ptr: this.ptr }); | ||
return ipc.recv().value; | ||
} | ||
|
||
set textContent(value) { | ||
ipc.send({ f: 'Element.setTextContent', value, ptr: this.ptr }); | ||
} | ||
} | ||
|
||
globalThis.document = { | ||
querySelector(selector) { | ||
ipc.send({ f: 'document.querySelector', selector }); | ||
const data = ipc.recv(); | ||
|
||
return new Element(data); | ||
} | ||
}; |
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,34 @@ | ||
const insideJS = await (await fetch('engine/js/ipc/inside.js')).text(); | ||
|
||
const funcs = { | ||
'document.querySelector': ({ selector }, send, doc) => { | ||
const el = doc.querySelector(selector); | ||
send({ ptr: el.ptr }); | ||
}, | ||
|
||
'Element.getTextContent': ({ ptr }, send, doc) => { | ||
const el = doc.getFromPtr(ptr); | ||
send({ value: el.textContent }); | ||
}, | ||
|
||
'Element.setTextContent': ({ value, ptr }, send, doc) => { | ||
const el = doc.getFromPtr(ptr); | ||
el.textContent = value; | ||
} | ||
}; | ||
|
||
export const run = async (backend, doc, _js) => { | ||
const js = insideJS + '\n\n' + _js.slice(); | ||
let send; | ||
|
||
await backend.run(js, msg => { | ||
// console.log('recv', msg); | ||
funcs[msg.f](msg, send, doc); | ||
}, | ||
fs => { | ||
send = msg => { | ||
// console.log('send', msg); | ||
fs.appendFileSync('/dev/stdin', JSON.stringify(msg) + '\n'); | ||
}; | ||
}) | ||
}; |
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