forked from mirrors/0ad
Changes ToJSVal to take JS::MutableHandleValue instead of JS::Value&.
JS::MutableHandleValue is similar to JS::HandleValue, but the whole JS::Value it points to can be replaced. This change is needed for support of exact stack rooting and moving GC. Contains a few other trivial API adjustments and style improvements too. Refs #2462 Refs #2415 This was SVN commit r15534.
This commit is contained in:
@@ -32,12 +32,12 @@
|
||||
|
||||
#define FAIL(msg) STMT(JS_ReportError(cx, msg); return false)
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<IComponent*>(JSContext* cx, JS::Value& ret, IComponent* const& val)
|
||||
template<> void ScriptInterface::ToJSVal<IComponent*>(JSContext* cx, JS::MutableHandleValue ret, IComponent* const& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
if (val == NULL)
|
||||
{
|
||||
ret = JS::NullValue();
|
||||
ret.setNull();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ template<> void ScriptInterface::ToJSVal<IComponent*>(JSContext* cx, JS::Value&
|
||||
JS::RootedValue instance(cx, val->GetJSInstance());
|
||||
if (!instance.isNull())
|
||||
{
|
||||
ret = instance;
|
||||
ret.set(instance);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -56,18 +56,18 @@ template<> void ScriptInterface::ToJSVal<IComponent*>(JSContext* cx, JS::Value&
|
||||
{
|
||||
// Report as an error, since scripts really shouldn't try to use unscriptable interfaces
|
||||
LOGERROR(L"IComponent does not have a scriptable interface");
|
||||
ret = JS::UndefinedValue();
|
||||
ret.setUndefined();
|
||||
return;
|
||||
}
|
||||
|
||||
JS_SetPrivate(obj, static_cast<void*>(val));
|
||||
ret = JS::ObjectValue(*obj);
|
||||
ret.setObject(*obj);
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<CParamNode>(JSContext* cx, JS::Value& ret, CParamNode const& val)
|
||||
template<> void ScriptInterface::ToJSVal<CParamNode>(JSContext* cx, JS::MutableHandleValue ret, CParamNode const& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
ret = val.ToJSVal(cx, true);
|
||||
ret.set(val.ToJSVal(cx, true));
|
||||
|
||||
// Prevent modifications to the object, so that it's safe to share between
|
||||
// components and to reconstruct on deserialization
|
||||
@@ -75,18 +75,18 @@ template<> void ScriptInterface::ToJSVal<CParamNode>(JSContext* cx, JS::Value& r
|
||||
JS_DeepFreezeObject(cx, &ret.toObject());
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<const CParamNode*>(JSContext* cx, JS::Value& ret, const CParamNode* const& val)
|
||||
template<> void ScriptInterface::ToJSVal<const CParamNode*>(JSContext* cx, JS::MutableHandleValue ret, const CParamNode* const& val)
|
||||
{
|
||||
if (val)
|
||||
ToJSVal(cx, ret, *val);
|
||||
else
|
||||
ret = JSVAL_VOID;
|
||||
ret.setUndefined();
|
||||
}
|
||||
|
||||
template<> bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, JS::HandleValue v, CColor& out)
|
||||
{
|
||||
if (!v.isObject())
|
||||
FAIL("jsval not an object");
|
||||
FAIL("JS::HandleValue not an object");
|
||||
|
||||
JSAutoRequest rq(cx);
|
||||
JS::RootedObject obj(cx, &v.toObject());
|
||||
@@ -107,13 +107,13 @@ template<> bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, JS::HandleValu
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::Value& ret, CColor const& val)
|
||||
template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::MutableHandleValue ret, CColor const& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
JS::RootedObject obj(cx, JS_NewObject(cx, NULL, NULL, NULL));
|
||||
if (!obj)
|
||||
{
|
||||
ret = JSVAL_VOID;
|
||||
ret.setUndefined();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -121,17 +121,17 @@ template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::Value& ret,
|
||||
JS::RootedValue g(cx);
|
||||
JS::RootedValue b(cx);
|
||||
JS::RootedValue a(cx);
|
||||
ToJSVal(cx, r.get(), val.r);
|
||||
ToJSVal(cx, g.get(), val.g);
|
||||
ToJSVal(cx, b.get(), val.b);
|
||||
ToJSVal(cx, a.get(), val.a);
|
||||
ToJSVal(cx, &r, val.r);
|
||||
ToJSVal(cx, &g, val.g);
|
||||
ToJSVal(cx, &b, val.b);
|
||||
ToJSVal(cx, &a, val.a);
|
||||
|
||||
JS_SetProperty(cx, obj, "r", r.address());
|
||||
JS_SetProperty(cx, obj, "g", g.address());
|
||||
JS_SetProperty(cx, obj, "b", b.address());
|
||||
JS_SetProperty(cx, obj, "a", a.address());
|
||||
|
||||
ret = JS::ObjectValue(*obj);
|
||||
ret.setObject(*obj);
|
||||
}
|
||||
|
||||
template<> bool ScriptInterface::FromJSVal<fixed>(JSContext* cx, JS::HandleValue v, fixed& out)
|
||||
@@ -146,9 +146,9 @@ template<> bool ScriptInterface::FromJSVal<fixed>(JSContext* cx, JS::HandleValue
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<fixed>(JSContext* UNUSED(cx), JS::Value& ret, const fixed& val)
|
||||
template<> void ScriptInterface::ToJSVal<fixed>(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const fixed& val)
|
||||
{
|
||||
ret = JS::NumberValue(val.ToDouble());
|
||||
ret.set(JS::NumberValue(val.ToDouble()));
|
||||
}
|
||||
|
||||
template<> bool ScriptInterface::FromJSVal<CFixedVector3D>(JSContext* cx, JS::HandleValue v, CFixedVector3D& out)
|
||||
@@ -172,7 +172,7 @@ template<> bool ScriptInterface::FromJSVal<CFixedVector3D>(JSContext* cx, JS::Ha
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::Value& ret, const CFixedVector3D& val)
|
||||
template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector3D& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
|
||||
@@ -184,22 +184,22 @@ template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::Valu
|
||||
|
||||
if (!obj)
|
||||
{
|
||||
ret = JSVAL_VOID;
|
||||
ret.setUndefined();
|
||||
return;
|
||||
}
|
||||
|
||||
JS::RootedValue x(cx);
|
||||
JS::RootedValue y(cx);
|
||||
JS::RootedValue z(cx);
|
||||
ToJSVal(cx, x.get(), val.X);
|
||||
ToJSVal(cx, y.get(), val.Y);
|
||||
ToJSVal(cx, z.get(), val.Z);
|
||||
ToJSVal(cx, &x, val.X);
|
||||
ToJSVal(cx, &y, val.Y);
|
||||
ToJSVal(cx, &z, val.Z);
|
||||
|
||||
JS_SetProperty(cx, obj, "x", x.address());
|
||||
JS_SetProperty(cx, obj, "y", y.address());
|
||||
JS_SetProperty(cx, obj, "z", z.address());
|
||||
|
||||
ret = JS::ObjectValue(*obj);
|
||||
ret.setObject(*obj);
|
||||
}
|
||||
|
||||
template<> bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, JS::HandleValue v, CFixedVector2D& out)
|
||||
@@ -220,7 +220,7 @@ template<> bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, JS::Ha
|
||||
return true;
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::Value& ret, const CFixedVector2D& val)
|
||||
template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector2D& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
|
||||
@@ -231,22 +231,22 @@ template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::Valu
|
||||
NULL));
|
||||
if (!obj)
|
||||
{
|
||||
ret = JSVAL_VOID;
|
||||
ret.setUndefined();
|
||||
return;
|
||||
}
|
||||
|
||||
JS::RootedValue x(cx);
|
||||
JS::RootedValue y(cx);
|
||||
ToJSVal(cx, x.get(), val.X);
|
||||
ToJSVal(cx, y.get(), val.Y);
|
||||
ToJSVal(cx, &x, val.X);
|
||||
ToJSVal(cx, &y, val.Y);
|
||||
|
||||
JS_SetProperty(cx, obj, "x", x.address());
|
||||
JS_SetProperty(cx, obj, "y", y.address());
|
||||
|
||||
ret = JS::ObjectValue(*obj);
|
||||
ret.setObject(*obj);
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::Value& ret, const Grid<u8>& val)
|
||||
template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u8>& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
u32 length = (u32)(val.m_W * val.m_H);
|
||||
@@ -257,18 +257,18 @@ template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::Value& re
|
||||
JS::RootedValue data(cx, JS::ObjectValue(*objArr));
|
||||
JS::RootedValue w(cx);
|
||||
JS::RootedValue h(cx);
|
||||
ScriptInterface::ToJSVal(cx, w.get(), val.m_W);
|
||||
ScriptInterface::ToJSVal(cx, h.get(), val.m_H);
|
||||
ScriptInterface::ToJSVal(cx, &w, val.m_W);
|
||||
ScriptInterface::ToJSVal(cx, &h, val.m_H);
|
||||
|
||||
JS::RootedObject obj(cx, JS_NewObject(cx, NULL, NULL, NULL));
|
||||
JS_SetProperty(cx, obj, "width", w.address());
|
||||
JS_SetProperty(cx, obj, "height", h.address());
|
||||
JS_SetProperty(cx, obj, "data", data.address());
|
||||
|
||||
ret = JS::ObjectValue(*obj);
|
||||
ret.setObject(*obj);
|
||||
}
|
||||
|
||||
template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::Value& ret, const Grid<u16>& val)
|
||||
template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u16>& val)
|
||||
{
|
||||
JSAutoRequest rq(cx);
|
||||
u32 length = (u32)(val.m_W * val.m_H);
|
||||
@@ -279,13 +279,13 @@ template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::Value& r
|
||||
JS::RootedValue data(cx, JS::ObjectValue(*objArr));
|
||||
JS::RootedValue w(cx);
|
||||
JS::RootedValue h(cx);
|
||||
ScriptInterface::ToJSVal(cx, w.get(), val.m_W);
|
||||
ScriptInterface::ToJSVal(cx, h.get(), val.m_H);
|
||||
ScriptInterface::ToJSVal(cx, &w, val.m_W);
|
||||
ScriptInterface::ToJSVal(cx, &h, val.m_H);
|
||||
|
||||
JS::RootedObject obj(cx, JS_NewObject(cx, NULL, NULL, NULL));
|
||||
JS_SetProperty(cx, obj, "width", w.address());
|
||||
JS_SetProperty(cx, obj, "height", h.address());
|
||||
JS_SetProperty(cx, obj, "data", data.address());
|
||||
|
||||
ret = JS::ObjectValue(*obj);
|
||||
ret.setObject(*obj);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user