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

@ -6,6 +6,7 @@
//! related to `type=module` for script thread or worker threads.
use std::collections::{HashMap, HashSet};
use std::ffi::CStr;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
@ -77,7 +78,7 @@ use crate::dom::window::Window;
use crate::dom::worker::TrustedWorkerAddress;
use crate::network_listener::{self, NetworkListener, PreInvoke, ResourceTimingListener};
use crate::realms::{AlreadyInRealm, InRealm, enter_realm};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
use crate::script_runtime::{CanGc, IntroductionType, JSContext as SafeJSContext};
use crate::task::TaskBox;
fn gen_type_error(global: &GlobalScope, string: String, can_gc: CanGc) -> RethrowError {
@ -467,11 +468,15 @@ impl ModuleTree {
mut module_script: RustMutableHandleObject,
inline: bool,
can_gc: CanGc,
introduction_type: Option<&'static CStr>,
) -> Result<(), RethrowError> {
let cx = GlobalScope::get_cx();
let _ac = JSAutoRealm::new(*cx, *global.reflector().get_jsobject());
let compile_options = unsafe { CompileOptionsWrapper::new(*cx, url.as_str(), 1) };
let mut compile_options = unsafe { CompileOptionsWrapper::new(*cx, url.as_str(), 1) };
if let Some(introduction_type) = introduction_type {
compile_options.set_introduction_type(introduction_type);
}
let mut module_source = ModuleSource {
source: module_script_text,
unminified_dir: global.unminified_js_dir(),
@ -1331,6 +1336,7 @@ impl FetchResponseListener for ModuleContext {
compiled_module.handle_mut(),
false,
CanGc::note(),
None,
);
match compiled_module_result {
@ -1895,6 +1901,7 @@ pub(crate) fn fetch_inline_module_script(
compiled_module.handle_mut(),
true,
can_gc,
Some(IntroductionType::INLINE_SCRIPT),
);
match compiled_module_result {