renamed static_constructor traits to static_traits
[swftools.git] / lib / as3 / abc.c
1 /* abc.c
2
3    Routines for handling Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include <stdarg.h>
25 #include <assert.h>
26 #include "../rfxswf.h"
27 #include "../q.h"
28 #include "abc.h"
29
30 char stringbuffer[2048];
31
32 int abc_RegisterNameSpace(abc_file_t*file, const char*name);
33 int abc_RegisterPackageNameSpace(abc_file_t*file, const char*name);
34 int abc_RegisterPackageInternalNameSpace(abc_file_t*file, const char*name);
35 int abc_RegisterProtectedNameSpace(abc_file_t*file, const char*name);
36 int abc_RegisterExplicitNameSpace(abc_file_t*file, const char*name);
37 int abc_RegisterStaticProtectedNameSpace(abc_file_t*file, const char*name);
38 int abc_RegisterPrivateNameSpace(abc_file_t*file, const char*name);
39
40 /* TODO: switch to a datastructure with just values */
41 #define NO_KEY ""
42
43 static char* params_tostring(multiname_list_t*list)
44 {
45     multiname_list_t*l;
46     int n = list_length(list);
47     char**names = (char**)malloc(sizeof(char*)*n);
48     
49     l = list;
50     n = 0;
51     int size = 0;
52     while(l) {
53         names[n] = multiname_tostring(l->multiname);
54         size += strlen(names[n]) + 2;
55         n++;l=l->next;
56     }
57
58     char* params = malloc(size+15);
59     params[0]='(';
60     params[1]=0;
61     l = list;
62     int s=0;
63     n = 0;
64     while(l) {
65         if(s)
66             strcat(params, ", ");
67         strcat(params, names[n]);
68         free(names[n]);
69         l = l->next;
70         n++;
71         s=1;
72     }
73     free(names);
74     /*char num[20];
75     sprintf(num, "[%d params]", n);
76     strcat(params, num);*/
77     strcat(params, ")");
78     int t;
79     return params;
80 }
81
82 //#define DEBUG
83 #define DEBUG if(0)
84
85 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
86 {
87     int t;
88     int num_metadata = swf_GetU30(tag);
89
90     DEBUG printf("%d metadata\n");
91     for(t=0;t<num_metadata;t++) {
92         const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
93         int num = swf_GetU30(tag);
94         int s;
95         DEBUG printf("  %s\n", entry_name);
96         array_t*items = array_new();
97         for(s=0;s<num;s++) {
98             int i1 = swf_GetU30(tag);
99             int i2 = swf_GetU30(tag);
100             char*key = i1?pool_lookup_string(pool, i1):"";
101             char*value = i2?pool_lookup_string(pool, i2):"";
102             DEBUG printf("    %s=%s\n", key, value);
103             array_append(items, key, strdup(value));
104         }
105         array_append(file->metadata, entry_name, items);
106     }
107 }
108
109 void swf_CopyData(TAG*to, TAG*from, int len)
110 {
111     unsigned char*data = malloc(len);
112     swf_GetBlock(from, data, len);
113     swf_SetBlock(to, data, len);
114     free(data);
115 }
116
117 abc_file_t*abc_file_new()
118 {
119     abc_file_t*f = malloc(sizeof(abc_file_t));
120     memset(f, 0, sizeof(abc_file_t));
121     f->metadata = array_new();
122
123     f->methods = array_new();
124     f->classes = array_new();
125     f->scripts = array_new();
126     f->method_bodies = array_new();
127     f->flags = ABCFILE_LAZY;
128
129     return f;
130 }
131
132 #define CLASS_SEALED 1
133 #define CLASS_FINAL 2
134 #define CLASS_INTERFACE 4
135 #define CLASS_PROTECTED_NS 8
136
137 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass) {
138     
139     NEW(abc_class_t,c);
140     array_append(file->classes, NO_KEY, c);
141
142     c->file = file;
143     c->classname = classname;
144     c->superclass = superclass;
145     c->flags = 0;
146     c->constructor = 0;
147     c->static_constructor = 0;
148     c->traits = list_new();
149     return c;
150 }
151 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass) 
152 {
153     return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
154 }
155
156 void abc_class_sealed(abc_class_t*c)
157 {
158     c->flags |= CLASS_SEALED;
159 }
160 void abc_class_final(abc_class_t*c)
161 {
162     c->flags |= CLASS_FINAL;
163 }
164 void abc_class_interface(abc_class_t*c)
165 {
166     c->flags |= CLASS_INTERFACE;
167 }
168 void abc_class_protectedNS(abc_class_t*c, char*namespace)
169 {
170     c->protectedNS = namespace_new_protected(namespace);
171     c->flags |= CLASS_PROTECTED_NS;
172 }
173 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
174 {
175     list_append(c->interfaces, interface);
176 }
177
178 abc_method_body_t* add_method(abc_file_t*file, abc_class_t*cls, multiname_t*returntype, int num_params, va_list va)
179 {
180     /* construct code (method body) object */
181     NEW(abc_method_body_t,c);
182     array_append(file->method_bodies, NO_KEY, c);
183     c->file = file;
184     c->traits = list_new();
185     c->code = 0;
186
187     /* construct method object */
188     NEW(abc_method_t,m);
189     array_append(file->methods, NO_KEY, m);
190
191     m->return_type = returntype;
192
193     int t;
194     for(t=0;t<num_params;t++) {
195         const char*param = va_arg(va, const char*);
196         list_append(m->parameters, multiname_fromstring(param));
197     }
198
199     /* crosslink the two objects */
200     m->body = c;
201     c->method = m;
202
203     return c;
204 }
205
206 abc_method_body_t* abc_class_constructor(abc_class_t*cls, multiname_t*returntype, int num_params, ...) 
207 {
208     va_list va;
209     va_start(va, num_params);
210     abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
211     va_end(va);
212     cls->constructor = c->method;
213     return c;
214 }
215
216 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, multiname_t*returntype, int num_params, ...) 
217 {
218     va_list va;
219     va_start(va, num_params);
220     abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
221     va_end(va);
222     cls->static_constructor = c->method;
223     return c;
224 }
225
226 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, constant_t*v)
227 {
228     trait_t*trait = malloc(sizeof(trait_t));
229     memset(trait, 0, sizeof(trait_t));
230     trait->kind = type&0x0f;
231     trait->attributes = type&0xf0;
232     trait->name = name;
233     trait->data1 = data1;
234     trait->data2 = data2;
235     trait->value = v;
236     return trait;
237 }
238 trait_t*trait_new_member(multiname_t*type, multiname_t*name,constant_t*v)
239 {
240     int kind = TRAIT_SLOT;
241     trait_t*trait = malloc(sizeof(trait_t));
242     memset(trait, 0, sizeof(trait_t));
243     trait->kind = kind&0x0f;
244     trait->attributes = kind&0xf0;
245     trait->name = name;
246     trait->type_name = type;
247     return trait;
248 }
249 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
250 {
251     int type = TRAIT_METHOD;
252     trait_t*trait = malloc(sizeof(trait_t));
253     memset(trait, 0, sizeof(trait_t));
254     trait->kind = type&0x0f;
255     trait->attributes = type&0xf0;
256     trait->name = name;
257     trait->method = m;
258     return trait;
259 }
260
261 abc_method_body_t* abc_class_method(abc_class_t*cls, multiname_t*returntype, char*name, int num_params, ...)
262 {
263     abc_file_t*file = cls->file;
264     va_list va;
265     va_start(va, num_params);
266     abc_method_body_t* c = add_method(cls->file, cls, returntype, num_params, va);
267     va_end(va);
268     list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
269     return c;
270 }
271
272 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*type)
273 {
274     abc_file_t*file = cls->file;
275     multiname_t*m_name = multiname_fromstring(name);
276     multiname_t*m_type = multiname_fromstring(type);
277     trait_t*t = trait_new_member(m_type, m_name, 0);
278     t->slot_id = list_length(cls->traits);
279     list_append(cls->traits, t);
280 }
281
282 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
283 {
284     abc_file_t*file = code->file;
285     multiname_t*m = multiname_fromstring(multiname);
286     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
287     trait->cls = cls;
288     list_append(code->traits, trait);
289 }
290
291 /* notice: traits of a method (body) belonging to an init script
292    and traits of the init script are *not* the same thing */
293 int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
294 {
295     abc_file_t*file = script->file;
296     multiname_t*m = multiname_clone(multiname);
297     int slotid = list_length(script->traits)+1;
298     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
299     trait->cls = cls;
300     list_append(script->traits, trait);
301     return slotid;
302 }
303
304 abc_script_t* abc_initscript(abc_file_t*file, multiname_t*returntype, int num_params, ...) 
305 {
306     va_list va;
307     va_start(va, num_params);
308     abc_method_body_t* c = add_method(file, 0, returntype, num_params, va);
309     abc_script_t* s = malloc(sizeof(abc_script_t));
310     s->method = c->method;
311     s->traits = list_new();
312     s->file = file;
313     array_append(file->scripts, NO_KEY, s);
314     va_end(va);
315     return s;
316 }
317
318 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
319
320 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
321 {
322     char*return_type = 0;
323     if(m->return_type)
324         return_type = multiname_tostring(m->return_type);
325     else
326         return_type = strdup("void");
327     char*paramstr = params_tostring(m->parameters);
328     fprintf(fo, "%s%s %s %s=%s %s (%d params)\n", prefix, type, return_type, name, m->name, paramstr, list_length(m->parameters));
329     free(paramstr);paramstr=0;
330     free(return_type);return_type=0;
331
332     abc_method_body_t*c = m->body;
333     if(!c) {
334         return;
335     }
336     
337     fprintf(fo, "%s[stack:%d locals:%d scope:%d-%d flags:", 
338             prefix, c->old.max_stack, c->old.local_count, c->old.init_scope_depth, 
339             c->old.max_scope_depth);
340
341
342     int flags = c->method->flags;
343     if(flags&METHOD_NEED_ARGUMENTS) {fprintf(fo, " need_arguments");flags&=~METHOD_NEED_ARGUMENTS;}
344     if(flags&METHOD_NEED_ACTIVATION) {fprintf(fo, " need_activation");flags&=~METHOD_NEED_ACTIVATION;}
345     if(flags&METHOD_NEED_REST) {fprintf(fo, " need_rest");flags&=~METHOD_NEED_REST;}
346     if(flags&METHOD_HAS_OPTIONAL) {fprintf(fo, " has_optional");flags&=~METHOD_HAS_OPTIONAL;}
347     if(flags&METHOD_SET_DXNS) {fprintf(fo, " set_dxns");flags&=~METHOD_SET_DXNS;}
348     if(flags&METHOD_HAS_PARAM_NAMES) {fprintf(fo, " has_param_names");flags&=~METHOD_HAS_PARAM_NAMES;}
349     if(flags) fprintf(fo, " %02x", flags);
350     fprintf(fo, "]\n");
351
352
353     char prefix2[80];
354     sprintf(prefix2, "%s    ", prefix);
355     if(c->traits)
356         traits_dump(fo, prefix, c->traits, file);
357     fprintf(fo, "%s{\n", prefix);
358     code_dump(c->code, c->exceptions, file, prefix2, fo);
359     fprintf(fo, "%s}\n\n", prefix);
360 }
361
362 static void traits_free(trait_list_t*traits) 
363 {
364     trait_list_t*t = traits;
365     while(t) {
366         if(t->trait->name) {
367             multiname_destroy(t->trait->name);t->trait->name = 0;
368         }
369         if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
370             multiname_destroy(t->trait->type_name);
371         }
372         if(t->trait->value) {
373             constant_free(t->trait->value);t->trait->value = 0;
374         }
375         free(t->trait);t->trait = 0;
376         t = t->next;
377     }
378     list_free(traits);
379 }
380             
381 static char trait_is_method(trait_t*trait)
382 {
383     return (trait->kind == TRAIT_METHOD || trait->kind == TRAIT_GETTER || 
384             trait->kind == TRAIT_SETTER || trait->kind == TRAIT_FUNCTION);
385 }
386
387 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
388 {
389     int num_traits = swf_GetU30(tag);
390     trait_list_t*traits = list_new();
391     int t;
392     if(num_traits) {
393         DEBUG printf("%d traits\n", num_traits);
394     }
395     
396     for(t=0;t<num_traits;t++) {
397         NEW(trait_t,trait);
398         list_append(traits, trait);
399
400         trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
401
402         const char*name = 0;
403         DEBUG name = multiname_tostring(trait->name);
404         U8 kind = swf_GetU8(tag);
405         U8 attributes = kind&0xf0;
406         kind&=0x0f;
407         trait->kind = kind;
408         trait->attributes = attributes;
409         DEBUG printf("  trait %d) %s type=%02x\n", t, name, kind);
410         if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
411             trait->disp_id = swf_GetU30(tag);
412             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
413             DEBUG printf("  method/getter/setter\n");
414         } else if(kind == TRAIT_FUNCTION) { // function
415             trait->slot_id =  swf_GetU30(tag);
416             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
417         } else if(kind == TRAIT_CLASS) { // class
418             trait->slot_id = swf_GetU30(tag);
419             trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
420             DEBUG printf("  class %s %d %d\n", name, trait->slot_id, trait->cls);
421         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
422             /* a slot is a variable in a class that is shared amonst all instances
423                of the same type, but which has a unique location in each object 
424                (in other words, slots are non-static, traits are static)
425              */
426             trait->slot_id = swf_GetU30(tag);
427             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
428             int vindex = swf_GetU30(tag);
429             if(vindex) {
430                 int vkind = swf_GetU8(tag);
431                 trait->value = constant_fromindex(pool, vindex, vkind);
432             }
433             DEBUG printf("  slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_tostring(trait->value));
434         } else {
435             fprintf(stderr, "Can't parse trait type %d\n", kind);
436         }
437         if(attributes&0x40) {
438             int num = swf_GetU30(tag);
439             int s;
440             for(s=0;s<num;s++) {
441                 swf_GetU30(tag); //index into metadata array
442             }
443         }
444     }
445     return traits;
446 }
447
448 void traits_skip(TAG*tag)
449 {
450     int num_traits = swf_GetU30(tag);
451     int t;
452     for(t=0;t<num_traits;t++) {
453         swf_GetU30(tag);
454         U8 kind = swf_GetU8(tag);
455         U8 attributes = kind&0xf0;
456         kind&=0x0f;
457         swf_GetU30(tag);
458         swf_GetU30(tag);
459         if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
460             if(swf_GetU30(tag)) swf_GetU8(tag);
461         } else if(kind>TRAIT_CONST) {
462             fprintf(stderr, "Can't parse trait type %d\n", kind);
463         }
464         if(attributes&0x40) {
465             int s, num = swf_GetU30(tag);
466             for(s=0;s<num;s++) swf_GetU30(tag);
467         }
468     }
469 }
470
471
472 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
473 {
474     if(!traits) {
475         swf_SetU30(tag, 0);
476         return;
477     }
478     swf_SetU30(tag, list_length(traits));
479     int s;
480
481     while(traits) {
482         trait_t*trait = traits->trait;
483
484         swf_SetU30(tag, pool_register_multiname(pool, trait->name));
485         swf_SetU8(tag, trait->kind|trait->attributes);
486
487         swf_SetU30(tag, trait->data1);
488
489         if(trait->kind == TRAIT_CLASS) {
490             swf_SetU30(tag, trait->cls->index);
491         } else if(trait->kind == TRAIT_GETTER ||
492                   trait->kind == TRAIT_SETTER ||
493                   trait->kind == TRAIT_METHOD) {
494             swf_SetU30(tag, trait->method->index);
495         } else if(trait->kind == TRAIT_SLOT ||
496                   trait->kind == TRAIT_CONST) {
497             int index = pool_register_multiname(pool, trait->type_name);
498             swf_SetU30(tag, index);
499         } else  {
500             swf_SetU30(tag, trait->data2);
501         }
502
503         if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
504             int vindex = constant_get_index(pool, trait->value);
505             swf_SetU30(tag, vindex);
506             if(vindex) {
507                 swf_SetU8(tag, trait->value->type);
508             }
509         }
510         if(trait->attributes&0x40) {
511             // metadata
512             swf_SetU30(tag, 0);
513         }
514         traits = traits->next;
515     }
516 }
517
518
519 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
520 {
521     int t;
522     while(traits) {
523         trait_t*trait = traits->trait;
524         char*name = multiname_tostring(trait->name);
525         U8 kind = trait->kind;
526         U8 attributes = trait->attributes;
527         if(kind == TRAIT_METHOD) {
528             abc_method_t*m = trait->method;
529             dump_method(fo, prefix, "method", name, m, file);
530         } else if(kind == TRAIT_GETTER) {
531             abc_method_t*m = trait->method;
532             dump_method(fo, prefix, "getter", name, m, file);
533         } else if(kind == TRAIT_SETTER) {
534             abc_method_t*m = trait->method;
535             dump_method(fo, prefix, "setter", name, m, file);
536         } else if(kind == TRAIT_FUNCTION) { // function
537             abc_method_t*m = trait->method;
538             dump_method(fo, prefix, "function", name, m, file);
539         } else if(kind == TRAIT_CLASS) { // class
540             abc_class_t*cls = trait->cls;
541             if(!cls) {
542                 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
543             } else {
544                 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
545             }
546         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
547             int slot_id = trait->slot_id;
548             char*type_name = multiname_tostring(trait->type_name);
549             char*value = constant_tostring(trait->value);
550             fprintf(fo, "%sslot %d: %s%s %s %s %s\n", prefix, trait->slot_id, 
551                     kind==TRAIT_CONST?"const ":"", type_name, name, 
552                     value?"=":"", value);
553             if(value) free(value);
554             free(type_name);
555         } else {
556             fprintf(fo, "%s    can't dump trait type %d\n", prefix, kind);
557         }
558         free(name);
559         traits=traits->next;
560     }
561 }
562
563 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
564 {
565     abc_file_t* file = (abc_file_t*)code;
566         
567     if(file->name) {
568         fprintf(fo, "%s#\n", prefix);
569         fprintf(fo, "%s#name: %s\n", prefix, file->name);
570         fprintf(fo, "%s#\n", prefix);
571     }
572
573     int t;
574     for(t=0;t<file->metadata->num;t++) {
575         const char*entry_name = array_getkey(file->metadata, t);
576         fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
577         int s;
578         array_t*items = (array_t*)array_getvalue(file->metadata, t);
579         for(s=0;s<items->num;s++) {
580             fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
581         }
582         fprintf(fo, "%s#\n", prefix);
583     }
584
585     for(t=0;t<file->classes->num;t++) {
586         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
587         char prefix2[80];
588         sprintf(prefix2, "%s    ", prefix);
589
590         fprintf(fo, "%s", prefix);
591         if(cls->flags&1) fprintf(fo, "sealed ");
592         if(cls->flags&2) fprintf(fo, "final ");
593         if(cls->flags&4) fprintf(fo, "interface ");
594         if(cls->flags&8) {
595             char*s = namespace_tostring(cls->protectedNS);
596             fprintf(fo, "protectedNS(%s) ", s);
597             free(s);
598         }
599
600         char*classname = multiname_tostring(cls->classname);
601         fprintf(fo, "class %s", classname);
602         free(classname);
603         if(cls->superclass) {
604             char*supername = multiname_tostring(cls->superclass);
605             fprintf(fo, " extends %s", supername);
606             free(supername);
607             multiname_list_t*ilist = cls->interfaces;
608             if(ilist)
609                 fprintf(fo, " implements");
610             while(ilist) {
611                 char*s = multiname_tostring(ilist->multiname);
612                 fprintf(fo, " %s", s);
613                 free(s);
614                 ilist = ilist->next;
615             }
616             ilist->next;
617         }
618         if(cls->flags&0xf0) 
619             fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
620         fprintf(fo, "%s{\n", prefix);
621
622         if(cls->static_constructor)
623             dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
624         traits_dump(fo, prefix2, cls->static_traits, file);
625         
626         char*n = multiname_tostring(cls->classname);
627         if(cls->constructor)
628             dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
629         free(n);
630         traits_dump(fo, prefix2,cls->traits, file);
631         fprintf(fo, "%s}\n", prefix);
632
633     }
634     fprintf(fo, "%s\n", prefix);
635
636     for(t=0;t<file->scripts->num;t++) {
637         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
638         dump_method(fo, prefix,"initmethod", "init", s->method, file);
639         traits_dump(fo, prefix, s->traits, file);
640     }
641     return file;
642 }
643
644 void* swf_ReadABC(TAG*tag)
645 {
646     abc_file_t* file = abc_file_new();
647     pool_t*pool = pool_new();
648
649     swf_SetTagPos(tag, 0);
650     int t;
651     if(tag->id == ST_DOABC) {
652         U32 abcflags = swf_GetU32(tag);
653         DEBUG printf("flags=%08x\n", abcflags);
654         char*name= swf_GetString(tag);
655         file->name = (name&&name[0])?strdup(name):0;
656     }
657     U32 version = swf_GetU32(tag);
658     if(version!=0x002e0010) {
659         fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
660     }
661
662     pool_read(pool, tag);
663
664     int num_methods = swf_GetU30(tag);
665     DEBUG printf("%d methods\n", num_methods);
666     for(t=0;t<num_methods;t++) {
667         NEW(abc_method_t,m);
668         int param_count = swf_GetU30(tag);
669         int return_type_index = swf_GetU30(tag);
670         if(return_type_index)
671             m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
672         else
673             m->return_type = 0;
674
675         int s;
676         for(s=0;s<param_count;s++) {
677             int type_index = swf_GetU30(tag);
678             
679             /* type_index might be 0, which probably means "..." (varargs) */
680             multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
681             list_append(m->parameters, param);
682         }
683
684         int namenr = swf_GetU30(tag);
685         if(namenr)
686             m->name = strdup(pool_lookup_string(pool, namenr));
687         else
688             m->name = strdup("");
689
690         m->flags = swf_GetU8(tag);
691         
692         DEBUG printf("method %d) %s flags=%02x\n", t, params_tostring(m->parameters), m->flags);
693
694         if(m->flags&0x08) {
695             /* TODO optional parameters */
696             m->optional_parameters = list_new();
697             int num = swf_GetU30(tag);
698             int s;
699             for(s=0;s<num;s++) {
700                 int vindex = swf_GetU30(tag);
701                 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
702                 constant_t*c = constant_fromindex(pool, vindex, vkind);
703                 list_append(m->optional_parameters, c);
704             }
705         }
706         if(m->flags&0x80) {
707             /* debug information- not used by avm2 */
708             multiname_list_t*l = m->parameters;
709             while(l) {
710                 char*name = pool_lookup_string(pool, swf_GetU30(tag));
711                 l = l->next;
712             }
713         }
714         array_append(file->methods, NO_KEY, m);
715     }
716             
717     parse_metadata(tag, file, pool);
718         
719     /* skip classes, and scripts for now, and do the real parsing later */
720     int num_classes = swf_GetU30(tag);
721     int classes_pos = tag->pos;
722     DEBUG printf("%d classes\n", num_classes);
723     for(t=0;t<num_classes;t++) {
724         abc_class_t*cls = malloc(sizeof(abc_class_t));
725         memset(cls, 0, sizeof(abc_class_t));
726         
727         DEBUG printf("class %d\n", t);
728         swf_GetU30(tag); //classname
729         swf_GetU30(tag); //supername
730
731         array_append(file->classes, NO_KEY, cls);
732
733         cls->flags = swf_GetU8(tag);
734         if(cls->flags&8) 
735             swf_GetU30(tag); //protectedNS
736         int s;
737         int inum = swf_GetU30(tag); //interface count
738         cls->interfaces = 0;
739         for(s=0;s<inum;s++) {
740             int interface_index = swf_GetU30(tag);
741             multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
742             list_append(cls->interfaces, m);
743             DEBUG printf("  class %d interface: %s\n", t, m->name);
744         }
745
746         swf_GetU30(tag); //iinit
747         traits_skip(tag);
748     }
749     for(t=0;t<num_classes;t++) {
750         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
751         int cinit = swf_GetU30(tag);
752         cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
753         traits_skip(tag);
754     }
755     int num_scripts = swf_GetU30(tag);
756     DEBUG printf("%d scripts\n", num_scripts);
757     for(t=0;t<num_scripts;t++) {
758         int init = swf_GetU30(tag);
759         traits_skip(tag);
760     }
761
762     int num_method_bodies = swf_GetU30(tag);
763     DEBUG printf("%d method bodies\n", num_method_bodies);
764     for(t=0;t<num_method_bodies;t++) {
765         int methodnr = swf_GetU30(tag);
766         if(methodnr >= file->methods->num) {
767             printf("Invalid method number: %d\n", methodnr);
768             return 0;
769         }
770         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
771         abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
772         memset(c, 0, sizeof(abc_method_body_t));
773         c->old.max_stack = swf_GetU30(tag);
774         c->old.local_count = swf_GetU30(tag);
775         c->old.init_scope_depth = swf_GetU30(tag);
776         c->old.max_scope_depth = swf_GetU30(tag);
777
778         c->init_scope_depth = c->old.init_scope_depth;
779         int code_length = swf_GetU30(tag);
780
781         c->method = m;
782         m->body = c;
783
784         int pos = tag->pos + code_length;
785         codelookup_t*codelookup = 0;
786         c->code = code_parse(tag, code_length, file, pool, &codelookup);
787         tag->pos = pos;
788
789         int exception_count = swf_GetU30(tag);
790         int s;
791         c->exceptions = list_new();
792         for(s=0;s<exception_count;s++) {
793             exception_t*e = malloc(sizeof(exception_t));
794
795             e->from = code_atposition(codelookup, swf_GetU30(tag));
796             e->to = code_atposition(codelookup, swf_GetU30(tag));
797             e->target = code_atposition(codelookup, swf_GetU30(tag));
798
799             e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
800             e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
801             //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
802             //if(e->var_name) e->var_name = strdup(e->var_name);
803             list_append(c->exceptions, e);
804         }
805         codelookup_free(codelookup);
806         c->traits = traits_parse(tag, pool, file);
807
808         DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
809
810         array_append(file->method_bodies, NO_KEY, c);
811     }
812     if(tag->len - tag->pos) {
813         fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
814         return 0;
815     }
816
817     swf_SetTagPos(tag, classes_pos);
818     for(t=0;t<num_classes;t++) {
819         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
820
821         int classname_index = swf_GetU30(tag);
822         int superclass_index = swf_GetU30(tag);
823         cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
824         cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
825         cls->flags = swf_GetU8(tag);
826         const char*ns = "";
827         if(cls->flags&8) {
828             int ns_index = swf_GetU30(tag);
829             cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
830         }
831         
832         int num_interfaces = swf_GetU30(tag); //interface count
833         int s;
834         for(s=0;s<num_interfaces;s++) {
835             swf_GetU30(tag); // multiname index TODO
836         }
837         int iinit = swf_GetU30(tag);
838         cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
839         cls->traits = traits_parse(tag, pool, file);
840     }
841     for(t=0;t<num_classes;t++) {
842         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
843         /* SKIP */
844         swf_GetU30(tag); // cindex
845         cls->static_traits = traits_parse(tag, pool, file);
846     }
847     int num_scripts2 = swf_GetU30(tag);
848     for(t=0;t<num_scripts2;t++) {
849         int init = swf_GetU30(tag);
850         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
851         
852         abc_script_t*s = malloc(sizeof(abc_script_t));
853         memset(s, 0, sizeof(abc_script_t));
854         s->method = m;
855         s->traits = traits_parse(tag, pool, file);
856         array_append(file->scripts, NO_KEY, s);
857     }
858
859     pool_destroy(pool);
860     return file;
861 }
862
863 void swf_WriteABC(TAG*abctag, void*code)
864 {
865     abc_file_t*file = (abc_file_t*)code;
866     pool_t*pool = pool_new();
867
868     TAG*tmp = swf_InsertTag(0,0);
869     TAG*tag = tmp;
870     int t;
871    
872     char need_null_method=0;
873     for(t=0;t<file->classes->num;t++) {
874         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
875         if(!c->constructor || !c->static_constructor) {
876             need_null_method=1;
877             break;
878         }
879     }
880
881     abc_method_t*nullmethod = 0;
882     if(need_null_method) {
883         NEW(abc_method_t,m);
884         nullmethod = m;
885         /*TODO: might be more efficient to have this at the beginning */
886         array_append(file->methods, NO_KEY, nullmethod);
887         
888         NEW(abc_method_body_t,body);
889         body->method = m;
890         m->body = body;
891         __ returnvoid(body);
892         array_append(file->method_bodies, NO_KEY, body);
893     }
894     for(t=0;t<file->classes->num;t++) {
895         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
896         if(!c->constructor)
897             c->constructor = nullmethod;
898         if(!c->static_constructor)
899             c->static_constructor = nullmethod;
900     }
901
902     swf_SetU30(tag, file->methods->num);
903     /* enumerate classes, methods and method bodies */
904     for(t=0;t<file->methods->num;t++) {
905         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
906         m->index = t;
907     }
908     for(t=0;t<file->classes->num;t++) {
909         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
910         c->index = t;
911     }
912     for(t=0;t<file->method_bodies->num;t++) {
913         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
914         m->index = t;
915     }
916     
917     /* generate code statistics */
918     for(t=0;t<file->method_bodies->num;t++) {
919         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
920         m->stats = code_get_statistics(m->code, m->exceptions);
921     }
922     
923     /* level init scope depths: The init scope depth of a method is
924        always as least as high as the init scope depth of it's surrounding
925        class.
926        A method has it's own init_scope_depth if it's an init method 
927        (then its init scope depth is zero), or if it's used as a closure.
928
929        Not sure yet what to do with methods which are used at different
930        locations- e.g. the nullmethod is used all over the place.
931
932        Also, I have the strong suspicion that flash player uses only
933        the difference between max_scope_stack and init_scope_stack, anyway.
934      */
935     for(t=0;t<file->classes->num;t++) {
936         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
937         trait_list_t*traits = c->traits;
938         if(c->constructor &&
939            c->constructor->body->init_scope_depth < c->init_scope_depth) {
940            c->constructor->body->init_scope_depth = c->init_scope_depth;
941         }
942         if(c->static_constructor &&
943            c->static_constructor->body->init_scope_depth < c->init_scope_depth) {
944            c->static_constructor->body->init_scope_depth = c->init_scope_depth;
945         }
946         while(traits) {
947             trait_t*trait = traits->trait;
948             if(trait_is_method(trait) && trait->method->body) {
949                 abc_method_body_t*body = trait->method->body;
950                 if(body->init_scope_depth < c->init_scope_depth) {
951                    body->init_scope_depth = c->init_scope_depth;
952                 }
953             }
954             traits = traits->next;
955         }
956     }
957     
958     for(t=0;t<file->methods->num;t++) {
959         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
960         int n = 0;
961         multiname_list_t*l = m->parameters;
962         int num_params = list_length(m->parameters);
963         swf_SetU30(tag, num_params);
964         if(m->return_type) 
965             swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
966         else
967             swf_SetU30(tag, 0);
968         int s;
969         while(l) {
970             swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
971             l = l->next;
972         }
973         if(m->name) {
974             swf_SetU30(tag, pool_register_string(pool, m->name));
975         } else {
976             swf_SetU30(tag, 0);
977         }
978
979         U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
980         if(m->optional_parameters)
981             flags |= METHOD_HAS_OPTIONAL;
982         if(m->body) {
983             flags |= m->body->stats->flags;
984         }
985
986         swf_SetU8(tag, flags);
987         if(flags&METHOD_HAS_OPTIONAL) {
988             swf_SetU30(tag, list_length(m->optional_parameters));
989             constant_list_t*l = m->optional_parameters;
990             while(l) {
991                 swf_SetU30(tag, constant_get_index(pool, l->constant));
992                 swf_SetU8(tag, l->constant->type);
993                 l = l->next;
994             }
995         }
996     }
997    
998     /* write metadata */
999     swf_SetU30(tag, file->metadata->num);
1000     for(t=0;t<file->metadata->num;t++) {
1001         const char*entry_name = array_getkey(file->metadata, t);
1002         swf_SetU30(tag, pool_register_string(pool, entry_name));
1003         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1004         swf_SetU30(tag, items->num);
1005         int s;
1006         for(s=0;s<items->num;s++) {
1007             int i1 = pool_register_string(pool, array_getkey(items, s));
1008             int i2 = pool_register_string(pool, array_getvalue(items, s));
1009             swf_SetU30(tag, i1);
1010             swf_SetU30(tag, i2);
1011         }
1012     }
1013
1014     swf_SetU30(tag, file->classes->num);
1015     for(t=0;t<file->classes->num;t++) {
1016         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1017    
1018         int classname_index = pool_register_multiname(pool, c->classname);
1019         int superclass_index = pool_register_multiname(pool, c->superclass);
1020
1021         swf_SetU30(tag, classname_index);
1022         swf_SetU30(tag, superclass_index);
1023
1024         swf_SetU8(tag, c->flags); // flags
1025         if(c->flags&0x08) {
1026             int ns_index = pool_register_namespace(pool, c->protectedNS);
1027             swf_SetU30(tag, ns_index);
1028         }
1029
1030         swf_SetU30(tag, list_length(c->interfaces));
1031         multiname_list_t*interface= c->interfaces;
1032         while(interface) {
1033             swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1034             interface = interface->next;
1035         }
1036
1037         if(!c->constructor) {
1038             swf_SetU30(tag, nullmethod->index);
1039         } else {
1040             swf_SetU30(tag, c->constructor->index);
1041         }
1042         traits_write(pool, tag, c->traits);
1043     }
1044     for(t=0;t<file->classes->num;t++) {
1045         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1046         if(!c->static_constructor) {
1047             swf_SetU30(tag, nullmethod->index);
1048         } else {
1049             swf_SetU30(tag, c->static_constructor->index);
1050         }
1051         traits_write(pool, tag, c->static_traits);
1052     }
1053
1054     swf_SetU30(tag, file->scripts->num);
1055     for(t=0;t<file->scripts->num;t++) {
1056         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1057         swf_SetU30(tag, s->method->index); //!=t!
1058         traits_write(pool, tag, s->traits);
1059     }
1060
1061     swf_SetU30(tag, file->method_bodies->num);
1062     for(t=0;t<file->method_bodies->num;t++) {
1063         abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1064         abc_method_t*m = c->method;
1065         swf_SetU30(tag, m->index);
1066
1067         //swf_SetU30(tag, c->old.max_stack);
1068         //swf_SetU30(tag, c->old.local_count);
1069         //swf_SetU30(tag, c->old.init_scope_depth);
1070         //swf_SetU30(tag, c->old.max_scope_depth);
1071
1072         swf_SetU30(tag, c->stats->max_stack);
1073         if(list_length(c->method->parameters)+1 <= c->stats->local_count)
1074             swf_SetU30(tag, c->stats->local_count);
1075         else
1076             swf_SetU30(tag, list_length(c->method->parameters)+1);
1077         swf_SetU30(tag, c->init_scope_depth);
1078         swf_SetU30(tag, c->stats->max_scope_depth+
1079                         c->init_scope_depth);
1080
1081         code_write(tag, c->code, pool, file);
1082
1083         swf_SetU30(tag, list_length(c->exceptions));
1084         exception_list_t*l = c->exceptions;
1085         while(l) {
1086             // warning: assumes "pos" in each code_t is up-to-date
1087             swf_SetU30(tag, l->exception->from->pos);
1088             swf_SetU30(tag, l->exception->to->pos);
1089             swf_SetU30(tag, l->exception->target->pos);
1090             swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
1091             swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
1092             l = l->next;
1093         }
1094
1095         traits_write(pool, tag, c->traits);
1096     }
1097    
1098     /* free temporary codestat data again. Notice: If we were to write this
1099        file multiple times, this can also be shifted to abc_file_free() */
1100     for(t=0;t<file->method_bodies->num;t++) {
1101         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1102         codestats_free(m->stats);m->stats=0;
1103     }
1104
1105     // --- start to write real tag --
1106     
1107     tag = abctag;
1108
1109     if(tag->id == ST_DOABC) {
1110         swf_SetU32(tag, file->flags); // flags
1111         swf_SetString(tag, file->name);
1112     }
1113
1114     swf_SetU16(tag, 0x10); //version
1115     swf_SetU16(tag, 0x2e);
1116     
1117     pool_write(pool, tag);
1118     
1119     swf_SetBlock(tag, tmp->data, tmp->len);
1120
1121     swf_DeleteTag(0, tmp);
1122     pool_destroy(pool);
1123 }
1124
1125 void abc_file_free(abc_file_t*file)
1126 {
1127     int t;
1128     if(file->metadata) {
1129         for(t=0;t<file->metadata->num;t++) {
1130             array_t*items = (array_t*)array_getvalue(file->metadata, t);
1131             int s;
1132             for(s=0;s<items->num;s++) {
1133                 free(array_getvalue(items, s));
1134             }
1135             array_free(items);
1136         }
1137         array_free(file->metadata);file->metadata=0;
1138     }
1139
1140     for(t=0;t<file->methods->num;t++) {
1141         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1142
1143         multiname_list_t*param = m->parameters;
1144         while(param) {
1145             multiname_destroy(param->multiname);param->multiname=0;
1146             param = param->next;
1147         }
1148         list_free(m->parameters);m->parameters=0;
1149        
1150         constant_list_t*opt = m->optional_parameters;
1151         while(opt) {
1152             constant_free(opt->constant);opt->constant=0;
1153             opt = opt->next;
1154         }
1155         list_free(m->optional_parameters);m->optional_parameters=0;
1156
1157         if(m->name) {
1158             free((void*)m->name);m->name=0;
1159         }
1160         if(m->return_type) {
1161             multiname_destroy(m->return_type);
1162         }
1163         free(m);
1164     }
1165     array_free(file->methods);file->methods=0;
1166
1167     for(t=0;t<file->classes->num;t++) {
1168         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1169         traits_free(cls->traits);cls->traits=0;
1170         traits_free(cls->static_traits);cls->static_traits=0;
1171
1172         if(cls->classname) {
1173             multiname_destroy(cls->classname);
1174         }
1175         if(cls->superclass) {
1176             multiname_destroy(cls->superclass);
1177         }
1178
1179         multiname_list_t*i = cls->interfaces;
1180         while(i) {
1181             multiname_destroy(i->multiname);i->multiname=0;
1182             i = i->next;
1183         }
1184         list_free(cls->interfaces);cls->interfaces=0;
1185
1186         if(cls->protectedNS) {
1187             namespace_destroy(cls->protectedNS);
1188         }
1189         free(cls);
1190     }
1191     array_free(file->classes);file->classes=0;
1192
1193     for(t=0;t<file->scripts->num;t++) {
1194         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1195         traits_free(s->traits);s->traits=0;
1196         free(s);
1197     }
1198     array_free(file->scripts);file->scripts=0;
1199
1200     for(t=0;t<file->method_bodies->num;t++) {
1201         abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1202         code_free(body->code);body->code=0;
1203         traits_free(body->traits);body->traits=0;
1204
1205         exception_list_t*ee = body->exceptions;
1206         while(ee) {
1207             exception_t*e=ee->exception;ee->exception=0;
1208             e->from = e->to = e->target = 0;
1209             multiname_destroy(e->exc_type);e->exc_type=0;
1210             multiname_destroy(e->var_name);e->var_name=0;
1211             free(e);
1212             ee=ee->next;
1213         }
1214         list_free(body->exceptions);body->exceptions=0;
1215         
1216         free(body);
1217     }
1218     array_free(file->method_bodies);file->method_bodies=0;
1219
1220     if(file->name) {
1221         free((void*)file->name);file->name=0;
1222     }
1223
1224     free(file);
1225 }
1226
1227 void swf_FreeABC(void*code)
1228 {
1229     abc_file_t*file= (abc_file_t*)code;
1230     abc_file_free(file);
1231 }
1232
1233 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1234 {
1235     int num_frames = 0;
1236     int has_buttons = 0;
1237     TAG*tag=swf->firstTag;
1238     while(tag) {
1239         if(tag->id == ST_SHOWFRAME)
1240             num_frames++;
1241         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1242             has_buttons = 1;
1243         tag = tag->next;
1244     }
1245
1246     abc_file_t*file = abc_file_new();
1247     abc_method_body_t*c = 0;
1248    
1249     abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1250     abc_class_protectedNS(cls, "rfx:MainTimeline");
1251   
1252     TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1253     
1254     tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1255     swf_SetU16(tag, 1);
1256     swf_SetU16(tag, 0);
1257     swf_SetString(tag, "rfx.MainTimeline");
1258
1259     c = abc_class_staticconstructor(cls, 0, 0);
1260     c->old.max_stack = 1;
1261     c->old.local_count = 1;
1262     c->old.init_scope_depth = 9;
1263     c->old.max_scope_depth = 10;
1264
1265     __ getlocal_0(c);
1266     __ pushscope(c);
1267     __ returnvoid(c);
1268
1269     c = abc_class_constructor(cls, 0, 0);
1270     c->old.max_stack = 3;
1271     c->old.local_count = 1;
1272     c->old.init_scope_depth = 10;
1273     c->old.max_scope_depth = 11;
1274     
1275     debugfile(c, "constructor.as");
1276
1277     __ getlocal_0(c);
1278     __ pushscope(c);
1279
1280     __ getlocal_0(c);
1281     __ constructsuper(c,0);
1282
1283     __ getlex(c, "[package]flash.system::Security");
1284     __ pushstring(c, "*");
1285     __ callpropvoid(c, "[package]::allowDomain", 1);
1286     
1287     if(stop_each_frame || has_buttons) {
1288         int frame = 0;
1289         tag = swf->firstTag;
1290         abc_method_body_t*f = 0; //frame script
1291         while(tag && tag->id!=ST_END) {
1292             char framename[80];
1293             char needs_framescript=0;
1294             char buttonname[80];
1295             char functionname[80];
1296             sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1297             
1298             if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1299                 /* make the contructor add a frame script */
1300                 __ findpropstrict(c,"[package]::addFrameScript");
1301                 __ pushbyte(c,frame);
1302                 __ getlex(c,framename);
1303                 __ callpropvoid(c,"[package]::addFrameScript",2);
1304
1305                 f = abc_class_method(cls, 0, framename, 0);
1306                 f->old.max_stack = 3;
1307                 f->old.local_count = 1;
1308                 f->old.init_scope_depth = 10;
1309                 f->old.max_scope_depth = 11;
1310                 __ debugfile(f, "framescript.as");
1311                 __ debugline(f, 1);
1312                 __ getlocal_0(f);
1313                 __ pushscope(f);
1314             }
1315
1316             if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1317                 U16 id = swf_GetDefineID(tag);
1318                 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1319                 __ getlex(f,buttonname);
1320                 __ getlex(f,"flash.events::MouseEvent");
1321                 __ getproperty(f, "::CLICK");
1322                 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1323                 __ getlex(f,functionname);
1324                 __ callpropvoid(f, "::addEventListener" ,2);
1325
1326                 if(stop_each_frame) {
1327                     __ findpropstrict(f, "[package]::stop");
1328                     __ callpropvoid(f, "[package]::stop", 0);
1329                 }
1330                 needs_framescript = 1;
1331
1332                 abc_method_body_t*h =
1333                     abc_class_method(cls, 0, functionname, 1, "flash.events::MouseEvent");
1334                 h->old.max_stack = 6;
1335                 h->old.local_count = 2;
1336                 h->old.init_scope_depth = 10;
1337                 h->old.max_scope_depth = 11;
1338                 __ getlocal_0(h);
1339                 __ pushscope(h);
1340
1341                 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1342                 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1343                     int framenr = GET16(oldaction->data);
1344                     if(framenr>254) {
1345                         fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1346                     }
1347                     if(!events) {
1348                         __ findpropstrict(h,"[package]::gotoAndStop");
1349                         __ pushbyte(h,framenr+1);
1350                         __ callpropvoid(h,"[package]::gotoAndStop", 1);
1351                     } else {
1352                         char framename[80];
1353                         sprintf(framename, "frame%d", framenr);
1354                         __ getlocal_0(h); //this
1355                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1356                         __ pushstring(h, "link");
1357                         __ pushtrue(h);
1358                         __ pushtrue(h);
1359                         __ pushstring(h, framename);
1360                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1361                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1362                     }
1363                 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1364                     if(!events) {
1365                         __ findpropstrict(h,"flash.net::navigateToURL");
1366                         __ findpropstrict(h,"flash.net::URLRequest");
1367                         // TODO: target _blank
1368                         __ pushstring(h,oldaction->data); //url
1369                         __ constructprop(h,"flash.net::URLRequest", 1);
1370                         __ callpropvoid(h,"flash.net::navigateToURL", 1);
1371                     } else {
1372                         __ getlocal_0(h); //this
1373                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1374                         __ pushstring(h, "link");
1375                         __ pushtrue(h);
1376                         __ pushtrue(h);
1377                         __ pushstring(h,oldaction->data); //url
1378                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1379                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1380                     }
1381                 } else if(oldaction) {
1382                     fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1383                 }
1384                 __ returnvoid(h);
1385                 swf_ActionFree(oldaction);
1386             }
1387             if(tag->id == ST_SHOWFRAME) {
1388                 if(f) {
1389                     __ returnvoid(f);
1390                     f = 0;
1391                 }
1392                 frame++;
1393             }
1394             tag = tag->next;
1395         }
1396         if(f) {
1397             __ returnvoid(f);
1398         }
1399     }
1400     __ returnvoid(c);
1401
1402     tag = swf->firstTag;
1403     while(tag) {
1404         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1405             char buttonname[80];
1406             sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1407             abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1408         }
1409         tag = tag->next;
1410     }
1411
1412
1413     abc_script_t*s = abc_initscript(file, 0, 0);
1414     c = s->method->body;
1415     c->old.max_stack = 2;
1416     c->old.local_count = 1;
1417     c->old.init_scope_depth = 1;
1418     c->old.max_scope_depth = 9;
1419
1420     __ getlocal_0(c);
1421     __ pushscope(c);
1422     __ getscopeobject(c, 0);
1423     __ getlex(c,"::Object");
1424     __ pushscope(c);
1425     __ getlex(c,"flash.events::EventDispatcher");
1426     __ pushscope(c);
1427     __ getlex(c,"flash.display::DisplayObject");
1428     __ pushscope(c);
1429     __ getlex(c,"flash.display::InteractiveObject");
1430     __ pushscope(c);
1431     __ getlex(c,"flash.display::DisplayObjectContainer");
1432     __ pushscope(c);
1433     __ getlex(c,"flash.display::Sprite");
1434     __ pushscope(c);
1435     __ getlex(c,"flash.display::MovieClip");
1436     __ pushscope(c);
1437     __ getlex(c,"flash.display::MovieClip");
1438     __ newclass(c,cls);
1439     __ popscope(c);
1440     __ popscope(c);
1441     __ popscope(c);
1442     __ popscope(c);
1443     __ popscope(c);
1444     __ popscope(c);
1445     __ popscope(c);
1446     __ initproperty(c,"rfx::MainTimeline");
1447     __ returnvoid(c);
1448
1449     //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1450     multiname_t*classname = multiname_fromstring("rfx::MainTimeline");
1451     abc_initscript_addClassTrait(s, classname, cls);
1452     multiname_destroy(classname);
1453
1454     swf_WriteABC(abctag, file);
1455 }
1456