Files
servo/components/script/dom/userscripts.rs
shuppy 3eddfeaee2 script: Tell SpiderMonkey whether scripts are inline (#38363)
to use the [SpiderMonkey Debugger
API](https://firefox-source-docs.mozilla.org/js/Debugger/) as the single
source of truth about scripts and their sources for devtools purposes
(servo/servo#38334), the debugger script needs to be able to distinguish
inline scripts from other scripts, because inline scripts are a special
case where the source contents need to come from the Servo parser.

the mechanism for this is
[Debugger.Script.prototype.**introductionType**](https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Source.html#introductiontype),
which is `inlineScript` for inline scripts or a variety of other values
for other kinds of scripts, but only the embedder can provide this
information.

this patch bumps mozjs to servo/mozjs#603, which expands on
CompileOptionsWrapper, making it a safe wrapper around CompileOptions.
to construct one from safe code, use Runtime::new_compile_options().
then you can call `set_introduction_type(&'static CStr)` on the new
instance. we also make Runtime::evaluate_script() take a
CompileOptionsWrapper from the caller, instead of constructing one
internally.

in this patch, we set the introductionType to `c"inlineScript"` when
calling run_a_classic_script() and compile_module_script() for inline
scripts, and leave it unset all other cases.

Testing: will undergo automated tests in #38334
Fixes: part of #36027, part of servo/servo#38378

---------

Signed-off-by: Delan Azabani <dazabani@igalia.com>
Co-authored-by: atbrakhi <atbrakhi@igalia.com>
2025-08-05 12:41:14 +00:00

47 lines
1.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::rc::Rc;
use js::jsval::UndefinedValue;
use script_bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::htmlheadelement::HTMLHeadElement;
use crate::dom::htmlscriptelement::SourceCode;
use crate::dom::node::NodeTraits;
use crate::dom::window::Window;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::CanGc;
pub(crate) fn load_script(head: &HTMLHeadElement) {
let doc = head.owner_document();
let userscripts = doc.window().userscripts().to_owned();
if userscripts.is_empty() {
return;
}
let win = DomRoot::from_ref(doc.window());
doc.add_delayed_task(task!(UserScriptExecute: |win: DomRoot<Window>| {
let cx = win.get_cx();
rooted!(in(*cx) let mut rval = UndefinedValue());
for user_script in userscripts {
let script_text = SourceCode::Text(
Rc::new(DOMString::from_string(user_script.script))
);
let global_scope = win.as_global_scope();
global_scope.evaluate_script_on_global_with_result(
&script_text,
&user_script.source_file.map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
rval.handle_mut(),
1,
ScriptFetchOptions::default_classic_script(global_scope),
global_scope.api_base_url(),
CanGc::note(),
None,
);
}
}));
}