Ticket #4698: serialization_vector_js_object.diff

File serialization_vector_js_object.diff, 6.9 KB (added by fatherbushido, 7 years ago)
  • binaries/data/mods/_test.sim/globalscripts/test-global-helper.js

    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)  
    2121    return this;
    2222};
    2323
     24Vector2D.prototype.nop = function(v)
     25{
     26warn("foo bar baz");
     27    return "working";
     28};
     29
    2430function Vector3D(x, y, z)
    2531{
    2632    if (arguments.length == 3)
  • binaries/data/mods/_test.sim/simulation/components/test-serialize.js

    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() {  
    8888};
    8989
    9090Engine.RegisterComponentType(IID_Test1, "TestScript1_consts", TestScript1_consts);
     91
     92// -------- //
     93
     94function TestScript1_vector() {}
     95
     96TestScript1_vector.prototype.Init = function() {
     97    this.list = [ {pos: new Vector2D(1, 2)}];
     98warn("foo");
     99warn(typeof this.list[0].pos);
     100warn(""+ (this.list[0].pos instanceof Vector2D));
     101};
     102
     103TestScript1_vector.prototype.GetX = function() {
     104warn(typeof this.list[0].pos);
     105warn("" + (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
     111Engine.RegisterComponentType(IID_Test1, "TestScript1_vector", TestScript1_vector);
  • source/simulation2/tests/test_ComponentManager.h

    diff --git a/source/simulation2/tests/test_ComponentManager.h b/source/simulation2/tests/test_ComponentManager.h
    index 73fcfacf20..7000ba80fc 100644
    a b public:  
    716716        man.LoadComponentTypes();
    717717        TS_ASSERT(man.LoadScript(L"simulation/components/test-serialize.js"));
    718718
    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;
    720720        CEntityHandle hnd1 = man.AllocateEntityHandle(ent1);
    721721        CEntityHandle hnd2 = man.AllocateEntityHandle(ent2);
    722722        CEntityHandle hnd3 = man.AllocateEntityHandle(ent3);
    723723        CEntityHandle hnd4 = man.AllocateEntityHandle(ent4);
     724        CEntityHandle hnd5 = man.AllocateEntityHandle(ent5);
    724725        CParamNode noParam;
    725726
    726727        CParamNode testParam;
    public:  
    729730        man.AddComponent(hnd1, man.LookupCID("TestScript1_values"), testParam);
    730731        man.AddComponent(hnd2, man.LookupCID("TestScript1_entity"), testParam);
    731732
     733// TODO is this actually accurate?
    732734        // TODO: Since the upgrade to SpiderMonkey v24 this test won't be able to correctly represent
    733735        // non-tree structures because sharp variables were removed (bug 566700).
    734736        // This also affects the debug serializer and it could make sense to implement correct serialization again.
    public:  
    736738
    737739        man.AddComponent(hnd4, man.LookupCID("TestScript1_custom"), testParam);
    738740
     741        man.AddComponent(hnd5, man.LookupCID("TestScript1_vector"), testParam);
     742
    739743        TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent1, IID_Test1))->GetX(), 1234);
    740744        {
    741745            TestLogger log; // swallow warnings about this.entity being read-only
    742746            TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man.QueryInterface(ent2, IID_Test1))->GetX(), (int)ent2);
    743747        }
    744748        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);
    745750
    746751        std::stringstream debugStream;
    747752        TS_ASSERT(man.DumpDebugState(debugStream, true));
    entities:\n\  
    780785    object: {\n\
    781786  \"c\": 1\n\
    782787}\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\
    783801\n"
    784802        );
    785803
    entities:\n\  
    799817            TS_ASSERT_EQUALS(static_cast<ICmpTest1*> (man2.QueryInterface(ent2, IID_Test1))->GetX(), (int)ent2);
    800818        }
    801819        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);
    802821    }
    803822
    804823    void test_script_serialization_errors()
  • source/simulation2/tests/test_Serializer.h

    diff --git a/source/simulation2/tests/test_Serializer.h b/source/simulation2/tests/test_Serializer.h
    index d9aee50552..304675286a 100644
    a b public:  
    289289
    290290    // TODO: test exceptions more thoroughly
    291291
    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)
    293293    {
    294294        ScriptInterface script("Test", "Test", g_ScriptRuntime);
     295
     296        if (loadglobalscripts)
     297            script.LoadGlobalScripts();
     298
    295299        JSContext* cx = script.GetContext();
    296300        JSAutoRequest rq(cx);
    297301
    public:  
    311315
    312316        serialize.ScriptVal("script", &obj);
    313317
     318// TODO hack
     319if (loadglobalscripts) {
     320    std::string foo_;
     321    TSM_ASSERT(msg, script.CallFunction(obj, "nop", foo_));
     322    TS_ASSERT_STR_EQUALS(foo_, "foobar");
     323}
     324
    314325        if (expstream)
    315326        {
    316327            TSM_ASSERT_STREAM(msg, stream, expstreamlen, expstream);
    public:  
    327338        std::string source;
    328339        TSM_ASSERT(msg, script.CallFunction(newobj, "toSource", source));
    329340        TS_ASSERT_STR_EQUALS(source, expected);
     341
     342// TODO hack
     343if (loadglobalscripts) {
     344    std::string foo;
     345    TSM_ASSERT(msg, script.CallFunction(newobj, "nop", foo));
     346    TS_ASSERT_STR_EQUALS(foo, "foobar");
     347}
    330348    }
    331349
    332350    void test_script_basic()
    public:  
    717735
    718736    // TODO: prototype objects
    719737
     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
    720767    void test_script_nonfinite()
    721768    {
    722769        helper_script_roundtrip("nonfinite", "[0, Infinity, -Infinity, NaN, -1/Infinity]", "[0, Infinity, -Infinity, NaN, -0]");
    public:  
    882929        DeleteDirectory(DataDir()/"_testcache");
    883930        CXeromyces::Terminate();
    884931    }
    885 
    886932};