Index: ps/trunk/binaries/data/mods/_test.sim/simulation/components/test-hotload1.js =================================================================== --- ps/trunk/binaries/data/mods/_test.sim/simulation/components/test-hotload1.js +++ ps/trunk/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: ps/trunk/binaries/data/mods/_test.sim/simulation/components/test-modding1.js =================================================================== --- ps/trunk/binaries/data/mods/_test.sim/simulation/components/test-modding1.js +++ ps/trunk/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: ps/trunk/binaries/data/mods/_test.sim/simulation/components/test-modding2.js =================================================================== --- ps/trunk/binaries/data/mods/_test.sim/simulation/components/test-modding2.js +++ ps/trunk/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: ps/trunk/source/scriptinterface/tests/test_ScriptConversions.h =================================================================== --- ps/trunk/source/scriptinterface/tests/test_ScriptConversions.h +++ ps/trunk/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 @@ -124,22 +124,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(JSVAL_INT_MAX - 1, "2147483646"); + roundtrip(JSVAL_INT_MAX, "2147483647"); + roundtrip(JSVAL_INT_MIN + 1, "-2147483647"); + roundtrip(JSVAL_INT_MIN, "-2147483648"); roundtrip(0, "0"); roundtrip(123, "123"); - roundtrip(1073741822, "1073741822"); // JSVAL_INT_MAX-1 - roundtrip(1073741823, "1073741823"); // JSVAL_INT_MAX + roundtrip(JSVAL_INT_MAX - 1, "2147483646"); + roundtrip(JSVAL_INT_MAX, "2147483647"); - { - 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(static_cast(JSVAL_INT_MAX) + 1, "2147483648"); std::string s1 = "test"; s1[1] = '\0'; @@ -184,9 +179,9 @@ // 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(cx, &val0, 0); - ScriptInterface::ToJSVal(cx, &val1, 2147483646); // JSVAL_INT_MAX-1 - ScriptInterface::ToJSVal(cx, &val2, 2147483647); // JSVAL_INT_MAX - ScriptInterface::ToJSVal(cx, &val3, -2147483647); // JSVAL_INT_MIN+1 + ScriptInterface::ToJSVal(cx, &val1, JSVAL_INT_MAX - 1); + ScriptInterface::ToJSVal(cx, &val2, JSVAL_INT_MAX); + ScriptInterface::ToJSVal(cx, &val3, JSVAL_INT_MIN + 1); ScriptInterface::ToJSVal(cx, &val4, -(i64)2147483648u); // JSVAL_INT_MIN TS_ASSERT(val0.isInt32()); TS_ASSERT(val1.isInt32()); Index: ps/trunk/source/scriptinterface/tests/test_ScriptInterface.h =================================================================== --- ps/trunk/source/scriptinterface/tests/test_ScriptInterface.h +++ ps/trunk/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: ps/trunk/source/simulation2/tests/test_ComponentManager.h =================================================================== --- ps/trunk/source/simulation2/tests/test_ComponentManager.h +++ ps/trunk/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;