mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Merge pull request #9403 from dotCooCoo/fix/promise-host-constructors
JavaScript: accept host constructors in Promise statics
This commit is contained in:
5 files changed
+70
-1
No files matched your search
@@ -33,6 +33,7 @@ use crate::bytecode::Code;
|
||||
use crate::heap::{
|
||||
BOUND_ARG_BYTES, CONFIGURABLE, 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::value::Value;
|
||||
|
||||
@@ -934,7 +935,8 @@ fn thrower(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult<V
|
||||
}
|
||||
|
||||
/// `v` is a constructor: a function written in JavaScript other than an arrow function, generator
|
||||
/// function or method, a built-in constructor, or a bound function whose target is one.
|
||||
/// function or method, a built-in or registered host constructor, or a bound function whose target
|
||||
/// is one.
|
||||
fn is_constructor(interp: &mut Interp, v: &Value) -> JsResult<bool> {
|
||||
let mut v = v.clone();
|
||||
loop {
|
||||
@@ -945,6 +947,7 @@ fn is_constructor(interp: &mut Interp, v: &Value) -> JsResult<bool> {
|
||||
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_constructible_host_class(o))),
|
||||
Func::Bound { target, .. } => {
|
||||
interp.use_fuel()?;
|
||||
v = Value::Object(target);
|
||||
|
||||
@@ -131,6 +131,8 @@ pub(crate) struct Entry {
|
||||
name: Rc<str>,
|
||||
proto: ObjId,
|
||||
ctor: ObjId,
|
||||
/// Whether the class was registered with a `construct` function, so scripts can construct it.
|
||||
constructible: bool,
|
||||
index: Option<HostFunction>,
|
||||
/// The class without a parent this class descends from (itself for such a class), as an index
|
||||
/// into the realm's classes.
|
||||
@@ -296,6 +298,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 name of the host class of `id`, when it is an object of a host class.
|
||||
pub(crate) fn host_class_name(&self, id: ObjId) -> JsResult<Option<Rc<str>>> {
|
||||
let Class::Host(h) = &self.object(id)?.class else { return Ok(None) };
|
||||
@@ -445,6 +452,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,
|
||||
|
||||
@@ -57,6 +57,60 @@ 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(), info: 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 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(), info: 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!(
|
||||
|
||||
@@ -2997,6 +2997,8 @@ own rules:
|
||||
the combinator calls the iterator's `return` method if present; the original exception remains the rejection reason
|
||||
even when `return` throws. An iterator whose step threw or finished is not closed. 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
|
||||
|
||||
@@ -55,6 +55,8 @@ XMP parsing rejects invalid qualified names, text other than XML whitespace besi
|
||||
|
||||
Scripts: `Promise.prototype[Symbol.toStringTag]` is `"Promise"` when Symbols are available, with a configurable, non-enumerable, non-writable descriptor. Promise instances report `[object Promise]` in engines without Symbols. Test: `promise_prototype_has_a_configurable_string_tag` in `crates/js/src/promise_tests.rs`.
|
||||
|
||||
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`.
|
||||
|
||||
Color conversion (#8804) builds one first-name index for the document's global swatch bases and uses indexed lookups for linked paints. Document Color Mode, new-art colors and text-style conversion keep first-record shadowing, spot handling, tint behavior and missing-link fallback. The base construction changes from quadratic name searches to O(N log N) index work; this is a source-complexity result, not a measured elapsed-time or budget improvement. Command-level semantic controls are in `crates/engine/src/tests/colormgmt.rs`; scores and estimates are unchanged.
|
||||
|
||||
History panel row context menus (2026-11-20, G12): Right-clicking any state offers New Document from State, Step to This State, and Clear History. Creating a document uses the clicked snapshot without stepping the source document; Step to This State uses undo/redo and is disabled on the current row; Clear History removes undo and redo steps without changing artwork and is disabled when history is empty. Headless tests in `crates/ui-egui/src/panels/history.rs` cover opening an earlier row menu, snapshot creation, backward/forward jumps, and clearing history. The snapshot chosen when the menu opens is retained while it stays open; pending tool edits are settled before resolving that snapshot to its new row; a discarded snapshot reports that it is unavailable instead of acting on a neighboring row. Jumps settle pending typing before counting undo/redo steps. Shift+F10 opens the focused row menu; Escape dismisses it and restores row focus. Switching documents dismisses the menu before it can act on another document. Headless tests cover these keyboard and document-switch paths. The list and menu use shared Craft UI controls. Scores and estimates unchanged.
|
||||
|
||||
Reference in new issue
Block a user