mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
Implement StructuredCloneData::write.
This commit is contained in:
parent
57aaa60fa5
commit
5978b21abf
3 changed files with 33 additions and 36 deletions
|
@ -2,7 +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::error::Fallible;
|
||||||
|
use dom::bindings::error::Error::DataClone;
|
||||||
|
|
||||||
|
use js::jsapi::JSContext;
|
||||||
|
use js::jsapi::{JS_WriteStructuredClone, JS_ClearPendingException};
|
||||||
|
use js::jsval::JSVal;
|
||||||
|
|
||||||
use libc::size_t;
|
use libc::size_t;
|
||||||
|
use std::ptr;
|
||||||
|
|
||||||
#[allow(raw_pointer_deriving)]
|
#[allow(raw_pointer_deriving)]
|
||||||
#[deriving(Copy)]
|
#[deriving(Copy)]
|
||||||
|
@ -10,3 +18,23 @@ pub struct StructuredCloneData {
|
||||||
pub data: *mut u64,
|
pub data: *mut u64,
|
||||||
pub nbytes: size_t,
|
pub nbytes: size_t,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl StructuredCloneData {
|
||||||
|
pub fn write(cx: *mut JSContext, message: JSVal)
|
||||||
|
-> Fallible<StructuredCloneData> {
|
||||||
|
let mut data = ptr::null_mut();
|
||||||
|
let mut nbytes = 0;
|
||||||
|
let result = unsafe {
|
||||||
|
JS_WriteStructuredClone(cx, message, &mut data, &mut nbytes,
|
||||||
|
ptr::null(), ptr::null_mut())
|
||||||
|
};
|
||||||
|
if result == 0 {
|
||||||
|
unsafe { JS_ClearPendingException(cx); }
|
||||||
|
return Err(DataClone);
|
||||||
|
}
|
||||||
|
Ok(StructuredCloneData {
|
||||||
|
data: data,
|
||||||
|
nbytes: nbytes,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,6 @@ use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
||||||
use dom::bindings::codegen::InheritTypes::DedicatedWorkerGlobalScopeDerived;
|
use dom::bindings::codegen::InheritTypes::DedicatedWorkerGlobalScopeDerived;
|
||||||
use dom::bindings::codegen::InheritTypes::{EventTargetCast, WorkerGlobalScopeCast};
|
use dom::bindings::codegen::InheritTypes::{EventTargetCast, WorkerGlobalScopeCast};
|
||||||
use dom::bindings::error::ErrorResult;
|
use dom::bindings::error::ErrorResult;
|
||||||
use dom::bindings::error::Error::DataClone;
|
|
||||||
use dom::bindings::global::GlobalRef;
|
use dom::bindings::global::GlobalRef;
|
||||||
use dom::bindings::js::{JSRef, Temporary, RootCollection};
|
use dom::bindings::js::{JSRef, Temporary, RootCollection};
|
||||||
use dom::bindings::refcounted::LiveDOMReferences;
|
use dom::bindings::refcounted::LiveDOMReferences;
|
||||||
|
@ -29,7 +28,7 @@ use servo_util::task_state;
|
||||||
use servo_util::task_state::{SCRIPT, IN_WORKER};
|
use servo_util::task_state::{SCRIPT, IN_WORKER};
|
||||||
|
|
||||||
use js::glue::JS_STRUCTURED_CLONE_VERSION;
|
use js::glue::JS_STRUCTURED_CLONE_VERSION;
|
||||||
use js::jsapi::{JSContext, JS_ReadStructuredClone, JS_WriteStructuredClone, JS_ClearPendingException};
|
use js::jsapi::{JSContext, JS_ReadStructuredClone};
|
||||||
use js::jsval::{JSVal, UndefinedValue};
|
use js::jsval::{JSVal, UndefinedValue};
|
||||||
use js::rust::Cx;
|
use js::rust::Cx;
|
||||||
|
|
||||||
|
@ -231,23 +230,8 @@ impl<'a> PrivateDedicatedWorkerGlobalScopeHelpers for JSRef<'a, DedicatedWorkerG
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> DedicatedWorkerGlobalScopeMethods for JSRef<'a, DedicatedWorkerGlobalScope> {
|
impl<'a> DedicatedWorkerGlobalScopeMethods for JSRef<'a, DedicatedWorkerGlobalScope> {
|
||||||
#[allow(unsafe_blocks)]
|
|
||||||
fn PostMessage(self, cx: *mut JSContext, message: JSVal) -> ErrorResult {
|
fn PostMessage(self, cx: *mut JSContext, message: JSVal) -> ErrorResult {
|
||||||
let mut data = ptr::null_mut();
|
let data = try!(StructuredCloneData::write(cx, message));
|
||||||
let mut nbytes = 0;
|
|
||||||
let result = unsafe {
|
|
||||||
JS_WriteStructuredClone(cx, message, &mut data, &mut nbytes,
|
|
||||||
ptr::null(), ptr::null_mut())
|
|
||||||
};
|
|
||||||
if result == 0 {
|
|
||||||
unsafe { JS_ClearPendingException(cx); }
|
|
||||||
return Err(DataClone);
|
|
||||||
}
|
|
||||||
let data = StructuredCloneData {
|
|
||||||
data: data,
|
|
||||||
nbytes: nbytes,
|
|
||||||
};
|
|
||||||
|
|
||||||
let worker = self.worker.borrow().as_ref().unwrap().clone();
|
let worker = self.worker.borrow().as_ref().unwrap().clone();
|
||||||
self.parent_sender.send(ScriptMsg::RunnableMsg(
|
self.parent_sender.send(ScriptMsg::RunnableMsg(
|
||||||
box WorkerMessageHandler::new(worker, data)));
|
box WorkerMessageHandler::new(worker, data)));
|
||||||
|
|
|
@ -7,7 +7,7 @@ use dom::bindings::codegen::Bindings::WorkerBinding::WorkerMethods;
|
||||||
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
||||||
use dom::bindings::codegen::InheritTypes::EventTargetCast;
|
use dom::bindings::codegen::InheritTypes::EventTargetCast;
|
||||||
use dom::bindings::error::{Fallible, ErrorResult};
|
use dom::bindings::error::{Fallible, ErrorResult};
|
||||||
use dom::bindings::error::Error::{Syntax, DataClone};
|
use dom::bindings::error::Error::Syntax;
|
||||||
use dom::bindings::global::{GlobalRef, GlobalField};
|
use dom::bindings::global::{GlobalRef, GlobalField};
|
||||||
use dom::bindings::js::{JSRef, Temporary};
|
use dom::bindings::js::{JSRef, Temporary};
|
||||||
use dom::bindings::refcounted::Trusted;
|
use dom::bindings::refcounted::Trusted;
|
||||||
|
@ -23,7 +23,7 @@ use servo_util::str::DOMString;
|
||||||
|
|
||||||
use js::glue::JS_STRUCTURED_CLONE_VERSION;
|
use js::glue::JS_STRUCTURED_CLONE_VERSION;
|
||||||
use js::jsapi::JSContext;
|
use js::jsapi::JSContext;
|
||||||
use js::jsapi::{JS_ReadStructuredClone, JS_WriteStructuredClone, JS_ClearPendingException};
|
use js::jsapi::JS_ReadStructuredClone;
|
||||||
use js::jsval::{JSVal, UndefinedValue};
|
use js::jsval::{JSVal, UndefinedValue};
|
||||||
use url::UrlParser;
|
use url::UrlParser;
|
||||||
|
|
||||||
|
@ -101,23 +101,8 @@ impl Worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> WorkerMethods for JSRef<'a, Worker> {
|
impl<'a> WorkerMethods for JSRef<'a, Worker> {
|
||||||
#[allow(unsafe_blocks)]
|
|
||||||
fn PostMessage(self, cx: *mut JSContext, message: JSVal) -> ErrorResult {
|
fn PostMessage(self, cx: *mut JSContext, message: JSVal) -> ErrorResult {
|
||||||
let mut data = ptr::null_mut();
|
let data = try!(StructuredCloneData::write(cx, message));
|
||||||
let mut nbytes = 0;
|
|
||||||
let result = unsafe {
|
|
||||||
JS_WriteStructuredClone(cx, message, &mut data, &mut nbytes,
|
|
||||||
ptr::null(), ptr::null_mut())
|
|
||||||
};
|
|
||||||
if result == 0 {
|
|
||||||
unsafe { JS_ClearPendingException(cx); }
|
|
||||||
return Err(DataClone);
|
|
||||||
}
|
|
||||||
let data = StructuredCloneData {
|
|
||||||
data: data,
|
|
||||||
nbytes: nbytes,
|
|
||||||
};
|
|
||||||
|
|
||||||
let address = Trusted::new(cx, self, self.global.root().r().script_chan().clone());
|
let address = Trusted::new(cx, self, self.global.root().r().script_chan().clone());
|
||||||
self.sender.send((address, ScriptMsg::DOMMessage(data)));
|
self.sender.send((address, ScriptMsg::DOMMessage(data)));
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue