when generating rust bindings for webidl typedef, the `<D>` generic Type
would not be added for record<Key, DomInterface> because
`containsDomInterface` does not take record into account. This change
add the is_Record check in `containsDomInterface`, so `typedef
record<Key, DomInterface>` can be successfully build.
Testing: Manually tested with
https://github.com/servo/servo/issues/42362, and building from scratch
with `./mach build` is successful.
Fixes: https://github.com/servo/servo/issues/42362
---------
Signed-off-by: rayguo17 <rayguo17@gmail.com>
Reviewable per commits:
As noted in
https://github.com/servo/servo/pull/42180#discussion_r2749861902
transmuting `&Reflector<AssociatedMemory>` to `&Reflector<()>` will
ignore AssociatedMemory information and thus it will not remove extra
associated memory. By returning `&Reflector<Self::ReflectorType>` from
`DomObject::reflector()` we will preserve this information and thus
correctly handle cases with associated memory. We also do not need
`overrideMemoryUsage` in bindings.conf anymore. 🎉
Instead of removing associated memory in drop code we should do it as
part of finalizers, otherwise we have problems in nested (inherited)
structs, where drop is run for both parent and child, but we only added
associated memory for child (on init_reflector) which already included
size of parent. The only exception here is promise, because it is RCed
and not finalized.
Testing: Tested locally that it fixes speedometer.
Fixes#42269
---------
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
By reporting memory usage of rust objects back to SM it can trigger GC
sooner and release more memory (I hope that we could use this to avoid
OOB in webgpu in the future).
Currently we use simple `size_of::<DomStruct>() +
size_of::<Box<DomStruct>>()` but it's possible to override reported
memory with `update_memory_size(self, nbytes);` on reflector. This is
mostly useful for canvases and buffers which usually have large
associated data.
Testing: None, but I am wondering if we can connect this to
`about:memory`.
Fixes: #42168
---------
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
Also fixes several issues with code generation when a dom type is part
of a dictionary.
Part of #34866Fixes#39398
Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
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>
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>
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>
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>
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>
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>
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>
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>
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>
*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>
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>
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>
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>