MoonBit bindings for V8 aimed at prototyping MoonBit-first runtimes in the style of Node or Deno.
Dependencies
Experimental release note: on Moon 0.1.20260309 / MoonBit v0.8.3, this package still requires consumer-side prebuild and link setup.
moon add mizchi/v8
node .mooncakes/mizchi/v8/src/scripts/setup-consumer.mjs --main-pkg cmd/main/moon.pkg
moon check --target nativenode .mooncakes/mizchi/v8/src/scripts/setup-consumer.mjs --main-pkg app/server/moon.pkgnode ../v8.mbt/src/scripts/setup-consumer.mjs --module-root . --main-pkg cmd/main/moon.pkg --build-bridgegit clone https://github.com/mizchi/v8.mbt
cd v8.mbt
just bootstrap
just testmatch @v8.eval_string("'Hello' + ', MoonBit!'") {
Ok(value) => println(value)
Err(err) => println(err.to_string())
}let builder = @v8.runtime_builder_new()
.eval_snapshot("globalThis.base = 40;")
.load_module("dep", "export const answer = globalThis.base + 2;")
match builder.with_runtime(fn(runtime) {
runtime.eval_async_json(
"(async () => { let mod = await import('dep'); return { answer: mod.answer } })()",
)
}) {
Ok(value) => println(value)
Err(err) => println(err.to_string())
}match @v8.runtime_new() {
Ok(runtime) => {
ignore(runtime.register_async_json_op("add"))
let promise = runtime.eval_promise_string("MoonBit.opAsync('add', [20, 22])")
match runtime.take_async_json_op() {
Ok(Some(op)) => ignore(runtime.resolve_async_json_op(op.id, "{\"answer\":42}"))
_ => ()
}
println(
promise
|> Result::map(fn(handle) { handle.await_json() })
|> Result::flatten,
)
runtime.dispose()
}
Err(err) => println(err.to_string())
}pub suberror V8Error {
DependencyMissing(String)
ModuleClosed
NativeError(String)
PromiseClosed
RuntimeClosed
}pub struct AsyncBytesOpRequest {
id : UInt64
name : String
payload : Bytes
}pub struct AsyncJsonOpRequest {
id : UInt64
name : String
payload_json : String
}pub struct ModuleEvalHandle {
// private fields
}async fn ModuleEvalHandle::await_ready_async(self : ModuleEvalHandle) -> Result[ModuleHandle, V8Error]pub struct ModuleHandle {
// private fields
}fn ModuleHandle::call_export_bytes(self : ModuleHandle, export_name : String, bytes : Bytes) -> Result[Bytes, V8Error]fn ModuleHandle::call_export_json(self : ModuleHandle, export_name : String, args_json : String) -> Result[String, V8Error]fn ModuleHandle::get_export_bytes(self : ModuleHandle, export_name : String) -> Result[Bytes, V8Error]fn ModuleHandle::get_export_json(self : ModuleHandle, export_name : String) -> Result[String, V8Error]pub struct PromiseHandle {
// private fields
}pub enum PromiseState {
Pending
Fulfilled
Rejected
}pub struct Runtime {
// private fields
}fn Runtime::eval_module_handle_async_string(self : Runtime, source : String) -> Result[ModuleEvalHandle, V8Error]fn Runtime::eval_module_handle_async_string_with_specifier(self : Runtime, specifier : String, source : String) -> Result[ModuleEvalHandle, V8Error]fn Runtime::eval_module_handle_string(self : Runtime, source : String) -> Result[ModuleHandle, V8Error]async fn Runtime::eval_module_handle_string_async(self : Runtime, source : String) -> Result[ModuleHandle, V8Error]async fn Runtime::eval_module_handle_string_async_with_specifier(self : Runtime, specifier : String, source : String) -> Result[ModuleHandle, V8Error]fn Runtime::eval_module_handle_string_with_specifier(self : Runtime, specifier : String, source : String) -> Result[ModuleHandle, V8Error]fn Runtime::eval_promise_string_with_name(self : Runtime, resource_name : String, source : String) -> Result[PromiseHandle, V8Error]pub struct RuntimeBuilder {
// private fields
}fn RuntimeBuilder::load_module(self : RuntimeBuilder, specifier : String, source : String) -> RuntimeBuilderfn RuntimeBuilder::load_modules(self : RuntimeBuilder, modules : Array[ModuleSource]) -> RuntimeBuilderfn[T] RuntimeBuilder::with_runtime(self : RuntimeBuilder, f : (Runtime) -> Result[T, V8Error]) -> Result[T, V8Error]fn RuntimeBuilder::with_snapshot_builder(self : RuntimeBuilder, snapshot_builder : SnapshotBuilder) -> RuntimeBuilderpub struct RuntimeImage {
// private fields
}fn RuntimeImage::load_module(self : RuntimeImage, specifier : String, source : String) -> RuntimeImagefn[T] RuntimeImage::with_runtime(self : RuntimeImage, f : (Runtime) -> Result[T, V8Error]) -> Result[T, V8Error]pub struct SnapshotBuilder {
// private fields
}fn[T] SnapshotBuilder::with_runtime(self : SnapshotBuilder, f : (Runtime) -> Result[T, V8Error]) -> Result[T, V8Error]pub struct SyncJsonOpRequest {
id : UInt64
name : String
payload_json : String
}fn eval_module_string_with_specifier(specifier : String, source : String) -> Result[String, V8Error]async fn eval_promise_bytes_async_with_name(resource_name : String, source : String) -> Result[Bytes, V8Error]async fn eval_promise_json_async_with_name(resource_name : String, source : String) -> Result[String, V8Error]async fn eval_promise_string_async_with_name(resource_name : String, source : String) -> Result[String, V8Error]MoonBit bindings for V8 aimed at prototyping MoonBit-first runtimes in the style of Node or Deno.
Dependencies