From b32d0b05409a3416c52a3a21fe2e38f0d9cde422 Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Wed, 22 Sep 2021 20:34:39 +0200 Subject: [PATCH] feat: `WasmStreaming::set_url`, `CompiledWasmModule::source_url` (#786) Needed for denoland/deno#12151 --- src/binding.cc | 12 ++++++++++++ src/wasm.rs | 31 +++++++++++++++++++++++++++++++ tests/test_api.rs | 13 ++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index 2d559b60..c344306e 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -2414,6 +2414,11 @@ void v8__WasmStreaming__Abort(WasmStreamingSharedPtr* self, self->inner->Abort(ptr_to_maybe_local(exception)); } +void v8__WasmStreaming__SetUrl(WasmStreamingSharedPtr* self, + const char* url, size_t len) { + self->inner->SetUrl(url, len); +} + using HeapSnapshotCallback = bool (*)(void*, const char*, size_t); void v8__HeapProfiler__TakeHeapSnapshot(v8::Isolate* isolate, @@ -2771,6 +2776,13 @@ const uint8_t* v8__CompiledWasmModule__GetWireBytesRef(v8::CompiledWasmModule* s return span.data(); } +const char* v8__CompiledWasmModule__SourceUrl(v8::CompiledWasmModule* self, + size_t* length) { + const std::string& source_url = self->source_url(); + *length = source_url.size(); + return source_url.data(); +} + void v8__CompiledWasmModule__DELETE(v8::CompiledWasmModule* self) { delete self; } diff --git a/src/wasm.rs b/src/wasm.rs index 9ca47b6a..77583e0a 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -4,6 +4,7 @@ use crate::function::FunctionCallbackArguments; use crate::function::FunctionCallbackInfo; use crate::scope::CallbackScope; use crate::scope::HandleScope; +use crate::support::char; use crate::support::Opaque; use crate::support::UnitType; use crate::Isolate; @@ -56,6 +57,18 @@ impl WasmStreaming { let exception = exception.map(|v| &*v as *const Value).unwrap_or(null()); unsafe { v8__WasmStreaming__Abort(&mut self.0, exception) } } + + /// Sets the UTF-8 encoded source URL for the `Script` object. This must be + /// called before [`Self::finish()`]. + pub fn set_url(&mut self, url: &str) { + unsafe { + v8__WasmStreaming__SetUrl( + &mut self.0, + url.as_ptr() as *const char, + url.len(), + ) + } + } } impl Drop for WasmStreaming { @@ -112,6 +125,15 @@ impl CompiledWasmModule { std::slice::from_raw_parts(ptr, len.try_into().unwrap()) } } + + pub fn source_url(&self) -> &str { + let mut len = 0; + unsafe { + let ptr = v8__CompiledWasmModule__SourceUrl(self.0, &mut len); + let bytes = std::slice::from_raw_parts(ptr as *const u8, len); + std::str::from_utf8_unchecked(bytes) + } + } } // TODO(andreubotella): Safety??? @@ -164,6 +186,11 @@ extern "C" { this: *mut WasmStreamingSharedPtr, exception: *const Value, ); + fn v8__WasmStreaming__SetUrl( + this: *mut WasmStreamingSharedPtr, + url: *const char, + len: usize, + ); fn v8__WasmModuleObject__FromCompiledModule( isolate: *mut Isolate, @@ -177,5 +204,9 @@ extern "C" { this: *mut InternalCompiledWasmModule, length: *mut isize, ) -> *const u8; + fn v8__CompiledWasmModule__SourceUrl( + this: *mut InternalCompiledWasmModule, + length: *mut usize, + ) -> *const char; fn v8__CompiledWasmModule__DELETE(this: *mut InternalCompiledWasmModule); } diff --git a/tests/test_api.rs b/tests/test_api.rs index 3c56d76b..ffc55ff0 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -4802,12 +4802,22 @@ fn wasm_streaming_callback() { ws.on_bytes_received(&[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]); assert!(global.get(scope, name).unwrap().is_null()); + ws.set_url("https://example2.com"); + assert!(global.get(scope, name).unwrap().is_null()); + ws.finish(); assert!(!scope.has_pending_background_tasks()); assert!(global.get(scope, name).unwrap().is_null()); scope.perform_microtask_checkpoint(); - assert!(global.get(scope, name).unwrap().is_wasm_module_object()); + + let result = global.get(scope, name).unwrap(); + assert!(result.is_wasm_module_object()); + + let wasm_module_object: v8::Local = + result.try_into().unwrap(); + let compiled_wasm_module = wasm_module_object.get_compiled_module(); + assert_eq!(compiled_wasm_module.source_url(), "https://example2.com"); let script = r#" globalThis.result = null; @@ -5446,6 +5456,7 @@ fn compiled_wasm_module() { 0x6F, 0x6F, 0x62, 0x61, 0x72 ] ); + assert_eq!(compiled_module.source_url(), "wasm://wasm/3e495052"); { let isolate = &mut v8::Isolate::new(Default::default());