aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bindings
diff options
context:
space:
mode:
authorbors-servo <metajack+bors@gmail.com>2015-01-10 09:18:45 -0700
committerbors-servo <metajack+bors@gmail.com>2015-01-10 09:18:45 -0700
commita07c3eadb33eca1fbb2209d168c63c5ec3db23df (patch)
tree59adff84613e3f1bc801f202ede840f6cf22798f /components/script/dom/bindings
parent1834359f1661fe504e5b8ff3ee2bc0cb8d744d21 (diff)
parent14ff55443fc872f0806a6267655689d48002a3b3 (diff)
downloadservo-a07c3eadb33eca1fbb2209d168c63c5ec3db23df.tar.gz
servo-a07c3eadb33eca1fbb2209d168c63c5ec3db23df.zip
auto merge of #4607 : Ms2ger/servo/clone, r=jdm
Diffstat (limited to 'components/script/dom/bindings')
-rw-r--r--components/script/dom/bindings/structuredclone.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/components/script/dom/bindings/structuredclone.rs b/components/script/dom/bindings/structuredclone.rs
new file mode 100644
index 00000000000..843b7bea30c
--- /dev/null
+++ b/components/script/dom/bindings/structuredclone.rs
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+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::jsapi::JS_ReadStructuredClone;
+use js::jsval::{JSVal, UndefinedValue};
+
+use libc::size_t;
+use std::ptr;
+
+#[allow(raw_pointer_deriving)]
+#[deriving(Copy)]
+pub struct StructuredCloneData {
+ pub data: *mut u64,
+ 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,
+ })
+ }
+
+ 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
+ }
+}