mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-12-23 15:50:11 -05:00
feat: WasmStreaming::set_url
, CompiledWasmModule::source_url
(#786)
Needed for denoland/deno#12151
This commit is contained in:
parent
c048052ca6
commit
b32d0b0540
3 changed files with 55 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
|
|
31
src/wasm.rs
31
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);
|
||||
}
|
||||
|
|
|
@ -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<v8::WasmModuleObject> =
|
||||
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());
|
||||
|
|
Loading…
Reference in a new issue