Skip to content

script: Readily accept all event names as Atoms#43066

Merged
mrobinson merged 1 commit into
servo:mainfrom
mrobinson:event-names-as-atoms
Mar 6, 2026
Merged

script: Readily accept all event names as Atoms#43066
mrobinson merged 1 commit into
servo:mainfrom
mrobinson:event-names-as-atoms

Conversation

@mrobinson
Copy link
Copy Markdown
Member

Accepting the name of the event as a DOMString means that when events
are constructed from within Servo (for instance via input event
handling), the static strings are converted to an owned String
wrapped in a DOMString and then finally converted to an Atom in
Event. This makes it so that all non-generated Event constructors
accept Atom, which can avoid a conversion entirely when the atom
already exists on the Stylo atoms list and eliminate one in-memory copy
in the case that it isn't on the atom list.

Eventually, all event names can be on the atom list and we can eliminate
all copies. This is a tiny optimization, but also makes the code much
friendlier everywhere.

Testing: This should not change behavior so should be covered by existing test.

Accepting the name of the event as a `DOMString` means that when events
are constructed from within Servo (for instance via input event
handling), the static strings are converted to an owned `String`
wrapped in a `DOMString` and then finally converted to an `Atom` in
`Event`. This makes it so that all non-generated `Event` constructors
accept `Atom`, which can avoid a conversion entirely when the atom
already exists on the Stylo atoms list and eliminate one in-memory copy
in the case that it isn't on the atom list.

Eventually, all event names can be on the atom list and we can eliminate
all copies. This is a tiny optimization, but also makes the code much
friendlier everywhere.

Signed-off-by: Martin Robinson <[email protected]>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 6, 2026
Copy link
Copy Markdown
Member

@sagudev sagudev left a comment

Choose a reason for hiding this comment

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

LGTM, tho we should have more consistent style for constructing atoms.

Comment on lines -2979 to 2981
FocusEventType::Blur => (DOMString::from("blur"), EventBubbles::DoesNotBubble),
FocusEventType::Focus => ("focus".into(), EventBubbles::DoesNotBubble),
FocusEventType::Blur => ("blur".into(), EventBubbles::DoesNotBubble),
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Use atom! here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think the issue is that "blur" is not on the list of atoms in Stylo, so atom! will fail as it resolves the static string at compile-time. I think that the solution for this is probably to create servo-atoms and look for all of the places that we convert static strings to atoms and add them to the servo-atoms list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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


// Send pointermove event before mousemove.
let pointer_event = mouse_event.to_pointer_event("pointermove", can_gc);
let pointer_event = mouse_event.to_pointer_event(Atom::from("pointermove"), can_gc);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

AFAIK atom! is preferred?

Suggested change
let pointer_event = mouse_event.to_pointer_event(Atom::from("pointermove"), can_gc);
let pointer_event = mouse_event.to_pointer_event(atom!("pointermove"), can_gc);

Some(&self.window), // view
0, // detail
&self.window, // window
"contextmenu".into(), // type
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"contextmenu".into(), // type
atom!("contextmenu"), // type

let dom_event = WheelEvent::new(
&self.window,
DOMString::from(wheel_event_type_string),
"wheel".into(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"wheel".into(),
atom!("wheel"),

PI / 2.0, // altitude_angle (perpendicular to surface)
0.0, // azimuth_angle
DOMString::from("touch"),
"touch".into(),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"touch".into(),
atom!("touch"),

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Mar 6, 2026
@mrobinson
Copy link
Copy Markdown
Member Author

I've generally tried to use into() for the atoms that aren't on Stylo's list of statically defined atoms. We should probably create servo-atoms and add all of these strings to the list of strings that are processed at compile-time.

@mrobinson
Copy link
Copy Markdown
Member Author

@sagudev Thank you for the review. I'll go ahead and land this as I think I cannot apply your suggestions. If there is any remaining issue I am happy to fix it in a followup.

@mrobinson mrobinson added this pull request to the merge queue Mar 6, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Mar 6, 2026
Merged via the queue into servo:main with commit 291e9e5 Mar 6, 2026
33 checks passed
@mrobinson mrobinson deleted the event-names-as-atoms branch March 6, 2026 21:15
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants