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