finished inner function implementation
[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 void params_dump(FILE*fo, multiname_list_t*l, constant_list_t*o)
44 {
45     int n = list_length(l);
46     int no = list_length(o);
47     int i = 0;
48
49     fprintf(fo, "(");
50     while(l) {
51         char*s = multiname_tostring(l->multiname);
52         fprintf(fo, s);
53         free(s);
54         if(i>=n-no) {
55             s = constant_tostring(o->constant);
56             fprintf(fo, " = ");
57             fprintf(fo, s);
58             free(s);
59             o = o->next;
60         }
61
62         if(l->next)
63             fprintf(fo, ", ");
64         l = l->next;i++;
65     }
66     fprintf(fo, ")");
67 }
68
69 //#define DEBUG
70 #define DEBUG if(0)
71
72 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
73 {
74     int t;
75     int num_metadata = swf_GetU30(tag);
76
77     DEBUG printf("%d metadata\n");
78     for(t=0;t<num_metadata;t++) {
79         const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
80         int num = swf_GetU30(tag);
81         int s;
82         DEBUG printf("  %s\n", entry_name);
83         array_t*items = array_new();
84         for(s=0;s<num;s++) {
85             int i1 = swf_GetU30(tag);
86             int i2 = swf_GetU30(tag);
87             const char*key = i1?pool_lookup_string(pool, i1):"";
88             const char*value = i2?pool_lookup_string(pool, i2):"";
89             DEBUG printf("    %s=%s\n", key, value);
90             array_append(items, key, strdup(value));
91         }
92         array_append(file->metadata, entry_name, items);
93     }
94 }
95
96 void swf_CopyData(TAG*to, TAG*from, int len)
97 {
98     unsigned char*data = malloc(len);
99     swf_GetBlock(from, data, len);
100     swf_SetBlock(to, data, len);
101     free(data);
102 }
103
104 abc_file_t*abc_file_new()
105 {
106     abc_file_t*f = malloc(sizeof(abc_file_t));
107     memset(f, 0, sizeof(abc_file_t));
108     f->metadata = array_new();
109
110     f->methods = array_new();
111     f->classes = array_new();
112     f->scripts = array_new();
113     f->method_bodies = array_new();
114     f->flags = ABCFILE_LAZY;
115
116     return f;
117 }
118
119 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass) {
120     
121     NEW(abc_class_t,c);
122     array_append(file->classes, NO_KEY, c);
123
124     c->file = file;
125     c->classname = multiname_clone(classname);
126     c->superclass = multiname_clone(superclass);
127     c->flags = 0;
128     c->constructor = 0;
129     c->static_constructor = 0;
130     c->traits = list_new();
131     return c;
132 }
133 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass) 
134 {
135     return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
136 }
137
138 void abc_class_sealed(abc_class_t*c)
139 {
140     c->flags |= CLASS_SEALED;
141 }
142 void abc_class_final(abc_class_t*c)
143 {
144     c->flags |= CLASS_FINAL;
145 }
146 void abc_class_interface(abc_class_t*c)
147 {
148     c->flags |= CLASS_INTERFACE;
149 }
150 void abc_class_protectedNS(abc_class_t*c, char*namespace)
151 {
152     c->protectedNS = namespace_new_protected(namespace);
153     c->flags |= CLASS_PROTECTED_NS;
154 }
155 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
156 {
157     list_append(c->interfaces, multiname_clone(interface));
158 }
159
160 void abc_method_init(abc_method_t*m, abc_file_t*file, multiname_t*returntype, char body)
161 {
162     /* construct method object */
163     m->index = array_length(file->methods);
164     array_append(file->methods, NO_KEY, m);
165     m->return_type = returntype;
166
167     if(body) {
168         /* construct code (method body) object */
169         NEW(abc_method_body_t,c);
170         array_append(file->method_bodies, NO_KEY, c);
171         c->index = array_length(file->method_bodies);
172         c->file = file;
173         c->traits = list_new();
174         c->code = 0;
175
176         /* crosslink the two objects */
177         m->body = c;
178         c->method = m;
179     }
180 }
181 abc_method_t* abc_method_new(abc_file_t*file, multiname_t*returntype, char body)
182 {
183     NEW(abc_method_t,m);
184     abc_method_init(m, file, returntype, body);
185     return m;
186 }
187
188 abc_method_t* abc_class_getconstructor(abc_class_t*cls, multiname_t*returntype)
189 {
190     if(cls->constructor) {
191         return cls->constructor;
192     }
193     abc_method_t* m = abc_method_new(cls->file, returntype, 1);
194     cls->constructor = m;
195     return m;
196 }
197
198 abc_method_t* abc_class_getstaticconstructor(abc_class_t*cls, multiname_t*returntype)
199 {
200     if(cls->static_constructor) {
201         return cls->static_constructor;
202     }
203     abc_method_t* m = abc_method_new(cls->file, returntype, 1);
204     cls->static_constructor = m;
205     return m;
206 }
207
208 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, constant_t*v)
209 {
210     trait_t*trait = malloc(sizeof(trait_t));
211     memset(trait, 0, sizeof(trait_t));
212     trait->kind = type&0x0f;
213     trait->attributes = type&0xf0;
214     trait->name = name;
215     trait->data1 = data1;
216     trait->data2 = data2;
217     trait->value = v;
218     return trait;
219 }
220
221 trait_t*trait_new_member(trait_list_t**traits, multiname_t*type, multiname_t*name,constant_t*v)
222 {
223     int kind = TRAIT_SLOT;
224     trait_t*trait = malloc(sizeof(trait_t));
225     memset(trait, 0, sizeof(trait_t));
226     trait->kind = kind&0x0f;
227     trait->attributes = kind&0xf0;
228     trait->name = name;
229     trait->type_name = type;
230    
231     trait->slot_id = list_length(*traits)+1;
232     trait_list_t*l = *traits;
233     list_append_(traits, trait);
234     return trait;
235 }
236 trait_t*trait_new_method(trait_list_t**traits, multiname_t*name, abc_method_t*m)
237 {
238     int type = TRAIT_METHOD;
239     trait_t*trait = malloc(sizeof(trait_t));
240     memset(trait, 0, sizeof(trait_t));
241     trait->kind = type&0x0f;
242     trait->attributes = type&0xf0;
243     trait->name = name;
244     trait->method = m;
245     
246     /* start assigning traits at position #1.
247        Weird things happen when assigning slot 0- slot 0 and 1 seem
248        to be identical */
249     trait->slot_id = list_length(*traits)+1;
250     list_append_(traits, trait);
251     return trait;
252 }
253
254 abc_method_t* abc_class_method(abc_class_t*cls, multiname_t*returntype, multiname_t*name)
255 {
256     abc_file_t*file = cls->file;
257     abc_method_t* m = abc_method_new(cls->file, returntype, !(cls->flags&CLASS_INTERFACE));
258     m->trait = trait_new_method(&cls->traits, multiname_clone(name), m);
259     return m;
260 }
261 abc_method_t* abc_class_staticmethod(abc_class_t*cls, multiname_t*returntype, multiname_t*name)
262 {
263     abc_file_t*file = cls->file;
264     abc_method_t* m = abc_method_new(cls->file, returntype, !(cls->flags&CLASS_INTERFACE));
265     m->trait = trait_new_method(&cls->static_traits, multiname_clone(name), m);
266     return m;
267 }
268
269 trait_t* abc_class_slot(abc_class_t*cls, multiname_t*name, multiname_t*type)
270 {
271     abc_file_t*file = cls->file;
272     multiname_t*m_name = multiname_clone(name);
273     multiname_t*m_type = multiname_clone(type);
274     trait_t*t = trait_new_member(&cls->traits, m_type, m_name, 0);
275     return t;
276 }
277 trait_t* abc_class_staticslot(abc_class_t*cls, multiname_t*name, multiname_t*type)
278 {
279     abc_file_t*file = cls->file;
280     multiname_t*m_name = multiname_clone(name);
281     multiname_t*m_type = multiname_clone(type);
282     trait_t*t = trait_new_member(&cls->static_traits, m_type, m_name, 0);
283     return t;
284 }
285
286
287 trait_t* traits_find_slotid(trait_list_t*traits, int slotid)
288 {
289     trait_list_t*l;
290     trait_t*t=0;
291     for(l=traits;l;l=l->next) {
292         if(l->trait->slot_id==slotid) {
293             t=l->trait; 
294             break;
295         }
296     }
297     return t;
298 }
299
300 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
301 {
302     abc_file_t*file = code->file;
303     multiname_t*m = multiname_fromstring(multiname);
304     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
305     trait->cls = cls;
306     list_append(code->traits, trait);
307 }
308
309 /* notice: traits of a method (body) belonging to an init script
310    and traits of the init script are *not* the same thing */
311 int abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls)
312 {
313     abc_file_t*file = script->file;
314     multiname_t*m = multiname_clone(multiname);
315     int slotid = list_length(script->traits)+1;
316     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0);
317     trait->cls = cls;
318     list_append(script->traits, trait);
319     return slotid;
320 }
321
322 abc_script_t* abc_initscript(abc_file_t*file)
323 {
324     abc_method_t*m = abc_method_new(file, 0, 1);
325     abc_script_t* s = malloc(sizeof(abc_script_t));
326     s->method = m;
327     s->traits = list_new();
328     s->file = file;
329     array_append(file->scripts, NO_KEY, s);
330     return s;
331 }
332
333 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file, dict_t*methods_seen);
334
335 static void dump_method(FILE*fo, const char*prefix, 
336                                  const char*attr, 
337                                  const char*type, 
338                                  const char*name, 
339                                  abc_method_t*m, abc_file_t*file, dict_t*methods_seen)
340 {
341     if(methods_seen)
342         dict_put(methods_seen, m, 0);
343
344     char*return_type = 0;
345     if(m->return_type)
346         return_type = multiname_tostring(m->return_type);
347     else
348         return_type = strdup("void");
349
350     fprintf(fo, "%s", prefix);
351     fprintf(fo, "%s %s ", attr, type);
352     fprintf(fo, "%s %s=%s", return_type, name, m->name);
353     params_dump(fo, m->parameters, m->optional_parameters);
354     fprintf(fo, "(%d params, %d optional)\n", list_length(m->parameters), list_length(m->optional_parameters));
355
356     free(return_type);return_type=0;
357
358     abc_method_body_t*c = m->body;
359     if(!c) {
360         return;
361     }
362     
363     fprintf(fo, "%s[stack:%d locals:%d scope:%d-%d flags:", 
364             prefix, c->old.max_stack, c->old.local_count, c->old.init_scope_depth, 
365             c->old.max_scope_depth);
366
367
368     int flags = c->method->flags;
369     if(flags&METHOD_NEED_ARGUMENTS) {fprintf(fo, " need_arguments");flags&=~METHOD_NEED_ARGUMENTS;}
370     if(flags&METHOD_NEED_ACTIVATION) {fprintf(fo, " need_activation");flags&=~METHOD_NEED_ACTIVATION;}
371     if(flags&METHOD_NEED_REST) {fprintf(fo, " need_rest");flags&=~METHOD_NEED_REST;}
372     if(flags&METHOD_HAS_OPTIONAL) {fprintf(fo, " has_optional");flags&=~METHOD_HAS_OPTIONAL;}
373     if(flags&METHOD_SET_DXNS) {fprintf(fo, " set_dxns");flags&=~METHOD_SET_DXNS;}
374     if(flags&METHOD_HAS_PARAM_NAMES) {fprintf(fo, " has_param_names");flags&=~METHOD_HAS_PARAM_NAMES;}
375     if(flags) fprintf(fo, " %02x", flags);
376     fprintf(fo, "]");
377
378     if(m->trait) {
379         fprintf(fo, " slot:%d", m->trait->slot_id);
380     }
381     fprintf(fo, "\n");
382
383
384     char prefix2[80];
385     sprintf(prefix2, "%s    ", prefix);
386     if(c->traits)
387         traits_dump(fo, prefix, c->traits, file, methods_seen);
388     fprintf(fo, "%s{\n", prefix);
389     code_dump2(c->code, c->exceptions, file, prefix2, fo);
390     fprintf(fo, "%s}\n\n", prefix);
391 }
392
393 static void traits_free(trait_list_t*traits) 
394 {
395     trait_list_t*t = traits;
396     while(t) {
397         if(t->trait->name) {
398             multiname_destroy(t->trait->name);t->trait->name = 0;
399         }
400         if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
401             multiname_destroy(t->trait->type_name);
402         }
403         if(t->trait->value) {
404             constant_free(t->trait->value);t->trait->value = 0;
405         }
406         free(t->trait);t->trait = 0;
407         t = t->next;
408     }
409     list_free(traits);
410 }
411             
412 static char trait_is_method(trait_t*trait)
413 {
414     return (trait->kind == TRAIT_METHOD || trait->kind == TRAIT_GETTER || 
415             trait->kind == TRAIT_SETTER || trait->kind == TRAIT_FUNCTION);
416 }
417
418 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
419 {
420     int num_traits = swf_GetU30(tag);
421     trait_list_t*traits = list_new();
422     int t;
423     if(num_traits) {
424         DEBUG printf("%d traits\n", num_traits);
425     }
426     
427     for(t=0;t<num_traits;t++) {
428         NEW(trait_t,trait);
429         list_append(traits, trait);
430
431         trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
432
433         const char*name = 0;
434         DEBUG name = multiname_tostring(trait->name);
435         U8 kind = swf_GetU8(tag);
436         U8 attributes = kind&0xf0;
437         kind&=0x0f;
438         trait->kind = kind;
439         trait->attributes = attributes;
440         DEBUG printf("  trait %d) %s type=%02x\n", t, name, kind);
441         if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
442             trait->disp_id = swf_GetU30(tag);
443             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
444             trait->method->trait = trait;
445             DEBUG printf("  method/getter/setter\n");
446         } else if(kind == TRAIT_FUNCTION) { // function
447             trait->slot_id =  swf_GetU30(tag);
448             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
449             trait->method->trait = trait;
450         } else if(kind == TRAIT_CLASS) { // class
451             trait->slot_id = swf_GetU30(tag);
452             trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
453             DEBUG printf("  class %s %d %d\n", name, trait->slot_id, trait->cls);
454         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
455             trait->slot_id = swf_GetU30(tag);
456             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
457             int vindex = swf_GetU30(tag);
458             if(vindex) {
459                 int vkind = swf_GetU8(tag);
460                 trait->value = constant_fromindex(pool, vindex, vkind);
461             }
462             DEBUG printf("  slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_tostring(trait->value));
463         } else {
464             fprintf(stderr, "Can't parse trait type %d\n", kind);
465         }
466         if(attributes&0x40) {
467             int num = swf_GetU30(tag);
468             int s;
469             for(s=0;s<num;s++) {
470                 swf_GetU30(tag); //index into metadata array
471             }
472         }
473     }
474     return traits;
475 }
476
477 void traits_skip(TAG*tag)
478 {
479     int num_traits = swf_GetU30(tag);
480     int t;
481     for(t=0;t<num_traits;t++) {
482         swf_GetU30(tag);
483         U8 kind = swf_GetU8(tag);
484         U8 attributes = kind&0xf0;
485         kind&=0x0f;
486         swf_GetU30(tag);
487         swf_GetU30(tag);
488         if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
489             if(swf_GetU30(tag)) swf_GetU8(tag);
490         } else if(kind>TRAIT_CONST) {
491             fprintf(stderr, "Can't parse trait type %d\n", kind);
492         }
493         if(attributes&0x40) {
494             int s, num = swf_GetU30(tag);
495             for(s=0;s<num;s++) swf_GetU30(tag);
496         }
497     }
498 }
499
500
501 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
502 {
503     if(!traits) {
504         swf_SetU30(tag, 0);
505         return;
506     }
507     swf_SetU30(tag, list_length(traits));
508     int s;
509
510     while(traits) {
511         trait_t*trait = traits->trait;
512
513         swf_SetU30(tag, pool_register_multiname(pool, trait->name));
514         swf_SetU8(tag, trait->kind|trait->attributes);
515
516         swf_SetU30(tag, trait->data1);
517
518         if(trait->kind == TRAIT_CLASS) {
519             swf_SetU30(tag, trait->cls->index);
520         } else if(trait->kind == TRAIT_GETTER ||
521                   trait->kind == TRAIT_SETTER ||
522                   trait->kind == TRAIT_METHOD) {
523             swf_SetU30(tag, trait->method->index);
524         } else if(trait->kind == TRAIT_SLOT ||
525                   trait->kind == TRAIT_CONST) {
526             int index = pool_register_multiname(pool, trait->type_name);
527             swf_SetU30(tag, index);
528         } else  {
529             swf_SetU30(tag, trait->data2);
530         }
531
532         if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
533             int vindex = constant_get_index(pool, trait->value);
534             swf_SetU30(tag, vindex);
535             if(vindex) {
536                 swf_SetU8(tag, trait->value->type);
537             }
538         }
539         if(trait->attributes&0x40) {
540             // metadata
541             swf_SetU30(tag, 0);
542         }
543         traits = traits->next;
544     }
545 }
546
547
548 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file, dict_t*methods_seen)
549 {
550     int t;
551     while(traits) {
552         trait_t*trait = traits->trait;
553         char*name = multiname_tostring(trait->name);
554         U8 kind = trait->kind;
555         U8 attributes = trait->attributes;
556
557         char a = attributes & (TRAIT_ATTR_OVERRIDE|TRAIT_ATTR_FINAL);
558         char* type = "";
559         if(a==TRAIT_ATTR_FINAL)
560             type = "final ";
561         else if(a==TRAIT_ATTR_OVERRIDE)
562             type = "override ";
563         else if(a==(TRAIT_ATTR_OVERRIDE|TRAIT_ATTR_FINAL))
564             type = "final override ";
565         
566         if(attributes&TRAIT_ATTR_METADATA)
567             fprintf(fo, "<metadata>");
568
569         if(kind == TRAIT_METHOD) {
570             abc_method_t*m = trait->method;
571             dump_method(fo, prefix, type, "method", name, m, file, methods_seen);
572         } else if(kind == TRAIT_GETTER) {
573             abc_method_t*m = trait->method;
574             dump_method(fo, prefix, type, "getter", name, m, file, methods_seen);
575         } else if(kind == TRAIT_SETTER) {
576             abc_method_t*m = trait->method;
577             dump_method(fo, prefix, type, "setter", name, m, file, methods_seen);
578         } else if(kind == TRAIT_FUNCTION) { // function
579             abc_method_t*m = trait->method;
580             dump_method(fo, prefix, type, "function", name, m, file, methods_seen);
581         } else if(kind == TRAIT_CLASS) { // class
582             abc_class_t*cls = trait->cls;
583             if(!cls) {
584                 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
585             } else {
586                 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
587             }
588         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
589             int slot_id = trait->slot_id;
590             char*type_name = multiname_tostring(trait->type_name);
591             char*value = constant_tostring(trait->value);
592             fprintf(fo, "%sslot %d: %s %s:%s %s %s\n", prefix, trait->slot_id, 
593                     kind==TRAIT_CONST?"const":"var", name, type_name, 
594                     value?"=":"", value?value:"");
595             if(value) free(value);
596             free(type_name);
597         } else {
598             fprintf(fo, "%s    can't dump trait type %d\n", prefix, kind);
599         }
600         free(name);
601         traits=traits->next;
602     }
603 }
604
605 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
606 {
607     abc_file_t* file = (abc_file_t*)code;
608
609     if(file->name) {
610         fprintf(fo, "%s#\n", prefix);
611         fprintf(fo, "%s#name: %s\n", prefix, file->name);
612         fprintf(fo, "%s#\n", prefix);
613     }
614
615     int t;
616     for(t=0;t<file->metadata->num;t++) {
617         const char*entry_name = array_getkey(file->metadata, t);
618         fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
619         int s;
620         array_t*items = (array_t*)array_getvalue(file->metadata, t);
621         for(s=0;s<items->num;s++) {
622             fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
623         }
624         fprintf(fo, "%s#\n", prefix);
625     }
626
627     dict_t*methods_seen = dict_new2(&ptr_type);
628     for(t=0;t<file->classes->num;t++) {
629         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
630         char prefix2[80];
631         sprintf(prefix2, "%s    ", prefix);
632
633         fprintf(fo, "%s", prefix);
634         if(cls->flags&1) fprintf(fo, "sealed ");
635         if(cls->flags&2) fprintf(fo, "final ");
636         if(cls->flags&4) fprintf(fo, "interface ");
637         if(cls->flags&8) {
638             char*s = namespace_tostring(cls->protectedNS);
639             fprintf(fo, "protectedNS(%s) ", s);
640             free(s);
641         }
642
643         char*classname = multiname_tostring(cls->classname);
644         fprintf(fo, "class %s", classname);
645         free(classname);
646         if(cls->superclass) {
647             char*supername = multiname_tostring(cls->superclass);
648             fprintf(fo, " extends %s", supername);
649             free(supername);
650             multiname_list_t*ilist = cls->interfaces;
651             if(ilist)
652                 fprintf(fo, " implements");
653             while(ilist) {
654                 char*s = multiname_tostring(ilist->multiname);
655                 fprintf(fo, " %s", s);
656                 free(s);
657                 ilist = ilist->next;
658             }
659             ilist->next;
660         }
661         if(cls->flags&0xf0) 
662             fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
663         fprintf(fo, "%s{\n", prefix);
664
665         dict_put(methods_seen, cls->static_constructor, 0);
666         dict_put(methods_seen, cls->constructor, 0);
667
668         if(cls->static_constructor) {
669             dump_method(fo, prefix2, "", "staticconstructor", "", cls->static_constructor, file, methods_seen);
670         }
671         traits_dump(fo, prefix2, cls->static_traits, file, methods_seen);
672         
673         char*n = multiname_tostring(cls->classname);
674         if(cls->constructor)
675             dump_method(fo, prefix2, "", "constructor", n, cls->constructor, file, methods_seen);
676         free(n);
677         traits_dump(fo, prefix2,cls->traits, file, methods_seen);
678         fprintf(fo, "%s}\n", prefix);
679     }
680     fprintf(fo, "%s\n", prefix);
681
682     for(t=0;t<file->scripts->num;t++) {
683         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
684         dump_method(fo, prefix, "", "initmethod", "init", s->method, file, methods_seen);
685         traits_dump(fo, prefix, s->traits, file, methods_seen);
686     }
687     
688     char extra=0;
689     for(t=0;t<file->methods->num;t++) {
690         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
691         if(!dict_contains(methods_seen, m)) {
692             if(!extra) {
693                 extra=1;
694                 fprintf(fo, "\n");
695                 fprintf(fo, "%s//internal (non-class non-script) methods:\n", prefix);
696             }
697             char name[18];
698             sprintf(name, "%08x ", m->index);
699             dump_method(fo, prefix, "", "internalmethod", name, m, file, methods_seen);
700         }
701     }
702     dict_destroy(methods_seen);
703
704     return file;
705 }
706
707 void* swf_ReadABC(TAG*tag)
708 {
709     abc_file_t* file = abc_file_new();
710     pool_t*pool = pool_new();
711
712     swf_SetTagPos(tag, 0);
713     int t;
714     if(tag->id == ST_DOABC) {
715         U32 abcflags = swf_GetU32(tag);
716         DEBUG printf("flags=%08x\n", abcflags);
717         char*name= swf_GetString(tag);
718         file->name = (name&&name[0])?strdup(name):0;
719     }
720     U32 version = swf_GetU32(tag);
721     if(version!=0x002e0010) {
722         fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
723     }
724
725     pool_read(pool, tag);
726
727     int num_methods = swf_GetU30(tag);
728     DEBUG printf("%d methods\n", num_methods);
729     for(t=0;t<num_methods;t++) {
730         NEW(abc_method_t,m);
731         int param_count = swf_GetU30(tag);
732         int return_type_index = swf_GetU30(tag);
733         if(return_type_index)
734             m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
735         else
736             m->return_type = 0;
737
738         int s;
739         for(s=0;s<param_count;s++) {
740             int type_index = swf_GetU30(tag);
741             
742             /* type_index might be 0 ("*") */
743             multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
744             list_append(m->parameters, param);
745         }
746
747         int namenr = swf_GetU30(tag);
748         if(namenr)
749             m->name = strdup(pool_lookup_string(pool, namenr));
750         else
751             m->name = strdup("");
752
753         m->flags = swf_GetU8(tag);
754         
755         DEBUG printf("method %d) %s ", m->name);
756         DEBUG params_dump(stdout, m->parameters, m->optional_parameters);
757         DEBUG printf("flags=%02x\n", t, m->flags);
758
759         if(m->flags&0x08) {
760             m->optional_parameters = list_new();
761             int num = swf_GetU30(tag);
762             int s;
763             for(s=0;s<num;s++) {
764                 int vindex = swf_GetU30(tag);
765                 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
766                 constant_t*c = constant_fromindex(pool, vindex, vkind);
767                 list_append(m->optional_parameters, c);
768
769             }
770         }
771         if(m->flags&0x80) {
772             /* debug information- not used by avm2 */
773             multiname_list_t*l = m->parameters;
774             while(l) {
775                 const char*name = pool_lookup_string(pool, swf_GetU30(tag));
776                 l = l->next;
777             }
778         }
779         m->index = array_length(file->methods);
780         array_append(file->methods, NO_KEY, m);
781     }
782             
783     parse_metadata(tag, file, pool);
784         
785     /* skip classes, and scripts for now, and do the real parsing later */
786     int num_classes = swf_GetU30(tag);
787     int classes_pos = tag->pos;
788     DEBUG printf("%d classes\n", num_classes);
789     for(t=0;t<num_classes;t++) {
790         abc_class_t*cls = malloc(sizeof(abc_class_t));
791         memset(cls, 0, sizeof(abc_class_t));
792         
793         swf_GetU30(tag); //classname
794         swf_GetU30(tag); //supername
795
796         array_append(file->classes, NO_KEY, cls);
797
798         cls->flags = swf_GetU8(tag);
799         DEBUG printf("class %d %02x\n", t, cls->flags);
800         if(cls->flags&8) 
801             swf_GetU30(tag); //protectedNS
802         int s;
803         int inum = swf_GetU30(tag); //interface count
804         cls->interfaces = 0;
805         for(s=0;s<inum;s++) {
806             int interface_index = swf_GetU30(tag);
807             multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
808             list_append(cls->interfaces, m);
809             DEBUG printf("  class %d interface: %s\n", t, m->name);
810         }
811
812         int iinit = swf_GetU30(tag); //iinit
813         DEBUG printf("--iinit-->%d\n", iinit);
814         traits_skip(tag);
815     }
816     for(t=0;t<num_classes;t++) {
817         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
818         int cinit = swf_GetU30(tag);
819         DEBUG printf("--cinit(%d)-->%d\n", t, cinit);
820         cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
821         traits_skip(tag);
822     }
823     int num_scripts = swf_GetU30(tag);
824     DEBUG printf("%d scripts\n", num_scripts);
825     for(t=0;t<num_scripts;t++) {
826         int init = swf_GetU30(tag);
827         traits_skip(tag);
828     }
829
830     int num_method_bodies = swf_GetU30(tag);
831     DEBUG printf("%d method bodies\n", num_method_bodies);
832     for(t=0;t<num_method_bodies;t++) {
833         int methodnr = swf_GetU30(tag);
834         if(methodnr >= file->methods->num) {
835             printf("Invalid method number: %d\n", methodnr);
836             return 0;
837         }
838         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
839         abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
840         memset(c, 0, sizeof(abc_method_body_t));
841         c->old.max_stack = swf_GetU30(tag);
842         c->old.local_count = swf_GetU30(tag);
843         c->old.init_scope_depth = swf_GetU30(tag);
844         c->old.max_scope_depth = swf_GetU30(tag);
845
846         c->init_scope_depth = c->old.init_scope_depth;
847         int code_length = swf_GetU30(tag);
848
849         c->method = m;
850         m->body = c;
851
852         int pos = tag->pos + code_length;
853         codelookup_t*codelookup = 0;
854         c->code = code_parse(tag, code_length, file, pool, &codelookup);
855         tag->pos = pos;
856
857         int exception_count = swf_GetU30(tag);
858         int s;
859         c->exceptions = list_new();
860         for(s=0;s<exception_count;s++) {
861             abc_exception_t*e = malloc(sizeof(abc_exception_t));
862
863             e->from = code_atposition(codelookup, swf_GetU30(tag));
864             e->to = code_atposition(codelookup, swf_GetU30(tag));
865             e->target = code_atposition(codelookup, swf_GetU30(tag));
866
867             e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
868             e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
869             //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
870             //if(e->var_name) e->var_name = strdup(e->var_name);
871             list_append(c->exceptions, e);
872         }
873         codelookup_free(codelookup);
874         c->traits = traits_parse(tag, pool, file);
875
876         DEBUG printf("method_body %d) (method %d), %d bytes of code\n", t, methodnr, code_length);
877
878         array_append(file->method_bodies, NO_KEY, c);
879     }
880     if(tag->len - tag->pos) {
881         fprintf(stderr, "ERROR: %d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
882         return 0;
883     }
884
885     swf_SetTagPos(tag, classes_pos);
886     for(t=0;t<num_classes;t++) {
887         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
888
889         int classname_index = swf_GetU30(tag);
890         int superclass_index = swf_GetU30(tag);
891         cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
892         cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
893         cls->flags = swf_GetU8(tag);
894         const char*ns = "";
895         if(cls->flags&8) {
896             int ns_index = swf_GetU30(tag);
897             cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
898         }
899         
900         int num_interfaces = swf_GetU30(tag); //interface count
901         int s;
902         for(s=0;s<num_interfaces;s++) {
903             swf_GetU30(tag);
904         }
905         int iinit = swf_GetU30(tag);
906         cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
907         cls->traits = traits_parse(tag, pool, file);
908     }
909     for(t=0;t<num_classes;t++) {
910         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
911         /* SKIP */
912         swf_GetU30(tag); // cindex
913         cls->static_traits = traits_parse(tag, pool, file);
914     }
915     int num_scripts2 = swf_GetU30(tag);
916     for(t=0;t<num_scripts2;t++) {
917         int init = swf_GetU30(tag);
918         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
919         
920         abc_script_t*s = malloc(sizeof(abc_script_t));
921         memset(s, 0, sizeof(abc_script_t));
922         s->method = m;
923         s->traits = traits_parse(tag, pool, file);
924         array_append(file->scripts, NO_KEY, s);
925     }
926
927     pool_destroy(pool);
928     return file;
929 }
930
931 static pool_t*writeABC(TAG*abctag, void*code, pool_t*pool)
932 {
933     abc_file_t*file = (abc_file_t*)code;
934     if(!pool) 
935         pool = pool_new();
936     if(!file)
937         file = abc_file_new();
938
939     TAG*tmp = swf_InsertTag(0,0);
940     TAG*tag = tmp;
941     int t;
942   
943     /* add method bodies where needed */
944     for(t=0;t<file->classes->num;t++) {
945         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
946         if(!c->constructor) {
947             if(!(c->flags&CLASS_INTERFACE)) {
948                 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
949                 NEW(abc_method_body_t,body);array_append(file->method_bodies, NO_KEY, body);
950                 // don't bother to set m->index
951                 body->method = m; m->body = body;
952                 __ returnvoid(body);
953                 c->constructor = m;
954             } else {
955                 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
956                 c->constructor = m;
957             }
958         }
959         if(!c->static_constructor) {
960             NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
961             NEW(abc_method_body_t,body);array_append(file->method_bodies, NO_KEY, body);
962             body->method = m; m->body = body;
963             __ returnvoid(body);
964             c->static_constructor = m;
965         }
966     }
967
968
969     swf_SetU30(tag, file->methods->num);
970     /* enumerate classes, methods and method bodies */
971     for(t=0;t<file->methods->num;t++) {
972         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
973         m->index = t;
974     }
975     for(t=0;t<file->classes->num;t++) {
976         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
977         c->index = t;
978     }
979     for(t=0;t<file->method_bodies->num;t++) {
980         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
981         m->index = t;
982     }
983     
984     /* generate code statistics */
985     for(t=0;t<file->method_bodies->num;t++) {
986         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
987         m->stats = code_get_statistics(m->code, m->exceptions);
988     }
989     
990     /* level init scope depths: The init scope depth of a method is
991        always as least as high as the init scope depth of it's surrounding
992        class.
993        A method has it's own init_scope_depth if it's an init method 
994        (then its init scope depth is zero), or if it's used as a closure.
995
996        Not sure yet what to do with methods which are used at different
997        locations- e.g. the nullmethod is used all over the place.
998        EDIT: flashplayer doesn't allow this anyway- a method can only
999              be used once
1000
1001        Also, I have the strong suspicion that flash player uses only
1002        the difference between max_scope_stack and init_scope_stack, anyway.
1003      */
1004     for(t=0;t<file->classes->num;t++) {
1005         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1006         trait_list_t*traits = c->traits;
1007         if(c->constructor && c->constructor->body &&
1008            c->constructor->body->init_scope_depth < c->init_scope_depth) {
1009            c->constructor->body->init_scope_depth = c->init_scope_depth;
1010         }
1011         if(c->static_constructor && c->static_constructor->body &&
1012            c->static_constructor->body->init_scope_depth < c->init_scope_depth) {
1013            c->static_constructor->body->init_scope_depth = c->init_scope_depth;
1014         }
1015         while(traits) {
1016             trait_t*trait = traits->trait;
1017             if(trait_is_method(trait) && trait->method->body) {
1018                 abc_method_body_t*body = trait->method->body;
1019                 if(body->init_scope_depth < c->init_scope_depth) {
1020                    body->init_scope_depth = c->init_scope_depth;
1021                 }
1022             }
1023             traits = traits->next;
1024         }
1025     }
1026     
1027     for(t=0;t<file->methods->num;t++) {
1028         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1029         int n = 0;
1030         multiname_list_t*l = m->parameters;
1031         int num_params = list_length(m->parameters);
1032         swf_SetU30(tag, num_params);
1033         if(m->return_type) 
1034             swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
1035         else
1036             swf_SetU30(tag, 0);
1037         int s;
1038         while(l) {
1039             swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
1040             l = l->next;
1041         }
1042         if(m->name) {
1043             swf_SetU30(tag, pool_register_string(pool, m->name));
1044         } else {
1045             swf_SetU30(tag, 0);
1046         }
1047
1048         U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
1049         if(m->optional_parameters)
1050             flags |= METHOD_HAS_OPTIONAL;
1051         if(m->body) {
1052             flags |= m->body->stats->flags;
1053         }
1054
1055         swf_SetU8(tag, flags);
1056         if(flags&METHOD_HAS_OPTIONAL) {
1057             swf_SetU30(tag, list_length(m->optional_parameters));
1058             constant_list_t*l = m->optional_parameters;
1059             while(l) {
1060                 int i = constant_get_index(pool, l->constant);
1061                 swf_SetU30(tag, i);
1062                 if(!i) {
1063                     swf_SetU8(tag, CONSTANT_NULL);
1064                 } else {
1065                     swf_SetU8(tag, l->constant->type);
1066                 }
1067                 l = l->next;
1068             }
1069         }
1070     }
1071    
1072     /* write metadata */
1073     swf_SetU30(tag, file->metadata->num);
1074     for(t=0;t<file->metadata->num;t++) {
1075         const char*entry_name = array_getkey(file->metadata, t);
1076         swf_SetU30(tag, pool_register_string(pool, entry_name));
1077         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1078         swf_SetU30(tag, items->num);
1079         int s;
1080         for(s=0;s<items->num;s++) {
1081             int i1 = pool_register_string(pool, array_getkey(items, s));
1082             int i2 = pool_register_string(pool, array_getvalue(items, s));
1083             swf_SetU30(tag, i1);
1084             swf_SetU30(tag, i2);
1085         }
1086     }
1087
1088     swf_SetU30(tag, file->classes->num);
1089     for(t=0;t<file->classes->num;t++) {
1090         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1091    
1092         int classname_index = pool_register_multiname(pool, c->classname);
1093         int superclass_index = pool_register_multiname(pool, c->superclass);
1094
1095         swf_SetU30(tag, classname_index);
1096         swf_SetU30(tag, superclass_index);
1097
1098         swf_SetU8(tag, c->flags); // flags
1099         if(c->flags&0x08) {
1100             int ns_index = pool_register_namespace(pool, c->protectedNS);
1101             swf_SetU30(tag, ns_index);
1102         }
1103
1104         swf_SetU30(tag, list_length(c->interfaces));
1105         multiname_list_t*interface= c->interfaces;
1106         while(interface) {
1107             swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1108             interface = interface->next;
1109         }
1110
1111         assert(c->constructor);
1112         swf_SetU30(tag, c->constructor->index);
1113
1114         traits_write(pool, tag, c->traits);
1115     }
1116     for(t=0;t<file->classes->num;t++) {
1117         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1118         assert(c->static_constructor);
1119         swf_SetU30(tag, c->static_constructor->index);
1120         
1121         traits_write(pool, tag, c->static_traits);
1122     }
1123
1124     swf_SetU30(tag, file->scripts->num);
1125     for(t=0;t<file->scripts->num;t++) {
1126         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1127         swf_SetU30(tag, s->method->index); //!=t!
1128         traits_write(pool, tag, s->traits);
1129     }
1130
1131     swf_SetU30(tag, file->method_bodies->num);
1132     for(t=0;t<file->method_bodies->num;t++) {
1133         abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1134         abc_method_t*m = c->method;
1135         swf_SetU30(tag, m->index);
1136
1137         //swf_SetU30(tag, c->old.max_stack);
1138         //swf_SetU30(tag, c->old.local_count);
1139         //swf_SetU30(tag, c->old.init_scope_depth);
1140         //swf_SetU30(tag, c->old.max_scope_depth);
1141
1142         swf_SetU30(tag, c->stats->max_stack);
1143         int param_num = list_length(c->method->parameters)+1;
1144         if(c->method->flags&METHOD_NEED_REST)
1145             param_num++;
1146         if(param_num <= c->stats->local_count)
1147             swf_SetU30(tag, c->stats->local_count);
1148         else
1149             swf_SetU30(tag, param_num);
1150
1151         swf_SetU30(tag, c->init_scope_depth);
1152         swf_SetU30(tag, c->stats->max_scope_depth+
1153                         c->init_scope_depth);
1154
1155         code_write(tag, c->code, pool, file);
1156
1157         swf_SetU30(tag, list_length(c->exceptions));
1158         abc_exception_list_t*l = c->exceptions;
1159         while(l) {
1160             // warning: assumes "pos" in each code_t is up-to-date
1161             swf_SetU30(tag, l->abc_exception->from->pos);
1162             swf_SetU30(tag, l->abc_exception->to->pos);
1163             swf_SetU30(tag, l->abc_exception->target->pos);
1164             swf_SetU30(tag, pool_register_multiname(pool, l->abc_exception->exc_type));
1165             swf_SetU30(tag, pool_register_multiname(pool, l->abc_exception->var_name));
1166             l = l->next;
1167         }
1168
1169         traits_write(pool, tag, c->traits);
1170     }
1171    
1172     /* free temporary codestat data again. Notice: If we were to write this
1173        file multiple times, this can also be shifted to abc_file_free() */
1174     for(t=0;t<file->method_bodies->num;t++) {
1175         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1176         codestats_free(m->stats);m->stats=0;
1177     }
1178
1179     // --- start to write real tag --
1180     
1181     tag = abctag;
1182
1183     if(tag->id == ST_DOABC) {
1184         swf_SetU32(tag, file->flags); // flags
1185         swf_SetString(tag, file->name);
1186     }
1187
1188     swf_SetU16(tag, 0x10); //version
1189     swf_SetU16(tag, 0x2e);
1190     
1191     pool_write(pool, tag);
1192     
1193     swf_SetBlock(tag, tmp->data, tmp->len);
1194
1195     swf_DeleteTag(0, tmp);
1196     return pool;
1197 }
1198
1199 void swf_WriteABC(TAG*abctag, void*code)
1200 {
1201     pool_t*pool = writeABC(abctag, code, 0);
1202     pool_optimize(pool);
1203     swf_ResetTag(abctag, abctag->id);
1204     writeABC(abctag, code, pool);
1205     pool_destroy(pool);
1206 }
1207
1208 void abc_file_free(abc_file_t*file)
1209 {
1210     if(!file)
1211         return;
1212     int t;
1213     if(file->metadata) {
1214         for(t=0;t<file->metadata->num;t++) {
1215             array_t*items = (array_t*)array_getvalue(file->metadata, t);
1216             int s;
1217             for(s=0;s<items->num;s++) {
1218                 free(array_getvalue(items, s));
1219             }
1220             array_free(items);
1221         }
1222         array_free(file->metadata);file->metadata=0;
1223     }
1224
1225     for(t=0;t<file->methods->num;t++) {
1226         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1227
1228         multiname_list_t*param = m->parameters;
1229         while(param) {
1230             multiname_destroy(param->multiname);param->multiname=0;
1231             param = param->next;
1232         }
1233         list_free(m->parameters);m->parameters=0;
1234        
1235         constant_list_t*opt = m->optional_parameters;
1236         while(opt) {
1237             constant_free(opt->constant);opt->constant=0;
1238             opt = opt->next;
1239         }
1240         list_free(m->optional_parameters);m->optional_parameters=0;
1241
1242         if(m->name) {
1243             free((void*)m->name);m->name=0;
1244         }
1245         if(m->return_type) {
1246             multiname_destroy(m->return_type);
1247         }
1248         free(m);
1249     }
1250     array_free(file->methods);file->methods=0;
1251
1252     for(t=0;t<file->classes->num;t++) {
1253         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1254         traits_free(cls->traits);cls->traits=0;
1255         traits_free(cls->static_traits);cls->static_traits=0;
1256
1257         if(cls->classname) {
1258             multiname_destroy(cls->classname);
1259         }
1260         if(cls->superclass) {
1261             multiname_destroy(cls->superclass);
1262         }
1263
1264         multiname_list_t*i = cls->interfaces;
1265         while(i) {
1266             multiname_destroy(i->multiname);i->multiname=0;
1267             i = i->next;
1268         }
1269         list_free(cls->interfaces);cls->interfaces=0;
1270
1271         if(cls->protectedNS) {
1272             namespace_destroy(cls->protectedNS);
1273         }
1274         free(cls);
1275     }
1276     array_free(file->classes);file->classes=0;
1277
1278     for(t=0;t<file->scripts->num;t++) {
1279         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1280         traits_free(s->traits);s->traits=0;
1281         free(s);
1282     }
1283     array_free(file->scripts);file->scripts=0;
1284
1285     for(t=0;t<file->method_bodies->num;t++) {
1286         abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1287         code_free(body->code);body->code=0;
1288         traits_free(body->traits);body->traits=0;
1289
1290         abc_exception_list_t*ee = body->exceptions;
1291         while(ee) {
1292             abc_exception_t*e=ee->abc_exception;ee->abc_exception=0;
1293             e->from = e->to = e->target = 0;
1294             multiname_destroy(e->exc_type);e->exc_type=0;
1295             multiname_destroy(e->var_name);e->var_name=0;
1296             free(e);
1297             ee=ee->next;
1298         }
1299         list_free(body->exceptions);body->exceptions=0;
1300         
1301         free(body);
1302     }
1303     array_free(file->method_bodies);file->method_bodies=0;
1304
1305     if(file->name) {
1306         free((void*)file->name);file->name=0;
1307     }
1308
1309     free(file);
1310 }
1311
1312 void swf_FreeABC(void*code)
1313 {
1314     abc_file_t*file= (abc_file_t*)code;
1315     abc_file_free(file);
1316 }
1317