18 #include "precompiled.h"
37 return js::TypedArray::TYPE_INT8;
39 return js::TypedArray::TYPE_UINT8;
41 return js::TypedArray::TYPE_INT16;
43 return js::TypedArray::TYPE_UINT16;
45 return js::TypedArray::TYPE_INT32;
47 return js::TypedArray::TYPE_UINT32;
49 return js::TypedArray::TYPE_FLOAT32;
51 return js::TypedArray::TYPE_FLOAT64;
53 return js::TypedArray::TYPE_UINT8_CLAMPED;
60 m_ScriptInterface(scriptInterface), m_Stream(stream)
71 #if DEBUG_SERIALIZER_ANNOTATE
87 m_Stream.read((
char*)data, (std::streamsize)len);
107 if (numBytes > 64*
MiB)
135 std::pair<std::map<u32, JSObject*>::iterator,
bool> it =
m_ScriptBackrefs.insert(std::make_pair(tag, obj));
179 obj = JS_NewArrayObject(cx, length, NULL);
183 obj = JS_NewObject(cx, NULL, NULL, NULL);
187 std::wstring prototypeName;
188 String(
"proto name", prototypeName, 0, 256);
195 JSObject* parent = JS_GetParent(cx, proto);
196 if (!proto || !parent)
199 obj = JS_NewObject(cx, NULL, proto, parent);
206 JSBool hasCustomDeserialize, hasCustomSerialize;
207 if (!JS_HasProperty(cx, obj,
"Serialize", &hasCustomSerialize) || !JS_HasProperty(cx, obj,
"Deserialize", &hasCustomDeserialize))
210 if (hasCustomDeserialize)
213 if (!JS_LookupProperty(cx, obj,
"Serialize", &serialize))
215 bool hasNullSerialize = hasCustomSerialize && JSVAL_IS_NULL(serialize);
219 if (!hasNullSerialize)
226 return OBJECT_TO_JSVAL(obj);
239 for (
uint32_t i = 0; i < numProps; ++i)
247 if (!JS_SetUCProperty(cx, obj, (
const jschar*)propname.data(), propname.length(), &propval))
251 return OBJECT_TO_JSVAL(obj);
257 return STRING_TO_JSVAL(str);
262 NumberI32(
"value", value, JSVAL_INT_MIN, JSVAL_INT_MAX);
263 return INT_TO_JSVAL(value);
270 if (!JS_NewNumberValue(cx, value, &rval))
278 return BOOLEAN_TO_JSVAL(value ? JS_TRUE : JS_FALSE);
287 return OBJECT_TO_JSVAL(obj);
294 if (!JS_NewNumberValue(cx, value, &val))
299 if (!JS_GetClassObject(cx, JS_GetGlobalObject(cx), JSProto_Number, &ctorobj))
302 JSObject* obj = JS_New(cx, ctorobj, 1, &val);
306 return OBJECT_TO_JSVAL(obj);
314 jsval val = STRING_TO_JSVAL(str);
318 if (!JS_GetClassObject(cx, JS_GetGlobalObject(cx), JSProto_String, &ctorobj))
321 JSObject* obj = JS_New(cx, ctorobj, 1, &val);
325 return OBJECT_TO_JSVAL(obj);
330 Bool(
"value", value);
331 jsval val = BOOLEAN_TO_JSVAL(value ? JS_TRUE : JS_FALSE);
335 if (!JS_GetClassObject(cx, JS_GetGlobalObject(cx), JSProto_Boolean, &ctorobj))
338 JSObject* obj = JS_New(cx, ctorobj, 1, &val);
342 return OBJECT_TO_JSVAL(obj);
347 u32 byteOffset, length;
359 if (!JSVAL_IS_OBJECT(bufferVal))
362 JSObject* bufferObj = JSVAL_TO_OBJECT(bufferVal);
363 if (!js_IsArrayBuffer(bufferObj))
366 JSObject* arrayObj = js_CreateTypedArrayWithBuffer(cx,
GetJSArrayType(arrayType), bufferObj, byteOffset, length);
372 return OBJECT_TO_JSVAL(arrayObj);
379 u8* bufferData =
new u8[length];
380 RawBytes(
"buffer data", bufferData, length);
382 #if BYTE_ORDER != LITTLE_ENDIAN
383 #error TODO: need to convert JS ArrayBuffer data from little-endian
386 JSObject* bufferObj = js_CreateArrayBuffer(cx, length);
392 js::ArrayBuffer* buffer = js::ArrayBuffer::fromJSObject(bufferObj);
393 memcpy(buffer->data, bufferData, length);
396 return OBJECT_TO_JSVAL(bufferObj);
409 Get(name, (
u8*)str.data(), len*2);
417 #if BYTE_ORDER != LITTLE_ENDIAN
418 #error TODO: probably need to convert JS strings from little-endian
443 if (!JSVAL_IS_OBJECT(obj))
CStdDeserializer(ScriptInterface &scriptInterface, std::istream &stream)
virtual void Bool(const char *name, bool &out)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
void FreeScriptBackrefs()
virtual void NumberDouble_Unbounded(const char *name, double &out)
ScriptInterface & m_ScriptInterface
static void out(const wchar_t *fmt,...)
bool CallFunctionVoid(jsval val, const char *name)
Call the named property on the given object, with void return type and 0 arguments.
virtual JSObject * GetScriptBackref(u32 tag)
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
A trivial wrapper around a jsval.
virtual std::istream & GetStream()
Returns a stream which can be used to deserialize data directly.
JSObject * GetSerializablePrototype(const std::wstring &name)
virtual void NumberU8_Unbounded(const char *name, uint8_t &out)
virtual void ScriptVal(const char *name, jsval &out)
Deserialize a jsval, replacing 'out'.
virtual u32 ReserveScriptBackref()
virtual void RequireBytesInStream(size_t numBytes)
Throws an exception if the stream definitely cannot provide the required number of bytes...
static uint32 GetJSArrayType(u8 arrayType)
virtual void Get(const char *name, u8 *data, size_t len)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
virtual void RawBytes(const char *name, u8 *data, size_t len)
bool IsSerializablePrototype(const std::wstring &name)
std::map< u32, JSObject * > m_ScriptBackrefs
virtual void SetReservedScriptBackref(u32 tag, JSObject *obj)
std::basic_string< utf16_t, utf16_traits > utf16string
virtual void NumberU8(const char *name, uint8_t &out, uint8_t lower, uint8_t upper)
virtual void ScriptObjectAppend(const char *name, jsval &obj)
Deserialize an object jsval, appending properties to object 'obj'.
std::map< std::wstring, JSObject * > m_SerializablePrototypes
virtual void String(const char *name, std::wstring &out, uint32_t minlength, uint32_t maxlength)
void ReadStringUTF16(const char *name, utf16string &str)
Abstraction around a SpiderMonkey JSContext.
virtual ~CStdDeserializer()
jsval ReadScriptVal(const char *name, JSObject *appendParent)
virtual void SetSerializablePrototypes(std::map< std::wstring, JSObject * > &prototypes)
virtual void ScriptString(const char *name, JSString *&out)
Deserialize a JSString.
JSContext * GetContext() const
virtual void NumberI32(const char *name, int32_t &out, int32_t lower, int32_t upper)
virtual void AddScriptBackref(JSObject *obj)