Implement StructuredCloneData::read.

This commit is contained in:
Ms2ger 2015-01-10 16:30:27 +01:00
parent 5978b21abf
commit 14ff55443f
3 changed files with 21 additions and 28 deletions

View file

@ -4,10 +4,13 @@
use dom::bindings::error::Fallible;
use dom::bindings::error::Error::DataClone;
use dom::bindings::global::GlobalRef;
use js::glue::JS_STRUCTURED_CLONE_VERSION;
use js::jsapi::JSContext;
use js::jsapi::{JS_WriteStructuredClone, JS_ClearPendingException};
use js::jsval::JSVal;
use js::jsapi::JS_ReadStructuredClone;
use js::jsval::{JSVal, UndefinedValue};
use libc::size_t;
use std::ptr;
@ -37,4 +40,15 @@ impl StructuredCloneData {
nbytes: nbytes,
})
}
pub fn read(self, global: GlobalRef) -> JSVal {
let mut message = UndefinedValue();
unsafe {
assert!(JS_ReadStructuredClone(
global.get_cx(), self.data as *const u64, self.nbytes,
JS_STRUCTURED_CLONE_VERSION, &mut message,
ptr::null(), ptr::null_mut()) != 0);
}
message
}
}

View file

@ -27,13 +27,11 @@ use servo_util::task::spawn_named;
use servo_util::task_state;
use servo_util::task_state::{SCRIPT, IN_WORKER};
use js::glue::JS_STRUCTURED_CLONE_VERSION;
use js::jsapi::{JSContext, JS_ReadStructuredClone};
use js::jsval::{JSVal, UndefinedValue};
use js::jsapi::JSContext;
use js::jsval::JSVal;
use js::rust::Cx;
use std::rc::Rc;
use std::ptr;
use url::Url;
/// A ScriptChan that can be cloned freely and will silently send a TrustedWorkerAddress with
@ -197,20 +195,12 @@ trait PrivateDedicatedWorkerGlobalScopeHelpers {
}
impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for JSRef<'a, DedicatedWorkerGlobalScope> {
#[allow(unsafe_blocks)]
fn handle_event(self, msg: ScriptMsg) {
match msg {
ScriptMsg::DOMMessage(data) => {
let mut message = UndefinedValue();
let scope: JSRef<WorkerGlobalScope> = WorkerGlobalScopeCast::from_ref(self);
unsafe {
assert!(JS_ReadStructuredClone(
scope.get_cx(), data.data as *const u64, data.nbytes,
JS_STRUCTURED_CLONE_VERSION, &mut message,
ptr::null(), ptr::null_mut()) != 0);
}
let target: JSRef<EventTarget> = EventTargetCast::from_ref(self);
let message = data.read(GlobalRef::Worker(scope));
MessageEvent::dispatch_jsval(target, GlobalRef::Worker(scope), message);
},
ScriptMsg::RunnableMsg(runnable) => {

View file

@ -21,14 +21,11 @@ use script_task::{ScriptChan, ScriptMsg, Runnable};
use servo_util::str::DOMString;
use js::glue::JS_STRUCTURED_CLONE_VERSION;
use js::jsapi::JSContext;
use js::jsapi::JS_ReadStructuredClone;
use js::jsval::{JSVal, UndefinedValue};
use js::jsval::JSVal;
use url::UrlParser;
use std::cell::Cell;
use std::ptr;
pub type TrustedWorkerAddress = Trusted<Worker>;
@ -80,22 +77,14 @@ impl Worker {
Ok(Temporary::from_rooted(worker.r()))
}
#[allow(unsafe_blocks)]
pub fn handle_message(address: TrustedWorkerAddress,
data: StructuredCloneData) {
let worker = address.to_temporary().root();
let global = worker.r().global.root();
let mut message = UndefinedValue();
unsafe {
assert!(JS_ReadStructuredClone(
global.r().get_cx(), data.data as *const u64, data.nbytes,
JS_STRUCTURED_CLONE_VERSION, &mut message,
ptr::null(), ptr::null_mut()) != 0);
}
let target: JSRef<EventTarget> = EventTargetCast::from_ref(worker.r());
let message = data.read(global.r());
MessageEvent::dispatch_jsval(target, global.r(), message);
}
}