mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 08:02:59 +00:00
feat(geometry): expose native geometry interfaces in workers
Install native DOMPoint, DOMRect, DOMQuad and DOMMatrix constructor/prototype bindings in worker realms. Keep string construction and Window-only matrix operations out of worker surfaces. Reuse existing intrinsic prototypes and native value factories, with the complete original worker geometry surface regression.
This commit is contained in:
@@ -268,7 +268,10 @@ mod worker_host;
|
||||
mod worker_location_runtime;
|
||||
|
||||
pub(super) use self::assets::ContextBootstrapAssets;
|
||||
use self::assets::{build_constructor_template, build_constructor_template_with_callback};
|
||||
use self::assets::{
|
||||
build_constructor_template, build_constructor_template_for_profile,
|
||||
build_constructor_template_with_callback,
|
||||
};
|
||||
pub(crate) use self::broadcast_channel::{
|
||||
dispatch_authorized_page_broadcast_channel_event, dispatch_broadcast_channel_events_for_channel,
|
||||
};
|
||||
@@ -693,6 +696,20 @@ pub(in crate::context_bootstrap) fn build_profiled_exposed_interface_template<'s
|
||||
return Ok(template);
|
||||
}
|
||||
let template = match spec.interface.name() {
|
||||
"DOMMatrixReadOnly" => build_constructor_template_with_callback(
|
||||
scope,
|
||||
spec,
|
||||
0,
|
||||
profile,
|
||||
geometry_runtime::dom_matrix_readonly_worker_constructor_callback,
|
||||
)?,
|
||||
"DOMMatrix" => build_constructor_template_with_callback(
|
||||
scope,
|
||||
spec,
|
||||
0,
|
||||
profile,
|
||||
geometry_runtime::dom_matrix_worker_constructor_callback,
|
||||
)?,
|
||||
"StorageManager" => {
|
||||
let template = navigator_runtime::build_storage_manager_worker_template(scope);
|
||||
template.read_only_prototype();
|
||||
@@ -726,9 +743,11 @@ pub(in crate::context_bootstrap) fn build_profiled_exposed_interface_template<'s
|
||||
"EventSource" => build_constructor_template_with_callback(
|
||||
scope,
|
||||
spec,
|
||||
1,
|
||||
profile,
|
||||
worker_unsupported_constructor_callback,
|
||||
)?,
|
||||
_ => build_constructor_template(scope, spec)?,
|
||||
_ => build_constructor_template_for_profile(scope, spec, profile)?,
|
||||
};
|
||||
if profile == exposed_interfaces::TemplateBuildProfile::DedicatedWorker
|
||||
&& spec.interface.name() == "FileSystemFileHandle"
|
||||
|
||||
@@ -3,6 +3,7 @@ mod global;
|
||||
mod prototype_bindings;
|
||||
|
||||
pub(super) use constructor_templates::{
|
||||
build_constructor_template, build_constructor_template_with_callback,
|
||||
build_constructor_template, build_constructor_template_for_profile,
|
||||
build_constructor_template_with_callback,
|
||||
};
|
||||
pub(crate) use global::ContextBootstrapAssets;
|
||||
|
||||
@@ -18,7 +18,7 @@ use super::super::{
|
||||
EventSubclassKind, ValueEventKind, build_event_subclass_template,
|
||||
build_value_event_template, event_constructor_callback,
|
||||
},
|
||||
exposed_interfaces::install_interface_template_metadata,
|
||||
exposed_interfaces::{TemplateBuildProfile, install_interface_template_metadata},
|
||||
file_api::{
|
||||
data_transfer_constructor_callback, file_constructor_callback,
|
||||
file_reader_constructor_callback, file_reader_sync_constructor_callback,
|
||||
@@ -89,6 +89,14 @@ use anyhow::{Result, anyhow};
|
||||
pub(in crate::context_bootstrap) fn build_constructor_template<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
spec: ConstructorSpec,
|
||||
) -> Result<v8::Local<'s, v8::FunctionTemplate>> {
|
||||
build_constructor_template_for_profile(scope, spec, TemplateBuildProfile::Window)
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap) fn build_constructor_template_for_profile<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
spec: ConstructorSpec,
|
||||
profile: TemplateBuildProfile,
|
||||
) -> Result<v8::Local<'s, v8::FunctionTemplate>> {
|
||||
let template = match spec.kind {
|
||||
ConstructorKind::Illegal => v8::FunctionTemplate::builder(illegal_constructor_callback)
|
||||
@@ -1093,24 +1101,27 @@ pub(in crate::context_bootstrap) fn build_constructor_template<'s>(
|
||||
.build(scope)
|
||||
}
|
||||
};
|
||||
finalize_constructor_template(scope, spec, template)
|
||||
finalize_constructor_template(scope, spec, template, profile)
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap) fn build_constructor_template_with_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
spec: ConstructorSpec,
|
||||
length: i32,
|
||||
profile: TemplateBuildProfile,
|
||||
callback: impl v8::MapFnTo<v8::FunctionCallback>,
|
||||
) -> Result<v8::Local<'s, v8::FunctionTemplate>> {
|
||||
let template = v8::FunctionTemplate::builder(callback)
|
||||
.length(1)
|
||||
.length(length)
|
||||
.build(scope);
|
||||
finalize_constructor_template(scope, spec, template)
|
||||
finalize_constructor_template(scope, spec, template, profile)
|
||||
}
|
||||
|
||||
fn finalize_constructor_template<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
spec: ConstructorSpec,
|
||||
template: v8::Local<'s, v8::FunctionTemplate>,
|
||||
profile: TemplateBuildProfile,
|
||||
) -> Result<v8::Local<'s, v8::FunctionTemplate>> {
|
||||
let class_name = v8_string(scope, spec.interface.name()).ok_or_else(|| {
|
||||
anyhow!(
|
||||
@@ -1127,7 +1138,7 @@ fn finalize_constructor_template<'s>(
|
||||
template.read_only_prototype();
|
||||
}
|
||||
|
||||
install_constructor_template_bindings(scope, template, spec);
|
||||
install_constructor_template_bindings(scope, template, spec, profile);
|
||||
install_interface_template_metadata(scope, template, spec.interface.name());
|
||||
|
||||
Ok(template)
|
||||
|
||||
@@ -18,6 +18,7 @@ use super::super::{
|
||||
dom_rect::install_dom_rect_template_bindings,
|
||||
dom_rect_list::install_dom_rect_list_template_bindings,
|
||||
event_template::install_event_template_bindings,
|
||||
exposed_interfaces::TemplateBuildProfile,
|
||||
file_api::install_file_api_template_bindings,
|
||||
geometry_runtime::install_geometry_template_bindings,
|
||||
idle_detection::install_idle_detector_template_bindings,
|
||||
@@ -406,6 +407,7 @@ pub(super) fn install_constructor_template_bindings<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
template: v8::Local<'s, v8::FunctionTemplate>,
|
||||
spec: ConstructorSpec,
|
||||
profile: TemplateBuildProfile,
|
||||
) {
|
||||
install_node_mixin_unscopables(scope, template, spec.interface.name());
|
||||
install_constructor_constant_template_bindings(scope, template, spec.interface.name());
|
||||
@@ -420,7 +422,7 @@ pub(super) fn install_constructor_template_bindings<'s>(
|
||||
install_dom_implementation_template_bindings(scope, template, spec.interface.name());
|
||||
install_custom_element_registry_template_bindings(scope, template, spec.interface.name());
|
||||
install_text_codec_template_bindings(scope, template, spec.interface.name());
|
||||
install_geometry_template_bindings(scope, template, spec.interface.name());
|
||||
install_geometry_template_bindings(scope, template, spec.interface.name(), profile);
|
||||
if spec.interface.name() == "EncodedVideoChunk" {
|
||||
super::super::encoded_video_chunk::install_encoded_video_chunk_template_bindings(
|
||||
scope, template,
|
||||
|
||||
@@ -61,6 +61,13 @@ pub(in crate::context_bootstrap) const WORKER_SHARED_INTERFACE_NAMES: &[&str] =
|
||||
"EventSource",
|
||||
"TextEncoder",
|
||||
"TextDecoder",
|
||||
"DOMPointReadOnly",
|
||||
"DOMPoint",
|
||||
"DOMRectReadOnly",
|
||||
"DOMRect",
|
||||
"DOMQuad",
|
||||
"DOMMatrixReadOnly",
|
||||
"DOMMatrix",
|
||||
"Crypto",
|
||||
"SubtleCrypto",
|
||||
"CryptoKey",
|
||||
|
||||
@@ -811,3 +811,69 @@ fn source_buffer_interfaces_are_exposed_only_to_window_and_dedicated_workers() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn worker_geometry_interfaces_use_worker_specific_matrix_surface() {
|
||||
crate::ensure_v8_for_test();
|
||||
let mut isolate = v8::Isolate::new(Default::default());
|
||||
let scope = pin!(v8::HandleScope::new(&mut isolate));
|
||||
let scope = &mut scope.init();
|
||||
let context = v8::Context::new(scope, Default::default());
|
||||
let scope = &mut v8::ContextScope::new(scope, context);
|
||||
let global = context.global(scope);
|
||||
crate::context_bootstrap::install_worker_lazy_exposed_interfaces(
|
||||
scope,
|
||||
global,
|
||||
super::RealmKind::DedicatedWorker,
|
||||
true,
|
||||
)
|
||||
.expect("worker geometry interfaces should install");
|
||||
|
||||
let source = v8::String::new(
|
||||
scope,
|
||||
r#"
|
||||
(() => {
|
||||
const names = [
|
||||
"DOMPointReadOnly", "DOMPoint", "DOMRectReadOnly", "DOMRect",
|
||||
"DOMQuad", "DOMMatrixReadOnly", "DOMMatrix"
|
||||
];
|
||||
const errorName = callback => {
|
||||
try {
|
||||
callback();
|
||||
return "missing";
|
||||
} catch (error) {
|
||||
return error.name;
|
||||
}
|
||||
};
|
||||
return JSON.stringify({
|
||||
constructors: names.map(name => typeof globalThis[name]).join(","),
|
||||
strings: [
|
||||
errorName(() => new DOMMatrix("matrix(1,0,0,1,0,0)")),
|
||||
errorName(() => new DOMMatrixReadOnly(""))
|
||||
].join(","),
|
||||
tags: [
|
||||
String(new DOMMatrix()),
|
||||
String(DOMMatrixReadOnly.fromMatrix({is2D: false})),
|
||||
String(new DOMMatrix([1, 0, 0, NaN, Infinity, -Infinity]))
|
||||
].join(","),
|
||||
windowOnly: [
|
||||
Object.hasOwn(DOMMatrixReadOnly.prototype, "toString"),
|
||||
"setMatrixValue" in DOMMatrix.prototype,
|
||||
"WebKitCSSMatrix" in globalThis
|
||||
].join(","),
|
||||
lengths: [DOMMatrix.length, DOMMatrixReadOnly.length].join(",")
|
||||
});
|
||||
})()
|
||||
"#,
|
||||
)
|
||||
.expect("worker geometry test source");
|
||||
let script = v8::Script::compile(scope, source, None).expect("worker geometry test compile");
|
||||
let result = crate::script_execution::execute_compiled_script(scope, script)
|
||||
.expect("worker geometry test evaluation")
|
||||
.to_rust_string_lossy(scope);
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
r#"{"constructors":"function,function,function,function,function,function,function","strings":"TypeError,TypeError","tags":"[object DOMMatrix],[object DOMMatrixReadOnly],[object DOMMatrix]","windowOnly":"false,false,false","lengths":"0,0"}"#
|
||||
);
|
||||
}
|
||||
|
||||
@@ -285,9 +285,6 @@ struct DomMatrixReadOnlyPrototypeMethodsDeclaration {
|
||||
#[webapi(method = "toJSON", enumerable, callback = dom_matrix_to_json_callback)]
|
||||
to_json: (),
|
||||
|
||||
#[webapi(method = "toString", callback = dom_matrix_to_string_callback)]
|
||||
to_string: (),
|
||||
|
||||
#[webapi(method = "toFloat32Array", callback = dom_matrix_to_float32_array_callback)]
|
||||
to_float32_array: (),
|
||||
|
||||
@@ -337,6 +334,13 @@ struct DomMatrixReadOnlyPrototypeMethodsDeclaration {
|
||||
transform_point: (),
|
||||
}
|
||||
|
||||
#[derive(WebApiFunctionTemplate)]
|
||||
#[webapi(interface = web_api_interfaces::DOMMatrixReadOnly, enumerable)]
|
||||
struct DomMatrixReadOnlyWindowPrototypeMethodsDeclaration {
|
||||
#[webapi(method = "toString", callback = dom_matrix_to_string_callback)]
|
||||
to_string: (),
|
||||
}
|
||||
|
||||
#[derive(WebApiFunctionTemplate)]
|
||||
#[webapi(interface = web_api_interfaces::DOMMatrixReadOnly, enumerable)]
|
||||
struct DomMatrixReadOnlyConstructorDeclaration {
|
||||
@@ -452,7 +456,11 @@ struct DomMatrixPrototypeMethodsDeclaration {
|
||||
|
||||
#[webapi(method = "invertSelf", callback = dom_matrix_invert_self_callback)]
|
||||
invert_self: (),
|
||||
}
|
||||
|
||||
#[derive(WebApiFunctionTemplate)]
|
||||
#[webapi(name = "DOMMatrix", enumerable)]
|
||||
struct DomMatrixWindowPrototypeMethodsDeclaration {
|
||||
#[webapi(method = "setMatrixValue", length = 1, callback = dom_matrix_set_matrix_value_callback)]
|
||||
set_matrix_value: (),
|
||||
}
|
||||
@@ -640,6 +648,44 @@ pub(super) fn dom_matrix_readonly_constructor_callback<'s>(
|
||||
rv.set(args.this().into());
|
||||
}
|
||||
|
||||
pub(super) fn dom_matrix_worker_constructor_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
if args.is_construct_call() && dom_matrix_worker_string_argument(&args) {
|
||||
throw_type_error(
|
||||
scope,
|
||||
"DOMMatrix string construction is not available in workers.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
dom_matrix_constructor_callback(scope, args, rv);
|
||||
}
|
||||
|
||||
pub(super) fn dom_matrix_readonly_worker_constructor_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
if args.is_construct_call() && dom_matrix_worker_string_argument(&args) {
|
||||
throw_type_error(
|
||||
scope,
|
||||
"DOMMatrixReadOnly string construction is not available in workers.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
dom_matrix_readonly_constructor_callback(scope, args, rv);
|
||||
}
|
||||
|
||||
fn dom_matrix_worker_string_argument(args: &v8::FunctionCallbackArguments<'_>) -> bool {
|
||||
if args.length() == 0 {
|
||||
return false;
|
||||
}
|
||||
let init = args.get(0);
|
||||
init.is_string() || init.is_string_object()
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap) fn initialize_dom_point_object<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
object: v8::Local<'s, v8::Object>,
|
||||
@@ -800,8 +846,10 @@ pub(in crate::context_bootstrap) fn install_geometry_template_bindings<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_, ()>,
|
||||
template: v8::Local<'s, v8::FunctionTemplate>,
|
||||
interface_name: &str,
|
||||
profile: super::exposed_interfaces::TemplateBuildProfile,
|
||||
) {
|
||||
let prototype = template.prototype_template(scope);
|
||||
let install_window_only = profile == super::exposed_interfaces::TemplateBuildProfile::Window;
|
||||
match interface_name {
|
||||
"DOMPointReadOnly" => {
|
||||
DomPointReadOnlyConstructorDeclaration::initialize_template(scope, template);
|
||||
@@ -824,11 +872,21 @@ pub(in crate::context_bootstrap) fn install_geometry_template_bindings<'s>(
|
||||
DomMatrixReadOnlyPrototypeMethodsDeclaration::initialize_prototype_template(
|
||||
scope, prototype,
|
||||
);
|
||||
if install_window_only {
|
||||
DomMatrixReadOnlyWindowPrototypeMethodsDeclaration::initialize_prototype_template(
|
||||
scope, prototype,
|
||||
);
|
||||
}
|
||||
}
|
||||
"DOMMatrix" => {
|
||||
DomMatrixConstructorDeclaration::initialize_template(scope, template);
|
||||
DomMatrixPrototypeAccessorsDeclaration::initialize_prototype_template(scope, prototype);
|
||||
DomMatrixPrototypeMethodsDeclaration::initialize_prototype_template(scope, prototype);
|
||||
if install_window_only {
|
||||
DomMatrixWindowPrototypeMethodsDeclaration::initialize_prototype_template(
|
||||
scope, prototype,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user