script: Tell SpiderMonkey whether scripts are inline

Co-authored-by: atbrakhi <atbrakhi@igalia.com>
Signed-off-by: Delan Azabani <dazabani@igalia.com>
This commit is contained in:
Delan Azabani 2025-07-30 20:58:12 +08:00
parent 048d0b0538
commit 615c9ec013
8 changed files with 41 additions and 8 deletions

4
Cargo.lock generated
View file

@ -5240,7 +5240,7 @@ dependencies = [
[[package]] [[package]]
name = "mozjs" name = "mozjs"
version = "0.14.1" version = "0.14.1"
source = "git+https://github.com/servo/mozjs#4035b0c4e9e2df5cacc68c4b71e7375a48605902" source = "git+https://github.com/servo/mozjs#913c2012ad14bd4e136bcf0b786320d4943f2a04"
dependencies = [ dependencies = [
"bindgen 0.71.1", "bindgen 0.71.1",
"cc", "cc",
@ -5252,7 +5252,7 @@ dependencies = [
[[package]] [[package]]
name = "mozjs_sys" name = "mozjs_sys"
version = "0.128.13-3" version = "0.128.13-3"
source = "git+https://github.com/servo/mozjs#4035b0c4e9e2df5cacc68c4b71e7375a48605902" source = "git+https://github.com/servo/mozjs#913c2012ad14bd4e136bcf0b786320d4943f2a04"
dependencies = [ dependencies = [
"bindgen 0.71.1", "bindgen 0.71.1",
"cc", "cc",

View file

@ -12,6 +12,7 @@ use devtools_traits::{
NodeInfo, NodeStyle, RuleModification, TimelineMarker, TimelineMarkerType, NodeInfo, NodeStyle, RuleModification, TimelineMarker, TimelineMarkerType,
}; };
use ipc_channel::ipc::IpcSender; use ipc_channel::ipc::IpcSender;
use js::glue::IntroductionType;
use js::jsval::UndefinedValue; use js::jsval::UndefinedValue;
use js::rust::ToString; use js::rust::ToString;
use servo_config::pref; use servo_config::pref;
@ -65,6 +66,7 @@ pub(crate) fn handle_evaluate_js(
ScriptFetchOptions::default_classic_script(global), ScriptFetchOptions::default_classic_script(global),
global.api_base_url(), global.api_base_url(),
can_gc, can_gc,
IntroductionType::Undefined,
); );
if rval.is_undefined() { if rval.is_undefined() {

View file

@ -14,6 +14,7 @@ use std::rc::Rc;
use deny_public_fields::DenyPublicFields; use deny_public_fields::DenyPublicFields;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use fnv::FnvHasher; use fnv::FnvHasher;
use js::glue::IntroductionType;
use js::jsapi::JS_GetFunctionObject; use js::jsapi::JS_GetFunctionObject;
use js::jsval::JSVal; use js::jsval::JSVal;
use js::rust::wrappers::CompileFunction; use js::rust::wrappers::CompileFunction;
@ -642,7 +643,12 @@ impl EventTarget {
let cx = GlobalScope::get_cx(); let cx = GlobalScope::get_cx();
let options = unsafe { let options = unsafe {
CompileOptionsWrapper::new(*cx, &handler.url.to_string(), handler.line as u32) CompileOptionsWrapper::new(
*cx,
&handler.url.to_string(),
handler.line as u32,
IntroductionType::Undefined,
)
}; };
// Step 3.9, subsection Scope steps 1-6 // Step 3.9, subsection Scope steps 1-6

View file

@ -28,7 +28,7 @@ use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg; use embedder_traits::EmbedderMsg;
use ipc_channel::ipc::{self, IpcSender}; use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER; use ipc_channel::router::ROUTER;
use js::glue::{IsWrapper, UnwrapObjectDynamic}; use js::glue::{IntroductionType, IsWrapper, UnwrapObjectDynamic};
use js::jsapi::{ use js::jsapi::{
Compile1, CurrentGlobalOrNull, GetNonCCWObjectGlobal, HandleObject, Heap, Compile1, CurrentGlobalOrNull, GetNonCCWObjectGlobal, HandleObject, Heap,
InstantiateGlobalStencil, InstantiateOptions, JSContext, JSObject, JSScript, SetScriptPrivate, InstantiateGlobalStencil, InstantiateOptions, JSContext, JSObject, JSScript, SetScriptPrivate,
@ -2768,6 +2768,7 @@ impl GlobalScope {
fetch_options, fetch_options,
script_base_url, script_base_url,
can_gc, can_gc,
IntroductionType::Undefined,
) )
} }
@ -2783,6 +2784,7 @@ impl GlobalScope {
fetch_options: ScriptFetchOptions, fetch_options: ScriptFetchOptions,
script_base_url: ServoUrl, script_base_url: ServoUrl,
can_gc: CanGc, can_gc: CanGc,
introduction_type: IntroductionType,
) -> bool { ) -> bool {
let cx = GlobalScope::get_cx(); let cx = GlobalScope::get_cx();
@ -2794,7 +2796,8 @@ impl GlobalScope {
rooted!(in(*cx) let mut compiled_script = std::ptr::null_mut::<JSScript>()); rooted!(in(*cx) let mut compiled_script = std::ptr::null_mut::<JSScript>());
match code { match code {
SourceCode::Text(text_code) => { SourceCode::Text(text_code) => {
let options = CompileOptionsWrapper::new(*cx, filename, line_number); let options =
CompileOptionsWrapper::new(*cx, filename, line_number, introduction_type);
debug!("compiling dom string"); debug!("compiling dom string");
compiled_script.set(Compile1( compiled_script.set(Compile1(

View file

@ -18,6 +18,7 @@ use encoding_rs::Encoding;
use html5ever::serialize::TraversalScope; use html5ever::serialize::TraversalScope;
use html5ever::{LocalName, Prefix, local_name, namespace_url, ns}; use html5ever::{LocalName, Prefix, local_name, namespace_url, ns};
use ipc_channel::ipc; use ipc_channel::ipc;
use js::glue::IntroductionType;
use js::jsval::UndefinedValue; use js::jsval::UndefinedValue;
use js::rust::{CompileOptionsWrapper, HandleObject, Stencil, transform_str_to_source_text}; use js::rust::{CompileOptionsWrapper, HandleObject, Stencil, transform_str_to_source_text};
use net_traits::http_status::HttpStatus; use net_traits::http_status::HttpStatus;
@ -1111,6 +1112,11 @@ impl HTMLScriptElement {
// Step 6. // Step 6.
let document = self.owner_document(); let document = self.owner_document();
let old_script = document.GetCurrentScript(); let old_script = document.GetCurrentScript();
let introduction_type = if script.external {
IntroductionType::Undefined
} else {
IntroductionType::InlineScript
};
match script.type_ { match script.type_ {
ScriptType::Classic => { ScriptType::Classic => {
@ -1119,7 +1125,7 @@ impl HTMLScriptElement {
} else { } else {
document.set_current_script(Some(self)) 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()); document.set_current_script(old_script.as_deref());
}, },
ScriptType::Module => { ScriptType::Module => {
@ -1145,7 +1151,12 @@ impl HTMLScriptElement {
} }
// https://html.spec.whatwg.org/multipage/#run-a-classic-script // 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: IntroductionType,
) {
// TODO use a settings object rather than this element's document/window // TODO use a settings object rather than this element's document/window
// Step 2 // Step 2
let document = self.owner_document(); let document = self.owner_document();
@ -1171,6 +1182,7 @@ impl HTMLScriptElement {
script.fetch_options.clone(), script.fetch_options.clone(),
script.url.clone(), script.url.clone(),
can_gc, can_gc,
introduction_type,
); );
} }

View file

@ -4,6 +4,7 @@
use std::rc::Rc; use std::rc::Rc;
use js::glue::IntroductionType;
use js::jsval::UndefinedValue; use js::jsval::UndefinedValue;
use script_bindings::root::DomRoot; use script_bindings::root::DomRoot;
@ -39,6 +40,7 @@ pub(crate) fn load_script(head: &HTMLHeadElement) {
ScriptFetchOptions::default_classic_script(global_scope), ScriptFetchOptions::default_classic_script(global_scope),
global_scope.api_base_url(), global_scope.api_base_url(),
CanGc::note(), CanGc::note(),
IntroductionType::Undefined,
); );
} }
})); }));

View file

@ -17,6 +17,7 @@ use crossbeam_channel::Receiver;
use devtools_traits::{DevtoolScriptControlMsg, WorkerId}; use devtools_traits::{DevtoolScriptControlMsg, WorkerId};
use dom_struct::dom_struct; use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSender; use ipc_channel::ipc::IpcSender;
use js::glue::IntroductionType;
use js::jsval::UndefinedValue; use js::jsval::UndefinedValue;
use js::panic::maybe_resume_unwind; use js::panic::maybe_resume_unwind;
use js::rust::{HandleValue, MutableHandleValue, ParentRuntime}; use js::rust::{HandleValue, MutableHandleValue, ParentRuntime};
@ -433,6 +434,7 @@ impl WorkerGlobalScopeMethods<crate::DomTypeHolder> for WorkerGlobalScope {
url.as_str(), url.as_str(),
1, 1,
rval.handle_mut(), rval.handle_mut(),
IntroductionType::Undefined,
); );
maybe_resume_unwind(); maybe_resume_unwind();
@ -645,6 +647,7 @@ impl WorkerGlobalScope {
self.worker_url.borrow().as_str(), self.worker_url.borrow().as_str(),
1, 1,
rval.handle_mut(), rval.handle_mut(),
IntroductionType::Undefined,
) { ) {
Ok(_) => (), Ok(_) => (),
Err(_) => { Err(_) => {

View file

@ -16,6 +16,7 @@ use headers::{HeaderMapExt, ReferrerPolicy as ReferrerPolicyHeader};
use html5ever::local_name; use html5ever::local_name;
use hyper_serde::Serde; use hyper_serde::Serde;
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
use js::glue::IntroductionType;
use js::jsapi::{ use js::jsapi::{
CompileModule1, ExceptionStackBehavior, FinishDynamicModuleImport, GetModuleRequestSpecifier, CompileModule1, ExceptionStackBehavior, FinishDynamicModuleImport, GetModuleRequestSpecifier,
GetModuleResolveHook, GetRequestedModuleSpecifier, GetRequestedModulesCount, GetModuleResolveHook, GetRequestedModuleSpecifier, GetRequestedModulesCount,
@ -468,11 +469,13 @@ impl ModuleTree {
mut module_script: RustMutableHandleObject, mut module_script: RustMutableHandleObject,
inline: bool, inline: bool,
can_gc: CanGc, can_gc: CanGc,
introduction_type: IntroductionType,
) -> Result<(), RethrowError> { ) -> Result<(), RethrowError> {
let cx = GlobalScope::get_cx(); let cx = GlobalScope::get_cx();
let _ac = JSAutoRealm::new(*cx, *global.reflector().get_jsobject()); let _ac = JSAutoRealm::new(*cx, *global.reflector().get_jsobject());
let compile_options = unsafe { CompileOptionsWrapper::new(*cx, url.as_str(), 1) }; let compile_options =
unsafe { CompileOptionsWrapper::new(*cx, url.as_str(), 1, introduction_type) };
let mut module_source = ModuleSource { let mut module_source = ModuleSource {
source: module_script_text, source: module_script_text,
unminified_dir: global.unminified_js_dir(), unminified_dir: global.unminified_js_dir(),
@ -1332,6 +1335,7 @@ impl FetchResponseListener for ModuleContext {
compiled_module.handle_mut(), compiled_module.handle_mut(),
false, false,
CanGc::note(), CanGc::note(),
IntroductionType::Undefined,
); );
match compiled_module_result { match compiled_module_result {
@ -1896,6 +1900,7 @@ pub(crate) fn fetch_inline_module_script(
compiled_module.handle_mut(), compiled_module.handle_mut(),
true, true,
can_gc, can_gc,
IntroductionType::InlineScript,
); );
match compiled_module_result { match compiled_module_result {