Remove compile

This commit is contained in:
Andrew Morris
2023-04-14 23:20:44 +09:00
parent 71d1e4611b
commit 5281f566c3

View File

@@ -2,9 +2,7 @@ use std::collections::HashMap;
use wasm_bindgen::prelude::*;
use valuescript_compiler::{
assemble, compile as compile_internal, CompileResult, Diagnostic, ResolvedPath,
};
use valuescript_compiler::{assemble, compile as compile_internal, Diagnostic, ResolvedPath};
use valuescript_vm::{LoadFunctionResult, ValTrait, VirtualMachine};
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
@@ -25,45 +23,6 @@ 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) -> 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));