From 7acdde5c86ba83e24c7e635bebc805843728e5c9 Mon Sep 17 00:00:00 2001 From: Yves Date: Fri, 25 Apr 2014 21:19:51 +0000 Subject: [PATCH] Changes tests to use global g_ScriptRuntime instead of creating a new JSRuntime for each test. The tests crashed on my Debian systems but not on my Ubuntu system. The crash happened in line 142 of SpiderMonkey's ThreadLocal.h. I know that I had to use a workarounnd for contexts to avoid destroying the context that was created first. I also had in mind that in newer versions a JS_Init function got introduced which presumably solves this kind of issues. Based on this experience I assumed runtimes could have a similar problem and this patch indeed fixes the issues. Unfortunately the correct usage of JSRuntimes in that regard is not documented. There's only a rater misterious comment in JSAPI.h which hasn't been cleared up so far and is most likely completely outdated (https://bugzilla.mozilla.org/show_bug.cgi?id=992641). This was SVN commit r14995. --- source/graphics/tests/test_LOSTexture.h | 2 +- source/network/tests/test_Net.h | 4 +- source/network/tests/test_NetMessage.h | 2 +- .../tests/test_ScriptConversions.h | 8 +-- .../tests/test_ScriptInterface.h | 28 ++++----- source/scriptinterface/tests/test_ScriptVal.h | 2 +- .../components/tests/test_CommandQueue.h | 2 +- .../tests/test_ObstructionManager.h | 2 +- .../components/tests/test_Pathfinder.h | 4 +- .../components/tests/test_Position.h | 4 +- .../components/tests/test_RangeManager.h | 2 +- .../components/tests/test_scripts.h | 2 +- .../tests/test_CmpTemplateManager.h | 10 ++-- .../simulation2/tests/test_ComponentManager.h | 59 +++++++++---------- source/simulation2/tests/test_Serializer.h | 24 ++++---- source/simulation2/tests/test_Simulation2.h | 6 +- source/test_setup.cpp | 2 + 17 files changed, 79 insertions(+), 84 deletions(-) diff --git a/source/graphics/tests/test_LOSTexture.h b/source/graphics/tests/test_LOSTexture.h index b04fcb11b4..50c804c232 100644 --- a/source/graphics/tests/test_LOSTexture.h +++ b/source/graphics/tests/test_LOSTexture.h @@ -27,7 +27,7 @@ class TestLOSTexture : public CxxTest::TestSuite public: void test_basic() { - CSimulation2 sim(NULL, ScriptInterface::CreateRuntime(), NULL); + CSimulation2 sim(NULL, g_ScriptRuntime, NULL); CLOSTexture tex(sim); const ssize_t size = 8; diff --git a/source/network/tests/test_Net.h b/source/network/tests/test_Net.h index 6bd7a5f9cd..94440a729c 100644 --- a/source/network/tests/test_Net.h +++ b/source/network/tests/test_Net.h @@ -134,7 +134,7 @@ public: // This doesn't actually test much, it just runs a very quick multiplayer game // and prints a load of debug output so you can see if anything funny's going on - ScriptInterface scriptInterface("Engine", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface scriptInterface("Engine", "Test", g_ScriptRuntime); TestStdoutLogger logger; std::vector clients; @@ -196,7 +196,7 @@ public: void test_rejoin_DISABLED() { - ScriptInterface scriptInterface("Engine", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface scriptInterface("Engine", "Test", g_ScriptRuntime); TestStdoutLogger logger; std::vector clients; diff --git a/source/network/tests/test_NetMessage.h b/source/network/tests/test_NetMessage.h index f25a2d84ef..494eaa9fe8 100644 --- a/source/network/tests/test_NetMessage.h +++ b/source/network/tests/test_NetMessage.h @@ -26,7 +26,7 @@ class TestNetMessage : public CxxTest::TestSuite public: void test_sim() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); CScriptValRooted val; script.Eval("[4]", val); CSimulationMessage msg(script, 1, 2, 3, val.get()); diff --git a/source/scriptinterface/tests/test_ScriptConversions.h b/source/scriptinterface/tests/test_ScriptConversions.h index 7c5a7e03f3..79295430ce 100644 --- a/source/scriptinterface/tests/test_ScriptConversions.h +++ b/source/scriptinterface/tests/test_ScriptConversions.h @@ -30,7 +30,7 @@ class TestScriptConversions : public CxxTest::TestSuite template void convert_to(const T& value, const std::string& expected) { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); @@ -48,7 +48,7 @@ class TestScriptConversions : public CxxTest::TestSuite template void roundtrip(const T& value, const char* expected) { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); @@ -125,7 +125,7 @@ public: void test_integers() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); @@ -158,7 +158,7 @@ public: roundtrip(-std::numeric_limits::infinity(), "-Infinity"); convert_to(std::numeric_limits::quiet_NaN(), "NaN"); // can't use roundtrip since nan != nan - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); diff --git a/source/scriptinterface/tests/test_ScriptInterface.h b/source/scriptinterface/tests/test_ScriptInterface.h index 86065ef136..8c07d2fc9e 100644 --- a/source/scriptinterface/tests/test_ScriptInterface.h +++ b/source/scriptinterface/tests/test_ScriptInterface.h @@ -28,7 +28,7 @@ class TestScriptInterface : public CxxTest::TestSuite public: void test_loadscript_basic() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); TestLogger logger; TS_ASSERT(script.LoadScript(L"test.js", "var x = 1+1;")); TS_ASSERT_WSTR_NOT_CONTAINS(logger.GetOutput(), L"JavaScript error"); @@ -37,7 +37,7 @@ public: void test_loadscript_error() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); TestLogger logger; TS_ASSERT(!script.LoadScript(L"test.js", "1+")); TS_ASSERT_WSTR_CONTAINS(logger.GetOutput(), L"JavaScript error: test.js line 1\nSyntaxError: syntax error"); @@ -45,7 +45,7 @@ public: void test_loadscript_strict_warning() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); TestLogger logger; TS_ASSERT(script.LoadScript(L"test.js", "1+1;")); TS_ASSERT_WSTR_CONTAINS(logger.GetOutput(), L"JavaScript warning: test.js line 1\nuseless expression"); @@ -53,7 +53,7 @@ public: void test_loadscript_strict_error() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); TestLogger logger; TS_ASSERT(!script.LoadScript(L"test.js", "with(1){}")); TS_ASSERT_WSTR_CONTAINS(logger.GetOutput(), L"JavaScript error: test.js line 1\nSyntaxError: strict mode code may not contain \'with\' statements"); @@ -61,9 +61,8 @@ public: void test_clone_basic() { - shared_ptr runtime = ScriptInterface::CreateRuntime(); - ScriptInterface script1("Test", "Test", runtime); - ScriptInterface script2("Test", "Test", runtime); + ScriptInterface script1("Test", "Test", g_ScriptRuntime); + ScriptInterface script2("Test", "Test", g_ScriptRuntime); CScriptVal obj1; TS_ASSERT(script1.Eval("({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})", obj1)); @@ -78,10 +77,8 @@ public: void test_clone_getters() { // The tests should be run with JS_SetGCZeal so this can try to find GC bugs - - shared_ptr runtime = ScriptInterface::CreateRuntime(); - ScriptInterface script1("Test", "Test", runtime); - ScriptInterface script2("Test", "Test", runtime); + ScriptInterface script1("Test", "Test", g_ScriptRuntime); + ScriptInterface script2("Test", "Test", g_ScriptRuntime); CScriptVal obj1; TS_ASSERT(script1.Eval("var s = '?'; var v = ({get x() { return 123 }, 'y': {'w':{get z() { delete v.y; delete v.n; v = null; s += s; return 4 }}}, 'n': 100}); v", obj1)); @@ -95,9 +92,8 @@ public: void test_clone_cyclic() { - shared_ptr runtime = ScriptInterface::CreateRuntime(); - ScriptInterface script1("Test", "Test", runtime); - ScriptInterface script2("Test", "Test", runtime); + ScriptInterface script1("Test", "Test", g_ScriptRuntime); + ScriptInterface script2("Test", "Test", g_ScriptRuntime); CScriptVal obj1; TS_ASSERT(script1.Eval("var x = []; x[0] = x; ({'a': x, 'b': x})", obj1)); @@ -121,7 +117,7 @@ public: void test_random() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); double d1, d2; TS_ASSERT(script.Eval("Math.random()", d1)); @@ -141,7 +137,7 @@ public: void test_json() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::string input = "({'x':1,'z':[2,'3\\u263A\\ud800'],\"y\":true})"; CScriptValRooted val; diff --git a/source/scriptinterface/tests/test_ScriptVal.h b/source/scriptinterface/tests/test_ScriptVal.h index 77fa10b83c..88c492ff2e 100644 --- a/source/scriptinterface/tests/test_ScriptVal.h +++ b/source/scriptinterface/tests/test_ScriptVal.h @@ -27,7 +27,7 @@ class TestScriptVal : public CxxTest::TestSuite public: void test_rooting() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); diff --git a/source/simulation2/components/tests/test_CommandQueue.h b/source/simulation2/components/tests/test_CommandQueue.h index c010cc8486..cb4e81b74f 100644 --- a/source/simulation2/components/tests/test_CommandQueue.h +++ b/source/simulation2/components/tests/test_CommandQueue.h @@ -34,7 +34,7 @@ public: void test_basic() { - ComponentTestHelper test(ScriptInterface::CreateRuntime()); + ComponentTestHelper test(g_ScriptRuntime); std::vector empty; diff --git a/source/simulation2/components/tests/test_ObstructionManager.h b/source/simulation2/components/tests/test_ObstructionManager.h index b909b78fae..ecbfb91212 100644 --- a/source/simulation2/components/tests/test_ObstructionManager.h +++ b/source/simulation2/components/tests/test_ObstructionManager.h @@ -68,7 +68,7 @@ public: ent3z = ent2z + ent2r + ent3r; // ensure it just touches the border of ent2 ent3g = ent3; - testHelper = new ComponentTestHelper(ScriptInterface::CreateRuntime()); + testHelper = new ComponentTestHelper(g_ScriptRuntime); cmp = testHelper->Add(CID_ObstructionManager, ""); cmp->SetBounds(fixed::FromInt(0), fixed::FromInt(0), fixed::FromInt(1000), fixed::FromInt(1000)); diff --git a/source/simulation2/components/tests/test_Pathfinder.h b/source/simulation2/components/tests/test_Pathfinder.h index a65dc4c6e2..6a233c7393 100644 --- a/source/simulation2/components/tests/test_Pathfinder.h +++ b/source/simulation2/components/tests/test_Pathfinder.h @@ -59,7 +59,7 @@ public: { CTerrain terrain; - CSimulation2 sim2(NULL, ScriptInterface::CreateRuntime(), &terrain); + CSimulation2 sim2(NULL, g_ScriptRuntime, &terrain); sim2.LoadDefaultScripts(); sim2.ResetState(); @@ -112,7 +112,7 @@ public: CTerrain terrain; terrain.Initialize(5, NULL); - CSimulation2 sim2(NULL, ScriptInterface::CreateRuntime(), &terrain); + CSimulation2 sim2(NULL, g_ScriptRuntime, &terrain); sim2.LoadDefaultScripts(); sim2.ResetState(); diff --git a/source/simulation2/components/tests/test_Position.h b/source/simulation2/components/tests/test_Position.h index bd2f7b7e29..21acf6ad74 100644 --- a/source/simulation2/components/tests/test_Position.h +++ b/source/simulation2/components/tests/test_Position.h @@ -39,7 +39,7 @@ public: void test_basic() { - ComponentTestHelper test(ScriptInterface::CreateRuntime()); + ComponentTestHelper test(g_ScriptRuntime); MockTerrain terrain; test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain); @@ -111,7 +111,7 @@ public: void test_serialize() { - ComponentTestHelper test(ScriptInterface::CreateRuntime()); + ComponentTestHelper test(g_ScriptRuntime); MockTerrain terrain; test.AddMock(SYSTEM_ENTITY, IID_Terrain, terrain); diff --git a/source/simulation2/components/tests/test_RangeManager.h b/source/simulation2/components/tests/test_RangeManager.h index 5b081af2b3..3c03dcffc4 100644 --- a/source/simulation2/components/tests/test_RangeManager.h +++ b/source/simulation2/components/tests/test_RangeManager.h @@ -77,7 +77,7 @@ public: void test_basic() { - ComponentTestHelper test(ScriptInterface::CreateRuntime()); + ComponentTestHelper test(g_ScriptRuntime); ICmpRangeManager* cmp = test.Add(CID_RangeManager, ""); diff --git a/source/simulation2/components/tests/test_scripts.h b/source/simulation2/components/tests/test_scripts.h index e28238e33a..e00f0653ec 100644 --- a/source/simulation2/components/tests/test_scripts.h +++ b/source/simulation2/components/tests/test_scripts.h @@ -68,7 +68,7 @@ public: for (size_t i = 0; i < paths.size(); ++i) { CSimContext context; - CComponentManager componentManager(context, ScriptInterface::CreateRuntime(), true); + CComponentManager componentManager(context, g_ScriptRuntime, true); ScriptTestSetup(componentManager.GetScriptInterface()); diff --git a/source/simulation2/tests/test_CmpTemplateManager.h b/source/simulation2/tests/test_CmpTemplateManager.h index 5f3d082f99..89e8394332 100644 --- a/source/simulation2/tests/test_CmpTemplateManager.h +++ b/source/simulation2/tests/test_CmpTemplateManager.h @@ -52,7 +52,7 @@ public: void test_LoadTemplate() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2; @@ -114,7 +114,7 @@ public: void test_LoadTemplate_scriptcache() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2; @@ -153,7 +153,7 @@ public: void test_LoadTemplate_errors() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2; @@ -185,7 +185,7 @@ public: void test_LoadTemplate_multiple() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2; @@ -244,7 +244,7 @@ public: void test_load_all_DISABLED() // disabled since it's a bit slow and noisy { CTerrain dummy; - CSimulation2 sim(NULL, ScriptInterface::CreateRuntime(), &dummy); + CSimulation2 sim(NULL, g_ScriptRuntime, &dummy); sim.LoadDefaultScripts(); sim.ResetState(); diff --git a/source/simulation2/tests/test_ComponentManager.h b/source/simulation2/tests/test_ComponentManager.h index 4850b566cf..d496578918 100644 --- a/source/simulation2/tests/test_ComponentManager.h +++ b/source/simulation2/tests/test_ComponentManager.h @@ -59,14 +59,14 @@ public: void test_Load() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); } void test_LookupCID() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT_EQUALS(man.LookupCID("Test1A"), (int)CID_Test1A); @@ -76,7 +76,7 @@ public: void test_AllocateNewEntity() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)2); TS_ASSERT_EQUALS(man.AllocateNewEntity(), (u32)3); @@ -99,7 +99,7 @@ public: void test_AddComponent_errors() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); CEntityHandle hnd1 = man.AllocateEntityHandle(1); @@ -122,7 +122,7 @@ public: void test_QueryInterface() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2; @@ -147,7 +147,7 @@ public: void test_SendMessage() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2, ent3 = 3, ent4 = 4; @@ -221,7 +221,7 @@ public: void test_ParamNode() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2; @@ -242,7 +242,7 @@ public: void test_script_basic() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test.js")); @@ -286,7 +286,7 @@ public: void test_script_helper_basic() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-helper.js")); TS_ASSERT(man.LoadScript(L"simulation/helpers/test-helper.js")); @@ -303,7 +303,7 @@ public: void test_script_global_helper() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-global-helper.js")); @@ -319,7 +319,7 @@ public: void test_script_interface() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/interfaces/test-interface.js")); TS_ASSERT(man.LoadScript(L"simulation/components/test-interface.js")); @@ -337,7 +337,7 @@ public: void test_script_errors() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); ScriptTestSetup(man.m_ScriptInterface); man.LoadComponentTypes(); @@ -354,7 +354,7 @@ public: void test_script_entityID() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); ScriptTestSetup(man.m_ScriptInterface); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-entityid.js")); @@ -374,7 +374,7 @@ public: void test_script_QueryInterface() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-query.js")); @@ -395,7 +395,7 @@ public: void test_script_AddEntity() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); @@ -428,7 +428,7 @@ public: void test_script_AddLocalEntity() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-addentity.js")); TS_ASSERT(man.LoadScript(L"simulation/components/addentity/test-addentity.js")); @@ -461,7 +461,7 @@ public: void test_script_DestroyEntity() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-destroyentity.js")); @@ -481,7 +481,7 @@ public: void test_script_messages() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-msg.js")); @@ -514,7 +514,7 @@ public: void test_script_template() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-param.js")); @@ -536,7 +536,7 @@ public: void test_script_template_readonly() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-param.js")); @@ -558,7 +558,7 @@ public: void test_script_hotload() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-hotload1.js")); @@ -594,8 +594,7 @@ public: void test_serialization() { CSimContext context; - shared_ptr runtime = ScriptInterface::CreateRuntime(); - CComponentManager man(context, runtime); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); entity_id_t ent1 = 1, ent2 = 2, ent3 = FIRST_LOCAL_ENTITY; @@ -665,7 +664,7 @@ public: ); CSimContext context2; - CComponentManager man2(context2, runtime); + CComponentManager man2(context2, g_ScriptRuntime); man2.LoadComponentTypes(); TS_ASSERT(man2.QueryInterface(ent1, IID_Test1) == NULL); @@ -684,9 +683,8 @@ public: void test_script_serialization() { CSimContext context; - shared_ptr runtime = ScriptInterface::CreateRuntime(); - CComponentManager man(context, runtime); + CComponentManager man(context, g_ScriptRuntime); ScriptTestSetup(man.m_ScriptInterface); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); @@ -762,7 +760,7 @@ entities:\n\ TS_ASSERT(man.SerializeState(stateStream)); CSimContext context2; - CComponentManager man2(context2, runtime); + CComponentManager man2(context2, g_ScriptRuntime); man2.LoadComponentTypes(); TS_ASSERT(man2.LoadScript(L"simulation/components/test-serialize.js")); @@ -779,7 +777,7 @@ entities:\n\ void test_script_serialization_errors() { CSimContext context; - CComponentManager man(context, ScriptInterface::CreateRuntime()); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); @@ -797,9 +795,8 @@ entities:\n\ void test_script_serialization_template() { CSimContext context; - shared_ptr runtime = ScriptInterface::CreateRuntime(); - CComponentManager man(context, runtime); + CComponentManager man(context, g_ScriptRuntime); man.LoadComponentTypes(); TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); man.InitSystemEntity(); @@ -823,7 +820,7 @@ entities:\n\ TS_ASSERT(man.SerializeState(stateStream)); CSimContext context2; - CComponentManager man2(context2, runtime); + CComponentManager man2(context2, g_ScriptRuntime); man2.LoadComponentTypes(); TS_ASSERT(man2.LoadScript(L"simulation/components/test-serialize.js")); diff --git a/source/simulation2/tests/test_Serializer.h b/source/simulation2/tests/test_Serializer.h index 5be347fb77..91cff56a13 100644 --- a/source/simulation2/tests/test_Serializer.h +++ b/source/simulation2/tests/test_Serializer.h @@ -74,7 +74,7 @@ public: void test_Debug_basic() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::stringstream stream; CDebugSerializer serialize(script, stream); serialize.NumberI32_Unbounded("x", -123); @@ -85,7 +85,7 @@ public: void test_Debug_floats() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::stringstream stream; CDebugSerializer serialize(script, stream); serialize.NumberFloat_Unbounded("x", 1e4f); @@ -116,7 +116,7 @@ public: void test_Debug_types() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::stringstream stream; CDebugSerializer serialize(script, stream); @@ -147,7 +147,7 @@ public: void test_Std_basic() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::stringstream stream; CStdSerializer serialize(script, stream); @@ -173,7 +173,7 @@ public: void test_Std_types() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::stringstream stream; CStdSerializer serialize(script, stream); @@ -239,7 +239,7 @@ public: void test_Hash_basic() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); CHashSerializer serialize(script); serialize.NumberI32_Unbounded("x", -123); @@ -253,7 +253,7 @@ public: void test_bounds() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); std::stringstream stream; CDebugSerializer serialize(script, stream); serialize.NumberI32("x", 16, -16, 16); @@ -266,7 +266,7 @@ public: void test_script_basic() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); CScriptVal obj; TS_ASSERT(script.Eval("({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})", obj)); @@ -339,7 +339,7 @@ public: void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL) { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); CScriptVal obj; TSM_ASSERT(msg, script.Eval(input, obj)); @@ -566,7 +566,7 @@ public: void test_script_exceptions() { - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); CScriptVal obj; std::stringstream stream; @@ -598,7 +598,7 @@ public: { const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x"; - ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime()); + ScriptInterface script("Test", "Test", g_ScriptRuntime); CScriptVal obj; TS_ASSERT(script.Eval(input, obj)); @@ -640,7 +640,7 @@ public: CTerrain terrain; - CSimulation2 sim2(NULL, ScriptInterface::CreateRuntime(), &terrain); + CSimulation2 sim2(NULL, g_ScriptRuntime, &terrain); sim2.LoadDefaultScripts(); sim2.ResetState(); diff --git a/source/simulation2/tests/test_Simulation2.h b/source/simulation2/tests/test_Simulation2.h index 1692cc7e10..bf4f356dc9 100644 --- a/source/simulation2/tests/test_Simulation2.h +++ b/source/simulation2/tests/test_Simulation2.h @@ -57,7 +57,7 @@ public: void test_AddEntity() { - CSimulation2 sim(NULL, ScriptInterface::CreateRuntime(), &m_Terrain); + CSimulation2 sim(NULL, g_ScriptRuntime, &m_Terrain); TS_ASSERT(sim.LoadScripts(L"simulation/components/addentity/")); sim.ResetState(true, true); @@ -77,7 +77,7 @@ public: void test_DestroyEntity() { - CSimulation2 sim(NULL, ScriptInterface::CreateRuntime(), &m_Terrain); + CSimulation2 sim(NULL, g_ScriptRuntime, &m_Terrain); TS_ASSERT(sim.LoadScripts(L"simulation/components/addentity/")); sim.ResetState(true, true); @@ -129,7 +129,7 @@ public: void test_hotload_scripts() { - CSimulation2 sim(NULL, ScriptInterface::CreateRuntime(), &m_Terrain); + CSimulation2 sim(NULL, g_ScriptRuntime, &m_Terrain); TS_ASSERT_OK(CreateDirectories(DataDir()/"mods"/"_test.sim"/"simulation"/"components"/"hotload"/"", 0700)); diff --git a/source/test_setup.cpp b/source/test_setup.cpp index 8839b08fbc..870e58a587 100644 --- a/source/test_setup.cpp +++ b/source/test_setup.cpp @@ -81,12 +81,14 @@ class MiscSetup : public CxxTest::GlobalFixture ThreadUtil::SetMainThread(); g_Profiler2.Initialise(); + g_ScriptRuntime = ScriptInterface::CreateRuntime(); return true; } virtual bool tearDownWorld() { + g_ScriptRuntime.reset(); g_Profiler2.Shutdown(); return true;