Bug 743615 - Add the JS_{Read,Write}StructuredClone api. r=jorendorff
authorBobby Holley <bobbyholley@gmail.com>
Tue, 24 Apr 2012 12:58:07 +0200
changeset 95506 270848da27e44971abe90b5ad1f2a7562fdae479
parent 95505 1b2dbf32886bba885d4fde24fe1fff5079bf9ed2
child 95507 ced1395a8ad05ff1db9052daaf83d59930f9f876
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs743615
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 743615 - Add the JS_{Read,Write}StructuredClone api. r=jorendorff
js/src/jsapi.h
js/src/jsclone.cpp
js/src/jsclone.h
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -5179,21 +5179,27 @@ JS_SetStructuredCloneCallbacks(JSRuntime
 
 JS_PUBLIC_API(JSBool)
 JS_ReadUint32Pair(JSStructuredCloneReader *r, uint32_t *p1, uint32_t *p2);
 
 JS_PUBLIC_API(JSBool)
 JS_ReadBytes(JSStructuredCloneReader *r, void *p, size_t len);
 
 JS_PUBLIC_API(JSBool)
+JS_ReadTypedArray(JSStructuredCloneReader *r, jsval *vp);
+
+JS_PUBLIC_API(JSBool)
 JS_WriteUint32Pair(JSStructuredCloneWriter *w, uint32_t tag, uint32_t data);
 
 JS_PUBLIC_API(JSBool)
 JS_WriteBytes(JSStructuredCloneWriter *w, const void *p, size_t len);
 
+JS_PUBLIC_API(JSBool)
+JS_WriteTypedArray(JSStructuredCloneWriter *w, jsval v);
+
 /************************************************************************/
 
 /*
  * Locale specific string conversion and error message callbacks.
  */
 struct JSLocaleCallbacks {
     JSLocaleToUpperCase     localeToUpperCase;
     JSLocaleToLowerCase     localeToLowerCase;
--- a/js/src/jsclone.cpp
+++ b/js/src/jsclone.cpp
@@ -419,16 +419,23 @@ JSStructuredCloneWriter::checkStack()
         JS_ASSERT(total <= ids.length());
 
     size_t j = objs.length();
     for (size_t i = 0; i < limit; i++)
         JS_ASSERT(memory.has(&objs[--j].toObject()));
 #endif
 }
 
+JS_PUBLIC_API(JSBool)
+JS_WriteTypedArray(JSStructuredCloneWriter *w, jsval v)
+{
+    JS_ASSERT(v.isObject());
+    return w->writeTypedArray(&v.toObject());
+}
+
 bool
 JSStructuredCloneWriter::writeTypedArray(JSObject *obj)
 {
     JSObject *arr = TypedArray::getTypedArray(obj);
     if (!out.writePair(ArrayTypeToTag(TypedArray::getType(arr)), TypedArray::getLength(arr)))
         return false;
 
     switch (TypedArray::getType(arr)) {
@@ -684,16 +691,26 @@ JSStructuredCloneReader::readString(uint
     if (!chars.allocate(nchars) || !in.readChars(chars.get(), nchars))
         return NULL;
     JSString *str = js_NewString(context(), chars.get(), nchars);
     if (str)
         chars.forget();
     return str;
 }
 
+JS_PUBLIC_API(JSBool)
+JS_ReadTypedArray(JSStructuredCloneReader *r, jsval *vp)
+{
+    uint32_t tag, nelems;
+    if (!r->input().readPair(&tag, &nelems))
+        return false;
+    JS_ASSERT(tag >= SCTAG_TYPED_ARRAY_MIN && tag <= SCTAG_TYPED_ARRAY_MAX);
+    return r->readTypedArray(tag, nelems, vp);
+}
+
 bool
 JSStructuredCloneReader::readTypedArray(uint32_t tag, uint32_t nelems, Value *vp)
 {
     JSObject *obj = NULL;
 
     switch (tag) {
       case SCTAG_TYPED_ARRAY_INT8:
         obj = JS_NewInt8Array(context(), nelems);
--- a/js/src/jsclone.h
+++ b/js/src/jsclone.h
@@ -138,16 +138,18 @@ struct JSStructuredCloneReader {
     // Stack of all objects read during this deserialization
     js::AutoValueVector allObjs;
 
     // The user defined callbacks that will be used for cloning.
     const JSStructuredCloneCallbacks *callbacks;
 
     // Any value passed to JS_ReadStructuredClone.
     void *closure;
+
+    friend JSBool JS_ReadTypedArray(JSStructuredCloneReader *r, jsval *vp);
 };
 
 struct JSStructuredCloneWriter {
   public:
     explicit JSStructuredCloneWriter(js::SCOutput &out, const JSStructuredCloneCallbacks *cb,
                                      void *cbClosure)
         : out(out), objs(out.context()), counts(out.context()), ids(out.context()),
           memory(out.context()), callbacks(cb), closure(cbClosure) { }
@@ -191,11 +193,13 @@ struct JSStructuredCloneWriter {
     typedef js::HashMap<JSObject *, uint32_t> CloneMemory;
     CloneMemory memory;
 
     // The user defined callbacks that will be used for cloning.
     const JSStructuredCloneCallbacks *callbacks;
 
     // Any value passed to JS_WriteStructuredClone.
     void *closure;
+
+    friend JSBool JS_WriteTypedArray(JSStructuredCloneWriter *w, jsval v);
 };
 
 #endif /* jsclone_h___ */