Index: binaries/data/mods/_test.sim/simulation/components/test-hotload1.js =================================================================== --- binaries/data/mods/_test.sim/simulation/components/test-hotload1.js +++ binaries/data/mods/_test.sim/simulation/components/test-hotload1.js @@ -16,11 +16,11 @@ function HotloadB() {} HotloadB.prototype.Init = function() { - this.x = +this.template.x; + this.x = +this.template.x; }; HotloadB.prototype.GetX = function() { - return this.x*2; + return this.x*2; }; Engine.RegisterComponentType(IID_Test1, "HotloadB", HotloadB); Index: binaries/data/mods/_test.sim/simulation/components/test-modding1.js =================================================================== --- /dev/null +++ binaries/data/mods/_test.sim/simulation/components/test-modding1.js @@ -0,0 +1,13 @@ +function Modding() {} + +Modding.prototype.Schema = ""; + +Modding.prototype.Init = function() { + this.x = +this.template.x; +}; + +Modding.prototype.GetX = function() { + return this.x; +}; + +Engine.RegisterComponentType(IID_Test1, "Modding", Modding); Index: binaries/data/mods/_test.sim/simulation/components/test-modding2.js =================================================================== --- /dev/null +++ binaries/data/mods/_test.sim/simulation/components/test-modding2.js @@ -0,0 +1,5 @@ +Modding.prototype.GetX = function() { + return this.x*10; +}; + +Engine.ReRegisterComponentType(IID_Test1, "Modding", Modding); Index: source/scriptinterface/tests/test_ScriptConversions.h =================================================================== --- source/scriptinterface/tests/test_ScriptConversions.h +++ source/scriptinterface/tests/test_ScriptConversions.h @@ -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 @@ -28,6 +28,9 @@ #include "jsapi.h" +cassert(JSVAL_INT_MIN == -2147483648); +cassert(JSVAL_INT_MAX == 2147483647); + class TestScriptConversions : public CxxTest::TestSuite { template @@ -124,22 +127,17 @@ roundtrip(0, "0"); roundtrip(123, "123"); roundtrip(-123, "-123"); - roundtrip(1073741822, "1073741822"); // JSVAL_INT_MAX-1 - roundtrip(1073741823, "1073741823"); // JSVAL_INT_MAX - roundtrip(-1073741823, "-1073741823"); // JSVAL_INT_MIN+1 - roundtrip(-1073741824, "-1073741824"); // JSVAL_INT_MIN + roundtrip(2147483646, "2147483646"); // JSVAL_INT_MAX-1 + roundtrip(2147483647, "2147483647"); // JSVAL_INT_MAX + roundtrip(-2147483647, "-2147483647"); // JSVAL_INT_MIN+1 + roundtrip(-2147483648, "-2147483648"); // JSVAL_INT_MIN roundtrip(0, "0"); roundtrip(123, "123"); - roundtrip(1073741822, "1073741822"); // JSVAL_INT_MAX-1 - roundtrip(1073741823, "1073741823"); // JSVAL_INT_MAX + roundtrip(2147483646, "2147483646"); // JSVAL_INT_MAX-1 + roundtrip(2147483647, "2147483647"); // JSVAL_INT_MAX - { - TestLogger log; // swallow warnings about values not being stored as integer JS::Values - roundtrip(1073741824, "1073741824"); // JSVAL_INT_MAX+1 - roundtrip(-1073741825, "-1073741825"); // JSVAL_INT_MIN-1 - roundtrip(1073741824, "1073741824"); // JSVAL_INT_MAX+1 - } + roundtrip(2147483648, "2147483648"); // JSVAL_INT_MAX+1 std::string s1 = "test"; s1[1] = '\0'; Index: source/scriptinterface/tests/test_ScriptInterface.h =================================================================== --- source/scriptinterface/tests/test_ScriptInterface.h +++ source/scriptinterface/tests/test_ScriptInterface.h @@ -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 @@ 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); + } }; Index: source/simulation2/tests/test_ComponentManager.h =================================================================== --- source/simulation2/tests/test_ComponentManager.h +++ source/simulation2/tests/test_ComponentManager.h @@ -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 @@ TS_ASSERT_EQUALS(static_cast (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, "100"), 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 (man.QueryInterface(ent1, IID_Test1))->GetX(), 100); + + TS_ASSERT(man.LoadScript(L"simulation/components/test-modding2.js")); + + TS_ASSERT_EQUALS(static_cast (man.QueryInterface(ent1, IID_Test1))->GetX(), 1000); + man.AddComponent(hnd2, man.LookupCID("Modding"), testParam); + TS_ASSERT_EQUALS(static_cast (man.QueryInterface(ent2, IID_Test1))->GetX(), 1000); + } + void test_serialization() { CSimContext context;