1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is JavaScript structured data serialization.
17 * The Initial Developer of the Original Code is
18 * the Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
23 * Jason Orendorff <jorendorff@mozilla.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
45 #include "js/HashTable.h"
46 #include "js/Vector.h"
51 WriteStructuredClone(JSContext
*cx
, const Value
&v
, uint64_t **bufp
, size_t *nbytesp
,
52 const JSStructuredCloneCallbacks
*cb
, void *cbClosure
);
55 ReadStructuredClone(JSContext
*cx
, const uint64_t *data
, size_t nbytes
, Value
*vp
,
56 const JSStructuredCloneCallbacks
*cb
, void *cbClosure
);
60 explicit SCOutput(JSContext
*cx
);
62 JSContext
*context() const { return cx
; }
64 bool write(uint64_t u
);
65 bool writePair(uint32_t tag
, uint32_t data
);
66 bool writeDouble(double d
);
67 bool writeBytes(const void *p
, size_t nbytes
);
68 bool writeChars(const jschar
*p
, size_t nchars
);
71 bool writeArray(const T
*p
, size_t nbytes
);
73 bool extractBuffer(uint64_t **datap
, size_t *sizep
);
75 uint64_t count() { return buf
.length(); }
79 js::Vector
<uint64_t> buf
;
84 SCInput(JSContext
*cx
, const uint64_t *data
, size_t nbytes
);
86 JSContext
*context() const { return cx
; }
88 bool read(uint64_t *p
);
89 bool readPair(uint32_t *tagp
, uint32_t *datap
);
90 bool readDouble(double *p
);
91 bool readBytes(void *p
, size_t nbytes
);
92 bool readChars(jschar
*p
, size_t nchars
);
95 bool readArray(T
*p
, size_t nelems
);
100 void staticAssertions() {
101 JS_STATIC_ASSERT(sizeof(jschar
) == 2);
102 JS_STATIC_ASSERT(sizeof(uint32_t) == 4);
103 JS_STATIC_ASSERT(sizeof(double) == 8);
107 const uint64_t *point
;
113 struct JSStructuredCloneReader
{
115 explicit JSStructuredCloneReader(js::SCInput
&in
, const JSStructuredCloneCallbacks
*cb
,
117 : in(in
), objs(in
.context()), allObjs(in
.context()),
118 callbacks(cb
), closure(cbClosure
) { }
120 js::SCInput
&input() { return in
; }
121 bool read(js::Value
*vp
);
124 JSContext
*context() { return in
.context(); }
126 bool checkDouble(double d
);
127 JSString
*readString(uint32_t nchars
);
128 bool readTypedArray(uint32_t tag
, uint32_t nelems
, js::Value
*vp
);
129 bool readArrayBuffer(uint32_t nbytes
, js::Value
*vp
);
130 bool readId(jsid
*idp
);
131 bool startRead(js::Value
*vp
);
135 // Stack of objects with properties remaining to be read.
136 js::AutoValueVector objs
;
138 // Stack of all objects read during this deserialization
139 js::AutoValueVector allObjs
;
141 // The user defined callbacks that will be used for cloning.
142 const JSStructuredCloneCallbacks
*callbacks
;
144 // Any value passed to JS_ReadStructuredClone.
147 friend JSBool
JS_ReadTypedArray(JSStructuredCloneReader
*r
, jsval
*vp
);
150 struct JSStructuredCloneWriter
{
152 explicit JSStructuredCloneWriter(js::SCOutput
&out
, const JSStructuredCloneCallbacks
*cb
,
154 : out(out
), objs(out
.context()), counts(out
.context()), ids(out
.context()),
155 memory(out
.context()), callbacks(cb
), closure(cbClosure
) { }
157 bool init() { return memory
.init(); }
159 bool write(const js::Value
&v
);
161 js::SCOutput
&output() { return out
; }
164 JSContext
*context() { return out
.context(); }
166 bool writeString(uint32_t tag
, JSString
*str
);
167 bool writeId(jsid id
);
168 bool writeArrayBuffer(JSObject
*obj
);
169 bool writeTypedArray(JSObject
*obj
);
170 bool startObject(JSObject
*obj
);
171 bool startWrite(const js::Value
&v
);
173 inline void checkStack();
177 // Vector of objects with properties remaining to be written.
179 // NB: These can span multiple compartments, so the compartment must be
180 // entered before any manipulation is performed.
181 js::AutoValueVector objs
;
183 // counts[i] is the number of properties of objs[i] remaining to be written.
184 // counts.length() == objs.length() and sum(counts) == ids.length().
185 js::Vector
<size_t> counts
;
187 // Ids of properties remaining to be written.
188 js::AutoIdVector ids
;
190 // The "memory" list described in the HTML5 internal structured cloning algorithm.
191 // memory is a superset of objs; items are never removed from Memory
192 // until a serialization operation is finished
193 typedef js::HashMap
<JSObject
*, uint32_t> CloneMemory
;
196 // The user defined callbacks that will be used for cloning.
197 const JSStructuredCloneCallbacks
*callbacks
;
199 // Any value passed to JS_WriteStructuredClone.
202 friend JSBool
JS_WriteTypedArray(JSStructuredCloneWriter
*w
, jsval v
);
205 #endif /* jsclone_h___ */