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