shared_ptr<ScriptInterface::StructuredClone> ScriptInterface::WriteStructuredClone(jsval v) { uint64* data = NULL; size_t nbytes = 0; if (!JS_WriteStructuredClone(m->m_cx, v, &data, &nbytes, NULL, NULL)) return shared_ptr<StructuredClone>(); // TODO: should we have better error handling? // Currently we'll probably continue and then crash in ReadStructuredClone shared_ptr<StructuredClone> ret (new StructuredClone); ret->m_Context = m->m_cx; ret->m_Data = data; ret->m_Size = nbytes; return ret; }
nsresult nsStructuredCloneContainer::InitFromVariant(nsIVariant *aData, JSContext *aCx) { NS_ENSURE_STATE(!mData); NS_ENSURE_ARG_POINTER(aData); NS_ENSURE_ARG_POINTER(aCx); // First, try to extract a jsval from the variant |aData|. This works only // if the variant implements GetAsJSVal. jsval jsData; nsresult rv = aData->GetAsJSVal(&jsData); NS_ENSURE_SUCCESS(rv, NS_ERROR_UNEXPECTED); // Make sure that we serialize in the right context. JSAutoRequest ar(aCx); JSAutoEnterCompartment ac; NS_ENSURE_STATE(ac.enter(aCx, JS_GetGlobalObject(aCx))); nsCxPusher cxPusher; cxPusher.Push(aCx); uint64_t* jsBytes = nsnull; bool success = JS_WriteStructuredClone(aCx, jsData, &jsBytes, &mSize, nsnull, nsnull); NS_ENSURE_STATE(success); NS_ENSURE_STATE(jsBytes); // Copy jsBytes into our own buffer. mData = (PRUint64*) malloc(mSize); if (!mData) { mSize = 0; mVersion = 0; // FIXME This should really be js::Foreground::Free, but that's not public. JS_free(aCx, jsBytes); return NS_ERROR_FAILURE; } else { mVersion = JS_STRUCTURED_CLONE_VERSION; } memcpy(mData, jsBytes, mSize); // FIXME Similarly, this should be js::Foreground::free. JS_free(aCx, jsBytes); return NS_OK; }
shared_ptr<ScriptInterface::StructuredClone> ScriptInterface::WriteStructuredClone(JS::HandleValue v) { JSAutoRequest rq(m->m_cx); u64* data = NULL; size_t nbytes = 0; if (!JS_WriteStructuredClone(m->m_cx, v, &data, &nbytes, NULL, NULL, JS::UndefinedHandleValue)) { debug_warn(L"Writing a structured clone with JS_WriteStructuredClone failed!"); return shared_ptr<StructuredClone>(); } shared_ptr<StructuredClone> ret (new StructuredClone); ret->m_Data = data; ret->m_Size = nbytes; return ret; }
static EventType *createEvent(JSContext *cx, WorkerParent *recipient, Worker *child, jsval v) { uint64_t *data; size_t nbytes; if (!JS_WriteStructuredClone(cx, v, &data, &nbytes, NULL, NULL)) return NULL; EventType *event = new EventType; if (!event) { JS_ReportOutOfMemory(cx); return NULL; } event->recipient = recipient; event->child = child; event->data = data; event->nbytes = nbytes; return event; }