mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Added off thread compilation
Co-authored-by: Gregory Terzian <2792687+gterzian@users.noreply.github.com> Co-authored-by: Abhishek Sharma <20724848+AbhishekSharma102@users.noreply.github.com>
This commit is contained in:
parent
bfb7bea704
commit
1119dd119e
8 changed files with 310 additions and 85 deletions
|
@ -186,6 +186,9 @@ mod gen {
|
||||||
allowed_in_nonsecure_contexts: bool,
|
allowed_in_nonsecure_contexts: bool,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
script: {
|
||||||
|
asynch: bool,
|
||||||
|
},
|
||||||
serviceworker: {
|
serviceworker: {
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
timeout_seconds: i64,
|
timeout_seconds: i64,
|
||||||
|
|
|
@ -14,6 +14,7 @@ use crate::dom::bindings::str::DOMString;
|
||||||
use crate::dom::document::AnimationFrameCallback;
|
use crate::dom::document::AnimationFrameCallback;
|
||||||
use crate::dom::element::Element;
|
use crate::dom::element::Element;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
|
use crate::dom::htmlscriptelement::SourceCode;
|
||||||
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
|
use crate::dom::node::{window_from_node, Node, ShadowIncluding};
|
||||||
use crate::realms::enter_realm;
|
use crate::realms::enter_realm;
|
||||||
use crate::script_module::ScriptFetchOptions;
|
use crate::script_module::ScriptFetchOptions;
|
||||||
|
@ -25,6 +26,7 @@ use js::jsval::UndefinedValue;
|
||||||
use js::rust::wrappers::ObjectClassName;
|
use js::rust::wrappers::ObjectClassName;
|
||||||
use msg::constellation_msg::PipelineId;
|
use msg::constellation_msg::PipelineId;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
use std::rc::Rc;
|
||||||
use std::str;
|
use std::str;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
@ -35,8 +37,9 @@ pub fn handle_evaluate_js(global: &GlobalScope, eval: String, reply: IpcSender<E
|
||||||
let cx = global.get_cx();
|
let cx = global.get_cx();
|
||||||
let _ac = enter_realm(global);
|
let _ac = enter_realm(global);
|
||||||
rooted!(in(*cx) let mut rval = UndefinedValue());
|
rooted!(in(*cx) let mut rval = UndefinedValue());
|
||||||
|
let source_code = SourceCode::Text(Rc::new(DOMString::from_string(eval)));
|
||||||
global.evaluate_script_on_global_with_result(
|
global.evaluate_script_on_global_with_result(
|
||||||
&eval,
|
&source_code,
|
||||||
"<eval>",
|
"<eval>",
|
||||||
rval.handle_mut(),
|
rval.handle_mut(),
|
||||||
1,
|
1,
|
||||||
|
|
|
@ -77,8 +77,10 @@ use hyper::Method;
|
||||||
use hyper::StatusCode;
|
use hyper::StatusCode;
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use ipc_channel::ipc::{IpcReceiver, IpcSender};
|
use ipc_channel::ipc::{IpcReceiver, IpcSender};
|
||||||
use js::glue::{CallObjectTracer, CallStringTracer, CallValueTracer};
|
use js::glue::{CallObjectTracer, CallScriptTracer, CallStringTracer, CallValueTracer};
|
||||||
use js::jsapi::{GCTraceKindToAscii, Heap, JSObject, JSString, JSTracer, JobQueue, TraceKind};
|
use js::jsapi::{
|
||||||
|
GCTraceKindToAscii, Heap, JSObject, JSScript, JSString, JSTracer, JobQueue, TraceKind,
|
||||||
|
};
|
||||||
use js::jsval::JSVal;
|
use js::jsval::JSVal;
|
||||||
use js::rust::{GCMethods, Handle, Runtime};
|
use js::rust::{GCMethods, Handle, Runtime};
|
||||||
use js::typedarray::TypedArray;
|
use js::typedarray::TypedArray;
|
||||||
|
@ -219,6 +221,18 @@ unsafe_no_jsmanaged_fields!(Cow<'static, str>);
|
||||||
|
|
||||||
unsafe_no_jsmanaged_fields!(CspList);
|
unsafe_no_jsmanaged_fields!(CspList);
|
||||||
|
|
||||||
|
/// Trace a `JSScript`.
|
||||||
|
pub fn trace_script(tracer: *mut JSTracer, description: &str, script: &Heap<*mut JSScript>) {
|
||||||
|
unsafe {
|
||||||
|
trace!("tracing {}", description);
|
||||||
|
CallScriptTracer(
|
||||||
|
tracer,
|
||||||
|
script.ptr.get() as *mut _,
|
||||||
|
GCTraceKindToAscii(TraceKind::Script),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Trace a `JSVal`.
|
/// Trace a `JSVal`.
|
||||||
pub fn trace_jsval(tracer: *mut JSTracer, description: &str, val: &Heap<JSVal>) {
|
pub fn trace_jsval(tracer: *mut JSTracer, description: &str, val: &Heap<JSVal>) {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -328,6 +342,15 @@ unsafe impl<T: JSTraceable> JSTraceable for RefCell<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl JSTraceable for Heap<*mut JSScript> {
|
||||||
|
unsafe fn trace(&self, trc: *mut JSTracer) {
|
||||||
|
if self.get().is_null() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
trace_script(trc, "heap script", self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unsafe impl JSTraceable for Heap<*mut JSObject> {
|
unsafe impl JSTraceable for Heap<*mut JSObject> {
|
||||||
unsafe fn trace(&self, trc: *mut JSTracer) {
|
unsafe fn trace(&self, trc: *mut JSTracer) {
|
||||||
if self.get().is_null() {
|
if self.get().is_null() {
|
||||||
|
|
|
@ -38,7 +38,7 @@ use crate::dom::file::File;
|
||||||
use crate::dom::gpudevice::GPUDevice;
|
use crate::dom::gpudevice::GPUDevice;
|
||||||
use crate::dom::gpuoutofmemoryerror::GPUOutOfMemoryError;
|
use crate::dom::gpuoutofmemoryerror::GPUOutOfMemoryError;
|
||||||
use crate::dom::gpuvalidationerror::GPUValidationError;
|
use crate::dom::gpuvalidationerror::GPUValidationError;
|
||||||
use crate::dom::htmlscriptelement::ScriptId;
|
use crate::dom::htmlscriptelement::{ScriptId, SourceCode};
|
||||||
use crate::dom::identityhub::Identities;
|
use crate::dom::identityhub::Identities;
|
||||||
use crate::dom::imagebitmap::ImageBitmap;
|
use crate::dom::imagebitmap::ImageBitmap;
|
||||||
use crate::dom::messageevent::MessageEvent;
|
use crate::dom::messageevent::MessageEvent;
|
||||||
|
@ -91,9 +91,9 @@ use js::jsval::PrivateValue;
|
||||||
use js::jsval::{JSVal, UndefinedValue};
|
use js::jsval::{JSVal, UndefinedValue};
|
||||||
use js::panic::maybe_resume_unwind;
|
use js::panic::maybe_resume_unwind;
|
||||||
use js::rust::transform_str_to_source_text;
|
use js::rust::transform_str_to_source_text;
|
||||||
use js::rust::wrappers::{JS_ExecuteScript, JS_GetScriptPrivate};
|
use js::rust::wrappers::{JS_ExecuteScript, JS_ExecuteScript1, JS_GetScriptPrivate};
|
||||||
use js::rust::{get_object_class, CompileOptionsWrapper, ParentRuntime, Runtime};
|
use js::rust::{get_object_class, CompileOptionsWrapper, ParentRuntime, Runtime};
|
||||||
use js::rust::{HandleValue, MutableHandleValue};
|
use js::rust::{HandleScript, HandleValue, MutableHandleValue};
|
||||||
use js::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
|
use js::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
|
||||||
use msg::constellation_msg::{
|
use msg::constellation_msg::{
|
||||||
BlobId, BroadcastChannelRouterId, MessagePortId, MessagePortRouterId, PipelineId,
|
BlobId, BroadcastChannelRouterId, MessagePortId, MessagePortRouterId, PipelineId,
|
||||||
|
@ -2552,8 +2552,9 @@ impl GlobalScope {
|
||||||
fetch_options: ScriptFetchOptions,
|
fetch_options: ScriptFetchOptions,
|
||||||
script_base_url: ServoUrl,
|
script_base_url: ServoUrl,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
let source_code = SourceCode::Text(Rc::new(DOMString::from_string((*code).to_string())));
|
||||||
self.evaluate_script_on_global_with_result(
|
self.evaluate_script_on_global_with_result(
|
||||||
code,
|
&source_code,
|
||||||
"",
|
"",
|
||||||
rval,
|
rval,
|
||||||
1,
|
1,
|
||||||
|
@ -2566,7 +2567,7 @@ impl GlobalScope {
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub fn evaluate_script_on_global_with_result(
|
pub fn evaluate_script_on_global_with_result(
|
||||||
&self,
|
&self,
|
||||||
code: &str,
|
code: &SourceCode,
|
||||||
filename: &str,
|
filename: &str,
|
||||||
rval: MutableHandleValue,
|
rval: MutableHandleValue,
|
||||||
line_number: u32,
|
line_number: u32,
|
||||||
|
@ -2595,14 +2596,17 @@ impl GlobalScope {
|
||||||
let _aes = AutoEntryScript::new(self);
|
let _aes = AutoEntryScript::new(self);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let options = CompileOptionsWrapper::new(*cx, filename.as_ptr(), line_number);
|
let result = match code {
|
||||||
|
SourceCode::Text(text_code) => {
|
||||||
|
let options =
|
||||||
|
CompileOptionsWrapper::new(*cx, filename.as_ptr(), line_number);
|
||||||
|
|
||||||
debug!("compiling Dom string");
|
debug!("compiling Dom string");
|
||||||
rooted!(in(*cx) let compiled_script =
|
rooted!(in(*cx) let compiled_script =
|
||||||
Compile1(
|
Compile1(
|
||||||
*cx,
|
*cx,
|
||||||
options.ptr,
|
options.ptr,
|
||||||
&mut transform_str_to_source_text(code),
|
&mut transform_str_to_source_text(text_code),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2636,7 +2640,14 @@ impl GlobalScope {
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("evaluating Dom string");
|
debug!("evaluating Dom string");
|
||||||
let result = JS_ExecuteScript(*cx, compiled_script.handle(), rval);
|
JS_ExecuteScript(*cx, compiled_script.handle(), rval)
|
||||||
|
},
|
||||||
|
SourceCode::Compiled(compiled_script) => {
|
||||||
|
let script = compiled_script.source_code.get();
|
||||||
|
let script_handle = HandleScript::new(&script);
|
||||||
|
JS_ExecuteScript1(*cx, script_handle)
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
if !result {
|
if !result {
|
||||||
debug!("error evaluating Dom string");
|
debug!("error evaluating Dom string");
|
||||||
|
|
|
@ -13,6 +13,7 @@ use crate::dom::bindings::reflector::DomObject;
|
||||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
use crate::dom::bindings::root::{Dom, DomRoot};
|
||||||
use crate::dom::bindings::settings_stack::AutoEntryScript;
|
use crate::dom::bindings::settings_stack::AutoEntryScript;
|
||||||
use crate::dom::bindings::str::{DOMString, USVString};
|
use crate::dom::bindings::str::{DOMString, USVString};
|
||||||
|
use crate::dom::bindings::trace::RootedTraceableBox;
|
||||||
use crate::dom::document::Document;
|
use crate::dom::document::Document;
|
||||||
use crate::dom::element::{
|
use crate::dom::element::{
|
||||||
cors_setting_for_element, reflect_cross_origin_attribute, set_cross_origin_attribute,
|
cors_setting_for_element, reflect_cross_origin_attribute, set_cross_origin_attribute,
|
||||||
|
@ -27,15 +28,25 @@ use crate::dom::performanceresourcetiming::InitiatorType;
|
||||||
use crate::dom::virtualmethods::VirtualMethods;
|
use crate::dom::virtualmethods::VirtualMethods;
|
||||||
use crate::fetch::create_a_potential_cors_request;
|
use crate::fetch::create_a_potential_cors_request;
|
||||||
use crate::network_listener::{self, NetworkListener, PreInvoke, ResourceTimingListener};
|
use crate::network_listener::{self, NetworkListener, PreInvoke, ResourceTimingListener};
|
||||||
|
use crate::realms::enter_realm;
|
||||||
use crate::script_module::fetch_inline_module_script;
|
use crate::script_module::fetch_inline_module_script;
|
||||||
use crate::script_module::{fetch_external_module_script, ModuleOwner, ScriptFetchOptions};
|
use crate::script_module::{fetch_external_module_script, ModuleOwner, ScriptFetchOptions};
|
||||||
|
use crate::task::TaskCanceller;
|
||||||
|
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
|
||||||
|
use crate::task_source::TaskSource;
|
||||||
|
use crate::task_source::TaskSourceName;
|
||||||
use content_security_policy as csp;
|
use content_security_policy as csp;
|
||||||
|
use core::ffi::c_void;
|
||||||
use dom_struct::dom_struct;
|
use dom_struct::dom_struct;
|
||||||
use encoding_rs::Encoding;
|
use encoding_rs::Encoding;
|
||||||
use html5ever::{LocalName, Prefix};
|
use html5ever::{LocalName, Prefix};
|
||||||
use ipc_channel::ipc;
|
use ipc_channel::ipc;
|
||||||
use ipc_channel::router::ROUTER;
|
use ipc_channel::router::ROUTER;
|
||||||
|
use js::jsapi::{
|
||||||
|
CanCompileOffThread, CompileOffThread, FinishOffThreadScript, Heap, JSScript, OffThreadToken,
|
||||||
|
};
|
||||||
use js::jsval::UndefinedValue;
|
use js::jsval::UndefinedValue;
|
||||||
|
use js::rust::{transform_u16_to_source_text, CompileOptionsWrapper};
|
||||||
use msg::constellation_msg::PipelineId;
|
use msg::constellation_msg::PipelineId;
|
||||||
use net_traits::request::{
|
use net_traits::request::{
|
||||||
CorsSettings, CredentialsMode, Destination, ParserMetadata, RequestBuilder,
|
CorsSettings, CredentialsMode, Destination, ParserMetadata, RequestBuilder,
|
||||||
|
@ -43,11 +54,13 @@ use net_traits::request::{
|
||||||
use net_traits::{FetchMetadata, FetchResponseListener, Metadata, NetworkError};
|
use net_traits::{FetchMetadata, FetchResponseListener, Metadata, NetworkError};
|
||||||
use net_traits::{ResourceFetchTiming, ResourceTimingType};
|
use net_traits::{ResourceFetchTiming, ResourceTimingType};
|
||||||
use servo_atoms::Atom;
|
use servo_atoms::Atom;
|
||||||
|
use servo_config::pref;
|
||||||
use servo_url::ImmutableOrigin;
|
use servo_url::ImmutableOrigin;
|
||||||
use servo_url::ServoUrl;
|
use servo_url::ServoUrl;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::fs::{create_dir_all, read_to_string, File};
|
use std::fs::{create_dir_all, read_to_string, File};
|
||||||
use std::io::{Read, Seek, Write};
|
use std::io::{Read, Seek, Write};
|
||||||
|
use std::mem::replace;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
@ -55,6 +68,82 @@ use std::sync::{Arc, Mutex};
|
||||||
use style::str::{StaticStringVec, HTML_SPACE_CHARACTERS};
|
use style::str::{StaticStringVec, HTML_SPACE_CHARACTERS};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub struct OffThreadCompilationContext {
|
||||||
|
script_element: Trusted<HTMLScriptElement>,
|
||||||
|
script_kind: ExternalScriptKind,
|
||||||
|
final_url: ServoUrl,
|
||||||
|
url: ServoUrl,
|
||||||
|
task_source: DOMManipulationTaskSource,
|
||||||
|
canceller: TaskCanceller,
|
||||||
|
script_text: String,
|
||||||
|
utf16_chars: Vec<u16>,
|
||||||
|
fetch_options: ScriptFetchOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A wrapper to mark OffThreadToken as Send,
|
||||||
|
/// which should be safe according to
|
||||||
|
/// mozjs/js/public/OffThreadScriptCompilation.h
|
||||||
|
struct OffThreadCompilationToken(*mut OffThreadToken);
|
||||||
|
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
unsafe impl Send for OffThreadCompilationToken {}
|
||||||
|
|
||||||
|
#[allow(unsafe_code)]
|
||||||
|
unsafe extern "C" fn off_thread_compilation_callback(
|
||||||
|
token: *mut OffThreadToken,
|
||||||
|
callback_data: *mut c_void,
|
||||||
|
) {
|
||||||
|
let mut context = Box::from_raw(callback_data as *mut OffThreadCompilationContext);
|
||||||
|
let token = OffThreadCompilationToken(token);
|
||||||
|
|
||||||
|
let url = context.url.clone();
|
||||||
|
let final_url = context.final_url.clone();
|
||||||
|
let script_element = context.script_element.clone();
|
||||||
|
let script_kind = context.script_kind.clone();
|
||||||
|
let script = replace(&mut context.script_text, String::new());
|
||||||
|
let fetch_options = context.fetch_options.clone();
|
||||||
|
|
||||||
|
// Continue with <https://html.spec.whatwg.org/multipage/#fetch-a-classic-script>
|
||||||
|
let _ = context.task_source.queue_with_canceller(
|
||||||
|
task!(off_thread_compile_continue: move || {
|
||||||
|
let elem = script_element.root();
|
||||||
|
let global = elem.global();
|
||||||
|
let cx = global.get_cx();
|
||||||
|
let _ar = enter_realm(&*global);
|
||||||
|
|
||||||
|
rooted!(in(*cx)
|
||||||
|
let compiled_script = FinishOffThreadScript(*cx, token.0)
|
||||||
|
);
|
||||||
|
|
||||||
|
let load = if compiled_script.get().is_null() {
|
||||||
|
Err(NetworkError::Internal(
|
||||||
|
"Off-thread compilation failed.".into(),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
let script_text = DOMString::from(script);
|
||||||
|
let heap = Heap::default();
|
||||||
|
let source_code = RootedTraceableBox::new(heap);
|
||||||
|
source_code.set(compiled_script.get());
|
||||||
|
let code = SourceCode::Compiled(CompiledSourceCode {
|
||||||
|
source_code: source_code,
|
||||||
|
original_text: Rc::new(script_text),
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(ScriptOrigin {
|
||||||
|
code,
|
||||||
|
url: final_url,
|
||||||
|
external: true,
|
||||||
|
fetch_options: fetch_options,
|
||||||
|
type_: ScriptType::Classic,
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
finish_fetching_a_classic_script(&*elem, script_kind, url, load);
|
||||||
|
}),
|
||||||
|
&context.canceller,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// An unique id for script element.
|
/// An unique id for script element.
|
||||||
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, PartialEq)]
|
#[derive(Clone, Copy, Debug, Eq, Hash, JSTraceable, PartialEq)]
|
||||||
pub struct ScriptId(Uuid);
|
pub struct ScriptId(Uuid);
|
||||||
|
@ -150,10 +239,24 @@ pub enum ScriptType {
|
||||||
Module,
|
Module,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(JSTraceable, MallocSizeOf)]
|
||||||
|
pub struct CompiledSourceCode {
|
||||||
|
#[ignore_malloc_size_of = "SM handles JS values"]
|
||||||
|
pub source_code: RootedTraceableBox<Heap<*mut JSScript>>,
|
||||||
|
#[ignore_malloc_size_of = "Rc is hard"]
|
||||||
|
pub original_text: Rc<DOMString>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(JSTraceable)]
|
||||||
|
pub enum SourceCode {
|
||||||
|
Text(Rc<DOMString>),
|
||||||
|
Compiled(CompiledSourceCode),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(JSTraceable, MallocSizeOf)]
|
#[derive(JSTraceable, MallocSizeOf)]
|
||||||
pub struct ScriptOrigin {
|
pub struct ScriptOrigin {
|
||||||
#[ignore_malloc_size_of = "Rc is hard"]
|
#[ignore_malloc_size_of = "Rc is hard"]
|
||||||
text: Rc<DOMString>,
|
code: SourceCode,
|
||||||
url: ServoUrl,
|
url: ServoUrl,
|
||||||
external: bool,
|
external: bool,
|
||||||
fetch_options: ScriptFetchOptions,
|
fetch_options: ScriptFetchOptions,
|
||||||
|
@ -168,7 +271,7 @@ impl ScriptOrigin {
|
||||||
type_: ScriptType,
|
type_: ScriptType,
|
||||||
) -> ScriptOrigin {
|
) -> ScriptOrigin {
|
||||||
ScriptOrigin {
|
ScriptOrigin {
|
||||||
text: text,
|
code: SourceCode::Text(text),
|
||||||
url: url,
|
url: url,
|
||||||
external: false,
|
external: false,
|
||||||
fetch_options,
|
fetch_options,
|
||||||
|
@ -183,7 +286,7 @@ impl ScriptOrigin {
|
||||||
type_: ScriptType,
|
type_: ScriptType,
|
||||||
) -> ScriptOrigin {
|
) -> ScriptOrigin {
|
||||||
ScriptOrigin {
|
ScriptOrigin {
|
||||||
text: text,
|
code: SourceCode::Text(text),
|
||||||
url: url,
|
url: url,
|
||||||
external: true,
|
external: true,
|
||||||
fetch_options,
|
fetch_options,
|
||||||
|
@ -192,8 +295,35 @@ impl ScriptOrigin {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn text(&self) -> Rc<DOMString> {
|
pub fn text(&self) -> Rc<DOMString> {
|
||||||
Rc::clone(&self.text)
|
match &self.code {
|
||||||
|
SourceCode::Text(text) => Rc::clone(&text),
|
||||||
|
SourceCode::Compiled(compiled_script) => Rc::clone(&compiled_script.original_text),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Final steps of <https://html.spec.whatwg.org/multipage/#fetch-a-classic-script>
|
||||||
|
fn finish_fetching_a_classic_script(
|
||||||
|
elem: &HTMLScriptElement,
|
||||||
|
script_kind: ExternalScriptKind,
|
||||||
|
url: ServoUrl,
|
||||||
|
load: ScriptResult,
|
||||||
|
) {
|
||||||
|
// Step 11, Asynchronously complete this algorithm with script,
|
||||||
|
// which refers to step 26.6 "When the chosen algorithm asynchronously completes",
|
||||||
|
// of https://html.spec.whatwg.org/multipage/#prepare-a-script
|
||||||
|
let document = document_from_node(&*elem);
|
||||||
|
|
||||||
|
match script_kind {
|
||||||
|
ExternalScriptKind::Asap => document.asap_script_loaded(&elem, load),
|
||||||
|
ExternalScriptKind::AsapInOrder => document.asap_in_order_script_loaded(&elem, load),
|
||||||
|
ExternalScriptKind::Deferred => document.deferred_script_loaded(&elem, load),
|
||||||
|
ExternalScriptKind::ParsingBlocking => {
|
||||||
|
document.pending_parsing_blocking_script_loaded(&elem, load)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
document.finish_load(LoadType::Script(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ScriptResult = Result<ScriptOrigin, NetworkError>;
|
pub type ScriptResult = Result<ScriptOrigin, NetworkError>;
|
||||||
|
@ -261,43 +391,79 @@ impl FetchResponseListener for ClassicContext {
|
||||||
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#fetch-a-classic-script>
|
/// <https://html.spec.whatwg.org/multipage/#fetch-a-classic-script>
|
||||||
/// step 4-9
|
/// step 4-9
|
||||||
|
#[allow(unsafe_code)]
|
||||||
fn process_response_eof(&mut self, response: Result<ResourceFetchTiming, NetworkError>) {
|
fn process_response_eof(&mut self, response: Result<ResourceFetchTiming, NetworkError>) {
|
||||||
// Step 5.
|
let (source_text, final_url) = match (response.as_ref(), self.status.as_ref()) {
|
||||||
let load = response.and(self.status.clone()).map(|_| {
|
(Err(err), _) | (_, Err(err)) => {
|
||||||
|
// Step 6, response is an error.
|
||||||
|
finish_fetching_a_classic_script(
|
||||||
|
&*self.elem.root(),
|
||||||
|
self.kind.clone(),
|
||||||
|
self.url.clone(),
|
||||||
|
Err(err.clone()),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
(Ok(_), Ok(_)) => {
|
||||||
let metadata = self.metadata.take().unwrap();
|
let metadata = self.metadata.take().unwrap();
|
||||||
|
|
||||||
// Step 6.
|
// Step 7.
|
||||||
let encoding = metadata
|
let encoding = metadata
|
||||||
.charset
|
.charset
|
||||||
.and_then(|encoding| Encoding::for_label(encoding.as_bytes()))
|
.and_then(|encoding| Encoding::for_label(encoding.as_bytes()))
|
||||||
.unwrap_or(self.character_encoding);
|
.unwrap_or(self.character_encoding);
|
||||||
|
|
||||||
// Step 7.
|
// Step 8.
|
||||||
let (source_text, _, _) = encoding.decode(&self.data);
|
let (source_text, _, _) = encoding.decode(&self.data);
|
||||||
ScriptOrigin::external(
|
(source_text, metadata.final_url)
|
||||||
Rc::new(DOMString::from(source_text)),
|
},
|
||||||
metadata.final_url,
|
};
|
||||||
self.fetch_options.clone(),
|
|
||||||
ScriptType::Classic,
|
let elem = self.elem.root();
|
||||||
)
|
let global = elem.global();
|
||||||
|
let cx = global.get_cx();
|
||||||
|
let _ar = enter_realm(&*global);
|
||||||
|
|
||||||
|
let options =
|
||||||
|
unsafe { CompileOptionsWrapper::new(*cx, final_url.as_str().as_ptr() as *const i8, 1) };
|
||||||
|
|
||||||
|
let can_compile_off_thread = pref!(dom.script.asynch) &&
|
||||||
|
unsafe { CanCompileOffThread(*cx, options.ptr as *const _, source_text.len()) };
|
||||||
|
|
||||||
|
if can_compile_off_thread {
|
||||||
|
let source_string = source_text.to_string();
|
||||||
|
let source_text: Vec<u16> = source_text.encode_utf16().collect();
|
||||||
|
|
||||||
|
let context = Box::new(OffThreadCompilationContext {
|
||||||
|
script_element: self.elem.clone(),
|
||||||
|
script_kind: self.kind.clone(),
|
||||||
|
final_url,
|
||||||
|
url: self.url.clone(),
|
||||||
|
task_source: global.dom_manipulation_task_source(),
|
||||||
|
canceller: global.task_canceller(TaskSourceName::DOMManipulation),
|
||||||
|
script_text: source_string,
|
||||||
|
utf16_chars: source_text,
|
||||||
|
fetch_options: self.fetch_options.clone(),
|
||||||
});
|
});
|
||||||
|
|
||||||
// Step 9.
|
unsafe {
|
||||||
// https://html.spec.whatwg.org/multipage/#prepare-a-script
|
assert!(CompileOffThread(
|
||||||
// Step 18.6 (When the chosen algorithm asynchronously completes).
|
*cx,
|
||||||
let elem = self.elem.root();
|
options.ptr as *const _,
|
||||||
let document = document_from_node(&*elem);
|
&mut transform_u16_to_source_text(&context.utf16_chars) as *mut _,
|
||||||
|
Some(off_thread_compilation_callback),
|
||||||
match self.kind {
|
Box::into_raw(context) as *mut c_void,
|
||||||
ExternalScriptKind::Asap => document.asap_script_loaded(&elem, load),
|
));
|
||||||
ExternalScriptKind::AsapInOrder => document.asap_in_order_script_loaded(&elem, load),
|
}
|
||||||
ExternalScriptKind::Deferred => document.deferred_script_loaded(&elem, load),
|
} else {
|
||||||
ExternalScriptKind::ParsingBlocking => {
|
let load = ScriptOrigin::external(
|
||||||
document.pending_parsing_blocking_script_loaded(&elem, load)
|
Rc::new(DOMString::from(source_text)),
|
||||||
},
|
final_url.clone(),
|
||||||
|
self.fetch_options.clone(),
|
||||||
|
ScriptType::Classic,
|
||||||
|
);
|
||||||
|
finish_fetching_a_classic_script(&*elem, self.kind.clone(), self.url.clone(), Ok(load));
|
||||||
}
|
}
|
||||||
|
|
||||||
document.finish_load(LoadType::Script(self.url.clone()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resource_timing_mut(&mut self) -> &mut ResourceFetchTiming {
|
fn resource_timing_mut(&mut self) -> &mut ResourceFetchTiming {
|
||||||
|
@ -702,7 +868,16 @@ impl HTMLScriptElement {
|
||||||
// unminified content.
|
// unminified content.
|
||||||
let (input, output) = (tempfile::NamedTempFile::new(), tempfile::tempfile());
|
let (input, output) = (tempfile::NamedTempFile::new(), tempfile::tempfile());
|
||||||
if let (Ok(mut input), Ok(mut output)) = (input, output) {
|
if let (Ok(mut input), Ok(mut output)) = (input, output) {
|
||||||
input.write_all(script.text.as_bytes()).unwrap();
|
match &script.code {
|
||||||
|
SourceCode::Text(text) => {
|
||||||
|
input.write_all(text.as_bytes()).unwrap();
|
||||||
|
},
|
||||||
|
SourceCode::Compiled(compiled_source_code) => {
|
||||||
|
input
|
||||||
|
.write_all(compiled_source_code.original_text.as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
},
|
||||||
|
}
|
||||||
match Command::new("js-beautify")
|
match Command::new("js-beautify")
|
||||||
.arg(input.path())
|
.arg(input.path())
|
||||||
.stdout(output.try_clone().unwrap())
|
.stdout(output.try_clone().unwrap())
|
||||||
|
@ -712,7 +887,7 @@ impl HTMLScriptElement {
|
||||||
let mut script_content = String::new();
|
let mut script_content = String::new();
|
||||||
output.seek(std::io::SeekFrom::Start(0)).unwrap();
|
output.seek(std::io::SeekFrom::Start(0)).unwrap();
|
||||||
output.read_to_string(&mut script_content).unwrap();
|
output.read_to_string(&mut script_content).unwrap();
|
||||||
script.text = Rc::new(DOMString::from(script_content));
|
script.code = SourceCode::Text(Rc::new(DOMString::from(script_content)));
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
warn!("Failed to execute js-beautify. Will store unmodified script");
|
warn!("Failed to execute js-beautify. Will store unmodified script");
|
||||||
|
@ -763,7 +938,13 @@ impl HTMLScriptElement {
|
||||||
debug!("script will be stored in {:?}", path);
|
debug!("script will be stored in {:?}", path);
|
||||||
|
|
||||||
match File::create(&path) {
|
match File::create(&path) {
|
||||||
Ok(mut file) => file.write_all(script.text.as_bytes()).unwrap(),
|
Ok(mut file) => match &script.code {
|
||||||
|
SourceCode::Text(text) => file.write_all(text.as_bytes()).unwrap(),
|
||||||
|
SourceCode::Compiled(compiled_source_code) => {
|
||||||
|
file.write_all(compiled_source_code.original_text.as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
},
|
||||||
|
},
|
||||||
Err(why) => warn!("Could not store script {:?}", why),
|
Err(why) => warn!("Could not store script {:?}", why),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -790,7 +971,7 @@ impl HTMLScriptElement {
|
||||||
match read_to_string(path.clone()) {
|
match read_to_string(path.clone()) {
|
||||||
Ok(local_script) => {
|
Ok(local_script) => {
|
||||||
debug!("Found script stored at: {:?}", path);
|
debug!("Found script stored at: {:?}", path);
|
||||||
script.text = Rc::new(DOMString::from(local_script));
|
script.code = SourceCode::Text(Rc::new(DOMString::from(local_script)));
|
||||||
},
|
},
|
||||||
Err(why) => warn!("Could not restore script from file {:?}", why),
|
Err(why) => warn!("Could not restore script from file {:?}", why),
|
||||||
}
|
}
|
||||||
|
@ -880,7 +1061,7 @@ impl HTMLScriptElement {
|
||||||
rooted!(in(*window.get_cx()) let mut rval = UndefinedValue());
|
rooted!(in(*window.get_cx()) let mut rval = UndefinedValue());
|
||||||
let global = window.upcast::<GlobalScope>();
|
let global = window.upcast::<GlobalScope>();
|
||||||
global.evaluate_script_on_global_with_result(
|
global.evaluate_script_on_global_with_result(
|
||||||
&script.text,
|
&script.code,
|
||||||
script.url.as_str(),
|
script.url.as_str(),
|
||||||
rval.handle_mut(),
|
rval.handle_mut(),
|
||||||
line_number,
|
line_number,
|
||||||
|
|
|
@ -4,14 +4,17 @@
|
||||||
|
|
||||||
use crate::dom::bindings::inheritance::Castable;
|
use crate::dom::bindings::inheritance::Castable;
|
||||||
use crate::dom::bindings::refcounted::Trusted;
|
use crate::dom::bindings::refcounted::Trusted;
|
||||||
|
use crate::dom::bindings::str::DOMString;
|
||||||
use crate::dom::globalscope::GlobalScope;
|
use crate::dom::globalscope::GlobalScope;
|
||||||
use crate::dom::htmlheadelement::HTMLHeadElement;
|
use crate::dom::htmlheadelement::HTMLHeadElement;
|
||||||
|
use crate::dom::htmlscriptelement::SourceCode;
|
||||||
use crate::dom::node::document_from_node;
|
use crate::dom::node::document_from_node;
|
||||||
use crate::script_module::ScriptFetchOptions;
|
use crate::script_module::ScriptFetchOptions;
|
||||||
use js::jsval::UndefinedValue;
|
use js::jsval::UndefinedValue;
|
||||||
use std::fs::{read_dir, File};
|
use std::fs::{read_dir, File};
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub fn load_script(head: &HTMLHeadElement) {
|
pub fn load_script(head: &HTMLHeadElement) {
|
||||||
let doc = document_from_node(head);
|
let doc = document_from_node(head);
|
||||||
|
@ -38,7 +41,9 @@ pub fn load_script(head: &HTMLHeadElement) {
|
||||||
let mut f = File::open(&file).unwrap();
|
let mut f = File::open(&file).unwrap();
|
||||||
let mut contents = vec![];
|
let mut contents = vec![];
|
||||||
f.read_to_end(&mut contents).unwrap();
|
f.read_to_end(&mut contents).unwrap();
|
||||||
let script_text = String::from_utf8_lossy(&contents);
|
let script_text = SourceCode::Text(
|
||||||
|
Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string()))
|
||||||
|
);
|
||||||
let global = win.upcast::<GlobalScope>();
|
let global = win.upcast::<GlobalScope>();
|
||||||
global.evaluate_script_on_global_with_result(
|
global.evaluate_script_on_global_with_result(
|
||||||
&script_text,
|
&script_text,
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
"dom.offscreen_canvas.enabled": false,
|
"dom.offscreen_canvas.enabled": false,
|
||||||
"dom.permissions.enabled": false,
|
"dom.permissions.enabled": false,
|
||||||
"dom.permissions.testing.allowed_in_nonsecure_contexts": false,
|
"dom.permissions.testing.allowed_in_nonsecure_contexts": false,
|
||||||
|
"dom.script.asynch": true,
|
||||||
"dom.serviceworker.enabled": false,
|
"dom.serviceworker.enabled": false,
|
||||||
"dom.serviceworker.timeout_seconds": 60,
|
"dom.serviceworker.timeout_seconds": 60,
|
||||||
"dom.servo_helpers.enabled": false,
|
"dom.servo_helpers.enabled": false,
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
[customized-built-in-constructor-exceptions.html]
|
|
||||||
expected: CRASH
|
|
Loading…
Add table
Add a link
Reference in a new issue