From bb3ceb56b81cd9c9ebb3466ab1c03731b96689e5 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 10:57:43 -0800 Subject: [PATCH] Charge Symbol registry hashing and correct key buffer reservations --- Cargo.lock | 2 +- crates/js/src/builtins/symbol.rs | 7 ++++--- crates/js/src/lib.rs | 3 ++- crates/js/src/symbol_tests.rs | 28 ++++++++++++++++++++++++++++ docs/mcp.md | 3 +++ docs/target-app-parity.md | 2 ++ 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 653aa64e73..905d33b8fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8480,7 +8480,7 @@ dependencies = [ "proptest", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "thiserror 2.0.21", ] diff --git a/crates/js/src/builtins/symbol.rs b/crates/js/src/builtins/symbol.rs index b671f51e42..b709a1caf1 100644 --- a/crates/js/src/builtins/symbol.rs +++ b/crates/js/src/builtins/symbol.rs @@ -5,7 +5,7 @@ //! (section 20.4.4.45). Installed in a realm with [`crate::Options::es2015`] only. use super::{arg, constructor, create_at, data, method, native, new_array}; -use crate::heap::{CONFIGURABLE, Class, NativeFn, NativeKind, ObjId, Object, Prop, SYMBOL_BYTES, string_bytes}; +use crate::heap::{CONFIGURABLE, Class, NativeFn, NativeKind, ObjId, Object, Prop, SYMBOL_BYTES}; use crate::interp::{Hint, Interp, JsResult}; use crate::symbol::{Es2015Objects, REGISTRY_ENTRY_BYTES, Sym, WellKnown}; use crate::value::{Str, Value}; @@ -87,11 +87,12 @@ fn symbol_ctor(interp: &mut Interp, _: Value, args: &[Value], construct: bool) - fn symbol_for(interp: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult { let key = interp.to_str(&arg(args, 0))?; let units = crate::string_builtins::flat_charged(interp, &key)?; + crate::string_builtins::charge_units(interp, units.len())?; if let Some(v) = interp.symbols.registry.get(&*units) { return Ok(v.clone()); } - let extra = REGISTRY_ENTRY_BYTES.saturating_add(string_bytes(units.len())); - let sym = new_symbol(interp, Some(Str::from_rc(units.clone())), true, extra)?; + crate::string_builtins::charge_units(interp, units.len())?; + let sym = new_symbol(interp, Some(Str::from_rc(units.clone())), true, REGISTRY_ENTRY_BYTES)?; interp.symbols.registry.insert(units, Value::Symbol(sym.clone())); Ok(Value::Symbol(sym)) } diff --git a/crates/js/src/lib.rs b/crates/js/src/lib.rs index 17f2b1de0f..f903a9519f 100644 --- a/crates/js/src/lib.rs +++ b/crates/js/src/lib.rs @@ -77,7 +77,8 @@ //! Number hint. A Symbol converted to a string or a number without `String(symbol)` or `toString` throws a //! `TypeError`. A string converts to a number in the `0b` and `0o` forms as well. Every iterator //! step and every prototype a Symbol-keyed lookup passes takes fuel, and every Symbol and registry -//! entry counts against the heap limit. +//! entry counts against the heap limit. Registry lookups and insertions take fuel for the key's +//! code units, and a registered description shares the key's existing buffer. //! //! With [`Options::es2015`], the realm also provides `Promise` (ECMA-262 11th edition, section //! 25.6, ): the constructor, `then`, diff --git a/crates/js/src/symbol_tests.rs b/crates/js/src/symbol_tests.rs index 2ddd86828a..c91a493424 100644 --- a/crates/js/src/symbol_tests.rs +++ b/crates/js/src/symbol_tests.rs @@ -57,6 +57,34 @@ fn symbol_for_and_key_for_use_the_registry() { throws("Symbol.for(Symbol())", "TypeError"); } +#[test] +fn symbol_registry_hashing_charges_flat_keys_on_hits_and_misses() { + for hit in [false, true] { + let mut realm = es2015(Limits::default()); + let key = "x".repeat(32_768); + assert_eq!(realm.run(&format!("var registryKey = '{key}';")), Ok(JsValue::Undefined)); + if hit { + assert_eq!(realm.run("typeof Symbol.for(registryKey)"), Ok(JsValue::String("symbol".into()))); + } + realm.set_limits(Limits { fuel: 500, ..Limits::default() }); + assert!(matches!(realm.run("Symbol.for(registryKey)"), Err(RunError { kind: RunErrorKind::Limit(LimitKind::Fuel), .. })), "hit={hit}"); + realm.set_limits(Limits { fuel: 10_000, ..Limits::default() }); + assert_eq!(realm.run("Symbol.for(registryKey) === Symbol.for(registryKey)"), Ok(JsValue::Bool(true))); + } +} + +#[test] +fn symbol_registry_keeps_a_flat_key_buffer_only_once() { + let mut realm = es2015(Limits::default()); + let key = "x".repeat(32_768); + assert_eq!(realm.run(&format!("var sharedKey = '{key}';")), Ok(JsValue::Undefined)); + let used = realm.collect_garbage(); + let room = crate::heap::SYMBOL_BYTES + crate::symbol::REGISTRY_ENTRY_BYTES + 4_096; + realm.set_limits(Limits { heap_bytes: used + room, ..Limits::default() }); + assert_eq!(realm.run("var registered = Symbol.for(sharedKey); Symbol.keyFor(registered) === sharedKey"), Ok(JsValue::Bool(true))); + assert!(realm.collect_garbage() <= used + room); +} + #[test] fn implicit_conversion_of_a_symbol_throws_a_type_error() { throws("Symbol() + ''", "TypeError"); diff --git a/docs/mcp.md b/docs/mcp.md index a12daa1b46..08bbc6d0c1 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -2695,6 +2695,9 @@ vectorcraft-cli run --cmd script.check --params '{"path": "Tidy Layers.jsx"}' ``` `script.run {path? | source?, name?, args?, answers?, maxFuel?}` runs a JavaScript file against the open documents. + +In an ES2015 realm, `Symbol.for` lookups and insertions take fuel for the key's code units. A registered description +shares the key's existing string buffer. File › Scripts › Other Script… (Cmd+F12) picks a `.js` or `.jsx` file and runs it the same way; the help page https://helpx.adobe.com/illustrator/desktop/automate-visualize-data/automate-actions/install-and-run-scripts.html describes that menu item. Pass exactly one of `path` and `source`. A `path` must end in `.js` or `.jsx`, in any diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index d56d4fd4cd..bf6293240a 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -32,6 +32,8 @@ ## Feature submission evidence +`Symbol.for` lookups and insertions take fuel for the key's code units, including registry hits on flat strings. A registered description shares the key's existing buffer. Regressions in `crates/js/src/symbol_tests.rs` cover hits, misses, and registering a rooted flat key when the heap has room for metadata but not another key buffer. + `vectorcraft-js` ends a paused or waiting run before `Realm::run` or `Realm::start` compiles new source. Its globals remain available. The earlier run's remaining code and queued jobs do not run. Regressions in `crates/js/src/host_tests.rs` cover paused scripts, Promise reactions, waiting host calls, invalid replacement source, and deferred language settings. 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.