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