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