SharedObject working strangly on native

I can’t find out what the hell is going on with shareobjects and arryas. here a small example that shows the problem

        var so = SharedObject.getLocal("savedData");

        [initialize so.data.characters if its empty]

        var chars:Array<String> = so.data.characters;
        chars[0] = "joe";
        if (chars[0] != so.data.characters[0])
        {
            throw new Error(chars[0] + "  " + so.data.characters[0]);
        }

the code is throwing the error when the data is loaded from the file on windows when in fact they should be pointing to the same value. I can only safly modify the values on the array if I use so.data.characters in all places. chars variable is behaving like a copy and not a ref of the actual array. Now the strange part is that if the array is created because you are runing it for the first time it works as expected.

It’s related to hxcpp bug / feature and haxe typing:

var data : Dynamic = {};
data.characters = ["a"];

var chars : Array<String> = data.characters;
chars[0] = "joe";

trace(data.characters[0]);

This code works fine on flash, js or neko, but on cpp this code give wrong result:

While this code:

var data : Dynamic = {};
data.characters = (cast ["a"] : Array<String>);

var chars : Array<String> = data.characters;
chars[0] = "joe";

trace(data.characters[0]);

Works fine everywhere.


P. S.
hxcpp generates different code for first and second examples.
First example (not working):

Dynamic data = tmp; // where tmp is hx::Anon_obj::Create()
data->FieldRef(HX_HCSTRING("characters","\xaa","\x58","\xce","\x55")) = cpp::ArrayBase_obj::__new().Add(HX_HCSTRING("a","\x61","\x00","\x00","\x00"));
Array<::String> chars = data->__Field(HX_HCSTRING("characters","\xaa","\x58","\xce","\x55"), hx::paccDynamic);

Second example (working):

Dynamic data = tmp; // where tmp is hx::Anon_obj::Create()
data->FieldRef(HX_HCSTRING("characters","\xaa","\x58","\xce","\x55")) = Array_obj<::String>::__new().Add(HX_HCSTRING("a","\x61","\x00","\x00","\x00"));
Array<::String> chars = data->__Field(HX_HCSTRING("characters","\xaa","\x58","\xce","\x55"), hx::paccDynamic);

The only difference is that second (working) code use Array_obj<::String>::new() instead of cpp::ArrayBase_obj::new()

I’m not sure, should this considered as bug or not. If you think that this is a bug, submit issue to hxcpp repo

2 Likes

Wow huge thanks for taking the time to test it and finding a solution!
I’m with you about if this is a bug or not. the behavior is diferent thats for sure. So if I understand the solution the problem is in fact a casting problem, can it be that you left the first line by mistake after the edit?
If you want to submit the issue feel free to do it, you are far more capable of explaining what its happening.

Thank you again for your help and your super fast solution!

Yes :slight_smile: Now post has correct first line.

1 Like