Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add WasmStreaming::set_url and CompiledWasmModule::source_url #786

Merged
merged 1 commit into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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???
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
13 changes: 12 additions & 1 deletion tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down