1
0
forked from 0ad/0ad

Update of the scripting tests. Test some modding features and update JS::Value integer limits.

Reviewed By: wraitii
Differential Revision: https://code.wildfiregames.com/D1850
This was SVN commit r22513.
This commit is contained in:
Nicolas Auvray 2019-07-19 15:41:27 +00:00
parent 6d332c0822
commit 6230bbea24
6 changed files with 81 additions and 20 deletions

View File

@ -20,7 +20,7 @@ HotloadB.prototype.Init = function() {
};
HotloadB.prototype.GetX = function() {
return this.x*2;
return this.x * 2;
};
Engine.RegisterComponentType(IID_Test1, "HotloadB", HotloadB);

View File

@ -0,0 +1,13 @@
function Modding() {}
Modding.prototype.Schema = "<ref name='anything'/>";
Modding.prototype.Init = function() {
this.x = +this.template.x;
};
Modding.prototype.GetX = function() {
return this.x;
};
Engine.RegisterComponentType(IID_Test1, "Modding", Modding);

View File

@ -0,0 +1,5 @@
Modding.prototype.GetX = function() {
return this.x * 10;
};
Engine.ReRegisterComponentType(IID_Test1, "Modding", Modding);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -124,22 +124,17 @@ public:
roundtrip<i32>(0, "0");
roundtrip<i32>(123, "123");
roundtrip<i32>(-123, "-123");
roundtrip<i32>(1073741822, "1073741822"); // JSVAL_INT_MAX-1
roundtrip<i32>(1073741823, "1073741823"); // JSVAL_INT_MAX
roundtrip<i32>(-1073741823, "-1073741823"); // JSVAL_INT_MIN+1
roundtrip<i32>(-1073741824, "-1073741824"); // JSVAL_INT_MIN
roundtrip<i32>(JSVAL_INT_MAX - 1, "2147483646");
roundtrip<i32>(JSVAL_INT_MAX, "2147483647");
roundtrip<i32>(JSVAL_INT_MIN + 1, "-2147483647");
roundtrip<i32>(JSVAL_INT_MIN, "-2147483648");
roundtrip<u32>(0, "0");
roundtrip<u32>(123, "123");
roundtrip<u32>(1073741822, "1073741822"); // JSVAL_INT_MAX-1
roundtrip<u32>(1073741823, "1073741823"); // JSVAL_INT_MAX
roundtrip<u32>(JSVAL_INT_MAX - 1, "2147483646");
roundtrip<u32>(JSVAL_INT_MAX, "2147483647");
{
TestLogger log; // swallow warnings about values not being stored as integer JS::Values
roundtrip<i32>(1073741824, "1073741824"); // JSVAL_INT_MAX+1
roundtrip<i32>(-1073741825, "-1073741825"); // JSVAL_INT_MIN-1
roundtrip<u32>(1073741824, "1073741824"); // JSVAL_INT_MAX+1
}
roundtrip<u32>(static_cast<u32>(JSVAL_INT_MAX) + 1, "2147483648");
std::string s1 = "test";
s1[1] = '\0';
@ -184,9 +179,9 @@ public:
// using new uninitialized variables each time to be sure the test doesn't succeeed if ToJSVal doesn't touch the value at all.
JS::RootedValue val0(cx), val1(cx), val2(cx), val3(cx), val4(cx), val5(cx), val6(cx), val7(cx), val8(cx);
ScriptInterface::ToJSVal<i32>(cx, &val0, 0);
ScriptInterface::ToJSVal<i32>(cx, &val1, 2147483646); // JSVAL_INT_MAX-1
ScriptInterface::ToJSVal<i32>(cx, &val2, 2147483647); // JSVAL_INT_MAX
ScriptInterface::ToJSVal<i32>(cx, &val3, -2147483647); // JSVAL_INT_MIN+1
ScriptInterface::ToJSVal<i32>(cx, &val1, JSVAL_INT_MAX - 1);
ScriptInterface::ToJSVal<i32>(cx, &val2, JSVAL_INT_MAX);
ScriptInterface::ToJSVal<i32>(cx, &val3, JSVAL_INT_MIN + 1);
ScriptInterface::ToJSVal<i32>(cx, &val4, -(i64)2147483648u); // JSVAL_INT_MIN
TS_ASSERT(val0.isInt32());
TS_ASSERT(val1.isInt32());

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -254,4 +254,27 @@ public:
TS_ASSERT(script.ParseJSON(stringified, &val));
TS_ASSERT_STR_EQUALS(script.ToString(&val), "({x:1, z:[2, \"3\\u263A\\uFFFD\"], y:true})");
}
// This function tests a common way to mod functions, by crating a wrapper that
// extends the functionality and is then assigned to the name of the function.
void test_function_override()
{
ScriptInterface script("Test", "Test", g_ScriptRuntime);
JSContext* cx = script.GetContext();
JSAutoRequest rq(cx);
TS_ASSERT(script.Eval(
"function f() { return 1; }"
"f = (function (originalFunction) {"
"return function () { return originalFunction() + 1; }"
"})(f);"
));
JS::RootedValue out(cx);
TS_ASSERT(script.Eval("f()", &out));
int outNbr = 0;
ScriptInterface::FromJSVal(cx, out, outNbr);
TS_ASSERT_EQUALS(2, outNbr);
}
};

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2019 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -617,6 +617,31 @@ public:
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent4, IID_Test1))->GetX(), 200);
}
void test_script_modding()
{
CSimContext context;
CComponentManager man(context, g_ScriptRuntime);
man.LoadComponentTypes();
CParamNode testParam;
TS_ASSERT_EQUALS(CParamNode::LoadXMLString(testParam, "<x>100</x>"), PSRETURN_OK);
entity_id_t ent1 = 1, ent2 = 2;
CEntityHandle hnd1 = man.AllocateEntityHandle(ent1);
CEntityHandle hnd2 = man.AllocateEntityHandle(ent2);
TS_ASSERT(man.LoadScript(L"simulation/components/test-modding1.js"));
man.AddComponent(hnd1, man.LookupCID("Modding"), testParam);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), 100);
TS_ASSERT(man.LoadScript(L"simulation/components/test-modding2.js"));
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), 1000);
man.AddComponent(hnd2, man.LookupCID("Modding"), testParam);
TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), 1000);
}
void test_serialization()
{
CSimContext context;