From 4df7b1179d3ae82eacdd89b7d73f2e96f676e635 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 06:50:40 -0800 Subject: [PATCH 1/2] JavaScript: accept host constructors in Promise statics --- crates/js/src/builtins/promise.rs | 4 +++- crates/js/src/promise_tests.rs | 31 +++++++++++++++++++++++++++++++ docs/mcp.md | 2 ++ docs/target-app-parity.md | 2 ++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/js/src/builtins/promise.rs b/crates/js/src/builtins/promise.rs index 276acf7e96..165a3d27cf 100644 --- a/crates/js/src/builtins/promise.rs +++ b/crates/js/src/builtins/promise.rs @@ -31,6 +31,7 @@ use super::{arg, constructor, create_at, method, native, new_array}; use crate::LimitKind; use crate::bytecode::Code; use crate::heap::{BOUND_ARG_BYTES, Class, Func, NativeFn, NativeKind, OBJECT_BYTES, OPEN, ObjId, Object, PROP_BYTES, Prop, Roots, key}; +use crate::host::ArgRead; use crate::interp::{Abrupt, ErrorType, Interp, JsResult}; use crate::string_builtins::flat_charged; use crate::value::Value; @@ -912,7 +913,7 @@ fn thrower(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult JsResult { let mut v = v.clone(); loop { @@ -923,6 +924,7 @@ fn is_constructor(interp: &mut Interp, v: &Value) -> JsResult { match func { Func::Closure { code, .. } => return Ok(!code.arrow && !code.method), Func::Native { constructor, .. } => return Ok(constructor), + Func::Data { read: ArgRead::Hosted, .. } => return Ok(v.as_object().is_some_and(|o| interp.is_host_constructor(o))), Func::Bound { target, .. } => { interp.use_fuel()?; v = Value::Object(target); diff --git a/crates/js/src/promise_tests.rs b/crates/js/src/promise_tests.rs index 915427de89..faff90af38 100644 --- a/crates/js/src/promise_tests.rs +++ b/crates/js/src/promise_tests.rs @@ -57,6 +57,37 @@ fn promise_prototype_cannot_be_replaced() { assert_eq!(string(src), "true,true,true"); } +#[test] +fn promise_statics_accept_registered_host_constructors_and_bound_targets() { + let mut r = realm(); + let make: HostFunction = std::rc::Rc::new(|_| Ok(Data::Object(Vec::new()))); + r.define_host_classes( + vec![HostClass { name: "Thing".into(), parent: None, members: Vec::new(), index: None, construct: Some(make) }], + Vec::new(), + ) + .unwrap(); + r.define_host_function("ordinary", Vec::new(), std::rc::Rc::new(|_| Ok(Data::Undefined))).unwrap(); + let result = r + .run( + "var constructors = [Thing, Thing.bind(null), Thing.bind(null).bind(null)]; \ + var methods = ['resolve', 'reject', 'all', 'allSettled', 'race']; var log = []; \ + for (var i = 0; i < constructors.length; i++) { \ + var C = constructors[i]; C.resolve = Promise.resolve; \ + log.push(new C() instanceof Thing); \ + for (var j = 0; j < methods.length; j++) { \ + var p = Promise[methods[j]].call(C, []); \ + log.push(p instanceof Promise); p.catch(function () {}); \ + } \ + } \ + for (var j = 0; j < methods.length; j++) { \ + try { Promise[methods[j]].call(ordinary, []); log.push(false); } \ + catch (e) { log.push(e instanceof TypeError); } \ + } log.join()", + ) + .unwrap(); + assert_eq!(result, JsValue::String(vec!["true"; 23].join(","))); +} + #[test] fn then_callbacks_run_after_the_scripts_code_in_order() { let src = "Promise.resolve(1).then(function (v) { log.push('a' + v); });\n\ diff --git a/docs/mcp.md b/docs/mcp.md index 437569a1f9..16eaba46c0 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2890,6 +2890,8 @@ own rules: - `all`, `allSettled` and `race` take an array, another object whose `length` is a whole number, or a string (one item per character); any other value rejects the promise they return with a `TypeError`. The promises `then` and the statics make are always plain `Promise` objects; `Symbol.species` and subclasses are not read. + The statics accept registered host class constructors and bound constructors as `this`; ordinary host functions + still give a `TypeError`. - A promise rejected with no rejection handler that still has none when the run ends successfully is reported: the report's `unhandledRejections` lists the first 20 as `{message, messageOrigin, line, column, file?}`, where it was rejected (the `throw`, or the call of its reject function or of `Promise.reject`), `unhandledRejectionCount` counts diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index b564a77730..206f0a4cd9 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -32,6 +32,8 @@ ## Feature submission evidence +Scripts: Promise statics accept registered host class constructors and their bound functions as `this`, while ordinary host functions give a `TypeError`. The statics still return built-in Promise objects. Test: `promise_statics_accept_registered_host_constructors_and_bound_targets` in `crates/js/src/promise_tests.rs`. + Variables panel row context menus (2026-11-20, G12): Right-click highlights the clicked variable without changing the canvas selection and offers Select Bound Object, Variable Options…, Unbind Variable, the four Make Dynamic actions, and Delete Variable. Shift+F10 opens the same menu for the focused row; Escape dismisses it. Row actions target that variable; dynamic actions use the canvas selection. Lock and selection checks disable unavailable actions. Headless pointer/accessibility tests cover opening the menu, highlighting, bound-object selection, deletion in one undo step and restoration, and disabled actions. Scores and estimates unchanged. Actions panel item context menus (2026-11-20): Right-clicking an Action Set row selects it and presents a context menu with New Action…, New Set…, Duplicate Set, Delete Set, and Set Options…. Right-clicking an Action row selects it and presents a context menu with Play, Record, Duplicate, Delete, Action Options…, and Batch…. Duplicate on an action duplicates that action and selects the duplicate; Delete removes it. Headless tests in `crates/ui-egui/src/panels/actions.rs`: `right_clicking_an_action_set_selects_it_and_presents_set_context_options`, `right_clicking_an_action_selects_it_and_presents_action_options`, and `clicking_duplicate_on_an_action_duplicates_that_action_in_the_set`. Scores and estimates unchanged. From c358968590a651d5835e4de6d6a6580a2b575650 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 00:39:56 -0800 Subject: [PATCH 2/2] JS: Promise statics reject host classes registered without construct A host class registered with construct: None still has a constructor function that throws when called, so Promise.resolve and the other statics accepted it as a constructor. The class entry now records whether a construct function was supplied, and the statics check that. `new` on such a class still throws its existing TypeError. --- crates/js/src/builtins/promise.rs | 2 +- crates/js/src/host_classes.rs | 8 ++++++++ crates/js/src/promise_tests.rs | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/js/src/builtins/promise.rs b/crates/js/src/builtins/promise.rs index 7118314dd1..7f684fbbf5 100644 --- a/crates/js/src/builtins/promise.rs +++ b/crates/js/src/builtins/promise.rs @@ -947,7 +947,7 @@ fn is_constructor(interp: &mut Interp, v: &Value) -> JsResult { match func { Func::Closure { code, .. } => return Ok(!code.arrow && !code.method && !code.generator), Func::Native { constructor, .. } => return Ok(constructor), - Func::Data { read: ArgRead::Hosted, .. } => return Ok(v.as_object().is_some_and(|o| interp.is_host_constructor(o))), + Func::Data { read: ArgRead::Hosted, .. } => return Ok(v.as_object().is_some_and(|o| interp.is_constructible_host_class(o))), Func::Bound { target, .. } => { interp.use_fuel()?; v = Value::Object(target); diff --git a/crates/js/src/host_classes.rs b/crates/js/src/host_classes.rs index 9d14718dee..c42e978208 100644 --- a/crates/js/src/host_classes.rs +++ b/crates/js/src/host_classes.rs @@ -83,6 +83,8 @@ pub(crate) struct Entry { name: Rc, proto: ObjId, ctor: ObjId, + /// Whether the class was registered with a `construct` function, so scripts can construct it. + constructible: bool, index: Option, /// The class without a parent this class descends from (itself for such a class), as an index /// into the realm's classes. @@ -215,6 +217,11 @@ impl Interp { self.host_classes.iter().any(|e| e.ctor == id) } + /// Whether `id` is the constructor of a host class registered with a `construct` function. + pub(crate) fn is_constructible_host_class(&self, id: ObjId) -> bool { + self.host_classes.iter().any(|e| e.ctor == id && e.constructible) + } + /// The indexer of the host class of `id`, when it is an object of a host class that has one. pub(crate) fn host_indexer(&self, id: ObjId) -> JsResult> { let Class::Host(h) = &self.object(id)?.class else { return Ok(None) }; @@ -325,6 +332,7 @@ fn install_class(i: &mut Interp, class: HostClass) -> JsResult<()> { name: class.name.as_str().into(), proto, ctor, + constructible: class.construct.is_some(), index: class.index, family, parent: parent_class, diff --git a/crates/js/src/promise_tests.rs b/crates/js/src/promise_tests.rs index 7dd28cbe16..21b03f7cb6 100644 --- a/crates/js/src/promise_tests.rs +++ b/crates/js/src/promise_tests.rs @@ -88,6 +88,26 @@ fn promise_statics_accept_registered_host_constructors_and_bound_targets() { assert_eq!(result, JsValue::String(vec!["true"; 23].join(","))); } +#[test] +fn promise_statics_reject_host_classes_registered_without_construct() { + let mut r = realm(); + r.define_host_classes(vec![HostClass { name: "Fixed".into(), parent: None, members: Vec::new(), index: None, construct: None }], Vec::new()) + .unwrap(); + let result = r + .run( + "var log = []; \ + try { new Fixed(); log.push('constructed'); } \ + catch (e) { log.push(e instanceof TypeError && e.message === 'Fixed cannot be constructed by a script'); } \ + var methods = ['resolve', 'reject', 'all', 'allSettled', 'race']; \ + for (var j = 0; j < methods.length; j++) { \ + try { Promise[methods[j]].call(Fixed, []); log.push('accepted'); } \ + catch (e) { log.push(e instanceof TypeError && e.message.indexOf('not a constructor') >= 0); } \ + } log.join()", + ) + .unwrap(); + assert_eq!(result, JsValue::String(["true"; 6].join(","))); +} + #[test] fn promise_prototype_has_a_configurable_string_tag() { assert_eq!(