Commit Graph

37 Commits

Author SHA1 Message Date
Sam
3047b9946f script: Remove AsVoidPtr and AsCCharPtrPtr traits (#42153)
We migrated most uses some years ago, so let's finally remove them. It's
more clear what we are doing if we inline stuff then hiding dangerous
cast behind trait.

Testing: Covered by WPT, try run:
https://github.com/sagudev/servo/actions/runs/21350967668

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-26 10:47:35 +00:00
Sam
2503cc2ab7 script: Pass &CStr insted of &str in {set,get}_dictionary_property (#42125)
It's stupid to create rust strings in the first place to only convert
them into C-style for SM anyway. Furthermore most of those strings are
const literals (comp time strings), so now we will just store additional
null byte within them and able to avoid any runtime conversions.
We should do this in more places.

Testing: Just refactor.

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-24 13:03:41 +00:00
Josh Matthews
cadcc605e0 script: Replace uses of RootedVec<JSVal> with Rooted<Vec<JSVal>>. (#41921)
This replaces uses of our custom RootedVec type that were storing JS GC
values with stack rooting that is better integrated with the engine.
This ensures that storing nursery-allocated objects in these vectors is
handled correctly during GC, unlike our RootedVec implementation which
trips an assertion in debug mozjs builds.

Testing: No observable difference in non-mozjs builds.
Fixes: part of #40141

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
2026-01-22 03:28:19 +00:00
webbeef
f13b8e6ed5 chore: suppress warnings when building without the testbinding feature (#42003)
When building without the `testbinding` feature of `libservo`, we end up
with these compilation warnings (and one error):

```
warning: unused import: `native_from_handlevalue`
   --> components/script_bindings/import.rs:108:9
    |
108 |         native_from_handlevalue, native_from_object_static,
    |         ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(unused_imports)]` (part of `#[warn(unused)]`) on by default

warning: unused imports: `DomSlice` and `Dom`
   --> components/script_bindings/import.rs:131:34
    |
131 |     pub(crate) use crate::root::{Dom, DomSlice, MaybeUnreflectedDom, Root};
    |                                  ^^^  ^^^^^^^^

warning: unused import: `crate::root::DomRoot`
 --> /home/webbeef/servo/target/debug/build/script_bindings-9c5650a8d750af8f/out/DomTypes.rs:3:5
  |
3 | use crate::root::DomRoot;
  |     ^^^^^^^^^^^^^^^^^^^^

warning: unused import: `Maplike`
   --> components/script_bindings/import.rs:124:34
    |
124 |     pub(crate) use crate::like::{Maplike, Setlike};
    |                                  ^^^^^^^

warning: `script_bindings` (lib) generated 4 warnings (run `cargo fix --lib -p script_bindings` to apply 3 suggestions)
error[E0412]: cannot find type `IdentityHub` in this scope
  --> components/script/dom/debuggerglobalscope.rs:76:63
   |
76 |         #[cfg(feature = "webgpu")] gpu_id_hub: std::sync::Arc<IdentityHub>,
   |                                                               ^^^^^^^^^^^ not found in this scope
   |
help: consider importing this struct
   |
 5 + use crate::dom::identityhub::IdentityHub;
   |

warning: method `worklet_id` is never used
   --> components/script/dom/worklet.rs:121:19
    |
 95 | impl Worklet {
    | ------------ method in this implementation
...
121 |     pub(crate) fn worklet_id(&self) -> WorkletId {
    |                   ^^^^^^^^^^
    |
    = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default
```

Testing: No test needed, this won't change the default builds.

Signed-off-by: webbeef <me@webbeef.org>
2026-01-19 07:44:15 +00:00
Sam
89cfe15b3e script: use &mut CurrentRealm in cross_origin_has_own (#41955)
Testing: Just refactor, but covered by existing WPT tests.
Part of #40600

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-16 11:05:26 +00:00
Sam
2def1fbdce script: &mut JSContext in cross_origin_own_property_keys and append_cross_origin_allowlisted_prop_keys (#41879)
Testing: Just refactor, but should be covered by WPT tests.
Fixes: #40600

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-13 15:49:09 +00:00
Sam
5b11f7619e script: add &mut JSContext to codegened toJSON (#41878)
Testing: Just refactor, but should be covered by WPT tests
Part of #40600

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-13 14:48:57 +00:00
Sam
5173ea12cd script: More &mut JSContext in codegen (#41808)
Reviewable per commit. First commit adds `&mut JSContext` to
`call_default_constructor`. Second commit adds `&mut JSContext` to
Interface, which in unfortunately required many changes (a lot of
passing `&mut JSContext` down).

Testing: Just a refactor, but should be covered by WPT
Part of #40600

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-09 14:50:54 +00:00
Sam
b1245dce4c script: Use &mut JSContext in html_constructor (#41782)
Testing: Just refactor, but should be covered by WPT.
Part of #40600

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2026-01-08 21:09:48 +00:00
Wayne Van Son
5b9263690d Refactor allow to expect (#41586)
Replace `allow` with `expect` lints for `unused`, `unsafe_code`,
`dead_code`, and `non_upper_case_globals`.

Testing: So far just check it compiled on `x86_64-linux` on NixOS. Need
to use the module `system.fontconfig.enable = true;` I think in my NixOS
config.
Part of: #40383 

Searching `allow\(.*\)` for `.rs` files shows the following. for
(total_results:total_files) went from `707:386` to `675:368`, a
reduction of `32:18`.

How many files is too many files per PR? I feel like the 20-30 I have is
too big.

---------

Signed-off-by: Wayne Van Son <waynevanson@gmail.com>
2026-01-01 13:40:20 +00:00
Sam
6e0e50505e script: Use more new JSContext/CurrentRealm in crossorigin codegen (#41537)
reviewable per commit

Testing: Should be covered by WPT tests.
try run: https://github.com/sagudev/servo/actions/runs/20540365373
Part of #40600

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-12-27 15:59:32 +00:00
Martin Robinson
75c6dc845f stylo: Enable vendor-prefixed properties (#41350)
This changes updates to the latest version of stylo which includes
vendor-prefixed properties such as `-moz-transform` and
`-webkit-transform`. This allows more web content to render properly.

Note: Unitless perspective is not supported as part of this yet, but
will be once https://phabricator.services.mozilla.com/D274128 is part
of our downstream version of Stylo.

Testing: There are tests for this change.
Fixes: #17635.
Fixes: #14941.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-12-17 19:08:43 +00:00
Josh Matthews
d86d2a90aa script: Use rooted typed arrays in generated code and buffer source APIs (#41228)
There are two flavours of the mozjs `TypedArray<T>` wrapper for typed
array objects: one stores a `Box<Heap<*mut JSObject>>`, while the other
just has a bare `*mut JSObject`. The second one must only be stored
inside the `CustomAutoRoot` rooting values, but we were using it in many
other places like WebIDL dictionaries without rooting it safely. These
changes make our typed array APIs always use
`RootedTraceableBox<TypedArray<T>>` with the `Box<Heap<*mut JSObject>>`
flavour, which ensures that the JS object stored inside the typed array
wrapper is always visible to the SpiderMonkey GC.

Testing: Adds a new test that crashes without these changes.
Fixes: #41206

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
2025-12-12 14:33:45 +00:00
Josh Matthews
c2bfb1c6c5 script_bindings: Disallow manual Drop implementations for DOM interfaces. (#40919)
This implements the solution from #26488 that prevents implementing Drop
manually for DOM interfaces, which is very easy to write in a way that
can cause memory safety errors during JS runtime teardown. Since we have
a bunch of pre-existing implementations that we are working on removing,
this PR adds an opt-out mechanism that those interfaces use.

Testing: Compile-time code generation; not possible to test.
Fixes: part of #26488

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
2025-11-27 19:53:59 +00:00
Sam
c27f8c565a Use safe JSContext/AutoRealm/CurrentRealm in codegen (#40716)
We replace many places that use `SafeJSContext` with `JSContext` and I
also rewrote `is_platform_object_same_origin` to use new `JSContext`.
Unfortunately using wrappers2 in them causes crashes (in handle code),
so I reverted that part in last commit and will fix handles in mozjs
later.

Testing: Refactor, but it is covered by WPT tests
Part of #40600

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-11-19 05:55:55 +00:00
Sam
fa12f7a5e5 script: Add cx_no_gc/cx/realm codegen option and demostrate them (#40582)
Companion to https://github.com/servo/mozjs/pull/650

We added 3 new options to bindings.conf, each more powerful then the
previous one, so one should use the least powerful as possible to keep
things flexible:
1 `cx_no_gc` prepends argument `&JSContext`, which allows creating NoGC
tokens and using functions that do not trigger GC.
2. `cx` prepends argument `&mut JSContext`, which allows everything that
previous one allows, but it also allows calling GC triggering functions.
3. `realm` prepends argument `&mut CurrentRealm`, which can be deref_mut
to `&mut JSContext` (so it can do everything that previous can), but it
also ensures that there is current entered realm, which can be used for
creation of InRealm.

next steps: #40600 

reviewable per commit

Testing: It's just refactoring
try run: https://github.com/sagudev/servo/actions/runs/19287700927

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-11-16 04:53:54 +00:00
Narfinger
423800eec4 Script: Lazily transform the DOMString into Rust String instead of immediately. (#39509)
This implements LazyDOMString (from now on DOMString) as outlined in
https://github.com/servo/servo/issues/39479.
Constructing from a *mut JSString we keep the in a
RootedTraceableBox<Heap<*mut JSString>> and transform
the string into a rust string if necessary via the `make_rust_string`
method.
Methods used in script are implemented on this string. Currently we
transform the string at all times.
But in the future more efficient implementations are possible.

We implement the safety critical sections in a separate module
DOMStringInner which allows simple constructors, `make_rust_string` and
the `bytes` method.
This method returns the new type `EncodedBytes` which contains the
reference to the underlying string in either format.

Testing: WPT tests still seem to work, so this should test this
functionality.

---------

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
2025-10-09 18:18:03 +00:00
Tim van der Lippe
02aab33987 Add AbortSignal support for event listeners (#39406)
Also fixes several issues with code generation when a dom type is part
of a dictionary.

Part of #34866
Fixes #39398

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
2025-09-21 18:57:10 +00:00
Kingsley Yung
e3818a33c0 script_bindings(python): Handle WebIDL methods named with Rust keywords (#39343)
Some WebIDL methods, attributes and dictionary members are named with
Rust keywords such as `type`, `use` and `continue`. Using those
identifiers directly in the generated Rust code would cause compilation
errors.

The code generator already addresses this issue for attributes and
dictionary members by adding a `_` suffix to the conflicting
identifiers, but does not yet apply the same treatment to methods. This
patch extends the handling to methods as well.

Fixes: #39286

Signed-off-by: Kingsley Yung <kingsley@kkoyung.dev>
2025-09-17 04:09:02 +00:00
JasonHonKL
839878c743 script: Make root_from_handle_value a safe function by accepting SafeJSContext (#39193)
Change from unsafe pointer of root_from_handle_value  to SafeJSContext. 

#39131

---------

Signed-off-by: JasonHonKL <j2004nol@gmail.com>
Signed-off-by: Jason <jason@198-61-252-113-on-nets.com>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Co-authored-by: Jason <jason@198-61-252-113-on-nets.com>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
2025-09-09 19:26:02 +00:00
lumiscosity
9584b9e57d script: Add can_gc to WebIDL dictionary constructors (#39195)
More progress on can_gc!

Testing: Internal change only, shouldn't change behavior.
Fixes: #38708

---------

Signed-off-by: lumiscosity <averyrudelphe@gmail.com>
2025-09-08 09:21:42 +00:00
lumiscosity
088d16d634 script: Make set_dictionary_property a safe function (#39191)
Makes `set_dictionary_property` safe.

Testing: Changes are internal, this shouldn't affect behavior.
Fixes: #39128

Signed-off-by: lumiscosity <averyrudelphe@gmail.com>
2025-09-07 11:56:20 +00:00
Jerens Lensun
4de84b0ca5 script_bindings(python): Fix all the rest of type annotation in script_bindings codegen (#39070)
This will complete the type annotation on `codegen.py` so we can safely
remove the ignore from `pyproject` configurataion

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
2025-09-02 12:47:38 +00:00
Jerens Lensun
fc5691a293 script_bindings(python): Add type around js jit class in codegen.py (#38998)
Add type around js jit class in codegen.py

Needed this PR: #38845

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
2025-09-01 07:00:03 +00:00
Jerens Lensun
00c1f79a1d script_bindings(python): Add type around CG class in codegen.py (#38845)
Add type around CG class for code generatio Web IDL in codegen.py


Testing: *Describe how this pull request is tested or why it doesn't
require tests*
Fixes: *Link to an issue this pull requests fixes or remove this line if
there is no issue*

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
2025-08-29 04:32:14 +00:00
criskell
9082217272 Move unsafe to the wrap method for global objects (#38896)
The method now doesn't need unsafe in its signature because it no longer
accepts unsafe pointers as arguments. We move the unsafe marker to the
method itself.

Testing: I opened the browser and went to google.com; I ran some WPT
(IndexedDB) tests.
Fixes: #38361

---------

Signed-off-by: criskell <96352451+criskell@users.noreply.github.com>
2025-08-25 22:02:55 +00:00
Jerens Lensun
48c2152f61 script_bindings(python): Add type to a function for codegen.py (#38771)
This PR focuses on adding type annotations to utility functions,
PropertyDefiner, and its child classes.

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
2025-08-21 14:45:45 +00:00
Josh Matthews
636e7211e0 script: Measure heap usage of various ignored fields (#38791)
These changes allow using MallocSizeOf/`#[conditional_malloc_size_of]`
on WebIDL callback values, and then fix a grab bag of places in the
script crate that previously ignored those values. There are also some
commits removing ignored fields that involved Arc/Rc that are not WebIDL
callbacks, since they are now easier to support with the
`#[conditional_malloc_size_of]` attribute.

Testing: Manual testing on about:memory for servo.org.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
2025-08-20 08:43:58 +00:00
webbeef
3225d19907 cargo: Bump rustc to 1.89 (#36818)
Update Rustc to 1.89.

Reviewable by commit.

Leftover work:
- #37330 
- #38777

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
Co-authored-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-08-19 11:07:53 +00:00
Jerens Lensun
788d6db94d script_bindings(python): Add ruff rule for strict typing in function (#38694)
This is part of introducing the Ruff rule [flake8-annotations
(ANN)](https://docs.astral.sh/ruff/rules/#flake8-annotations-ann) into
the script_bindings folder.

Since `codegen.py` has 10k lines of code, the strategy is to introduce
the rule first while ignoring the `ANN` Ruff rule for `codegen.py`. We
will then gradually add type annotations slice by slice to ensure no new
errors occur outside of `ANN`.

Testing: `./mach test-wpt webidl`

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
2025-08-18 07:26:30 +00:00
Josh Matthews
bd9bb77295 script_bindings: Assert that serializable/transferable types have accurate WebIDL annotations (#38615)
These changes add compile-time assertions that:
* any type that implements the Serializable/Transferable trait has a
`[Serializable]` or `[Transferable]` annotation in the interface WebIDL
* any WebIDL interface with the `[Serializable]` or `[Transferable]`
annotation implements the corresponding trait

This is useful because it means that WebIDL definitions will be less
confusing if you're trying to figure out whether Servo supports
serializing/transferring a particular interface type. It also makes
fixing #21715 in the future a little bit easier, because the annotations
will remain up to date.

Testing: compile-time only; no point in writing tests for this since it
involves webidl codegen.

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
2025-08-13 08:36:04 +00:00
Martin Robinson
5c307a38df script_bindings Start wrapping unsafe code in unsafe {} (#38545)
This is useful to better isolate `unsafe` code. Once all unsafe calls
are wrapped we can enable the Rust warning.  This also explicitly
disables the warning for generated code, which is a much more difficult
task. After this change there are 211 warnings left in
`script_bindings`.

Testing: This should not change behavior and is thus covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-08-08 12:21:31 +00:00
Jerens Lensun
5b148cf5de script_bindings: Add type definitions for configuration.py (#38450)
*Describe the changes that this pull request makes here. This will be
the commit message.*

Testing: *Describe how this pull request is tested or why it doesn't
require tests*
Fixes: *Link to an issue this pull requests fixes or remove this line if
there is no issue*

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
2025-08-06 05:44:05 +00:00
Taym Haddadi
04ec710e60 Add AsHandleValue trait to Heap<Value> and make Heap values rooted (#38024)
Encapsulates the unsafe conversion from Heap<Value> to HandleValue<'a>,
and reducing repetitive unsafe code at call.

fix #37258
2025-08-04 16:42:53 +00:00
Jerens Lensun
b05d265de5 script_binding: Add type check on servo script bindings (#38161)
Introduce type checking with Pyrefly in `components/script_bindings`

This commit adds Pyrefly-based type checking to the
`components/script_bindings` directory. The primary goal is to catch
type inconsistencies early and reduce the likelihood of unexpected
runtime errors.

This change affects the `webidl` component, as these script bindings are
responsible for connecting WebIDL specifications to the Rust codebase.

Testing: `./mach test-wpt webidl`
Fixes: *Link to an issue this pull requests fixes or remove this line if
there is no issue*

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
2025-08-01 04:34:24 +00:00
shuppy
52f53f61e1 script: Add support for creating globals in isolated compartments (#38236)
to use the [SpiderMonkey Debugger
API](https://firefox-source-docs.mozilla.org/js/Debugger/), we need to
call it from an internal debugger script that we will supply. this
script must run in the same runtime as the debuggee(s), but in a
separate
[compartment](https://udn.realityripple.com/docs/Mozilla/Projects/SpiderMonkey/Compartments)
([more
details](https://hacks.mozilla.org/2020/03/future-proofing-firefoxs-javascript-debugger-implementation/)).

when creating two globals in the same runtime, Servo will generally try
to reuse an existing compartment for the second global, and there are
other places in script where we rely on this reuse. but for the debugger
script, we can’t do this.

this patch adds a `use_system_compartment` flag to global object
descriptors, which isolates the global in its own compartment in both
possible directions:
- it forces the global to create a new compartment, preventing the reuse
of any debuggee’s compartment
- it fails the check in select_compartment(), preventing future
debuggees from reusing the global’s compartment

Testing: manually tested working in devtools debugger patch (#37667),
where it will undergo automated tests

---------

Signed-off-by: Delan Azabani <dazabani@igalia.com>
Co-authored-by: atbrakhi <atbrakhi@igalia.com>
2025-07-23 11:34:37 +00:00
Ashwin Naren
ac2cfdb801 script: Use Pythonic file names for code generation and use functools.cache (#38047)
Renames the files to pythonic file names. Also replaces the bespoke
caching function with the functools caching function which is
functionally equivalent.

---------

Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
2025-07-14 19:38:55 +00:00