feat: add WasmModuleObject::compile bindings (#960)

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Luca Casonato 2022-05-07 18:02:39 +02:00 committed by GitHub
parent 196145abf1
commit 2dcced58d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 12 deletions

View File

@ -2938,6 +2938,13 @@ v8::CompiledWasmModule* v8__WasmModuleObject__GetCompiledModule(
return new v8::CompiledWasmModule(std::move(cwm));
}
const v8::WasmModuleObject* v8__WasmModuleObject__Compile(
v8::Isolate* isolate, uint8_t* wire_bytes_data, size_t length) {
v8::MemorySpan<const uint8_t> wire_bytes(wire_bytes_data, length);
return maybe_local_to_ptr(
v8::WasmModuleObject::Compile(isolate, wire_bytes));
}
const uint8_t* v8__CompiledWasmModule__GetWireBytesRef(
v8::CompiledWasmModule* self, size_t* length) {
v8::MemorySpan<const uint8_t> span = self->GetWireBytesRef();

View File

@ -103,6 +103,22 @@ impl WasmModuleObject {
let ptr = unsafe { v8__WasmModuleObject__GetCompiledModule(self) };
CompiledWasmModule(ptr)
}
/// Compile a Wasm module from the provided uncompiled bytes.
pub fn compile<'s>(
scope: &mut HandleScope<'s>,
wire_bytes: &[u8],
) -> Option<Local<'s, WasmModuleObject>> {
unsafe {
scope.cast_local(|sd| {
v8__WasmModuleObject__Compile(
sd.get_isolate_ptr(),
wire_bytes.as_ptr(),
wire_bytes.len(),
)
})
}
}
}
// Type-erased v8::CompiledWasmModule. We need this because the C++
@ -201,6 +217,11 @@ extern "C" {
fn v8__WasmModuleObject__GetCompiledModule(
this: *const WasmModuleObject,
) -> *mut InternalCompiledWasmModule;
fn v8__WasmModuleObject__Compile(
isolate: *mut Isolate,
wire_bytes_data: *const u8,
length: usize,
) -> *mut WasmModuleObject;
fn v8__CompiledWasmModule__GetWireBytesRef(
this: *mut InternalCompiledWasmModule,

View File

@ -6063,18 +6063,11 @@ fn compiled_wasm_module() {
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);
let module: v8::Local<v8::WasmModuleObject> = eval(
scope,
r#"
new WebAssembly.Module(Uint8Array.from([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x00, 0x07, 0x03, 0x66, 0x6F, 0x6F, 0x62, 0x61, 0x72
]));
"#,
)
.unwrap()
.try_into()
.unwrap();
let wire_bytes = &[
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x03, 0x66,
0x6F, 0x6F, 0x62, 0x61, 0x72,
];
let module = v8::WasmModuleObject::compile(scope, wire_bytes).unwrap();
module.get_compiled_module()
};