Set private reference for classic script

Web developers can use `Dynamic Import` in a classic script; thus, we
need to save the script's private reference so that we can reuse it when
we're going to fetch a dynamic import module for a classic script.

Besides, because it's possible to use different executing context for a
dynamic import module (like `dynamic-import/string-compilation-other-document.html` WPT test),
we can't initialize a module owner at the timing of `SetScriptPrivate`;
thus, if the private module script doesn't hold an owner, we'll use a
DynamicImport owner for it.
This commit is contained in:
CYBAI 2020-07-11 22:44:21 +09:00
parent 99e832a345
commit d1715918f0
10 changed files with 204 additions and 71 deletions

View file

@ -95,7 +95,7 @@ use msg::constellation_msg::{ServiceWorkerId, ServiceWorkerRegistrationId};
use net_traits::filemanager_thread::RelativePos;
use net_traits::image::base::{Image, ImageMetadata};
use net_traits::image_cache::{ImageCache, PendingImageId};
use net_traits::request::{Referrer, Request, RequestBuilder};
use net_traits::request::{CredentialsMode, ParserMetadata, Referrer, Request, RequestBuilder};
use net_traits::response::HttpsState;
use net_traits::response::{Response, ResponseBody};
use net_traits::storage_thread::StorageType;
@ -556,6 +556,8 @@ unsafe_no_jsmanaged_fields!(StyleSharedRwLock);
unsafe_no_jsmanaged_fields!(USVString);
unsafe_no_jsmanaged_fields!(Referrer);
unsafe_no_jsmanaged_fields!(ReferrerPolicy);
unsafe_no_jsmanaged_fields!(CredentialsMode);
unsafe_no_jsmanaged_fields!(ParserMetadata);
unsafe_no_jsmanaged_fields!(Response);
unsafe_no_jsmanaged_fields!(ResponseBody);
unsafe_no_jsmanaged_fields!(ResourceThreads);

View file

@ -52,6 +52,7 @@ use crate::dom::workletglobalscope::WorkletGlobalScope;
use crate::microtask::{Microtask, MicrotaskQueue, UserMicrotask};
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
use crate::script_module::{DynamicModuleList, ModuleTree};
use crate::script_module::{ModuleScript, ScriptFetchOptions};
use crate::script_runtime::{
CommonScriptMsg, ContextForRequestInterrupt, JSContext as SafeJSContext, ScriptChan, ScriptPort,
};
@ -79,8 +80,10 @@ use ipc_channel::router::ROUTER;
use js::glue::{IsWrapper, UnwrapObjectDynamic};
use js::jsapi::Compile1;
use js::jsapi::{CurrentGlobalOrNull, GetNonCCWObjectGlobal};
use js::jsapi::{GetScriptPrivate, SetScriptPrivate};
use js::jsapi::{HandleObject, Heap};
use js::jsapi::{JSContext, JSObject};
use js::jsval::PrivateValue;
use js::jsval::{JSVal, UndefinedValue};
use js::panic::maybe_resume_unwind;
use js::rust::transform_str_to_source_text;
@ -2533,8 +2536,21 @@ impl GlobalScope {
}
/// Evaluate JS code on this global scope.
pub fn evaluate_js_on_global_with_result(&self, code: &str, rval: MutableHandleValue) -> bool {
self.evaluate_script_on_global_with_result(code, "", rval, 1)
pub fn evaluate_js_on_global_with_result(
&self,
code: &str,
rval: MutableHandleValue,
fetch_options: ScriptFetchOptions,
script_base_url: ServoUrl,
) -> bool {
self.evaluate_script_on_global_with_result(
code,
"",
rval,
1,
fetch_options,
script_base_url,
)
}
/// Evaluate a JS script on this global scope.
@ -2545,6 +2561,8 @@ impl GlobalScope {
filename: &str,
rval: MutableHandleValue,
line_number: u32,
fetch_options: ScriptFetchOptions,
script_base_url: ServoUrl,
) -> bool {
let metadata = profile_time::TimerMetadata {
url: if filename.is_empty() {
@ -2566,33 +2584,56 @@ impl GlobalScope {
let ar = enter_realm(&*self);
let _aes = AutoEntryScript::new(self);
let options =
unsafe { CompileOptionsWrapper::new(*cx, filename.as_ptr(), line_number) };
rooted!(in(*cx) let compiled_script = unsafe {
Compile1(
*cx,
options.ptr,
&mut transform_str_to_source_text(code),
)
});
unsafe {
let options = CompileOptionsWrapper::new(*cx, filename.as_ptr(), line_number);
if compiled_script.is_null() {
debug!("error compiling Dom string");
report_pending_exception(*cx, true, InRealm::Entered(&ar));
return false;
debug!("compiling Dom string");
rooted!(in(*cx) let compiled_script =
Compile1(
*cx,
options.ptr,
&mut transform_str_to_source_text(code),
)
);
if compiled_script.is_null() {
debug!("error compiling Dom string");
report_pending_exception(*cx, true, InRealm::Entered(&ar));
return false;
}
// When `ScriptPrivate` for the compiled script is undefined,
// we need to set it so that it can be used in dynamic import context.
if GetScriptPrivate(*compiled_script).is_undefined() {
debug!("Set script private for {}", script_base_url);
let module_script_data = Box::new(ModuleScript::new(
script_base_url,
fetch_options,
// We can't initialize an module owner here because
// the executing context of script might be different
// from the dynamic import script's executing context.
None,
));
SetScriptPrivate(
*compiled_script,
&PrivateValue(Box::into_raw(module_script_data) as *const _),
);
}
debug!("evaluating Dom string");
let result = JS_ExecuteScript(*cx, compiled_script.handle(), rval);
if !result {
debug!("error evaluating Dom string");
report_pending_exception(*cx, true, InRealm::Entered(&ar));
}
maybe_resume_unwind();
result
}
debug!("evaluating Dom string");
let result = unsafe { JS_ExecuteScript(*cx, compiled_script.handle(), rval) };
if !result {
debug!("error evaluating Dom string");
unsafe { report_pending_exception(*cx, true, InRealm::Entered(&ar)) };
}
maybe_resume_unwind();
result
},
)
}

View file

@ -156,24 +156,37 @@ pub struct ScriptOrigin {
text: Rc<DOMString>,
url: ServoUrl,
external: bool,
fetch_options: ScriptFetchOptions,
type_: ScriptType,
}
impl ScriptOrigin {
pub fn internal(text: Rc<DOMString>, url: ServoUrl, type_: ScriptType) -> ScriptOrigin {
pub fn internal(
text: Rc<DOMString>,
url: ServoUrl,
fetch_options: ScriptFetchOptions,
type_: ScriptType,
) -> ScriptOrigin {
ScriptOrigin {
text: text,
url: url,
external: false,
fetch_options,
type_,
}
}
pub fn external(text: Rc<DOMString>, url: ServoUrl, type_: ScriptType) -> ScriptOrigin {
pub fn external(
text: Rc<DOMString>,
url: ServoUrl,
fetch_options: ScriptFetchOptions,
type_: ScriptType,
) -> ScriptOrigin {
ScriptOrigin {
text: text,
url: url,
external: true,
fetch_options,
type_,
}
}
@ -202,6 +215,8 @@ struct ClassicContext {
url: ServoUrl,
/// Indicates whether the request failed, and why
status: Result<(), NetworkError>,
/// The fetch options of the script
fetch_options: ScriptFetchOptions,
/// Timing object for this resource
resource_timing: ResourceFetchTiming,
}
@ -262,6 +277,7 @@ impl FetchResponseListener for ClassicContext {
ScriptOrigin::external(
Rc::new(DOMString::from(source_text)),
metadata.final_url,
self.fetch_options.clone(),
ScriptType::Classic,
)
});
@ -358,7 +374,7 @@ fn fetch_a_classic_script(
cors_setting,
doc.origin().immutable().clone(),
script.global().pipeline_id(),
options,
options.clone(),
);
// TODO: Step 3, Add custom steps to perform fetch
@ -371,6 +387,7 @@ fn fetch_a_classic_script(
metadata: None,
url: url.clone(),
status: Ok(()),
fetch_options: options,
resource_timing: ResourceFetchTiming::new(ResourceTimingType::Resource),
}));
@ -630,6 +647,7 @@ impl HTMLScriptElement {
let result = Ok(ScriptOrigin::internal(
Rc::clone(&text_rc),
base_url.clone(),
options.clone(),
script_type.clone(),
));
@ -866,6 +884,8 @@ impl HTMLScriptElement {
script.url.as_str(),
rval.handle_mut(),
line_number,
script.fetch_options.clone(),
script.url.clone(),
);
}

View file

@ -7,6 +7,7 @@ use crate::dom::bindings::refcounted::Trusted;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlheadelement::HTMLHeadElement;
use crate::dom::node::document_from_node;
use crate::script_module::ScriptFetchOptions;
use js::jsval::UndefinedValue;
use std::fs::{read_dir, File};
use std::io::Read;
@ -38,13 +39,15 @@ pub fn load_script(head: &HTMLHeadElement) {
let mut contents = vec![];
f.read_to_end(&mut contents).unwrap();
let script_text = String::from_utf8_lossy(&contents);
win.upcast::<GlobalScope>()
.evaluate_script_on_global_with_result(
&script_text,
&file.to_string_lossy(),
rval.handle_mut(),
1,
);
let global = win.upcast::<GlobalScope>();
global.evaluate_script_on_global_with_result(
&script_text,
&file.to_string_lossy(),
rval.handle_mut(),
1,
ScriptFetchOptions::default_classic_script(&global),
global.api_base_url(),
);
}
}));
}

View file

@ -11,6 +11,7 @@ use crate::dom::paintworkletglobalscope::PaintWorkletTask;
use crate::dom::testworkletglobalscope::TestWorkletGlobalScope;
use crate::dom::testworkletglobalscope::TestWorkletTask;
use crate::dom::worklet::WorkletExecutor;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::JSContext;
use crate::script_thread::MainThreadScriptMsg;
use crossbeam_channel::Sender;
@ -88,10 +89,14 @@ impl WorkletGlobalScope {
/// Evaluate a JS script in this global.
pub fn evaluate_js(&self, script: &str) -> bool {
debug!("Evaluating Dom.");
debug!("Evaluating Dom in a worklet.");
rooted!(in (*self.globalscope.get_cx()) let mut rval = UndefinedValue());
self.globalscope
.evaluate_js_on_global_with_result(&*script, rval.handle_mut())
self.globalscope.evaluate_js_on_global_with_result(
&*script,
rval.handle_mut(),
ScriptFetchOptions::default_classic_script(&self.globalscope),
self.globalscope.api_base_url(),
)
}
/// Register a paint worklet to the script thread.