mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Pass a parent JS runtime when creating DOM Worker runtimes
This enables sharing data with the parent runtime, decreasing memory usage and startup time. Also contains an update to current rust-mozjs, because that's required for this to work.
This commit is contained in:
parent
2ce9eba3dc
commit
7536afff2a
7 changed files with 22 additions and 13 deletions
|
@ -213,7 +213,8 @@ impl DedicatedWorkerGlobalScope {
|
||||||
worker_url: Url,
|
worker_url: Url,
|
||||||
id: PipelineId,
|
id: PipelineId,
|
||||||
from_devtools_receiver: IpcReceiver<DevtoolScriptControlMsg>,
|
from_devtools_receiver: IpcReceiver<DevtoolScriptControlMsg>,
|
||||||
main_thread_rt: Arc<Mutex<Option<SharedRt>>>,
|
parent_rt: SharedRt,
|
||||||
|
worker_rt_for_mainthread: Arc<Mutex<Option<SharedRt>>>,
|
||||||
worker: TrustedWorkerAddress,
|
worker: TrustedWorkerAddress,
|
||||||
parent_sender: Box<ScriptChan + Send>,
|
parent_sender: Box<ScriptChan + Send>,
|
||||||
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
|
own_sender: Sender<(TrustedWorkerAddress, WorkerScriptMsg)>,
|
||||||
|
@ -240,8 +241,8 @@ impl DedicatedWorkerGlobalScope {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let runtime = unsafe { new_rt_and_cx() };
|
let runtime = unsafe { new_rt_and_cx(parent_rt.rt()) };
|
||||||
*main_thread_rt.lock().unwrap() = Some(SharedRt::new(&runtime));
|
*worker_rt_for_mainthread.lock().unwrap() = Some(SharedRt::new(&runtime));
|
||||||
|
|
||||||
let (devtools_mpsc_chan, devtools_mpsc_port) = channel();
|
let (devtools_mpsc_chan, devtools_mpsc_port) = channel();
|
||||||
ROUTER.route_ipc_receiver_to_mpsc_sender(from_devtools_receiver, devtools_mpsc_chan);
|
ROUTER.route_ipc_receiver_to_mpsc_sender(from_devtools_receiver, devtools_mpsc_chan);
|
||||||
|
|
|
@ -22,7 +22,7 @@ use dom::messageevent::MessageEvent;
|
||||||
use dom::workerglobalscope::WorkerGlobalScopeInit;
|
use dom::workerglobalscope::WorkerGlobalScopeInit;
|
||||||
use ipc_channel::ipc;
|
use ipc_channel::ipc;
|
||||||
use js::jsapi::{HandleValue, JSContext, JSRuntime, RootedValue};
|
use js::jsapi::{HandleValue, JSContext, JSRuntime, RootedValue};
|
||||||
use js::jsapi::{JSAutoCompartment, JS_RequestInterruptCallback};
|
use js::jsapi::{JSAutoCompartment, JS_GetRuntime, JS_RequestInterruptCallback};
|
||||||
use js::jsval::UndefinedValue;
|
use js::jsval::UndefinedValue;
|
||||||
use js::rust::Runtime;
|
use js::rust::Runtime;
|
||||||
use msg::constellation_msg::{PipelineId, ReferrerPolicy};
|
use msg::constellation_msg::{PipelineId, ReferrerPolicy};
|
||||||
|
@ -91,6 +91,7 @@ impl Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-worker
|
// https://html.spec.whatwg.org/multipage/#dom-worker
|
||||||
|
#[allow(unsafe_code)]
|
||||||
pub fn Constructor(global: GlobalRef, script_url: DOMString) -> Fallible<Root<Worker>> {
|
pub fn Constructor(global: GlobalRef, script_url: DOMString) -> Fallible<Root<Worker>> {
|
||||||
// Step 2-4.
|
// Step 2-4.
|
||||||
let worker_url = match global.api_base_url().join(&script_url) {
|
let worker_url = match global.api_base_url().join(&script_url) {
|
||||||
|
@ -145,8 +146,10 @@ impl Worker {
|
||||||
closing: closing,
|
closing: closing,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let shared_rt = SharedRt { rt: unsafe { JS_GetRuntime(global.get_cx()) } };
|
||||||
|
|
||||||
DedicatedWorkerGlobalScope::run_worker_scope(
|
DedicatedWorkerGlobalScope::run_worker_scope(
|
||||||
init, worker_url, global.pipeline(), devtools_receiver, worker.runtime.clone(), worker_ref,
|
init, worker_url, global.pipeline(), devtools_receiver, shared_rt, worker.runtime.clone(), worker_ref,
|
||||||
global.script_chan(), sender, receiver, worker_load_origin);
|
global.script_chan(), sender, receiver, worker_load_origin);
|
||||||
|
|
||||||
Ok(worker)
|
Ok(worker)
|
||||||
|
@ -309,6 +312,10 @@ impl SharedRt {
|
||||||
JS_RequestInterruptCallback(self.rt);
|
JS_RequestInterruptCallback(self.rt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn rt(&self) -> *mut JSRuntime {
|
||||||
|
self.rt
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
|
|
|
@ -95,9 +95,9 @@ impl<'a> Drop for StackRootTLS<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
pub unsafe fn new_rt_and_cx() -> Runtime {
|
pub unsafe fn new_rt_and_cx(parent_rt: *mut JSRuntime) -> Runtime {
|
||||||
LiveDOMReferences::initialize();
|
LiveDOMReferences::initialize();
|
||||||
let runtime = Runtime::new();
|
let runtime = Runtime::new(parent_rt);
|
||||||
|
|
||||||
JS_AddExtraGCRootsTracer(runtime.rt(), Some(trace_rust_roots), ptr::null_mut());
|
JS_AddExtraGCRootsTracer(runtime.rt(), Some(trace_rust_roots), ptr::null_mut());
|
||||||
JS_AddExtraGCRootsTracer(runtime.rt(), Some(trace_refcounted_objects), ptr::null_mut());
|
JS_AddExtraGCRootsTracer(runtime.rt(), Some(trace_refcounted_objects), ptr::null_mut());
|
||||||
|
|
|
@ -88,6 +88,7 @@ use std::borrow::ToOwned;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::option::Option;
|
use std::option::Option;
|
||||||
|
use std::ptr;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::result::Result;
|
use std::result::Result;
|
||||||
use std::sync::atomic::{Ordering, AtomicBool};
|
use std::sync::atomic::{Ordering, AtomicBool};
|
||||||
|
@ -456,8 +457,8 @@ impl ScriptThreadFactory for ScriptThread {
|
||||||
let mem_profiler_chan = state.mem_profiler_chan.clone();
|
let mem_profiler_chan = state.mem_profiler_chan.clone();
|
||||||
let window_size = state.window_size;
|
let window_size = state.window_size;
|
||||||
let script_thread = ScriptThread::new(state,
|
let script_thread = ScriptThread::new(state,
|
||||||
script_port,
|
script_port,
|
||||||
script_chan.clone());
|
script_chan.clone());
|
||||||
|
|
||||||
SCRIPT_THREAD_ROOT.with(|root| {
|
SCRIPT_THREAD_ROOT.with(|root| {
|
||||||
*root.borrow_mut() = Some(&script_thread as *const _);
|
*root.borrow_mut() = Some(&script_thread as *const _);
|
||||||
|
@ -531,7 +532,7 @@ impl ScriptThread {
|
||||||
port: Receiver<MainThreadScriptMsg>,
|
port: Receiver<MainThreadScriptMsg>,
|
||||||
chan: Sender<MainThreadScriptMsg>)
|
chan: Sender<MainThreadScriptMsg>)
|
||||||
-> ScriptThread {
|
-> ScriptThread {
|
||||||
let runtime = unsafe { new_rt_and_cx() };
|
let runtime = unsafe { new_rt_and_cx(ptr::null_mut()) };
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
JS_SetWrapObjectCallbacks(runtime.rt(),
|
JS_SetWrapObjectCallbacks(runtime.rt(),
|
||||||
|
|
2
components/servo/Cargo.lock
generated
2
components/servo/Cargo.lock
generated
|
@ -1069,7 +1069,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js"
|
name = "js"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
source = "git+https://github.com/servo/rust-mozjs#3352139c1dedbdff5fe1078152f46522cd04b2f3"
|
source = "git+https://github.com/servo/rust-mozjs#fae7efd0adf42c0dde517382b83734525e11c6cc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
2
ports/cef/Cargo.lock
generated
2
ports/cef/Cargo.lock
generated
|
@ -981,7 +981,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js"
|
name = "js"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
source = "git+https://github.com/servo/rust-mozjs#3352139c1dedbdff5fe1078152f46522cd04b2f3"
|
source = "git+https://github.com/servo/rust-mozjs#fae7efd0adf42c0dde517382b83734525e11c6cc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
2
ports/gonk/Cargo.lock
generated
2
ports/gonk/Cargo.lock
generated
|
@ -969,7 +969,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js"
|
name = "js"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
source = "git+https://github.com/servo/rust-mozjs#3352139c1dedbdff5fe1078152f46522cd04b2f3"
|
source = "git+https://github.com/servo/rust-mozjs#fae7efd0adf42c0dde517382b83734525e11c6cc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"heapsize 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue