Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web REPL cleanups #5863

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion www/public/repl/repl.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ li {
}

#loading-message {
margin: 40% 10%;
text-align: center;
height: 96px; /* approximately match height after loading and printing help message */
}

.history-item {
Expand Down
40 changes: 17 additions & 23 deletions www/public/repl/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ console.error = function displayErrorInHistoryPanel(string) {
};

import * as roc_repl_wasm from "./roc_repl_wasm.js";
import { getMockWasiImports } from "./wasi.js";

// ----------------------------------------------------------------------------
// REPL state
Expand All @@ -32,7 +31,7 @@ const repl = {
app: null,

// Temporary storage for values passing back and forth between JS and Wasm
result: { addr: 0, buffer: new ArrayBuffer() },
result_addr: { addr: 0, buffer: new ArrayBuffer() },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, this is wrong. This field was changed to a number everywhere else.
The initial value always get overwritten, so everything works. But it's confusing.
I'll make another PR.

};

// Initialise
Expand Down Expand Up @@ -165,40 +164,35 @@ async function processInputQueue() {
// Callbacks to JS from Rust
// ----------------------------------------------------------------------------

// Create an executable Wasm instance from an array of bytes
// (Browser validates the module and does the final compilation.)
// Load Wasm code into the browser's virtual machine, so we can run it later.
// This operation is async, so we call it before entering any code shared
// with the command-line REPL, which is sync.
async function js_create_app(wasm_module_bytes) {
const wasiLinkObject = {}; // gives the WASI functions a reference to the app so they can write to its memory
const importObj = getMockWasiImports(wasiLinkObject);
const { instance } = await WebAssembly.instantiate(
wasm_module_bytes,
importObj
);
wasiLinkObject.instance = instance;
const { instance } = await WebAssembly.instantiate(wasm_module_bytes);
// Keep the instance alive so we can run it later from shared REPL code
repl.app = instance;
}

// Call the main function of the app, via the test wrapper
// Cache the result and return the size of the app's memory
// Call the `main` function of the user app, via the `wrapper` function.
function js_run_app() {
const { wrapper, memory } = repl.app.exports;
const addr = wrapper();
const { buffer } = memory;
repl.result = { addr, buffer };

// Run the user code, and remember the result address
// We'll pass it to Rust in the next callback
repl.result_addr = wrapper();

// Tell Rust how much space to reserve for its copy of the app's memory buffer.
// This is not predictable, since the app can resize its own memory via malloc.
return buffer.byteLength;
// We couldn't know that size until we actually ran the app.
return memory.buffer.byteLength;
}

// After the Rust app has allocated space for the app's memory buffer,
// it calls this function and we copy it, and return the result too
// After Rust has allocated space for the app's memory buffer,
// we copy it, and return the result address too
function js_get_result_and_memory(buffer_alloc_addr) {
const { addr, buffer } = repl.result;
const appMemory = new Uint8Array(buffer);
const appMemory = new Uint8Array(repl.app.exports.memory.buffer);
const compilerMemory = new Uint8Array(repl.compiler.memory.buffer);
compilerMemory.set(appMemory, buffer_alloc_addr);
return addr;
return repl.result_addr;
}

// ----------------------------------------------------------------------------
Expand Down
192 changes: 0 additions & 192 deletions www/public/repl/wasi.js

This file was deleted.