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