Array.prototype magic not working in loaded SWF

43 views
Skip to first unread message

Tarler

unread,
Jan 10, 2014, 7:23:06 AM1/10/14
to haxe...@googlegroups.com
Hi,

I am loading a Haxe generated SWF into an third party AS3 SWF.

From previous experience (https://groups.google.com/d/msg/haxelang/AuMPb7-nWUo/VX4_GaWrKMUJ) , I've found I need to get a reference to the Boot class and add it to the stage for the Haxe system to be "switched on". I can test before and after this process and can see that after it's happened some new static fields become available (e.g. Math.NaN).  So the Boot process seems to have worked.

However when the Haxe SWF is run inside the third party SWF, runtime errors are thrown when I try and iterate through a StringMap. The array generated by the __keys__ method does not inherit the iterator method from Array.prototype. I've done some tests, and Arrays are showing some odd behaviour:

//create Array from Class
var arr = Type.createInstance(Array, []);
 
var arrClass = Type.getClass(arr);
 trace
("arrClass:" + arrClass);//arrClass:#Array
 trace
("arr.iterator:" + Reflect.getProperty(arr, "iterator"));//arr.iterator:<function>
//i.e. Arrrays created in this way inherit the new methods from the prototype


//create Array in normal way
 
var arr2 = [];
 
var arr2Class = Type.getClass(arr2);
 trace
("arr2Class:" + arr2Class);//arrClass2:#Array
 trace
("arr2.iterator:" + Reflect.getProperty(arr2, "iterator"));//arr2.iterator:null
//i.e. Arrrays created in normal way don't inherit the new methods from the prototype - same for arr2 = new Array()
 
 trace
("(arrClass == arr2Class):" + (arrClass == arr2Class));//(arrClass == arr2Class):true
//i.e. the classes of both arrays seem to be the same
 
//create a new Array from the class of the one that didn't inherit
 
var arr2Inst = Type.createInstance(arr2Class, []);
 trace
("arr2Inst.iterator:" + Reflect.getProperty(arr2Inst, "iterator"));//arr2Inst.iterator:<function>

If I load the same Haxe SWF into a simple AS3 wrapper that I've created, the arrays work as expected. The third party SWF is unfortunately beyond my control. 

So, does anyone have any idea what might explain this odd issue with Arrays, and how I might hack around it. 

Where is __keys__ turned into literal AS3 - is this a macro? (I was guessing if I could find this, I could force the Arrays it generates to be of the correct sort, although obviously I'd like to fix it further upstream than this. ). 

Any ideas at all would be much appreciated.

Thanks,
James

(PS sorry for repost - code sample was wrong)

Tarler

unread,
Feb 2, 2014, 5:31:51 PM2/2/14
to haxe...@googlegroups.com
Just in case anyone comes across this issue, the root problem is that arrays created like this
var arr = [];


don't inherit the additional array prototype methods that Haxe tries to add in the Boot class.

So I had to instantiate all arrays via a utility method which effectively does this:
var arr = Type.createInstance(Array, []);

It's a horrible hack, but it got the job done.
Reply all
Reply to author
Forward
0 new messages