1
0
forked from 0ad/0ad

Test FixedVector{2,3}D script conversions, and test calling functions of the prototypes.

Also ENSURE that the given value identifier is actually present in the
cache instead of creating one if it is not.

This was SVN commit r17603.
This commit is contained in:
leper 2016-01-04 21:41:40 +00:00
parent 4c4beb8450
commit b18cd3254c
4 changed files with 109 additions and 8 deletions

View File

@ -2,3 +2,44 @@ function GlobalSubtractionHelper(a, b)
{
return a-b;
}
function Vector2D(x, y)
{
if (arguments.length == 2)
{
this.x = x;
this.y = y;
}
else
this.x = this.y = 0;
}
Vector2D.prototype.add = function(v)
{
this.x += v.x;
this.y += v.y;
return this;
};
function Vector3D(x, y, z)
{
if (arguments.length == 3)
{
this.x = x;
this.y = y;
this.z = z;
}
else
this.x = this.y = this.z = 0;
}
Vector3D.prototype.add = function(v)
{
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
};
// make the prototypes easily accessible to C++
const Vector2Dprototype = Vector2D.prototype;
const Vector3Dprototype = Vector3D.prototype;

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2016 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -449,7 +449,9 @@ ScriptInterface::CxPrivate* ScriptInterface::GetScriptInterfaceAndCBData(JSConte
JS::Value ScriptInterface::GetCachedValue(CACHED_VAL valueIdentifier)
{
return m->m_ScriptValCache[valueIdentifier].get();
std::map<ScriptInterface::CACHED_VAL, DefPersistentRooted<JS::Value>>::iterator it = m->m_ScriptValCache.find(valueIdentifier);
ENSURE(it != m->m_ScriptValCache.end());
return it->second.get();
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2015 Wildfire Games.
/* Copyright (C) 2016 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -20,8 +20,11 @@
#include "scriptinterface/ScriptInterface.h"
#include "maths/Fixed.h"
#include "maths/FixedVector2D.h"
#include "maths/FixedVector3D.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "jsapi.h"
@ -31,6 +34,7 @@ class TestScriptConversions : public CxxTest::TestSuite
void convert_to(const T& value, const std::string& expected)
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
TS_ASSERT(script.LoadGlobalScripts());
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
@ -50,6 +54,7 @@ class TestScriptConversions : public CxxTest::TestSuite
void roundtrip(const T& value, const char* expected)
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
TS_ASSERT(script.LoadGlobalScripts());
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
@ -68,7 +73,44 @@ class TestScriptConversions : public CxxTest::TestSuite
TS_ASSERT_EQUALS(value, v2);
}
template <typename T>
void call_prototype_function(const T& u, const T& v, const std::string& func, const std::string& expected)
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
TS_ASSERT(script.LoadGlobalScripts());
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
JS::RootedValue v1(cx);
ScriptInterface::ToJSVal(cx, &v1, v);
JS::RootedValue u1(cx);
ScriptInterface::ToJSVal(cx, &u1, u);
T r;
JS::RootedValue r1(cx);
TS_ASSERT(script.CallFunction(u1, func.c_str(), v1, r));
ScriptInterface::ToJSVal(cx, &r1, r);
std::string source;
JS::RootedValue global(cx, script.GetGlobalObject());
TS_ASSERT(script.CallFunction(global, "uneval", r1, source));
TS_ASSERT_STR_EQUALS(source, expected);
}
public:
void setUp()
{
g_VFS = CreateVfs(20 * MiB);
TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/"mods"/"_test.sim", VFS_MOUNT_MUST_EXIST));
}
void tearDown()
{
g_VFS.reset();
}
void test_roundtrip()
{
roundtrip<bool>(true, "true");
@ -178,12 +220,10 @@ public:
TS_ASSERT(isnan(f));
}
// TODO: test vectors
// NOTE: fixed and vector conversions are defined in simulation2/scripting/EngineScriptConversions.cpp
void test_fixed()
{
// NOTE: fixed conversions are defined in simulation2/scripting/EngineScriptConversions.cpp
fixed f;
f.SetInternalValue(10590283);
@ -198,4 +238,22 @@ public:
f.SetInternalValue(2000000001);
roundtrip<fixed>(f, "30517.57814025879");
}
void test_vector2d()
{
CFixedVector2D v(fixed::Zero(), fixed::Pi());
roundtrip<CFixedVector2D>(v, "({x:0, y:3.1415863037109375})");
CFixedVector2D u(fixed::FromInt(1), fixed::Zero());
call_prototype_function<CFixedVector2D>(u, v, "add", "({x:1, y:3.1415863037109375})");
}
void test_vector3d()
{
CFixedVector3D v(fixed::Zero(), fixed::Pi(), fixed::FromInt(1));
roundtrip<CFixedVector3D>(v, "({x:0, y:3.1415863037109375, z:1})");
CFixedVector3D u(fixed::Pi(), fixed::Zero(), fixed::FromInt(2));
call_prototype_function<CFixedVector3D>(u, v, "add", "({x:3.1415863037109375, y:3.1415863037109375, z:3})");
}
};

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2012 Wildfire Games.
/* Copyright (C) 2016 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -291,7 +291,7 @@ template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::MutableH
ret.setObject(*obj);
}
// TODO: This is copy-pasted from scriptinterface/ScriptConversions.h (#define VECTOR stuff), would be nice to remove the duplication
// TODO: This is copy-pasted from scriptinterface/ScriptConversions.cpp (#define VECTOR stuff), would be nice to remove the duplication
template<> void ScriptInterface::ToJSVal<std::vector<CFixedVector2D> >(JSContext* cx, JS::MutableHandleValue ret, const std::vector<CFixedVector2D>& val)
{
JSAutoRequest rq(cx);