Restore compile, passing args, remove step limit from playground

This commit is contained in:
Andrew Morris
2023-05-25 08:35:16 +10:00
parent 8f3263fdea
commit d885873844
4 changed files with 67 additions and 10 deletions

View File

@@ -6,8 +6,8 @@ use std::{
use wasm_bindgen::prelude::*;
use valuescript_compiler::{
asm::Value, assemble, assembly_parser::AssemblyParser, compile as compile_internal, Diagnostic,
ResolvedPath,
asm::Value, assemble, assembly_parser::AssemblyParser, compile as compile_internal,
CompileResult, Diagnostic, ResolvedPath,
};
use valuescript_vm::{
vs_array::VsArray, vs_object::VsObject, vs_value::Val, LoadFunctionResult, ValTrait,
@@ -32,6 +32,45 @@ struct RunResult {
output: Result<String, String>,
}
#[derive(serde::Serialize)]
struct CompilerOutputWasm {
diagnostics: HashMap<String, Vec<Diagnostic>>,
assembly: Vec<String>,
}
impl CompilerOutputWasm {
fn from_compile_result(result: CompileResult) -> CompilerOutputWasm {
CompilerOutputWasm {
diagnostics: result
.diagnostics // TODO: Avoid conversion
.into_iter()
.map(|(path, diagnostics)| (path.to_string(), diagnostics))
.collect(),
assembly: match result.module {
Some(module) => module.as_lines(),
None => vec![],
},
}
}
}
#[wasm_bindgen]
pub fn compile(entry_point: &str, read_file: &js_sys::Function) -> String {
let compile_result = compile_internal(ResolvedPath::from(entry_point.to_string()), |path| {
let call_result = read_file.call1(&JsValue::UNDEFINED, &JsValue::from_str(path));
match call_result {
Ok(result) => result
.as_string()
.ok_or_else(|| "read_file from JS produced non-string".into()),
Err(err) => Err(js_get_error_message(&err)),
}
});
serde_json::to_string(&CompilerOutputWasm::from_compile_result(compile_result))
.expect("Failed json serialization")
}
fn run_to_result(entry_point: &str, read_file: &js_sys::Function, args: &str) -> RunResult {
let compile_result = compile_internal(ResolvedPath::from(entry_point.to_string()), |path| {
let call_result = read_file.call1(&JsValue::UNDEFINED, &JsValue::from_str(path));
@@ -95,7 +134,7 @@ fn run_to_result(entry_point: &str, read_file: &js_sys::Function, args: &str) ->
}
};
let vm_result = vm.run(&bytecode, Some(1000000), &val_args);
let vm_result = vm.run(&bytecode, None, &val_args);
RunResult {
diagnostics: HashMap::default(),

View File

@@ -166,7 +166,7 @@ let currentFile = "";
fs.write(currentFile, source);
compileJob = vslibPool.compile(currentFile, fs.files);
runJob = vslibPool.run(currentFile, fs.files);
runJob = vslibPool.run(currentFile, fs.files, []);
renderJob(compileJob, vsmEl, (el, compilerOutput) => {
el.textContent = compilerOutput.assembly.join("\n");

View File

@@ -23,11 +23,11 @@ async function main() {
}
if (method === "run") {
const [entryPoint, files] = args;
const [entryPoint, files, argsStr] = args;
try {
self.postMessage({
ok: vslib.run(entryPoint, makeLookupFile(files)),
ok: vslib.run(entryPoint, makeLookupFile(files), argsStr),
});
} catch (err) {
self.postMessage({ err });
@@ -99,8 +99,16 @@ export function mapJob<U, V>(job: Job<U>, f: (x: U) => V): Job<V> {
export default class VslibPool {
#pool = new valuescript.WorkerPool(workerUrl);
run(entryPoint: string, files: Record<string, string | nil>) {
return this.#Job("run", [entryPoint, files]) as Job<RunResult>;
run(
entryPoint: string,
files: Record<string, string | nil>,
args: unknown[],
) {
return this.#Job("run", [
entryPoint,
files,
JSON.stringify(args),
]) as Job<RunResult>;
}
compile(entryPoint: string, files: Record<string, string | nil>) {

View File

@@ -125,7 +125,11 @@ export async function initVslib() {
}
}
function run(entry_point: string, read_file: (path: string) => string) {
function run(
entry_point: string,
read_file: (path: string) => string,
args: string,
) {
let r0 = undefined;
let r1 = undefined;
@@ -137,7 +141,13 @@ export async function initVslib() {
wasm.__wbindgen_realloc,
);
const len0 = WASM_VECTOR_LEN;
wasm.run(retptr, ptr0, len0, addBorrowedObject(read_file));
const ptr1 = passStringToWasm0(
args,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len1 = WASM_VECTOR_LEN;
wasm.run(retptr, ptr0, len0, addBorrowedObject(read_file), ptr1, len1);
r0 = getInt32Memory0()[retptr / 4 + 0];
r1 = getInt32Memory0()[retptr / 4 + 1];
return getStringFromWasm0(r0, r1);