HaxeFoundation / hxcpp

Runtime files for c++ backend for haxe
Other
294 stars 186 forks source link

Unserializing class references explode #149

Closed thomasuster closed 9 years ago

thomasuster commented 9 years ago

Unserializing class references explodes "exception thrown : Invalid Cast".

I created a boiled down test.

Commit https://github.com/thomasuster/hxcpp/commit/b330ed3b152b9d1cf8d2ce6d94344d4b4989246e Branch https://github.com/thomasuster/hxcpp/tree/master_classRef_serialization

hughsando commented 9 years ago

This appears to be a fundamental problem with serialization of Class types. eg:

class Test
{
   public static function main():Void
   {
      var t = Test;
      trace( t == haxe.Unserializer.run( haxe.Serializer.run(t) ) );
   }
}

Throws on neko and fails on hxcpp. Did you say it was working earlier? It may have been silently failing. I think saving the class name, rather than Class<Dynamic> is the only way serialization is going to work for now.

hughsando commented 9 years ago

Reported over in haxe: https://github.com/HaxeFoundation/haxe/issues/3715

thomasuster commented 9 years ago

Before it would omit class references, but the rest of the fields would serialize / unserialize correctly. I use this a lot for making prototypical instances.

Get copy with most fields var instance = Unserializer.run(Serializer.run(proto)); Set class refs since they silently get omitted instance.classReference = proto.classReference;

When I actually need to serialize class references then I override hxSerialize/hxUnSerialize. But like you said, it would be awesome if that worked instead of silently omitting.

hughsando commented 9 years ago

I have fixed this in the haxe git code. You may be able to copy the haxe.Serialize/Unserialize from the git repo and have it work. I will think about whether I should offer control over silent failing on cases similar to this.