Skip to content

Commit

Permalink
Merge pull request #333 from moonbitlang/bump-v8
Browse files Browse the repository at this point in the history
bump v8 and enable wasm-js-builtins
  • Loading branch information
lijunchen authored Sep 23, 2024
2 parents 84acae3 + 86f34ed commit 770cd62
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ clippy:

add-header:
hawkeye format

install-moonrun:
cargo install --path ./crates/moonrun --debug --offline --root ~/.moon --force --locked
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/moonrun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ serde_json_lenient.workspace = true
rand = "0.8.5"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
v8 = "0.102.0"
v8 = "0.106.0"

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
Expand Down
40 changes: 34 additions & 6 deletions crates/moonrun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use clap::Parser;
use std::any::Any;
use std::io::{self, Write};
use std::path::Path;
use std::{cell::Cell, io::Read, path::PathBuf, time::Instant};

mod fs_api_temp;
Expand Down Expand Up @@ -191,6 +192,24 @@ fn read_char(
}
}

fn read_file_to_bytes(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
mut ret: v8::ReturnValue,
) {
let arg = args.get(0);
let path = PathBuf::from(arg.to_rust_string_lossy(scope));
let bytes = std::fs::read(path).unwrap();
let buffer = v8::ArrayBuffer::new(scope, bytes.len());
let ab = v8::Uint8Array::new(scope, buffer, 0, bytes.len()).unwrap();

unsafe {
std::ptr::copy(bytes.as_ptr(), get_array_buffer_ptr(buffer), bytes.len());
}

ret.set(ab.into());
}

fn write_char(
scope: &mut v8::HandleScope,
args: v8::FunctionCallbackArguments,
Expand Down Expand Up @@ -317,6 +336,14 @@ fn init_env(dtors: &mut Vec<Box<dyn Any>>, scope: &mut v8::HandleScope, args: &[
let obj = fs_api_temp::init_fs(obj, scope);
global_proxy.set(scope, identifier.into(), obj.into());

{
let identifier = v8::String::new(scope, "read_file_to_bytes").unwrap();
let value = v8::Function::builder(read_file_to_bytes)
.build(scope)
.unwrap();
global_proxy.set(scope, identifier.into(), value.into());
}

{
let identifier = v8::String::new(scope, "__moonbit_io_unstable").unwrap();
let obj = v8::Object::new(scope);
Expand Down Expand Up @@ -384,12 +411,13 @@ fn create_script_origin<'s>(scope: &mut v8::HandleScope<'s>, name: &str) -> v8::
}

fn wasm_mode(
file: &PathBuf,
file: &Path,
args: &[String],
no_stack_trace: bool,
test_mode: bool,
) -> anyhow::Result<()> {
v8::V8::set_flags_from_string("--experimental-wasm-exnref");
v8::V8::set_flags_from_string("--experimental-wasm-imported-strings");
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();
Expand All @@ -402,11 +430,11 @@ fn wasm_mode(
{
let global_proxy = scope.get_current_context().global(scope);

let file = std::fs::read(file)?;
let wasm_mod = v8::WasmModuleObject::compile(scope, &file)
.ok_or_else(|| anyhow::format_err!("Failed to compile wasm module"))?;
let module_key = v8::String::new(scope, "module").unwrap().into();
global_proxy.set(scope, module_key, wasm_mod.into());
let module_key = v8::String::new(scope, "module_name").unwrap().into();
let module_name = v8::String::new(scope, file.to_string_lossy().as_ref())
.unwrap()
.into();
global_proxy.set(scope, module_key, module_name);
}

let mut dtors = Vec::new();
Expand Down
2 changes: 2 additions & 0 deletions crates/moonrun/src/template/js_glue.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const spectest = {
};

try {
let bytes = read_file_to_bytes(module_name);
let module = new WebAssembly.Module(bytes, { builtins: ['js-string'], importedStringConstants: "moonbit:constant_strings" });
let instance = new WebAssembly.Instance(module, spectest);
if (test_mode) {
for (param of testParams) {
Expand Down

0 comments on commit 770cd62

Please sign in to comment.