Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions core/engine/src/object/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,20 +1273,28 @@ impl JsObject {
constructor: &Self,
context: &mut Context,
) -> JsResult<()> {
let constructor_function = constructor
.downcast_ref::<OrdinaryFunction>()
.js_expect("class constructor must be function object")?;
// Copied, as the initializers may change the constructor itself, e.g. its static private
// fields, which they couldn't while it's borrowed:
let (methods, fields) = {
let constructor_function = constructor
.downcast_ref::<OrdinaryFunction>()
.js_expect("class constructor must be function object")?;
(
constructor_function.get_private_methods().to_vec(),
constructor_function.get_fields().to_vec(),
)
};

// 1. Let methods be the value of constructor.[[PrivateMethods]].
// 2. For each PrivateElement method of methods, do
for (name, method) in constructor_function.get_private_methods() {
for (name, method) in &methods {
// a. Perform ? PrivateMethodOrAccessorAdd(O, method).
self.private_method_or_accessor_add(name, method, context)?;
}

// 3. Let fields be the value of constructor.[[Fields]].
// 4. For each element fieldRecord of fields, do
for field_record in constructor_function.get_fields() {
for field_record in &fields {
// a. Perform ? DefineField(O, fieldRecord).
self.define_field(field_record, context)?;
}
Expand Down
15 changes: 15 additions & 0 deletions core/engine/src/tests/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,18 @@ fn class_boolean_literal_getter_setter_names() {
TestAction::assert_eq("c.false", 84),
]);
}

#[test]
fn class_field_initializer_sets_a_static_private_field() {
run_test_actions([
TestAction::run(indoc! {r#"
class C {
static #count = 0;
id = ++C.#count;
}
new C();
let c = new C();
"#}),
TestAction::assert_eq("c.id", 2),
]);
}