Skip to content

Veercodeprog/feat/5144 boa class plain properties#5242

Open
Veercodeprog wants to merge 4 commits intoboa-dev:mainfrom
Veercodeprog:veercodeprog/feat/5144-boa-class-plain-properties
Open

Veercodeprog/feat/5144 boa class plain properties#5242
Veercodeprog wants to merge 4 commits intoboa-dev:mainfrom
Veercodeprog:veercodeprog/feat/5144-boa-class-plain-properties

Conversation

@Veercodeprog
Copy link

feat(macros): add #[boa(js_init)] support to #[boa_class]

Closes #5144 (partial — instance-side initialization only)

Summary

Introduces the #[boa(js_init)] attribute to the #[boa_class] macro,
allowing embedders to hook into object_constructor without having to
hand-write a full impl Class block. This enables setting own data
properties on a freshly constructed JsObject instance — the first
concrete step toward the plain-property support discussed in #5144.

Motivation

Currently, #[boa_class] only exposes data_constructor via
#[boa(constructor)]. If you want to set own JS properties after the
object exists (e.g. instance.upcast().set("label", ...)), you have to
drop out of the macro entirely and write impl Class manually. The
object_constructor hook already exists on the Class trait for
exactly this purpose — this PR just wires it up through the macro.

Changes

core/macros/src/class.rs

  • Parse #[boa(js_init)] on at most one function per #[boa_class] impl block.
  • Validate the expected shape: no self, no generics or async, exactly
    three parameters (instance, args, context), return type must be
    (), JsResult<()>, or omitted (normalized to ()).
  • Emit fn object_constructor(...) in the generated impl Class when a
    js_init function is present; otherwise fall back to the no-op trait
    default.
  • Exclude js_init-annotated functions from method/static registration
    so they are not accidentally exposed as JS-callable methods.

core/macros/src/lib.rs

  • Add #[boa(js_init)] to the boa_class attribute documentation with
    a brief usage example.

tests/macros/tests/class.rs

  • New Widget class and boa_class_js_init_sets_own_property test:
    #[boa(constructor)] stores the native id field; #[boa(js_init)]
    sets an own data property label from a second constructor argument,
    verifying end-to-end that instance-side initialization works.

Scope

Covered by this PR:

  • Instance-side property initialization via the existing
    object_constructor trait hook.

Out of scope / follow-up work for #5144:

  • Prototype and static property declarations
    (#[boa(property)] / #[boa(static_property)]ClassBuilder::property).
  • Declarative const-style properties, particularly where values involve
    non-const types such as JsValue.
  • Unifying data_constructor and object_constructor into a single
    constructor model (if the project pursues that direction).
  • Proc-macro-level type validation for js_init parameters — wrong types
    still produce a compile error at the forwarded Self::… call site.

Testing

# Run the new macro integration test
cargo test -p boa_macros_tests --test class

# Lint check
cargo clippy -p boa_macros --all-targets -- -D warnings

@Veercodeprog Veercodeprog requested a review from a team as a code owner March 23, 2026 21:05
@github-actions github-actions bot added the Waiting On Review Waiting on reviews from the maintainers label Mar 23, 2026
@github-actions github-actions bot added this to the v1.0.0 milestone Mar 23, 2026
@github-actions github-actions bot added C-Tests Issues and PRs related to the tests. C-Builtins PRs and Issues related to builtins/intrinsics labels Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Builtins PRs and Issues related to builtins/intrinsics 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.

Support defining plain properties in #[boa_class]

1 participant