mirror of
https://github.com/servo/servo.git
synced 2025-07-18 21:03:45 +01:00
script: Reduce usage of Trusted in Node::insert. (#37762)
These changes introduce a new kind of task that uses a variation of the `task!` syntax. Existing `task!` usages create task structs that have a `Send` bound, which requires the use of `Trusted<T>` to reference a DOM object T inside of the task closure. The new syntax replaces the `Send` bound with a `JSTraceable` bound, which requires explicit capture clauses with types. This looks like: ```rust task!(ScriptPrepare: {script: DomRoot<HTMLScriptElement>} |script| { script.prepare(CanGc::note()); }), ``` The capture clauses must list every value that will be referenced from the closure's environment—these values are moved into fields in the generated task structure, which allows them to be traced by the GC as part of a generated JSTraceable implementation. Since the closure itself is not a `move` closure, any attempts to reference values not explicitly captured will generate a borrow checker error since the closure requires the `'static` lifetime. Testing: Existing WPT tests exercise these code paths. I also attempted to write incorrect tasks that capture references or use values not explicitly captured, and the compiler correctly errors out. Fixes: part of #35517 Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
parent
f5b0165e54
commit
9e2ee0029a
5 changed files with 74 additions and 27 deletions
|
@ -1414,15 +1414,15 @@ impl VirtualMethods for HTMLScriptElement {
|
|||
}
|
||||
|
||||
if self.upcast::<Node>().is_connected() && !self.parser_inserted.get() {
|
||||
let script = Trusted::new(self);
|
||||
let script = DomRoot::from_ref(self);
|
||||
// This method can be invoked while there are script/layout blockers present
|
||||
// as DOM mutations have not yet settled. We use a delayed task to avoid
|
||||
// running any scripts until the DOM tree is safe for interactions.
|
||||
self.owner_document()
|
||||
.add_delayed_task(task!(ScriptPrepare: move || {
|
||||
let this = script.root();
|
||||
this.prepare(CanGc::note());
|
||||
}));
|
||||
self.owner_document().add_delayed_task(
|
||||
task!(ScriptPrepare: |script: DomRoot<HTMLScriptElement>| {
|
||||
script.prepare(CanGc::note());
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue