Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/scripting/EngineScriptConversions.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | template<> bool ScriptInterface::FromJSVal<CFixedVector3D>(JSContext* cx, JS::HandleValue v, CFixedVector3D& out) | ||||
if (!JS_GetProperty(cx, obj, "z", &p)) return false; | if (!JS_GetProperty(cx, obj, "z", &p)) return false; | ||||
if (!FromJSVal(cx, p, out.Z)) return false; | if (!FromJSVal(cx, p, out.Z)) return false; | ||||
return true; | return true; | ||||
} | } | ||||
template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector3D& val) | template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector3D& val) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | |||||
ScriptInterface::CmptPrivate* pCmptPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); | ScriptInterface::CmptPrivate* pCmptPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); | ||||
JS::RootedObject global(cx, &pCmptPrivate->pScriptInterface->GetGlobalObject().toObject()); | ScriptInterface::Request rq(pCmptPrivate); | ||||
JS::RootedValue valueVector3D(cx); | |||||
if (!JS_GetProperty(cx, global, "Vector3D", &valueVector3D)) | JS::RootedObject global(rq.cx, rq.glob); | ||||
JS::RootedValue valueVector3D(rq.cx); | |||||
if (!JS_GetProperty(rq.cx, global, "Vector3D", &valueVector3D)) | |||||
FAIL_VOID("Failed to get Vector3D constructor"); | FAIL_VOID("Failed to get Vector3D constructor"); | ||||
JS::AutoValueArray<3> args(cx); | JS::AutoValueArray<3> args(rq.cx); | ||||
args[0].setNumber(val.X.ToDouble()); | args[0].setNumber(val.X.ToDouble()); | ||||
args[1].setNumber(val.Y.ToDouble()); | args[1].setNumber(val.Y.ToDouble()); | ||||
args[2].setNumber(val.Z.ToDouble()); | args[2].setNumber(val.Z.ToDouble()); | ||||
if (!JS::Construct(cx, valueVector3D, args, ret)) | if (!JS::Construct(rq.cx, valueVector3D, args, ret)) | ||||
FAIL_VOID("Failed to construct Vector3D object"); | FAIL_VOID("Failed to construct Vector3D object"); | ||||
} | } | ||||
template<> bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, JS::HandleValue v, CFixedVector2D& out) | template<> bool ScriptInterface::FromJSVal<CFixedVector2D>(JSContext* cx, JS::HandleValue v, CFixedVector2D& out) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
if (!v.isObject()) | if (!v.isObject()) | ||||
return false; // TODO: report type error | return false; // TODO: report type error | ||||
JS::RootedObject obj(cx, &v.toObject()); | JS::RootedObject obj(cx, &v.toObject()); | ||||
JS::RootedValue p(cx); | JS::RootedValue p(cx); | ||||
if (!JS_GetProperty(cx, obj, "x", &p)) return false; // TODO: report type errors | if (!JS_GetProperty(cx, obj, "x", &p)) return false; // TODO: report type errors | ||||
if (!FromJSVal(cx, p, out.X)) return false; | if (!FromJSVal(cx, p, out.X)) return false; | ||||
if (!JS_GetProperty(cx, obj, "y", &p)) return false; | if (!JS_GetProperty(cx, obj, "y", &p)) return false; | ||||
if (!FromJSVal(cx, p, out.Y)) return false; | if (!FromJSVal(cx, p, out.Y)) return false; | ||||
return true; | return true; | ||||
} | } | ||||
template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector2D& val) | template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector2D& val) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | |||||
ScriptInterface::CmptPrivate* pCmptPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); | ScriptInterface::CmptPrivate* pCmptPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); | ||||
JS::RootedObject global(cx, &pCmptPrivate->pScriptInterface->GetGlobalObject().toObject()); | ScriptInterface::Request rq(pCmptPrivate); | ||||
JS::RootedValue valueVector2D(cx); | |||||
if (!JS_GetProperty(cx, global, "Vector2D", &valueVector2D)) | JS::RootedObject global(rq.cx, rq.glob); | ||||
JS::RootedValue valueVector2D(rq.cx); | |||||
if (!JS_GetProperty(rq.cx, global, "Vector2D", &valueVector2D)) | |||||
FAIL_VOID("Failed to get Vector2D constructor"); | FAIL_VOID("Failed to get Vector2D constructor"); | ||||
JS::AutoValueArray<2> args(cx); | JS::AutoValueArray<2> args(rq.cx); | ||||
args[0].setNumber(val.X.ToDouble()); | args[0].setNumber(val.X.ToDouble()); | ||||
args[1].setNumber(val.Y.ToDouble()); | args[1].setNumber(val.Y.ToDouble()); | ||||
if (!JS::Construct(cx, valueVector2D, args, ret)) | if (!JS::Construct(rq.cx, valueVector2D, args, ret)) | ||||
FAIL_VOID("Failed to construct Vector2D object"); | FAIL_VOID("Failed to construct Vector2D object"); | ||||
} | } | ||||
template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u8>& val) | template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u8>& val) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
u32 length = (u32)(val.m_W * val.m_H); | u32 length = (u32)(val.m_W * val.m_H); | ||||
u32 nbytes = (u32)(length * sizeof(u8)); | u32 nbytes = (u32)(length * sizeof(u8)); | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator