repl: Split repl_www from repl_wasm

This commit is contained in:
Brian Carroll 2022-02-07 14:54:07 +00:00
parent 858a3c3527
commit 2c8b957763
6 changed files with 31 additions and 33 deletions

View File

@ -2,7 +2,7 @@
/dist
/notes.md
# Binary data folder
# Binary data (pre-linked Wasm object file)
/data
# wasm-pack

View File

@ -143,7 +143,7 @@ impl<'a> ReplApp<'a> for WasmReplApp<'a> {
}
}
// #[wasm_bindgen]
#[wasm_bindgen]
pub async fn repl_wasm_entrypoint_from_js(src: String) -> Result<String, String> {
let arena = &Bump::new();
let pre_linked_binary: &'static [u8] = include_bytes!("../data/pre_linked_binary.o");

1
repl_www/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

22
repl_www/build.sh Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
if [[ ! -d repl_www ]]
then
echo "This script should be run from the project root"
exit 1
fi
if ! which wasm-pack
then
cargo install wasm-pack
fi
WWW_DIR="repl_www/build"
mkdir -p $WWW_DIR
cp repl_www/public/* $WWW_DIR
# Pass all script arguments through to wasm-pack (such as --release)
wasm-pack build --target web "$@" repl_wasm
cp repl_wasm/pkg/*.wasm $WWW_DIR
cp repl_wasm/pkg/*.js $WWW_DIR

View File

@ -75,31 +75,7 @@
<body>
<div class="body-wrapper">
<section class="text">
<h1>A mockin' Roc REPL!</h1>
<p>
This is a <strong>mock-up</strong> Web REPL for a fake compiler. The
only valid inputs are the numbers 0-255! The output program is a Wasm
module that counts backwards from that number to 1.
</p>
<p>
The same web page should work with minimal adjustments whenever we
manage to get a WebAssembly build of the Roc compiler working. But
this way, I was able to build up the functionality more gradually.
</p>
<p>How it works</p>
<ul>
<li>There are two Wasm modules: a compiler, and an app</li>
<li>
The compiler simply modifies a single byte in an otherwise fixed
Wasm binary, using your input.
</li>
<li>The compiler sends the Wasm binary to JS, which runs it</li>
<li>
JS calls back into another function in the compiler that stringifies
the result from the app
</li>
<li>JS takes the output string and displays it</li>
</ul>
<h1>The rockin' Roc REPL</h1>
</section>
<section class="history">
@ -112,8 +88,7 @@
<input
id="source-input"
class="code"
type="number"
placeholder="Type a number 0-255 and press Enter"
placeholder="Type some Roc code and press Enter"
/>
</section>
</div>

View File

@ -1,8 +1,8 @@
// wasm_bindgen's JS code expects our imported functions to be global
// wasm_bindgen treats our `extern` declarations as JS globals, so let's keep it happy
window.js_create_app = js_create_app;
window.js_run_app = js_run_app;
window.js_get_result_and_memory = js_get_result_and_memory;
import * as mock_repl from "./mock_repl.js";
import * as roc_repl_wasm from "./roc_repl_wasm.js";
// ----------------------------------------------------------------------------
// REPL state
@ -27,7 +27,7 @@ const repl = {
// Initialise
repl.elemSourceInput.addEventListener("change", onInputChange);
mock_repl.default().then((instance) => {
roc_repl_wasm.default().then((instance) => {
repl.compiler = instance;
});
@ -56,7 +56,7 @@ async function processInputQueue() {
let outputText;
let ok = true;
try {
outputText = await mock_repl.webrepl_run(inputText);
outputText = await roc_repl_wasm.repl_wasm_entrypoint_from_js(inputText);
} catch (e) {
outputText = `${e}`;
ok = false;