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>
This commit is contained in:
shuppy 2025-08-05 20:41:14 +08:00 committed by GitHub
parent 0bf8676345
commit 3eddfeaee2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 52 additions and 13 deletions

View file

@ -4,6 +4,7 @@
#![allow(unused_imports)]
use core::ffi::c_void;
use std::cell::Cell;
use std::ffi::CStr;
use std::fs::read_to_string;
use std::path::PathBuf;
use std::process::Command;
@ -80,7 +81,7 @@ use crate::script_module::{
ImportMap, ModuleOwner, ScriptFetchOptions, fetch_external_module_script,
fetch_inline_module_script, parse_an_import_map_string, register_import_map,
};
use crate::script_runtime::CanGc;
use crate::script_runtime::{CanGc, IntroductionType};
use crate::task_source::{SendableTaskSource, TaskSourceName};
use crate::unminify::{ScriptSource, unminify_js};
@ -1145,6 +1146,7 @@ impl HTMLScriptElement {
// Step 6.
let document = self.owner_document();
let old_script = document.GetCurrentScript();
let introduction_type = (!script.external).then_some(IntroductionType::INLINE_SCRIPT);
match script.type_ {
ScriptType::Classic => {
@ -1153,7 +1155,7 @@ impl HTMLScriptElement {
} else {
document.set_current_script(Some(self))
}
self.run_a_classic_script(&script, can_gc);
self.run_a_classic_script(&script, can_gc, introduction_type);
document.set_current_script(old_script.as_deref());
},
ScriptType::Module => {
@ -1179,7 +1181,12 @@ impl HTMLScriptElement {
}
// https://html.spec.whatwg.org/multipage/#run-a-classic-script
pub(crate) fn run_a_classic_script(&self, script: &ScriptOrigin, can_gc: CanGc) {
pub(crate) fn run_a_classic_script(
&self,
script: &ScriptOrigin,
can_gc: CanGc,
introduction_type: Option<&'static CStr>,
) {
// TODO use a settings object rather than this element's document/window
// Step 2
let document = self.owner_document();
@ -1205,6 +1212,7 @@ impl HTMLScriptElement {
script.fetch_options.clone(),
script.url.clone(),
can_gc,
introduction_type,
);
}