2012-01-29 22:21:11 -08:00
|
|
|
/* Copyright (C) 2012 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 "scriptinterface/ScriptInterface.h"
|
2011-07-16 16:24:14 -07:00
|
|
|
#include "scriptinterface/ScriptExtraHeaders.h" // for typed arrays
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
#include "maths/Fixed.h"
|
2010-10-02 05:41:29 -07:00
|
|
|
#include "maths/FixedVector2D.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "maths/FixedVector3D.h"
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
#include "ps/Overlay.h"
|
|
|
|
|
#include "ps/utf16string.h"
|
2011-02-10 08:06:28 -08:00
|
|
|
#include "simulation2/helpers/Grid.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "simulation2/system/IComponent.h"
|
|
|
|
|
#include "simulation2/system/ParamNode.h"
|
|
|
|
|
|
2011-03-23 01:12:04 -07:00
|
|
|
#define FAIL(msg) STMT(JS_ReportError(cx, msg); return false)
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<IComponent*>(JSContext* cx, JS::MutableHandleValue ret, IComponent* const& val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (val == NULL)
|
2014-03-28 13:26:32 -07:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setNull();
|
2014-03-28 13:26:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// If this is a scripted component, just return the JS object directly
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue instance(cx, val->GetJSInstance());
|
|
|
|
|
if (!instance.isNull())
|
|
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.set(instance);
|
2014-03-28 13:26:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// Otherwise we need to construct a wrapper object
|
|
|
|
|
// (TODO: cache wrapper objects?)
|
2014-07-13 08:31:48 -07:00
|
|
|
JS::RootedObject obj(cx);
|
|
|
|
|
if (!val->NewJSObject(*ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface, &obj))
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
// Report as an error, since scripts really shouldn't try to use unscriptable interfaces
|
|
|
|
|
LOGERROR(L"IComponent does not have a scriptable interface");
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setUndefined();
|
2014-03-28 13:26:32 -07:00
|
|
|
return;
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS_SetPrivate(obj, static_cast<void*>(val));
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setObject(*obj);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<CParamNode>(JSContext* cx, JS::MutableHandleValue ret, CParamNode const& val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
2014-11-09 03:08:53 -08:00
|
|
|
val.ToJSVal(cx, true, ret);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
// Prevent modifications to the object, so that it's safe to share between
|
|
|
|
|
// components and to reconstruct on deserialization
|
2014-03-28 13:26:32 -07:00
|
|
|
if (ret.isObject())
|
|
|
|
|
JS_DeepFreezeObject(cx, &ret.toObject());
|
2010-05-06 17:24:22 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<const CParamNode*>(JSContext* cx, JS::MutableHandleValue ret, const CParamNode* const& val)
|
2010-01-25 15:43:58 -08:00
|
|
|
{
|
|
|
|
|
if (val)
|
2014-03-28 13:26:32 -07:00
|
|
|
ToJSVal(cx, ret, *val);
|
2010-01-25 15:43:58 -08:00
|
|
|
else
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setUndefined();
|
2010-01-25 15:43:58 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-12 12:08:39 -07:00
|
|
|
template<> bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, JS::HandleValue v, CColor& out)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!v.isObject())
|
2014-07-14 12:52:35 -07:00
|
|
|
FAIL("JS::HandleValue not an object");
|
2011-03-23 01:12:04 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
JS::RootedObject obj(cx, &v.toObject());
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue r(cx);
|
|
|
|
|
JS::RootedValue g(cx);
|
|
|
|
|
JS::RootedValue b(cx);
|
|
|
|
|
JS::RootedValue a(cx);
|
|
|
|
|
if (!JS_GetProperty(cx, obj, "r", r.address()) || !FromJSVal(cx, r, out.r))
|
2011-03-23 01:12:04 -07:00
|
|
|
FAIL("Failed to get property CColor.r");
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "g", g.address()) || !FromJSVal(cx, g, out.g))
|
2011-03-23 01:12:04 -07:00
|
|
|
FAIL("Failed to get property CColor.g");
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "b", b.address()) || !FromJSVal(cx, b, out.b))
|
2011-03-23 01:12:04 -07:00
|
|
|
FAIL("Failed to get property CColor.b");
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "a", a.address()) || !FromJSVal(cx, a, out.a))
|
2011-03-23 01:12:04 -07:00
|
|
|
FAIL("Failed to get property CColor.a");
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::MutableHandleValue ret, CColor const& val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
JS::RootedObject obj(cx, JS_NewObject(cx, NULL, NULL, NULL));
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!obj)
|
2014-03-28 13:26:32 -07:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setUndefined();
|
2014-03-28 13:26:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue r(cx);
|
|
|
|
|
JS::RootedValue g(cx);
|
|
|
|
|
JS::RootedValue b(cx);
|
|
|
|
|
JS::RootedValue a(cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ToJSVal(cx, &r, val.r);
|
|
|
|
|
ToJSVal(cx, &g, val.g);
|
|
|
|
|
ToJSVal(cx, &b, val.b);
|
|
|
|
|
ToJSVal(cx, &a, val.a);
|
2014-03-28 13:26:32 -07:00
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setObject(*obj);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-12 12:08:39 -07:00
|
|
|
template<> bool ScriptInterface::FromJSVal<fixed>(JSContext* cx, JS::HandleValue v, fixed& out)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
double ret;
|
|
|
|
|
if (!JS::ToNumber(cx, v, &ret))
|
2010-01-09 11:20:14 -08:00
|
|
|
return false;
|
2010-05-02 13:32:37 -07:00
|
|
|
out = fixed::FromDouble(ret);
|
2010-07-04 12:47:31 -07:00
|
|
|
// double can precisely represent the full range of fixed, so this is a non-lossy conversion
|
|
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<fixed>(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const fixed& val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.set(JS::NumberValue(val.ToDouble()));
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-12 12:08:39 -07:00
|
|
|
template<> bool ScriptInterface::FromJSVal<CFixedVector3D>(JSContext* cx, JS::HandleValue v, CFixedVector3D& out)
|
2010-03-07 13:38:39 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!v.isObject())
|
2010-03-07 13:38:39 -08:00
|
|
|
return false; // TODO: report type error
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
JS::RootedObject obj(cx, &v.toObject());
|
|
|
|
|
JS::RootedValue p(cx);
|
2010-03-07 13:38:39 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "x", p.address())) return false; // TODO: report type errors
|
2010-03-07 13:38:39 -08:00
|
|
|
if (!FromJSVal(cx, p, out.X)) return false;
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "y", p.address())) return false;
|
2010-03-07 13:38:39 -08:00
|
|
|
if (!FromJSVal(cx, p, out.Y)) return false;
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "z", p.address())) return false;
|
2010-03-07 13:38:39 -08:00
|
|
|
if (!FromJSVal(cx, p, out.Z)) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector3D& val)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
|
2014-01-23 03:32:08 -08:00
|
|
|
// apply the Vector3D prototype to the return value;
|
|
|
|
|
ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx);
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedObject obj(cx, JS_NewObject(cx, NULL,
|
|
|
|
|
&pCxPrivate->pScriptInterface->GetCachedValue(ScriptInterface::CACHE_VECTOR3DPROTO).get().toObject(),
|
|
|
|
|
NULL));
|
2014-01-23 03:32:08 -08:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!obj)
|
2014-03-28 13:26:32 -07:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setUndefined();
|
2014-03-28 13:26:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue x(cx);
|
|
|
|
|
JS::RootedValue y(cx);
|
|
|
|
|
JS::RootedValue z(cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ToJSVal(cx, &x, val.X);
|
|
|
|
|
ToJSVal(cx, &y, val.Y);
|
|
|
|
|
ToJSVal(cx, &z, val.Z);
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS_SetProperty(cx, obj, "x", x.address());
|
|
|
|
|
JS_SetProperty(cx, obj, "y", y.address());
|
|
|
|
|
JS_SetProperty(cx, obj, "z", z.address());
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setObject(*obj);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
2010-10-02 05:41:29 -07:00
|
|
|
|
2014-07-12 12:08:39 -07:00
|
|
|
template<> bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, JS::HandleValue v, CFixedVector2D& out)
|
2010-10-02 05:41:29 -07:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
if (!v.isObject())
|
2010-10-02 05:41:29 -07:00
|
|
|
return false; // TODO: report type error
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedObject obj(cx, &v.toObject());
|
2010-10-02 05:41:29 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue p(cx);
|
2010-10-02 05:41:29 -07:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "x", p.address())) return false; // TODO: report type errors
|
2010-10-02 05:41:29 -07:00
|
|
|
if (!FromJSVal(cx, p, out.X)) return false;
|
|
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
if (!JS_GetProperty(cx, obj, "y", p.address())) return false;
|
2010-10-02 05:41:29 -07:00
|
|
|
if (!FromJSVal(cx, p, out.Y)) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-01-15 15:35:20 -08:00
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector2D& val)
|
2011-01-15 15:35:20 -08:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
|
2014-01-23 03:32:08 -08:00
|
|
|
// apply the Vector2D prototype to the return value
|
|
|
|
|
ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx);
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedObject obj(cx, JS_NewObject(cx, NULL,
|
|
|
|
|
&pCxPrivate->pScriptInterface->GetCachedValue(ScriptInterface::CACHE_VECTOR2DPROTO).get().toObject(),
|
|
|
|
|
NULL));
|
2011-01-15 15:35:20 -08:00
|
|
|
if (!obj)
|
2014-03-28 13:26:32 -07:00
|
|
|
{
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setUndefined();
|
2014-03-28 13:26:32 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2011-02-10 08:06:28 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS::RootedValue x(cx);
|
|
|
|
|
JS::RootedValue y(cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ToJSVal(cx, &x, val.X);
|
|
|
|
|
ToJSVal(cx, &y, val.Y);
|
2011-02-10 08:06:28 -08:00
|
|
|
|
2014-03-28 13:26:32 -07:00
|
|
|
JS_SetProperty(cx, obj, "x", x.address());
|
|
|
|
|
JS_SetProperty(cx, obj, "y", y.address());
|
2011-02-10 08:06:28 -08:00
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setObject(*obj);
|
2011-02-10 08:06:28 -08:00
|
|
|
}
|
2011-10-15 19:55:58 -07:00
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u8>& val)
|
2011-10-15 19:55:58 -07:00
|
|
|
{
|
2014-03-28 13:26:32 -07:00
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
u32 length = (u32)(val.m_W * val.m_H);
|
|
|
|
|
u32 nbytes = (u32)(length * sizeof(u8));
|
|
|
|
|
JS::RootedObject objArr(cx, JS_NewUint8Array(cx, length));
|
|
|
|
|
memcpy((void*)JS_GetUint8ArrayData(objArr), val.m_Data, nbytes);
|
|
|
|
|
|
|
|
|
|
JS::RootedValue data(cx, JS::ObjectValue(*objArr));
|
|
|
|
|
JS::RootedValue w(cx);
|
|
|
|
|
JS::RootedValue h(cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ScriptInterface::ToJSVal(cx, &w, val.m_W);
|
|
|
|
|
ScriptInterface::ToJSVal(cx, &h, val.m_H);
|
2014-03-28 13:26:32 -07:00
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setObject(*obj);
|
2011-10-15 19:55:58 -07:00
|
|
|
}
|
2014-03-28 13:26:32 -07:00
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u16>& val)
|
2014-03-28 13:26:32 -07:00
|
|
|
{
|
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
u32 length = (u32)(val.m_W * val.m_H);
|
|
|
|
|
u32 nbytes = (u32)(length * sizeof(u16));
|
|
|
|
|
JS::RootedObject objArr(cx, JS_NewUint16Array(cx, length));
|
|
|
|
|
memcpy((void*)JS_GetUint16ArrayData(objArr), val.m_Data, nbytes);
|
|
|
|
|
|
|
|
|
|
JS::RootedValue data(cx, JS::ObjectValue(*objArr));
|
|
|
|
|
JS::RootedValue w(cx);
|
|
|
|
|
JS::RootedValue h(cx);
|
2014-07-14 12:52:35 -07:00
|
|
|
ScriptInterface::ToJSVal(cx, &w, val.m_W);
|
|
|
|
|
ScriptInterface::ToJSVal(cx, &h, val.m_H);
|
2014-03-28 13:26:32 -07:00
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
2014-07-14 12:52:35 -07:00
|
|
|
ret.setObject(*obj);
|
2012-01-29 22:21:11 -08:00
|
|
|
}
|