Commit Graph

25 Commits

Author SHA1 Message Date
Tim van der Lippe
8e0c2d5750 Implement WindowOrWorkerGlobalScope::reportError (#40654)
This web API is alternative API to `throw e`, which is why we can reuse
a lot of the existing machinery.

The one testcase that isn't passing yet is because it reports an empty
`TypeError`. The current logic in `ErrorInfo` only retrieves the message
data, but doesn't include the type of the exception. For that, we need
to use `(*report)._base.errorNumber` and map that back to the original
type codes. However, deferring that to a follow-up as that requires some
more work in mozjs.

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
2025-11-16 09:30:16 +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
Martin Robinson
0a1061d8d7 script: Use ScriptEventLoopSender for Runtime event loop callbacks (#40590)
Instead of using a task source for SpiderMonkey runtime callbacks, use a
`ScriptEventLoopSender`. Task sources are associated with a particular
Pipeline, but the runtime callback is run indepenently of any particular
Pipeline and could theoretically happen when no Pipeline exists at all.
This reduces the dependency of the `ScriptThread` on the existence of
the first Pipeline.

Testing: This should not change observable behavior, so is covered by
existing tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-11-12 18:16:59 +00:00
Martin Robinson
bfde51c0db script: Have FetchResponseListener::process_response_eof consume the listener (#40556)
The goal of this change is to prevent having to copy so much data out of
listeners when a fetch completes, which will be particularly important
for off-the-main thread parsing of CSS (see #22478). This change has
pros and cons:

Pros:
- This makes the design of the `FetchResponseListener` a great deal
simpler.
They no longer individually store a dummy `ResourceFetchTiming` that is
   only replaced right before `process_response_eof`.
 - The creation of the `Arc<Mutex<FetchResponseListener>>` in the
   `NetworkListener` is abstracted away from clients and now they just
   pass the `FetchResponseListener` to the fetch methods in the global.

Cons:
 - Now each `FetchResponseListener` must explicitly call `submit_timing`
   instead of having the `NetworkListener` do it. This is arguably a bit
   easier to follow in the code.
 - Since the internal data of the `NetworkListener` is now an
   `Arc<Mutex<Option<FetchResponseListener>>>`, when the fetching code
   needs to share state with the `NetworkListener` it either needs to
   share an `Option` or some sort of internal state. In one case I've
   stored the `Option` and in another case, I've stored a new inner
   shared value.

Testing: This should not change observable behavior and is thus covered
by existing tests.
Fixes: #22550

---------

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-11-11 22:30:40 +00:00
Sam
b73b908dbd script: Start preparation to use safer JSContext (#40465)
This PR is companion to https://github.com/servo/mozjs/pull/638,
currently it only make stuff compile. In follwups we will start
passing/using safer JSContext down everywhere.

Testing: Not needed as it's just refactorings.
try run:
https://github.com/sagudev/servo/actions/runs/19196312972/job/54879090390

---------

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-11-09 05:17:26 +00:00
Martin Robinson
99daf8c644 script: Wrap remaining unsafe code and enable unsafe_op_in_unsafe_fn (#40499)
This is last step toward enabling the default rustc
`unsafe_op_in_unsafe_fn` warning for the script crate. It wraps the
remaining unsafe code in `unsafe {}` and removes the line disabling this
warning from `script`'s `Cargo.toml`. In addition, two variables are
renamed from `v` to something slightly more descriptive.

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

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-11-08 14:27:35 +00:00
Martin Robinson
8f6edca4dd net: Simplify FetchResponseListener and move it to script (#40461)
`FetchReponseListener` has traditionally lived in `net` even though it
is only used in `script` currently. Because of the two way dependency,
it has also use a lot of templating to implement something pretty basic
(call methods on a trait object).

This change moves the trait to `script` and removes several levels of
templating, making the code quite a bit shorter and easier to
understand.

This change is preparation for fixing #22550 and implementing
off-the-main-thread CSS parsing.

Testing: This should not change any behavior so is covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-11-06 14:44:01 +00:00
WaterWhisperer
158bf97e30 script/dom/: Change some #[allow]s to #[expect]s (#40454)
Removes some unneeded lints, especially `#[allow(unsafe_code)]`.

Testing: Refactor
Part of: #40383

Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com>
2025-11-06 11:06:03 +00:00
Gae24
44a0570ae5 script: remove microtask queue from globalscope (#40371)
Remove the microtask queue from `GlobalScope`. The queue is moved inside
worker global scopes, while for window globals the script thread's queue
is accessed.

Testing: Covered by existing tests
Fixes: #20908

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
2025-11-03 12:51:35 +00:00
Simon Wülker
c4ae451612 Turn all commented links before functions into doccomments (#40331)
Servo has a lot of comments like this:
```rust
// https://example-spec.com/#do-the-thing
fn do_the_thing() {}
```
and I keep turning these into doc comments whenever I'm working close to
one of them. Doing so allows me to hover over a function call in an IDE
and open its specification without having to jump to the function
definition first. This change fixes all of these comments at once.

This was done using `find components -name '*.rs' -exec perl -i -0777
-pe 's|^([ \t]*)// (https?://.*)\n\1(fn )|\1/// <$2>\n\1$3|mg' {} +`.

Note that these comments should be doc comments even within trait `impl`
blocks, because rustdoc will use them as fallback documentation when the
method definition on the trait does not have documentation.

Testing: Comments only, no testing required
Preparation for https://github.com/servo/servo/pull/39552

---------

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
2025-11-01 05:32:45 +00:00
WaterWhisperer
32c0c41d11 script: Move performance DOM interfaces to script/dom/performance/ (#40262)
Moves interfaces defined by the performance spec to the
`script/dom/performance/` module from `script/dom/`.

Testing: Just a refactor shouldn't need any testing
Fixes: Partially #38901

Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com>
2025-10-29 11:41:52 +00:00
Yerkebulan Tulibergenov
e0cf1a5589 rename WorkerGlobalScope.get_worker_id to WorkerGlobalScope.worker_id (#40195)
rename `WorkerGlobalScope.get_worker_id` to
`WorkerGlobalScope.worker_id`

Testing: This is a simple rename. No testing is necessary, since
compiler will complain if something is wrong.

Context: In
https://github.com/servo/servo/pull/40156#discussion_r2462627713 I
received feedback to use Rust getter name
[convention](https://rust-lang.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter)
and avoid adding `get_` methods. I initially chose `get_worker_name()`
to match existing `get_worker_id()` below it. Overall I think it does
make sense to rename `get_worker_id()` to `worker_id()`.

`WorkerGlobalScope` has 2 more methods that start with `get_`:
`get_cx(&self) -> JSContext` and `get_url`. `get_url` has a
corresponding `set_url` as well in the same file. I am not sure if folks
prefer to have those renamed as well or not, so I left those out of this
PR, and instead started a discussion in a separate
[issue](https://github.com/servo/servo/issues/40192) to figure out a
consistent policy on Rust getter name convention in Servo codebase.

Signed-off-by: Yerkebulan Tulibergenov <yerkebulan@gmail.com>
2025-10-27 01:03:13 +00:00
Gae24
af6b62d960 script: cleanup memory reporting code inside dedicatedworkerglobalscope (#40170)
- Remove a couple of ignore_malloc_size_of since are not required
anymore
- Avoid calling random and use the worker_id
- Pass DedicatedWorker event_loop_sender to run_with_memory_profiling

Testing: Covered by tests, only code related to memory reporting was
changed.
Fixes: #11855

---------

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
2025-10-26 14:02:39 +00:00
Yerkebulan Tulibergenov
daf4dd5d99 script: Add an implementation of DedicatedWorkerGlobalScope.name and DedicatedWorkerGlobalScope.onmessageerror (#40156)
add `DedicatedWorkerGlobalScope.Name` and
`DedicatedWorkerGlobalScope.onmessageerror`

Closes https://github.com/servo/servo/issues/40114

---------

Signed-off-by: Yerkebulan Tulibergenov <yerkebulan@gmail.com>
2025-10-26 08:27:09 +00:00
Yerkebulan Tulibergenov
aced94224e add CanGc as argument to structuredclone::read (#40157)
add CanGc as argument to structuredclone::read

Testing: These changes do not require tests because they are a refactor.
Closes https://github.com/servo/servo/issues/40154

Signed-off-by: Yerkebulan Tulibergenov <yerkebulan@gmail.com>
2025-10-25 08:53:05 +00:00
Gae24
8557bf6fb3 script: implement remaining WorkerGlobalScope's event handlers (#40102)
Clean up WorkerGlobalScope webidl interface and implement all event
handlers that WorkerGlobalScope and subclasses should support.

Testing: Covered by existing tests, needs to update test expectations.

---------

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
2025-10-23 17:24:25 +00:00
WaterWhisperer
dd1fc07306 script: Move IndexedDB DOM interfaces to script/dom/indexeddb/ (#40091)
Moves interfaces defined by the indexeddb spec to the
`script/dom/indexeddb/` module from `script/dom/`.

Testing: Just a refactor shouldn't need any testing
Fixes: Partially #38901

Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com>
2025-10-23 01:50:08 +00:00
WaterWhisperer
cdff8a4c6d script: Validate worker_type in WorkerGlobalScope::ImportScripts (#40078)
Implement the first step of the "import scripts into worker global
scope": throw a `TypeError` when `importScripts()` is called in a module
worker.

According to the
[spec](https://html.spec.whatwg.org/multipage/workers.html#import-scripts-into-worker-global-scope):
> If worker global scope's type is "module", throw a TypeError
exception.

This check is performed at the beginning of `ImportScripts()`, before
any URL parsing or network operations.

Fixes: #40035

[Try
build](https://github.com/WaterWhisperer/servo/actions/runs/18712341350)

Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com>
2025-10-22 13:55:27 +00:00
Gae24
8e689034a2 script: use run_a_classic_script algorithm for worker scripts (#39905)
Workers now call `run_a_classic_worker_script`, also scripts gets
unminified.

Testing: no new tests passes are expected

---------

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
2025-10-21 07:52:12 +00:00
WaterWhisperer
51b0ecc7c4 Validate response status and MIME type in WorkerGlobalScope::ImportScripts (#40023)
## Changes
- Add response status validation (must be an ok status)
- Add MIME type validation (must be a JavaScript MIME type)
- Both checks return `NetworkError` on failure, as required by the
[spec](https://html.spec.whatwg.org/multipage/webappapis.html#fetch-a-classic-worker-imported-script)

Fixes: #39993

---------

Signed-off-by: WaterWhisperer <waterwhisperer24@qq.com>
2025-10-21 05:46:25 +00:00
Martin Robinson
c776475b3b Remove Servo's wrapper around rand (#39979)
This wrapper was added in order to eliminate the number of file
descriptors used accessing `/dev/urandom`, but these days `osrandom` and
by proxy `rand` will try to use `getrandom` on Linux and similar system
APIs on other platforms [^1].

This is a trial balloon for removing the wrapper, since almost all
modern Linux systems have `getrandom`  (available since Linux
3.17).

[^1]: https://docs.rs/getrandom/0.3.4/getrandom/#supported-targets

Testing: Should not change observable behavior (only in random ways), so
should
be covered by WPT tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2025-10-18 13:54:20 +00:00
Gae24
6353999184 script: replace load_whole_resource with async fetch when constructing a worker (#39671)
Testing: no behaviour changes in tests result should happen
Fixes: #39621

---------

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
2025-10-13 21:11:45 +00:00
Tim van der Lippe
9042b68d70 Add policy container to all requests (#39775)
In preparation of adding a client to a request, we first need to add a
policy container to all requests. Some where missing and relying on
fallbacks (that need to be removed).

For worklets this adds the appropriate policy container from the
containing global, but since that's still behind a pref, no tests are
affected.

For font contexts I tried to pass through the relevant information, but
it is a tad too much. Once fontcontext knows more information about its
containing global (as the TODO also points to referrer), we should clean
this up.

Part of #35035

Signed-off-by: Tim van der Lippe <tvanderlippe@gmail.com>
2025-10-12 12:13:19 +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
Gae24
b270387541 script: Move worker DOM interfaces to script/dom/workers/ (#39718)
Moves code related to the Web Worker spec to a new mod. 

Testing: A successful build is enough
Part of #38901

Signed-off-by: Gae24 <96017547+Gae24@users.noreply.github.com>
2025-10-08 17:56:05 +00:00