18 #include "precompiled.h"
34 case js::TypedArray::TYPE_INT8:
36 case js::TypedArray::TYPE_UINT8:
38 case js::TypedArray::TYPE_INT16:
40 case js::TypedArray::TYPE_UINT16:
42 case js::TypedArray::TYPE_INT32:
44 case js::TypedArray::TYPE_UINT32:
46 case js::TypedArray::TYPE_FLOAT32:
48 case js::TypedArray::TYPE_FLOAT64:
50 case js::TypedArray::TYPE_UINT8_CLAMPED:
53 LOGERROR(L
"Cannot serialize unrecognized typed array view: %d", arrayType);
59 m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_Rooter(m_ScriptInterface),
68 switch (JS_TypeOfValue(cx, val))
82 if (JSVAL_IS_NULL(val))
88 JSObject* obj = JSVAL_TO_OBJECT(val);
100 if (JS_IsArrayObject(cx, obj))
108 if (!JS_GetArrayLength(cx, obj, &length))
112 else if (js_IsTypedArray(obj))
116 js::TypedArray* typedArray = js::TypedArray::fromJSObject(obj);
127 else if (js_IsArrayBuffer(obj))
131 js::ArrayBuffer* arrayBuffer = js::ArrayBuffer::fromJSObject(obj);
133 #if BYTE_ORDER != LITTLE_ENDIAN
134 #error TODO: need to convert JS ArrayBuffer data to little-endian
137 u32 length = arrayBuffer->byteLength;
145 JSClass* jsclass = JS_GET_CLASS(cx, obj);
148 JSProtoKey protokey = JSCLASS_CACHED_PROTO_KEY(jsclass);
150 if (protokey == JSProto_Object)
153 JSObject* proto = JS_GetPrototype(cx, obj);
175 JSBool hasCustomSerialize;
176 if (!JS_HasProperty(cx, obj,
"Serialize", &hasCustomSerialize))
179 if (hasCustomSerialize)
182 if (!JS_LookupProperty(cx, obj,
"Serialize", &serialize))
186 if (!JSVAL_IS_NULL(serialize))
197 else if (protokey == JSProto_Number)
203 if (!JS_ValueToNumber(cx, val, &d))
208 else if (protokey == JSProto_String)
213 JSString* str = JS_ValueToString(cx, val);
219 else if (protokey == JSProto_Boolean)
225 if (!JS_ValueToBoolean(cx, val, &b))
233 LOGERROR(L
"Cannot serialise JS objects with unrecognized class '%hs'", jsclass->name);
249 for (
size_t i = 0; i < ida.
length(); ++i)
253 jsval idval, propval;
256 if (!JS_IdToValue(cx,
id, &idval))
258 JSString* idstr = JS_ValueToString(cx, idval);
266 if (!JS_LookupPropertyById(cx, obj,
id, &propval))
274 case JSTYPE_FUNCTION:
277 std::wstring funcname(L
"(unnamed)");
278 JSFunction* func = JS_ValueToFunction(cx, val);
281 JSString*
string = JS_GetFunctionId(func);
285 const jschar* ch = JS_GetStringCharsAndLength(cx,
string, &length);
286 if (ch && length > 0)
287 funcname = std::wstring(ch, ch + length);
291 LOGERROR(L
"Cannot serialise JS objects of type 'function': %ls", funcname.c_str());
303 if (JSVAL_IS_INT(val))
310 ENSURE(JSVAL_IS_DOUBLE(val));
313 jsdouble d = JSVAL_TO_DOUBLE(val);
315 if (JSDOUBLE_IS_INT32(d, &i))
332 JSBool b = JSVAL_TO_BOOLEAN(val);
338 LOGERROR(L
"Cannot serialise JS objects of type 'xml'");
353 const jschar* chars = JS_GetStringCharsAndLength(cx,
string, &length);
358 #if BYTE_ORDER != LITTLE_ENDIAN
359 #error TODO: probably need to convert JS strings to little-endian
381 return it.first->second;
ScriptInterface & m_ScriptInterface
Serialization interface; see serialization overview.
void NumberU8_Unbounded(const char *name, uint8_t value)
Serialize a number.
ISerializer & m_Serializer
std::map< JSObject *, u32, std::less< JSObject * >, ScriptBackrefsAlloc > backrefs_t
void ScriptString(const char *name, JSString *string)
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
static u8 GetArrayType(uint32 arrayType)
void SetSerializablePrototypes(std::map< JSObject *, std::wstring > &prototypes)
void Bool(const char *name, bool value)
Serialize a boolean.
std::map< JSObject *, std::wstring > m_SerializablePrototypes
CBinarySerializerScriptImpl(ScriptInterface &scriptInterface, ISerializer &serializer)
void NumberU32_Unbounded(const char *name, uint32_t value)
Serialize a number.
void String(const char *name, const std::wstring &value, uint32_t minlength, uint32_t maxlength)
Serialize a Unicode string.
void RawBytes(const char *name, const u8 *data, size_t len)
Serialize a stream of bytes.
bool CallFunction(jsval val, const char *name, R &ret)
Call the named property on the given object, with return type R and 0 arguments.
void NumberDouble_Unbounded(const char *name, double value)
Serialize a number.
fully STL-compatible allocator that simply draws upon another Allocator.
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Abstraction around a SpiderMonkey JSContext.
void NumberI32_Unbounded(const char *name, int32_t value)
Serialize a number.
#define debug_warn(expr)
display the error dialog with the given text.
backrefs_t m_ScriptBackrefs
std::wstring GetPrototypeName(JSObject *prototype)
void HandleScriptVal(jsval val)
JSContext * GetContext() const
u32 GetScriptBackrefTag(JSObject *obj)
RAII wrapper for JSIdArray*.
bool IsSerializablePrototype(JSObject *prototype)