Skip to content

fix(engine): let class field initializers set the class's static private fields - #5690

Open
lehni wants to merge 1 commit into
boa-dev:mainfrom
lehni:fix/class-field-static-private-set
Open

lehni wants to merge 1 commit into
boa-dev:mainfrom
lehni:fix/class-field-static-private-set

Conversation

@lehni

@lehni lehni commented Nov 24, 2026

Copy link
Copy Markdown

This Pull Request fixes a panic when an instance field initializer sets a static private field of its own class:

class C {
    static #count = 0;
    id = ++C.#count;
}
new C(); // panics: Object already borrowed: BorrowMutError

initialize_instance_elements kept the class constructor borrowed (downcast_ref::<OrdinaryFunction>()) while it ran the field initializers. PrivateSet on the constructor then needed it mutably.

It changes the following:

  • initialize_instance_elements copies the constructor's private methods and fields first, so the borrow ends before any initializer runs
  • Adds a test, class_field_initializer_sets_a_static_private_field

…ate fields

- initialize_instance_elements copies the constructor's private methods and fields instead of keeping it borrowed while their initializers run
@lehni
lehni requested a review from a team as a code owner November 24, 2026 21:43
@github-actions github-actions Bot added Waiting On Review Waiting on reviews from the maintainers C-Tests Issues and PRs related to the tests. labels Nov 24, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 24, 2026
lehni added a commit to lehni/scriptographer-vectorcraft that referenced this pull request Nov 24, 2026
- Core 524a90a with Paper.js release/1.0's hosted build; projects take their host as `project.host`
- Boa pinned to lehni/boa with the fixes for boa-dev/boa#5690 and storytold#5691, which 1.0's classes hit
- Tests follow 1.0: new symbols keep their place, circles start at the right as in SVG

@abdulhamid999 abdulhamid999 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed against core/engine/src/object/operations.rs, core/engine/src/builtins/function/mod.rs, and core/engine/src/vm/opcode/environment/mod.rs (static inspection; not compiled locally).

Findings

  • Root cause & fix: In JsObject::initialize_instance_elements, define_field invokes initializer.call(...), so an instance field initializer mutating a static private field (++C.#count) reaches C.private_set -> self.borrow_mut() while constructor_function (GcRef<'_, OrdinaryFunction>) was still alive, causing a BorrowMutError panic. Cloning (methods, fields) in an inner block drops the borrow before either loop runs.
  • Why dropping before methods too is safer: private_method_or_accessor_add calls context.host_hooks().ensure_can_add_private_element(self, context)? and self.is_extensible(context)?. If a derived constructor's super() returns a Proxy whose isExtensible trap accesses C.#staticField, releasing the borrow before the methods loop prevents the same panic there as well.
  • Callers & fast path: Both call sites (ClassConstructor path in core/engine/src/builtins/function/mod.rs where function is already dropped beforehand, and SuperCall in core/engine/src/vm/opcode/environment/mod.rs which clones active_function) do not hold competing borrows on constructor. Also, when a class has no private methods or instance fields, .to_vec() on empty slices does not allocate on the heap.
  • Test coverage: class_field_initializer_sets_a_static_private_field directly reproduces and guards the reported crash.

Clean and minimal fix!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants