Veercodeprog/feat/5144 boa class plain properties#5242
Open
Veercodeprog wants to merge 4 commits intoboa-dev:mainfrom
Open
Veercodeprog/feat/5144 boa class plain properties#5242Veercodeprog wants to merge 4 commits intoboa-dev:mainfrom
Veercodeprog wants to merge 4 commits intoboa-dev:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_constructorwithout having tohand-write a full
impl Classblock. This enables setting own dataproperties on a freshly constructed
JsObjectinstance — the firstconcrete step toward the plain-property support discussed in #5144.
Motivation
Currently,
#[boa_class]only exposesdata_constructorvia#[boa(constructor)]. If you want to set own JS properties after theobject exists (e.g.
instance.upcast().set("label", ...)), you have todrop out of the macro entirely and write
impl Classmanually. Theobject_constructorhook already exists on theClasstrait forexactly this purpose — this PR just wires it up through the macro.
Changes
core/macros/src/class.rs#[boa(js_init)]on at most one function per#[boa_class] implblock.self, no generics orasync, exactlythree parameters (
instance,args,context), return type must be(),JsResult<()>, or omitted (normalized to()).fn object_constructor(...)in the generatedimpl Classwhen ajs_initfunction is present; otherwise fall back to the no-op traitdefault.
js_init-annotated functions from method/static registrationso they are not accidentally exposed as JS-callable methods.
core/macros/src/lib.rs#[boa(js_init)]to theboa_classattribute documentation witha brief usage example.
tests/macros/tests/class.rsWidgetclass andboa_class_js_init_sets_own_propertytest:#[boa(constructor)]stores the nativeidfield;#[boa(js_init)]sets an own data property
labelfrom a second constructor argument,verifying end-to-end that instance-side initialization works.
Scope
Covered by this PR:
object_constructortrait hook.Out of scope / follow-up work for #5144:
(
#[boa(property)]/#[boa(static_property)]→ClassBuilder::property).const-style properties, particularly where values involvenon-
consttypes such asJsValue.data_constructorandobject_constructorinto a singleconstructor model (if the project pursues that direction).
js_initparameters — wrong typesstill produce a compile error at the forwarded
Self::…call site.Testing