2021-03-28 09:48:25 -07:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2010-01-09 11:20:14 -08:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "BinarySerializer.h"
|
|
|
|
|
|
2011-04-30 06:22:46 -07:00
|
|
|
#include "lib/alignment.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2016-08-02 09:12:11 -07:00
|
|
|
#include "scriptinterface/ScriptExtraHeaders.h"
|
2014-03-28 13:26:32 -07:00
|
|
|
#include "SerializedScriptTypes.h"
|
2013-05-26 14:57:24 -07:00
|
|
|
|
2016-09-02 09:36:40 -07:00
|
|
|
static u8 GetArrayType(js::Scalar::Type arrayType)
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
switch(arrayType)
|
|
|
|
|
{
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Int8:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT8;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Uint8:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT8;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Int16:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT16;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Uint16:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT16;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Int32:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_INT32;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Uint32:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT32;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Float32:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_FLOAT32;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Float64:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_FLOAT64;
|
2016-09-02 09:36:40 -07:00
|
|
|
case js::Scalar::Uint8Clamped:
|
2013-05-26 14:57:24 -07:00
|
|
|
return SCRIPT_TYPED_ARRAY_UINT8_CLAMPED;
|
|
|
|
|
default:
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Cannot serialize unrecognized typed array view: %d", arrayType);
|
2013-05-26 14:57:24 -07:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-25 11:44:33 -07:00
|
|
|
|
2017-08-23 17:32:42 -07:00
|
|
|
CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(const ScriptInterface& scriptInterface, ISerializer& serializer) :
|
Use Symbols to store JS object references when serialising and delete ObjectIDCache
When serialising JS objects, we keep track of any encountered object,
and serialize it only once. Any further serialisation instead stores an
ID referring to the original object (essentially an opaque pointer).
The trouble of course is to have a unique, persistent identifier for
such an object.
svn uses an ObjectIDCache, essentially a "JS Object -> ID" map (which
internally is essentially a "JS heap pointer -> ID" map).
JS, since ES15, includes a "Symbol" primitive type, which is a unique,
immutable identifier. They are also not iterable by for..in or
GetOwnPropertyName or related.
This means they can be used to store the tag directly on the object
(since it's impossible overwrite a user property).
Thanks to this, we can forgo ObjectIDCache in the serializers, and since
following D2897 it becomes unused, we can delete it, along with the
Finalization code it used.
Part of SM52 migration, stage: SM45-compatible changes.
Patch by: Itms
Tested By: Freagarach
Refs #4893
Differential Revision: https://code.wildfiregames.com/D3085
This was SVN commit r24167.
2020-11-11 22:40:19 -08:00
|
|
|
m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_ScriptBackrefsNext(0)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
Use Symbols to store JS object references when serialising and delete ObjectIDCache
When serialising JS objects, we keep track of any encountered object,
and serialize it only once. Any further serialisation instead stores an
ID referring to the original object (essentially an opaque pointer).
The trouble of course is to have a unique, persistent identifier for
such an object.
svn uses an ObjectIDCache, essentially a "JS Object -> ID" map (which
internally is essentially a "JS heap pointer -> ID" map).
JS, since ES15, includes a "Symbol" primitive type, which is a unique,
immutable identifier. They are also not iterable by for..in or
GetOwnPropertyName or related.
This means they can be used to store the tag directly on the object
(since it's impossible overwrite a user property).
Thanks to this, we can forgo ObjectIDCache in the serializers, and since
following D2897 it becomes unused, we can delete it, along with the
Finalization code it used.
Part of SM52 migration, stage: SM45-compatible changes.
Patch by: Itms
Tested By: Freagarach
Refs #4893
Differential Revision: https://code.wildfiregames.com/D3085
This was SVN commit r24167.
2020-11-11 22:40:19 -08:00
|
|
|
|
2021-01-12 10:43:45 -08:00
|
|
|
JS_AddExtraGCRootsTracer(m_ScriptInterface.GetGeneralJSContext(), Trace, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CBinarySerializerScriptImpl::~CBinarySerializerScriptImpl()
|
|
|
|
|
{
|
|
|
|
|
JS_RemoveExtraGCRootsTracer(m_ScriptInterface.GetGeneralJSContext(), Trace, this);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-31 12:18:40 -07:00
|
|
|
void CBinarySerializerScriptImpl::HandleScriptVal(JS::HandleValue val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
switch (JS_TypeOfValue(rq.cx, val))
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2020-11-24 07:47:03 -08:00
|
|
|
case JSTYPE_UNDEFINED:
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_VOID);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_NULL: // This type is never actually returned (it's a JS2 feature)
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_OBJECT:
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
if (val.isNull())
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedObject obj(rq.cx, &val.toObject());
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// If we've already serialized this object, just output a reference to it
|
2021-01-12 10:43:45 -08:00
|
|
|
u32 tag = GetScriptBackrefTag(obj);
|
|
|
|
|
if (tag != 0)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BACKREF);
|
2021-01-12 10:43:45 -08:00
|
|
|
m_Serializer.NumberU32("tag", tag, 0, JSVAL_INT_MAX);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 10:15:47 -08:00
|
|
|
// Arrays, Maps and Sets are special cases of Objects
|
Upgrade SpiderMonkey to version 45.0.2, refs #4893.
- Various build changes, in particular NSPR is not needed on Unix
anymore
- Add js/Initialization.h to source/scriptinterface/ScriptEngine.h
- Use nullptr instead of JS::NullPtr(), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1164602
- Remove `JS::RuntimeOptionsRef.varObjFix`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1171177
- Remove uses of `AutoIdArray`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1191529
- `JS_InternUCStringN` has been renamed, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1178581
- `JS::Evaluate` now takes scope chains explicitly, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1097987
- Array functions (such as `JS_IsArrayObject`) are fallible and output
to params, see https://bugzilla.mozilla.org/show_bug.cgi?id=f3d35d8
- Remove `JSCLASS_CACHED_PROTO_WIDTH` workaround in our code, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1236373
- Remove compile'n go (`setCompileAndGo`) and replace it by
`setIsRunOnce` which will become the default in the future, see
https://bugzilla.mozilla.org/show_bug.cgi?id=679939
- Mark shared memory in direct access operations
(`JS_GetUint16ArrayData` and `JS_GetUint8ArrayData`), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1176214
- Use new `JS::ObjectOpResult`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1113369
Thanks to wraitii, elexis, Krinkle and historic_bruno for contributions
and comments, and to gentz, madpilot, s0600204 and Stan for testing and
indirect contributions.
Differential Revision: https://code.wildfiregames.com/D1510
This was SVN commit r22627.
2019-08-07 15:37:43 -07:00
|
|
|
bool isArray;
|
2020-11-26 10:15:47 -08:00
|
|
|
bool isMap;
|
|
|
|
|
bool isSet;
|
|
|
|
|
|
2020-12-06 06:03:02 -08:00
|
|
|
if (JS::IsArrayObject(rq.cx, obj, &isArray) && isArray)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY);
|
2010-01-09 11:20:14 -08:00
|
|
|
// TODO: probably should have a more efficient storage format
|
2011-10-27 13:56:32 -07:00
|
|
|
|
|
|
|
|
// Arrays like [1, 2, ] have an 'undefined' at the end which is part of the
|
|
|
|
|
// length but seemingly isn't enumerated, so store the length explicitly
|
2014-03-28 13:26:32 -07:00
|
|
|
uint length = 0;
|
2020-12-06 06:03:02 -08:00
|
|
|
if (!JS::GetArrayLength(rq.cx, obj, &length))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::GetArrayLength failed");
|
2011-10-27 13:56:32 -07:00
|
|
|
m_Serializer.NumberU32_Unbounded("array length", length);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
2014-03-28 13:26:32 -07:00
|
|
|
else if (JS_IsTypedArrayObject(obj))
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_TYPED_ARRAY);
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("array type", GetArrayType(JS_GetArrayBufferViewType(obj)));
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("byte offset", JS_GetTypedArrayByteOffset(obj));
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("length", JS_GetTypedArrayLength(obj));
|
2013-05-26 14:57:24 -07:00
|
|
|
|
Upgrade SpiderMonkey to version 45.0.2, refs #4893.
- Various build changes, in particular NSPR is not needed on Unix
anymore
- Add js/Initialization.h to source/scriptinterface/ScriptEngine.h
- Use nullptr instead of JS::NullPtr(), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1164602
- Remove `JS::RuntimeOptionsRef.varObjFix`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1171177
- Remove uses of `AutoIdArray`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1191529
- `JS_InternUCStringN` has been renamed, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1178581
- `JS::Evaluate` now takes scope chains explicitly, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1097987
- Array functions (such as `JS_IsArrayObject`) are fallible and output
to params, see https://bugzilla.mozilla.org/show_bug.cgi?id=f3d35d8
- Remove `JSCLASS_CACHED_PROTO_WIDTH` workaround in our code, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1236373
- Remove compile'n go (`setCompileAndGo`) and replace it by
`setIsRunOnce` which will become the default in the future, see
https://bugzilla.mozilla.org/show_bug.cgi?id=679939
- Mark shared memory in direct access operations
(`JS_GetUint16ArrayData` and `JS_GetUint8ArrayData`), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1176214
- Use new `JS::ObjectOpResult`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1113369
Thanks to wraitii, elexis, Krinkle and historic_bruno for contributions
and comments, and to gentz, madpilot, s0600204 and Stan for testing and
indirect contributions.
Differential Revision: https://code.wildfiregames.com/D1510
This was SVN commit r22627.
2019-08-07 15:37:43 -07:00
|
|
|
bool sharedMemory;
|
2013-05-26 14:57:24 -07:00
|
|
|
// Now handle its array buffer
|
|
|
|
|
// this may be a backref, since ArrayBuffers can be shared by multiple views
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue bufferVal(rq.cx, JS::ObjectValue(*JS_GetArrayBufferViewBuffer(rq.cx, obj, &sharedMemory)));
|
2014-07-31 12:18:40 -07:00
|
|
|
HandleScriptVal(bufferVal);
|
2013-05-26 14:57:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2020-11-30 01:03:20 -08:00
|
|
|
else if (JS::IsArrayBufferObject(obj))
|
2013-05-26 14:57:24 -07:00
|
|
|
{
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY_BUFFER);
|
|
|
|
|
|
|
|
|
|
#if BYTE_ORDER != LITTLE_ENDIAN
|
|
|
|
|
#error TODO: need to convert JS ArrayBuffer data to little-endian
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-30 01:03:20 -08:00
|
|
|
u32 length = JS::GetArrayBufferByteLength(obj);
|
2013-05-26 14:57:24 -07:00
|
|
|
m_Serializer.NumberU32_Unbounded("buffer length", length);
|
2016-09-02 09:38:31 -07:00
|
|
|
JS::AutoCheckCannotGC nogc;
|
Upgrade SpiderMonkey to version 45.0.2, refs #4893.
- Various build changes, in particular NSPR is not needed on Unix
anymore
- Add js/Initialization.h to source/scriptinterface/ScriptEngine.h
- Use nullptr instead of JS::NullPtr(), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1164602
- Remove `JS::RuntimeOptionsRef.varObjFix`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1171177
- Remove uses of `AutoIdArray`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1191529
- `JS_InternUCStringN` has been renamed, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1178581
- `JS::Evaluate` now takes scope chains explicitly, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1097987
- Array functions (such as `JS_IsArrayObject`) are fallible and output
to params, see https://bugzilla.mozilla.org/show_bug.cgi?id=f3d35d8
- Remove `JSCLASS_CACHED_PROTO_WIDTH` workaround in our code, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1236373
- Remove compile'n go (`setCompileAndGo`) and replace it by
`setIsRunOnce` which will become the default in the future, see
https://bugzilla.mozilla.org/show_bug.cgi?id=679939
- Mark shared memory in direct access operations
(`JS_GetUint16ArrayData` and `JS_GetUint8ArrayData`), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1176214
- Use new `JS::ObjectOpResult`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1113369
Thanks to wraitii, elexis, Krinkle and historic_bruno for contributions
and comments, and to gentz, madpilot, s0600204 and Stan for testing and
indirect contributions.
Differential Revision: https://code.wildfiregames.com/D1510
This was SVN commit r22627.
2019-08-07 15:37:43 -07:00
|
|
|
bool sharedMemory;
|
2020-11-30 01:03:20 -08:00
|
|
|
m_Serializer.RawBytes("buffer data", (const u8*)JS::GetArrayBufferData(obj, &sharedMemory, nogc), length);
|
2013-05-26 14:57:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2020-11-26 10:15:47 -08:00
|
|
|
|
|
|
|
|
else if (JS::IsMapObject(rq.cx, obj, &isMap) && isMap)
|
|
|
|
|
{
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_MAP);
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("map size", JS::MapSize(rq.cx, obj));
|
|
|
|
|
|
|
|
|
|
JS::RootedValue keyValueIterator(rq.cx);
|
|
|
|
|
if (!JS::MapEntries(rq.cx, obj, &keyValueIterator))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::MapEntries failed");
|
|
|
|
|
|
|
|
|
|
JS::ForOfIterator it(rq.cx);
|
|
|
|
|
if (!it.init(keyValueIterator))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::init failed");
|
|
|
|
|
|
|
|
|
|
JS::RootedValue keyValuePair(rq.cx);
|
|
|
|
|
bool done;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (!it.next(&keyValuePair, &done))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::next failed");
|
|
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
JS::RootedObject keyValuePairObj(rq.cx, &keyValuePair.toObject());
|
|
|
|
|
JS::RootedValue key(rq.cx);
|
|
|
|
|
JS::RootedValue value(rq.cx);
|
|
|
|
|
ENSURE(JS_GetElement(rq.cx, keyValuePairObj, 0, &key));
|
|
|
|
|
ENSURE(JS_GetElement(rq.cx, keyValuePairObj, 1, &value));
|
|
|
|
|
|
|
|
|
|
HandleScriptVal(key);
|
|
|
|
|
HandleScriptVal(value);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (JS::IsSetObject(rq.cx, obj, &isSet) && isSet)
|
|
|
|
|
{
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_SET);
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("set size", JS::SetSize(rq.cx, obj));
|
|
|
|
|
|
|
|
|
|
JS::RootedValue valueIterator(rq.cx);
|
|
|
|
|
if (!JS::SetValues(rq.cx, obj, &valueIterator))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::SetValues failed");
|
|
|
|
|
|
|
|
|
|
JS::ForOfIterator it(rq.cx);
|
|
|
|
|
if (!it.init(valueIterator))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::init failed");
|
|
|
|
|
|
|
|
|
|
JS::RootedValue value(rq.cx);
|
|
|
|
|
bool done;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (!it.next(&value, &done))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::next failed");
|
|
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
HandleScriptVal(value);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
else
|
|
|
|
|
{
|
2013-05-26 14:57:24 -07:00
|
|
|
// Find type of object
|
2015-01-24 06:46:52 -08:00
|
|
|
const JSClass* jsclass = JS_GetClass(obj);
|
2013-05-26 14:57:24 -07:00
|
|
|
if (!jsclass)
|
2014-03-28 13:26:32 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetClass failed");
|
Upgrade SpiderMonkey to version 45.0.2, refs #4893.
- Various build changes, in particular NSPR is not needed on Unix
anymore
- Add js/Initialization.h to source/scriptinterface/ScriptEngine.h
- Use nullptr instead of JS::NullPtr(), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1164602
- Remove `JS::RuntimeOptionsRef.varObjFix`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1171177
- Remove uses of `AutoIdArray`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1191529
- `JS_InternUCStringN` has been renamed, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1178581
- `JS::Evaluate` now takes scope chains explicitly, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1097987
- Array functions (such as `JS_IsArrayObject`) are fallible and output
to params, see https://bugzilla.mozilla.org/show_bug.cgi?id=f3d35d8
- Remove `JSCLASS_CACHED_PROTO_WIDTH` workaround in our code, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1236373
- Remove compile'n go (`setCompileAndGo`) and replace it by
`setIsRunOnce` which will become the default in the future, see
https://bugzilla.mozilla.org/show_bug.cgi?id=679939
- Mark shared memory in direct access operations
(`JS_GetUint16ArrayData` and `JS_GetUint8ArrayData`), see
https://bugzilla.mozilla.org/show_bug.cgi?id=1176214
- Use new `JS::ObjectOpResult`, see
https://bugzilla.mozilla.org/show_bug.cgi?id=1113369
Thanks to wraitii, elexis, Krinkle and historic_bruno for contributions
and comments, and to gentz, madpilot, s0600204 and Stan for testing and
indirect contributions.
Differential Revision: https://code.wildfiregames.com/D1510
This was SVN commit r22627.
2019-08-07 15:37:43 -07:00
|
|
|
|
2013-05-26 14:57:24 -07:00
|
|
|
JSProtoKey protokey = JSCLASS_CACHED_PROTO_KEY(jsclass);
|
|
|
|
|
|
|
|
|
|
if (protokey == JSProto_Object)
|
|
|
|
|
{
|
2020-12-27 09:18:13 -08:00
|
|
|
// Object class - check for user-defined prototype
|
|
|
|
|
JS::RootedObject proto(rq.cx);
|
|
|
|
|
if (!JS_GetPrototype(rq.cx, obj, &proto))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPrototype failed");
|
|
|
|
|
|
|
|
|
|
SPrototypeSerialization protoInfo = GetPrototypeInfo(rq, proto);
|
|
|
|
|
|
|
|
|
|
if (protoInfo.name == "Object")
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_PROTOTYPE);
|
|
|
|
|
m_Serializer.String("proto", wstring_from_utf8(protoInfo.name), 0, 256);
|
|
|
|
|
|
|
|
|
|
// Does it have custom Serialize function?
|
|
|
|
|
// if so, we serialize the data it returns, rather than the object's properties directly
|
|
|
|
|
if (protoInfo.hasCustomSerialize)
|
|
|
|
|
{
|
|
|
|
|
// If serialize is null, don't serialize anything more
|
|
|
|
|
if (!protoInfo.hasNullSerialize)
|
|
|
|
|
{
|
|
|
|
|
JS::RootedValue data(rq.cx);
|
|
|
|
|
if (!m_ScriptInterface.CallFunction(val, "Serialize", &data))
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("Prototype Serialize function failed");
|
|
|
|
|
m_Serializer.ScriptVal("data", &data);
|
|
|
|
|
}
|
|
|
|
|
// Break here to skip the custom object property serialization logic below.
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-26 14:57:24 -07:00
|
|
|
}
|
|
|
|
|
else if (protokey == JSProto_Number)
|
|
|
|
|
{
|
|
|
|
|
// Get primitive value
|
2014-03-28 13:26:32 -07:00
|
|
|
double d;
|
2020-11-13 05:18:22 -08:00
|
|
|
if (!JS::ToNumber(rq.cx, val, &d))
|
2014-03-28 13:26:32 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS::ToNumber failed");
|
2021-03-28 09:48:25 -07:00
|
|
|
|
|
|
|
|
// Refuse to serialize NaN values: their representation can differ, leading to OOS
|
|
|
|
|
// and in general this is indicative of an underlying bug rather than desirable behaviour.
|
|
|
|
|
if (std::isnan(d))
|
|
|
|
|
{
|
|
|
|
|
LOGERROR("Cannot serialize NaN values.");
|
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Standard Number object
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_NUMBER);
|
2013-05-26 14:57:24 -07:00
|
|
|
m_Serializer.NumberDouble_Unbounded("value", d);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (protokey == JSProto_String)
|
|
|
|
|
{
|
|
|
|
|
// Standard String object
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_STRING);
|
|
|
|
|
// Get primitive value
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedString str(rq.cx, JS::ToString(rq.cx, val));
|
2013-05-26 14:57:24 -07:00
|
|
|
if (!str)
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
|
|
|
|
|
ScriptString("value", str);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else if (protokey == JSProto_Boolean)
|
|
|
|
|
{
|
|
|
|
|
// Standard Boolean object
|
|
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_BOOLEAN);
|
|
|
|
|
// Get primitive value
|
2014-03-28 13:26:32 -07:00
|
|
|
bool b = JS::ToBoolean(val);
|
|
|
|
|
m_Serializer.Bool("value", b);
|
2013-05-26 14:57:24 -07:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Unrecognized class
|
2015-01-22 12:36:24 -08:00
|
|
|
LOGERROR("Cannot serialise JS objects with unrecognized class '%s'", jsclass->name);
|
2013-05-26 14:57:24 -07:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find all properties (ordered by insertion time)
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::Rooted<JS::IdVector> ida(rq.cx, JS::IdVector(rq.cx));
|
|
|
|
|
if (!JS_Enumerate(rq.cx, obj, &ida))
|
2010-11-16 15:00:52 -08:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_Enumerate failed");
|
2010-05-25 11:24:12 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberU32_Unbounded("num props", (u32)ida.length());
|
2010-06-27 04:57:00 -07:00
|
|
|
|
2010-11-16 15:00:52 -08:00
|
|
|
for (size_t i = 0; i < ida.length(); ++i)
|
2010-06-27 04:57:00 -07:00
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedId id(rq.cx, ida[i]);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue idval(rq.cx);
|
|
|
|
|
JS::RootedValue propval(rq.cx);
|
2016-01-11 12:03:33 -08:00
|
|
|
|
|
|
|
|
// Forbid getters, which might delete values and mess things up.
|
2020-11-18 06:39:04 -08:00
|
|
|
JS::Rooted<JS::PropertyDescriptor> desc(rq.cx);
|
2020-11-13 05:18:22 -08:00
|
|
|
if (!JS_GetPropertyDescriptorById(rq.cx, obj, id, &desc))
|
2016-01-11 12:03:33 -08:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPropertyDescriptorById failed");
|
|
|
|
|
if (desc.hasGetterObject())
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("Cannot serialize property getters");
|
|
|
|
|
|
2010-05-05 15:36:35 -07:00
|
|
|
// Get the property name as a string
|
2020-11-13 05:18:22 -08:00
|
|
|
if (!JS_IdToValue(rq.cx, id, &idval))
|
2010-05-05 15:36:35 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_IdToValue failed");
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedString idstr(rq.cx, JS::ToString(rq.cx, idval));
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!idstr)
|
2010-05-05 15:36:35 -07:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
ScriptString("prop name", idstr);
|
2010-05-05 15:36:35 -07:00
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
if (!JS_GetPropertyById(rq.cx, obj, id, &propval))
|
2016-01-11 12:03:33 -08:00
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetPropertyById failed");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
HandleScriptVal(propval);
|
|
|
|
|
}
|
2010-05-25 11:24:12 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_FUNCTION:
|
|
|
|
|
{
|
2012-02-28 14:12:30 -08:00
|
|
|
// We can't serialise functions, but we can at least name the offender (hopefully)
|
2012-02-29 15:41:23 -08:00
|
|
|
std::wstring funcname(L"(unnamed)");
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedFunction func(rq.cx, JS_ValueToFunction(rq.cx, val));
|
2012-02-28 14:12:30 -08:00
|
|
|
if (func)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedString string(rq.cx, JS_GetFunctionId(func));
|
2012-02-28 14:12:30 -08:00
|
|
|
if (string)
|
|
|
|
|
{
|
2016-09-02 09:51:09 -07:00
|
|
|
if (JS_StringHasLatin1Chars(string))
|
|
|
|
|
{
|
|
|
|
|
size_t length;
|
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
2020-11-13 05:18:22 -08:00
|
|
|
const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, string, &length);
|
2016-09-02 09:51:09 -07:00
|
|
|
if (ch && length > 0)
|
|
|
|
|
funcname.assign(ch, ch + length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
size_t length;
|
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
2020-11-13 05:18:22 -08:00
|
|
|
const char16_t* ch = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, string, &length);
|
2016-09-02 09:51:09 -07:00
|
|
|
if (ch && length > 0)
|
|
|
|
|
funcname.assign(ch, ch + length);
|
|
|
|
|
}
|
2012-02-28 14:12:30 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-22 12:37:38 -08:00
|
|
|
LOGERROR("Cannot serialise JS objects of type 'function': %s", utf8_from_wstring(funcname));
|
2010-01-09 11:20:14 -08:00
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_STRING:
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_STRING);
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedString stringVal(rq.cx, val.toString());
|
2015-01-24 06:46:52 -08:00
|
|
|
ScriptString("string", stringVal);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_NUMBER:
|
|
|
|
|
{
|
2021-03-28 09:48:25 -07:00
|
|
|
// Refuse to serialize NaN values: their representation can differ, leading to OOS
|
|
|
|
|
// and in general this is indicative of an underlying bug rather than desirable behaviour.
|
|
|
|
|
if (val == JS::NaNValue())
|
|
|
|
|
{
|
|
|
|
|
LOGERROR("Cannot serialize NaN values.");
|
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
// To reduce the size of the serialized data, we handle integers and doubles separately.
|
|
|
|
|
// We can't check for val.isInt32 and val.isDouble directly, because integer numbers are not guaranteed
|
|
|
|
|
// to be represented as integers. A number like 33 could be stored as integer on the computer of one player
|
2016-11-23 05:02:58 -08:00
|
|
|
// and as double on the other player's computer. That would cause out of sync errors in multiplayer games because
|
2014-03-28 13:26:32 -07:00
|
|
|
// their binary representation and thus the hash would be different.
|
|
|
|
|
double d;
|
|
|
|
|
d = val.toNumber();
|
|
|
|
|
i32 integer;
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
if (JS_DoubleIsInt32(d, &integer))
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_INT);
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberI32_Unbounded("value", integer);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_DOUBLE);
|
|
|
|
|
m_Serializer.NumberDouble_Unbounded("value", d);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case JSTYPE_BOOLEAN:
|
|
|
|
|
{
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BOOLEAN);
|
2016-09-02 09:23:44 -07:00
|
|
|
bool b = val.toBoolean();
|
2010-05-25 12:01:30 -07:00
|
|
|
m_Serializer.NumberU8_Unbounded("value", b ? 1 : 0);
|
2010-01-09 11:20:14 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
debug_warn(L"Invalid TypeOfValue");
|
|
|
|
|
throw PSERROR_Serialize_InvalidScriptValue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
void CBinarySerializerScriptImpl::ScriptString(const char* name, JS::HandleString string)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2014-03-28 13:26:32 -07:00
|
|
|
|
2010-05-25 11:07:41 -07:00
|
|
|
#if BYTE_ORDER != LITTLE_ENDIAN
|
|
|
|
|
#error TODO: probably need to convert JS strings to little-endian
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-09-02 09:51:09 -07:00
|
|
|
size_t length;
|
|
|
|
|
JS::AutoCheckCannotGC nogc;
|
|
|
|
|
// Serialize strings directly as UTF-16 or Latin1, to avoid expensive encoding conversions
|
|
|
|
|
bool isLatin1 = JS_StringHasLatin1Chars(string);
|
|
|
|
|
m_Serializer.Bool("isLatin1", isLatin1);
|
|
|
|
|
if (isLatin1)
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
const JS::Latin1Char* chars = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, string, &length);
|
2016-09-02 09:51:09 -07:00
|
|
|
if (!chars)
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetLatin1StringCharsAndLength failed");
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("string length", (u32)length);
|
|
|
|
|
m_Serializer.RawBytes(name, (const u8*)chars, length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-13 05:18:22 -08:00
|
|
|
const char16_t* chars = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, string, &length);
|
2016-09-02 09:51:09 -07:00
|
|
|
|
|
|
|
|
if (!chars)
|
|
|
|
|
throw PSERROR_Serialize_ScriptError("JS_GetTwoByteStringCharsAndLength failed");
|
|
|
|
|
m_Serializer.NumberU32_Unbounded("string length", (u32)length);
|
|
|
|
|
m_Serializer.RawBytes(name, (const u8*)chars, length*2);
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-12 10:43:45 -08:00
|
|
|
void CBinarySerializerScriptImpl::Trace(JSTracer *trc, void *data)
|
|
|
|
|
{
|
|
|
|
|
CBinarySerializerScriptImpl* serializer = static_cast<CBinarySerializerScriptImpl*>(data);
|
|
|
|
|
serializer->m_ScriptBackrefTags.trace(trc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 CBinarySerializerScriptImpl::GetScriptBackrefTag(JS::HandleObject obj)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// To support non-tree structures (e.g. "var x = []; var y = [x, x];"), we need a way
|
|
|
|
|
// to indicate multiple references to one object(/array). So every time we serialize a
|
Use Symbols to store JS object references when serialising and delete ObjectIDCache
When serialising JS objects, we keep track of any encountered object,
and serialize it only once. Any further serialisation instead stores an
ID referring to the original object (essentially an opaque pointer).
The trouble of course is to have a unique, persistent identifier for
such an object.
svn uses an ObjectIDCache, essentially a "JS Object -> ID" map (which
internally is essentially a "JS heap pointer -> ID" map).
JS, since ES15, includes a "Symbol" primitive type, which is a unique,
immutable identifier. They are also not iterable by for..in or
GetOwnPropertyName or related.
This means they can be used to store the tag directly on the object
(since it's impossible overwrite a user property).
Thanks to this, we can forgo ObjectIDCache in the serializers, and since
following D2897 it becomes unused, we can delete it, along with the
Finalization code it used.
Part of SM52 migration, stage: SM45-compatible changes.
Patch by: Itms
Tested By: Freagarach
Refs #4893
Differential Revision: https://code.wildfiregames.com/D3085
This was SVN commit r24167.
2020-11-11 22:40:19 -08:00
|
|
|
// new object, we give it a new tag; when we serialize it a second time we just refer
|
|
|
|
|
// to that tag.
|
2015-01-24 06:46:52 -08:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
2016-09-18 02:34:45 -07:00
|
|
|
|
2021-01-12 10:43:45 -08:00
|
|
|
ObjectTagMap::Ptr ptr = m_ScriptBackrefTags.lookup(JS::Heap<JSObject*>(obj.get()));
|
|
|
|
|
if (!ptr.found())
|
2020-12-27 09:18:13 -08:00
|
|
|
{
|
2021-01-30 01:13:13 -08:00
|
|
|
if (!m_ScriptBackrefTags.put(JS::Heap<JSObject*>(obj.get()), ++m_ScriptBackrefsNext))
|
|
|
|
|
{
|
|
|
|
|
JS::RootedValue objval(rq.cx, JS::ObjectValue(*obj.get()));
|
|
|
|
|
LOGERROR("BinarySerializer: error at insertion. Object was %s", m_ScriptInterface.ToString(&objval));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-01-12 10:43:45 -08:00
|
|
|
// Return 0 to mean "you have to serialize this object";
|
|
|
|
|
return 0;
|
2020-12-27 09:18:13 -08:00
|
|
|
}
|
2021-01-12 10:43:45 -08:00
|
|
|
else
|
|
|
|
|
return ptr->value();
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|