Store the JSContext in a field on the worker global scope.

This commit is contained in:
Ms2ger 2014-07-15 20:02:30 +02:00
parent b11440750b
commit 3074b4747a
4 changed files with 32 additions and 14 deletions

View file

@ -35,7 +35,7 @@ impl<'a> GlobalRef<'a> {
pub fn get_cx(&self) -> *mut JSContext { pub fn get_cx(&self) -> *mut JSContext {
match *self { match *self {
Window(ref window) => window.get_cx(), Window(ref window) => window.get_cx(),
Worker(_) => fail!("NYI"), Worker(ref worker) => worker.get_cx(),
} }
} }

View file

@ -12,7 +12,6 @@ use dom::workerglobalscope::DedicatedGlobalScope;
use dom::workerglobalscope::WorkerGlobalScope; use dom::workerglobalscope::WorkerGlobalScope;
use script_task::ScriptTask; use script_task::ScriptTask;
use js::jsapi::JSContext;
use js::rust::Cx; use js::rust::Cx;
use std::rc::Rc; use std::rc::Rc;
@ -23,21 +22,24 @@ pub struct DedicatedWorkerGlobalScope {
} }
impl DedicatedWorkerGlobalScope { impl DedicatedWorkerGlobalScope {
pub fn new_inherited() -> DedicatedWorkerGlobalScope { pub fn new_inherited(cx: Rc<Cx>) -> DedicatedWorkerGlobalScope {
DedicatedWorkerGlobalScope { DedicatedWorkerGlobalScope {
workerglobalscope: WorkerGlobalScope::new_inherited(DedicatedGlobalScope), workerglobalscope: WorkerGlobalScope::new_inherited(DedicatedGlobalScope, cx),
} }
} }
pub fn new(cx: *mut JSContext) -> Temporary<DedicatedWorkerGlobalScope> { pub fn new(cx: Rc<Cx>) -> Temporary<DedicatedWorkerGlobalScope> {
let scope = box DedicatedWorkerGlobalScope::new_inherited(); let scope = box DedicatedWorkerGlobalScope::new_inherited(cx.clone());
DedicatedWorkerGlobalScopeBinding::Wrap(cx, scope) DedicatedWorkerGlobalScopeBinding::Wrap(cx.ptr, scope)
} }
pub fn init() -> (Rc<Cx>, Temporary<DedicatedWorkerGlobalScope>) { pub fn init() -> Temporary<DedicatedWorkerGlobalScope> {
let (_js_runtime, js_context) = ScriptTask::new_rt_and_cx(); let (_js_runtime, js_context) = ScriptTask::new_rt_and_cx();
let global = DedicatedWorkerGlobalScope::new(js_context.ptr); DedicatedWorkerGlobalScope::new(js_context.clone())
(js_context, global) }
pub fn get_rust_cx<'a>(&'a self) -> &'a Rc<Cx> {
self.workerglobalscope.get_rust_cx()
} }
} }

View file

@ -45,9 +45,9 @@ impl Worker {
} }
}; };
let (cx, global) = DedicatedWorkerGlobalScope::init(); let global = DedicatedWorkerGlobalScope::init().root();
let global = global.root(); match global.get_rust_cx().evaluate_script(
match cx.evaluate_script(global.reflector().get_jsobject(), source, filename.to_str(), 1) { global.reflector().get_jsobject(), source, filename.to_str(), 1) {
Ok(_) => (), Ok(_) => (),
Err(_) => println!("evaluate_script failed") Err(_) => println!("evaluate_script failed")
} }

View file

@ -2,9 +2,15 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::trace::Untraceable;
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::utils::{Reflectable, Reflector};
use dom::eventtarget::{EventTarget, WorkerGlobalScopeTypeId}; use dom::eventtarget::{EventTarget, WorkerGlobalScopeTypeId};
use js::jsapi::JSContext;
use js::rust::Cx;
use std::rc::Rc;
#[deriving(PartialEq,Encodable)] #[deriving(PartialEq,Encodable)]
pub enum WorkerGlobalScopeId { pub enum WorkerGlobalScopeId {
DedicatedGlobalScope, DedicatedGlobalScope,
@ -13,14 +19,24 @@ pub enum WorkerGlobalScopeId {
#[deriving(Encodable)] #[deriving(Encodable)]
pub struct WorkerGlobalScope { pub struct WorkerGlobalScope {
pub eventtarget: EventTarget, pub eventtarget: EventTarget,
js_context: Untraceable<Rc<Cx>>,
} }
impl WorkerGlobalScope { impl WorkerGlobalScope {
pub fn new_inherited(type_id: WorkerGlobalScopeId) -> WorkerGlobalScope { pub fn new_inherited(type_id: WorkerGlobalScopeId,
cx: Rc<Cx>) -> WorkerGlobalScope {
WorkerGlobalScope { WorkerGlobalScope {
eventtarget: EventTarget::new_inherited(WorkerGlobalScopeTypeId(type_id)), eventtarget: EventTarget::new_inherited(WorkerGlobalScopeTypeId(type_id)),
js_context: Untraceable::new(cx),
} }
} }
pub fn get_rust_cx<'a>(&'a self) -> &'a Rc<Cx> {
&*self.js_context
}
pub fn get_cx(&self) -> *mut JSContext {
self.js_context.ptr
}
} }
pub trait WorkerGlobalScopeMethods { pub trait WorkerGlobalScopeMethods {