diff --git a/binaries/data/mods/_test.sim/globalscripts/test-global-helper.js b/binaries/data/mods/_test.sim/globalscripts/test-global-helper.js
index 884d084927..87fa440110 100644
a
|
b
|
Vector2D.prototype.add = function(v)
|
21 | 21 | return this; |
22 | 22 | }; |
23 | 23 | |
| 24 | Vector2D.prototype.nop = function(v) |
| 25 | { |
| 26 | warn("foo bar baz"); |
| 27 | return "working"; |
| 28 | }; |
| 29 | |
24 | 30 | function Vector3D(x, y, z) |
25 | 31 | { |
26 | 32 | if (arguments.length == 3) |
diff --git a/binaries/data/mods/_test.sim/simulation/components/test-serialize.js b/binaries/data/mods/_test.sim/simulation/components/test-serialize.js
index 9893d6d794..79ef74475f 100644
a
|
b
|
TestScript1_consts.prototype.GetX = function() {
|
88 | 88 | }; |
89 | 89 | |
90 | 90 | Engine.RegisterComponentType(IID_Test1, "TestScript1_consts", TestScript1_consts); |
| 91 | |
| 92 | // -------- // |
| 93 | |
| 94 | function TestScript1_vector() {} |
| 95 | |
| 96 | TestScript1_vector.prototype.Init = function() { |
| 97 | this.list = [ {pos: new Vector2D(1, 2)}]; |
| 98 | warn("foo"); |
| 99 | warn(typeof this.list[0].pos); |
| 100 | warn(""+ (this.list[0].pos instanceof Vector2D)); |
| 101 | }; |
| 102 | |
| 103 | TestScript1_vector.prototype.GetX = function() { |
| 104 | warn(typeof this.list[0].pos); |
| 105 | warn("" + (this.list[0].pos instanceof Vector2D)); |
| 106 | this.list[0].pos.add({x: 1, y: 1}); // How not to write a getter; mostly to make sure it really is that type |
| 107 | |
| 108 | return this.list[0].pos.x; |
| 109 | }; |
| 110 | |
| 111 | Engine.RegisterComponentType(IID_Test1, "TestScript1_vector", TestScript1_vector); |
diff --git a/source/simulation2/tests/test_ComponentManager.h b/source/simulation2/tests/test_ComponentManager.h
index 73fcfacf20..7000ba80fc 100644
a
|
b
|
public:
|
716 | 716 | man.LoadComponentTypes(); |
717 | 717 | TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js")); |
718 | 718 | |
719 | | entity_id_t ent1 = 1, ent2 = 2, ent3 = 3, ent4 = 4; |
| 719 | entity_id_t ent1 = 1, ent2 = 2, ent3 = 3, ent4 = 4, ent5 = 5; |
720 | 720 | CEntityHandle hnd1 = man.AllocateEntityHandle(ent1); |
721 | 721 | CEntityHandle hnd2 = man.AllocateEntityHandle(ent2); |
722 | 722 | CEntityHandle hnd3 = man.AllocateEntityHandle(ent3); |
723 | 723 | CEntityHandle hnd4 = man.AllocateEntityHandle(ent4); |
| 724 | CEntityHandle hnd5 = man.AllocateEntityHandle(ent5); |
724 | 725 | CParamNode noParam; |
725 | 726 | |
726 | 727 | CParamNode testParam; |
… |
… |
public:
|
729 | 730 | man.AddComponent(hnd1, man.LookupCID("TestScript1_values"), testParam); |
730 | 731 | man.AddComponent(hnd2, man.LookupCID("TestScript1_entity"), testParam); |
731 | 732 | |
| 733 | // TODO is this actually accurate? |
732 | 734 | // TODO: Since the upgrade to SpiderMonkey v24 this test won't be able to correctly represent |
733 | 735 | // non-tree structures because sharp variables were removed (bug 566700). |
734 | 736 | // This also affects the debug serializer and it could make sense to implement correct serialization again. |
… |
… |
public:
|
736 | 738 | |
737 | 739 | man.AddComponent(hnd4, man.LookupCID("TestScript1_custom"), testParam); |
738 | 740 | |
| 741 | man.AddComponent(hnd5, man.LookupCID("TestScript1_vector"), testParam); |
| 742 | |
739 | 743 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), 1234); |
740 | 744 | { |
741 | 745 | TestLogger log; // swallow warnings about this.entity being read-only |
742 | 746 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), (int)ent2); |
743 | 747 | } |
744 | 748 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent3, IID_Test1))->GetX(), 8); |
| 749 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent5, IID_Test1))->GetX(), 2); |
745 | 750 | |
746 | 751 | std::stringstream debugStream; |
747 | 752 | TS_ASSERT(man.DumpDebugState(debugStream, true)); |
… |
… |
entities:\n\
|
780 | 785 | object: {\n\ |
781 | 786 | \"c\": 1\n\ |
782 | 787 | }\n\ |
| 788 | \n\ |
| 789 | - id: 5\n\ |
| 790 | TestScript1_vector:\n\ |
| 791 | object: {\n\ |
| 792 | \"list\": [\n\ |
| 793 | {\n\ |
| 794 | \"pos\": {\n\ |
| 795 | \"x\": 2,\n\ |
| 796 | \"y\": 3\n\ |
| 797 | }\n\ |
| 798 | }\n\ |
| 799 | ]\n\ |
| 800 | }\n\ |
783 | 801 | \n" |
784 | 802 | ); |
785 | 803 | |
… |
… |
entities:\n\
|
799 | 817 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man2.QueryInterface(ent2, IID_Test1))->GetX(), (int)ent2); |
800 | 818 | } |
801 | 819 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man2.QueryInterface(ent3, IID_Test1))->GetX(), 12); |
| 820 | TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent5, IID_Test1))->GetX(), 3); |
802 | 821 | } |
803 | 822 | |
804 | 823 | void test_script_serialization_errors() |
diff --git a/source/simulation2/tests/test_Serializer.h b/source/simulation2/tests/test_Serializer.h
index d9aee50552..304675286a 100644
a
|
b
|
public:
|
289 | 289 | |
290 | 290 | // TODO: test exceptions more thoroughly |
291 | 291 | |
292 | | void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL) |
| 292 | void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL, bool loadglobalscripts = false) |
293 | 293 | { |
294 | 294 | ScriptInterface script("Test", "Test", g_ScriptRuntime); |
| 295 | |
| 296 | if (loadglobalscripts) |
| 297 | script.LoadGlobalScripts(); |
| 298 | |
295 | 299 | JSContext* cx = script.GetContext(); |
296 | 300 | JSAutoRequest rq(cx); |
297 | 301 | |
… |
… |
public:
|
311 | 315 | |
312 | 316 | serialize.ScriptVal("script", &obj); |
313 | 317 | |
| 318 | // TODO hack |
| 319 | if (loadglobalscripts) { |
| 320 | std::string foo_; |
| 321 | TSM_ASSERT(msg, script.CallFunction(obj, "nop", foo_)); |
| 322 | TS_ASSERT_STR_EQUALS(foo_, "foobar"); |
| 323 | } |
| 324 | |
314 | 325 | if (expstream) |
315 | 326 | { |
316 | 327 | TSM_ASSERT_STREAM(msg, stream, expstreamlen, expstream); |
… |
… |
public:
|
327 | 338 | std::string source; |
328 | 339 | TSM_ASSERT(msg, script.CallFunction(newobj, "toSource", source)); |
329 | 340 | TS_ASSERT_STR_EQUALS(source, expected); |
| 341 | |
| 342 | // TODO hack |
| 343 | if (loadglobalscripts) { |
| 344 | std::string foo; |
| 345 | TSM_ASSERT(msg, script.CallFunction(newobj, "nop", foo)); |
| 346 | TS_ASSERT_STR_EQUALS(foo, "foobar"); |
| 347 | } |
330 | 348 | } |
331 | 349 | |
332 | 350 | void test_script_basic() |
… |
… |
public:
|
717 | 735 | |
718 | 736 | // TODO: prototype objects |
719 | 737 | |
| 738 | void test_script_vector() |
| 739 | { |
| 740 | g_VFS = CreateVfs(20 * MiB); |
| 741 | TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/"mods"/"_test.sim", VFS_MOUNT_MUST_EXIST)); |
| 742 | TS_ASSERT_OK(g_VFS->Mount(L"cache", DataDir()/"_testcache")); |
| 743 | |
| 744 | helper_script_roundtrip("Vector2D", |
| 745 | "var a = new Vector2D(1, 2); a.add({x:1, y:1}); warn(\"\"+(a instanceof Vector2D)); a", |
| 746 | /* expected: */ |
| 747 | "({x:2, y:3})", |
| 748 | /* expected stream */ |
| 749 | 27, |
| 750 | "\x03" // SCRIPT_TYPE_OBJECT |
| 751 | "\x02\0\0\0" // num props |
| 752 | "\x01\x01\0\0\0" "x" // "x" |
| 753 | "\x05" // SCRIPT_TYPE_INT |
| 754 | "\x02\0\0\0" // 2 |
| 755 | |
| 756 | "\x01\x01\0\0\0" "y" // "y" |
| 757 | "\x05" // SCRIPT_TYPE_INT |
| 758 | "\x03\0\0\0" // 3 |
| 759 | , |
| 760 | NULL, |
| 761 | true |
| 762 | |
| 763 | ); |
| 764 | g_VFS.reset(); |
| 765 | } |
| 766 | |
720 | 767 | void test_script_nonfinite() |
721 | 768 | { |
722 | 769 | helper_script_roundtrip("nonfinite", "[0, Infinity, -Infinity, NaN, -1/Infinity]", "[0, Infinity, -Infinity, NaN, -0]"); |
… |
… |
public:
|
882 | 929 | DeleteDirectory(DataDir()/"_testcache"); |
883 | 930 | CXeromyces::Terminate(); |
884 | 931 | } |
885 | | |
886 | 932 | }; |