fixed bug in slot assignment
[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->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, multiname_t*returntype)
333 {
334     abc_method_t*m = abc_method_new(file, returntype, 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_dump(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             /* a slot is a variable in a class that is shared amonst all instances
465                of the same type, but which has a unique location in each object 
466                (in other words, slots are non-static, traits are static)
467              */
468             trait->slot_id = swf_GetU30(tag);
469             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
470             int vindex = swf_GetU30(tag);
471             if(vindex) {
472                 int vkind = swf_GetU8(tag);
473                 trait->value = constant_fromindex(pool, vindex, vkind);
474             }
475             DEBUG printf("  slot %s %d %s (%s)\n", name, trait->slot_id, trait->type_name->name, constant_tostring(trait->value));
476         } else {
477             fprintf(stderr, "Can't parse trait type %d\n", kind);
478         }
479         if(attributes&0x40) {
480             int num = swf_GetU30(tag);
481             int s;
482             for(s=0;s<num;s++) {
483                 swf_GetU30(tag); //index into metadata array
484             }
485         }
486     }
487     return traits;
488 }
489
490 void traits_skip(TAG*tag)
491 {
492     int num_traits = swf_GetU30(tag);
493     int t;
494     for(t=0;t<num_traits;t++) {
495         swf_GetU30(tag);
496         U8 kind = swf_GetU8(tag);
497         U8 attributes = kind&0xf0;
498         kind&=0x0f;
499         swf_GetU30(tag);
500         swf_GetU30(tag);
501         if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
502             if(swf_GetU30(tag)) swf_GetU8(tag);
503         } else if(kind>TRAIT_CONST) {
504             fprintf(stderr, "Can't parse trait type %d\n", kind);
505         }
506         if(attributes&0x40) {
507             int s, num = swf_GetU30(tag);
508             for(s=0;s<num;s++) swf_GetU30(tag);
509         }
510     }
511 }
512
513
514 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
515 {
516     if(!traits) {
517         swf_SetU30(tag, 0);
518         return;
519     }
520     swf_SetU30(tag, list_length(traits));
521     int s;
522
523     while(traits) {
524         trait_t*trait = traits->trait;
525
526         swf_SetU30(tag, pool_register_multiname(pool, trait->name));
527         swf_SetU8(tag, trait->kind|trait->attributes);
528
529         swf_SetU30(tag, trait->data1);
530
531         if(trait->kind == TRAIT_CLASS) {
532             swf_SetU30(tag, trait->cls->index);
533         } else if(trait->kind == TRAIT_GETTER ||
534                   trait->kind == TRAIT_SETTER ||
535                   trait->kind == TRAIT_METHOD) {
536             swf_SetU30(tag, trait->method->index);
537         } else if(trait->kind == TRAIT_SLOT ||
538                   trait->kind == TRAIT_CONST) {
539             int index = pool_register_multiname(pool, trait->type_name);
540             swf_SetU30(tag, index);
541         } else  {
542             swf_SetU30(tag, trait->data2);
543         }
544
545         if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
546             int vindex = constant_get_index(pool, trait->value);
547             swf_SetU30(tag, vindex);
548             if(vindex) {
549                 swf_SetU8(tag, trait->value->type);
550             }
551         }
552         if(trait->attributes&0x40) {
553             // metadata
554             swf_SetU30(tag, 0);
555         }
556         traits = traits->next;
557     }
558 }
559
560
561 static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file, dict_t*methods_seen)
562 {
563     int t;
564     while(traits) {
565         trait_t*trait = traits->trait;
566         char*name = multiname_tostring(trait->name);
567         U8 kind = trait->kind;
568         U8 attributes = trait->attributes;
569
570         char a = attributes & (TRAIT_ATTR_OVERRIDE|TRAIT_ATTR_FINAL);
571         char* type = "";
572         if(a==TRAIT_ATTR_FINAL)
573             type = "final ";
574         else if(a==TRAIT_ATTR_OVERRIDE)
575             type = "override ";
576         else if(a==(TRAIT_ATTR_OVERRIDE|TRAIT_ATTR_FINAL))
577             type = "final override ";
578         
579         if(attributes&TRAIT_ATTR_METADATA)
580             fprintf(fo, "<metadata>");
581
582         if(kind == TRAIT_METHOD) {
583             abc_method_t*m = trait->method;
584             dump_method(fo, prefix, type, "method", name, m, file, methods_seen);
585         } else if(kind == TRAIT_GETTER) {
586             abc_method_t*m = trait->method;
587             dump_method(fo, prefix, type, "getter", name, m, file, methods_seen);
588         } else if(kind == TRAIT_SETTER) {
589             abc_method_t*m = trait->method;
590             dump_method(fo, prefix, type, "setter", name, m, file, methods_seen);
591         } else if(kind == TRAIT_FUNCTION) { // function
592             abc_method_t*m = trait->method;
593             dump_method(fo, prefix, type, "function", name, m, file, methods_seen);
594         } else if(kind == TRAIT_CLASS) { // class
595             abc_class_t*cls = trait->cls;
596             if(!cls) {
597                 fprintf(fo, "%sslot %d: class %s=00000000\n", prefix, trait->slot_id, name);
598             } else {
599                 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
600             }
601         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
602             int slot_id = trait->slot_id;
603             char*type_name = multiname_tostring(trait->type_name);
604             char*value = constant_tostring(trait->value);
605             fprintf(fo, "%sslot %d: %s%s %s %s %s\n", prefix, trait->slot_id, 
606                     kind==TRAIT_CONST?"const ":"", type_name, name, 
607                     value?"=":"", value);
608             if(value) free(value);
609             free(type_name);
610         } else {
611             fprintf(fo, "%s    can't dump trait type %d\n", prefix, kind);
612         }
613         free(name);
614         traits=traits->next;
615     }
616 }
617
618 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
619 {
620     abc_file_t* file = (abc_file_t*)code;
621         
622     if(file->name) {
623         fprintf(fo, "%s#\n", prefix);
624         fprintf(fo, "%s#name: %s\n", prefix, file->name);
625         fprintf(fo, "%s#\n", prefix);
626     }
627
628     int t;
629     for(t=0;t<file->metadata->num;t++) {
630         const char*entry_name = array_getkey(file->metadata, t);
631         fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
632         int s;
633         array_t*items = (array_t*)array_getvalue(file->metadata, t);
634         for(s=0;s<items->num;s++) {
635             fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
636         }
637         fprintf(fo, "%s#\n", prefix);
638     }
639
640     dict_t*methods_seen = dict_new2(&ptr_type);
641     for(t=0;t<file->classes->num;t++) {
642         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
643         char prefix2[80];
644         sprintf(prefix2, "%s    ", prefix);
645
646         fprintf(fo, "%s", prefix);
647         if(cls->flags&1) fprintf(fo, "sealed ");
648         if(cls->flags&2) fprintf(fo, "final ");
649         if(cls->flags&4) fprintf(fo, "interface ");
650         if(cls->flags&8) {
651             char*s = namespace_tostring(cls->protectedNS);
652             fprintf(fo, "protectedNS(%s) ", s);
653             free(s);
654         }
655
656         char*classname = multiname_tostring(cls->classname);
657         fprintf(fo, "class %s", classname);
658         free(classname);
659         if(cls->superclass) {
660             char*supername = multiname_tostring(cls->superclass);
661             fprintf(fo, " extends %s", supername);
662             free(supername);
663             multiname_list_t*ilist = cls->interfaces;
664             if(ilist)
665                 fprintf(fo, " implements");
666             while(ilist) {
667                 char*s = multiname_tostring(ilist->multiname);
668                 fprintf(fo, " %s", s);
669                 free(s);
670                 ilist = ilist->next;
671             }
672             ilist->next;
673         }
674         if(cls->flags&0xf0) 
675             fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
676         fprintf(fo, "%s{\n", prefix);
677
678         dict_put(methods_seen, cls->static_constructor, 0);
679         dict_put(methods_seen, cls->constructor, 0);
680
681         if(cls->static_constructor) {
682             dump_method(fo, prefix2, "", "staticconstructor", "", cls->static_constructor, file, methods_seen);
683         }
684         traits_dump(fo, prefix2, cls->static_traits, file, methods_seen);
685         
686         char*n = multiname_tostring(cls->classname);
687         if(cls->constructor)
688             dump_method(fo, prefix2, "", "constructor", n, cls->constructor, file, methods_seen);
689         free(n);
690         traits_dump(fo, prefix2,cls->traits, file, methods_seen);
691         fprintf(fo, "%s}\n", prefix);
692     }
693     fprintf(fo, "%s\n", prefix);
694
695     for(t=0;t<file->scripts->num;t++) {
696         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
697         dump_method(fo, prefix, "", "initmethod", "init", s->method, file, methods_seen);
698         traits_dump(fo, prefix, s->traits, file, methods_seen);
699     }
700     
701     char extra=0;
702     for(t=0;t<file->methods->num;t++) {
703         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
704         if(!dict_contains(methods_seen, m)) {
705             if(!extra) {
706                 extra=1;
707                 fprintf(fo, "\n");
708                 fprintf(fo, "%s//internal (non-class non-script) methods:\n", prefix);
709             }
710             char name[18];
711             sprintf(name, "%08x ", m);
712             dump_method(fo, prefix, "", "internalmethod", name, m, file, methods_seen);
713         }
714     }
715     dict_destroy(methods_seen);
716
717     return file;
718 }
719
720 void* swf_ReadABC(TAG*tag)
721 {
722     abc_file_t* file = abc_file_new();
723     pool_t*pool = pool_new();
724
725     swf_SetTagPos(tag, 0);
726     int t;
727     if(tag->id == ST_DOABC) {
728         U32 abcflags = swf_GetU32(tag);
729         DEBUG printf("flags=%08x\n", abcflags);
730         char*name= swf_GetString(tag);
731         file->name = (name&&name[0])?strdup(name):0;
732     }
733     U32 version = swf_GetU32(tag);
734     if(version!=0x002e0010) {
735         fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
736     }
737
738     pool_read(pool, tag);
739
740     int num_methods = swf_GetU30(tag);
741     DEBUG printf("%d methods\n", num_methods);
742     for(t=0;t<num_methods;t++) {
743         NEW(abc_method_t,m);
744         int param_count = swf_GetU30(tag);
745         int return_type_index = swf_GetU30(tag);
746         if(return_type_index)
747             m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
748         else
749             m->return_type = 0;
750
751         int s;
752         for(s=0;s<param_count;s++) {
753             int type_index = swf_GetU30(tag);
754             
755             /* type_index might be 0, which probably means "..." (varargs) */
756             multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
757             list_append(m->parameters, param);
758         }
759
760         int namenr = swf_GetU30(tag);
761         if(namenr)
762             m->name = strdup(pool_lookup_string(pool, namenr));
763         else
764             m->name = strdup("");
765
766         m->flags = swf_GetU8(tag);
767         
768         DEBUG printf("method %d) %s %s flags=%02x\n", t, m->name, params_tostring(m->parameters), m->flags);
769
770         if(m->flags&0x08) {
771             m->optional_parameters = list_new();
772             int num = swf_GetU30(tag);
773             int s;
774             for(s=0;s<num;s++) {
775                 int vindex = swf_GetU30(tag);
776                 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
777                 constant_t*c = constant_fromindex(pool, vindex, vkind);
778                 list_append(m->optional_parameters, c);
779             }
780         }
781         if(m->flags&0x80) {
782             /* debug information- not used by avm2 */
783             multiname_list_t*l = m->parameters;
784             while(l) {
785                 const char*name = pool_lookup_string(pool, swf_GetU30(tag));
786                 l = l->next;
787             }
788         }
789         m->index = array_length(file->methods);
790         array_append(file->methods, NO_KEY, m);
791     }
792             
793     parse_metadata(tag, file, pool);
794         
795     /* skip classes, and scripts for now, and do the real parsing later */
796     int num_classes = swf_GetU30(tag);
797     int classes_pos = tag->pos;
798     DEBUG printf("%d classes\n", num_classes);
799     for(t=0;t<num_classes;t++) {
800         abc_class_t*cls = malloc(sizeof(abc_class_t));
801         memset(cls, 0, sizeof(abc_class_t));
802         
803         swf_GetU30(tag); //classname
804         swf_GetU30(tag); //supername
805
806         array_append(file->classes, NO_KEY, cls);
807
808         cls->flags = swf_GetU8(tag);
809         DEBUG printf("class %d %02x\n", t, cls->flags);
810         if(cls->flags&8) 
811             swf_GetU30(tag); //protectedNS
812         int s;
813         int inum = swf_GetU30(tag); //interface count
814         cls->interfaces = 0;
815         for(s=0;s<inum;s++) {
816             int interface_index = swf_GetU30(tag);
817             multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
818             list_append(cls->interfaces, m);
819             DEBUG printf("  class %d interface: %s\n", t, m->name);
820         }
821
822         int iinit = swf_GetU30(tag); //iinit
823         DEBUG printf("--iinit-->%d\n", iinit);
824         traits_skip(tag);
825     }
826     for(t=0;t<num_classes;t++) {
827         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
828         int cinit = swf_GetU30(tag);
829         DEBUG printf("--cinit(%d)-->%d\n", t, cinit);
830         cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
831         traits_skip(tag);
832     }
833     int num_scripts = swf_GetU30(tag);
834     DEBUG printf("%d scripts\n", num_scripts);
835     for(t=0;t<num_scripts;t++) {
836         int init = swf_GetU30(tag);
837         traits_skip(tag);
838     }
839
840     int num_method_bodies = swf_GetU30(tag);
841     DEBUG printf("%d method bodies\n", num_method_bodies);
842     for(t=0;t<num_method_bodies;t++) {
843         int methodnr = swf_GetU30(tag);
844         if(methodnr >= file->methods->num) {
845             printf("Invalid method number: %d\n", methodnr);
846             return 0;
847         }
848         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
849         abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
850         memset(c, 0, sizeof(abc_method_body_t));
851         c->old.max_stack = swf_GetU30(tag);
852         c->old.local_count = swf_GetU30(tag);
853         c->old.init_scope_depth = swf_GetU30(tag);
854         c->old.max_scope_depth = swf_GetU30(tag);
855
856         c->init_scope_depth = c->old.init_scope_depth;
857         int code_length = swf_GetU30(tag);
858
859         c->method = m;
860         m->body = c;
861
862         int pos = tag->pos + code_length;
863         codelookup_t*codelookup = 0;
864         c->code = code_parse(tag, code_length, file, pool, &codelookup);
865         tag->pos = pos;
866
867         int exception_count = swf_GetU30(tag);
868         int s;
869         c->exceptions = list_new();
870         for(s=0;s<exception_count;s++) {
871             abc_exception_t*e = malloc(sizeof(abc_exception_t));
872
873             e->from = code_atposition(codelookup, swf_GetU30(tag));
874             e->to = code_atposition(codelookup, swf_GetU30(tag));
875             e->target = code_atposition(codelookup, swf_GetU30(tag));
876
877             e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
878             e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
879             //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
880             //if(e->var_name) e->var_name = strdup(e->var_name);
881             list_append(c->exceptions, e);
882         }
883         codelookup_free(codelookup);
884         c->traits = traits_parse(tag, pool, file);
885
886         DEBUG printf("method_body %d) (method %d), %d bytes of code\n", t, methodnr, code_length);
887
888         array_append(file->method_bodies, NO_KEY, c);
889     }
890     if(tag->len - tag->pos) {
891         fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
892         return 0;
893     }
894
895     swf_SetTagPos(tag, classes_pos);
896     for(t=0;t<num_classes;t++) {
897         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
898
899         int classname_index = swf_GetU30(tag);
900         int superclass_index = swf_GetU30(tag);
901         cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
902         cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
903         cls->flags = swf_GetU8(tag);
904         const char*ns = "";
905         if(cls->flags&8) {
906             int ns_index = swf_GetU30(tag);
907             cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
908         }
909         
910         int num_interfaces = swf_GetU30(tag); //interface count
911         int s;
912         for(s=0;s<num_interfaces;s++) {
913             swf_GetU30(tag);
914         }
915         int iinit = swf_GetU30(tag);
916         cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
917         cls->traits = traits_parse(tag, pool, file);
918     }
919     for(t=0;t<num_classes;t++) {
920         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
921         /* SKIP */
922         swf_GetU30(tag); // cindex
923         cls->static_traits = traits_parse(tag, pool, file);
924     }
925     int num_scripts2 = swf_GetU30(tag);
926     for(t=0;t<num_scripts2;t++) {
927         int init = swf_GetU30(tag);
928         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
929         
930         abc_script_t*s = malloc(sizeof(abc_script_t));
931         memset(s, 0, sizeof(abc_script_t));
932         s->method = m;
933         s->traits = traits_parse(tag, pool, file);
934         array_append(file->scripts, NO_KEY, s);
935     }
936
937     pool_destroy(pool);
938     return file;
939 }
940
941 void swf_WriteABC(TAG*abctag, void*code)
942 {
943     abc_file_t*file = (abc_file_t*)code;
944     pool_t*pool = pool_new();
945
946     TAG*tmp = swf_InsertTag(0,0);
947     TAG*tag = tmp;
948     int t;
949   
950     /* add method bodies where needed */
951     for(t=0;t<file->classes->num;t++) {
952         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
953         if(!c->constructor) {
954             if(!(c->flags&CLASS_INTERFACE)) {
955                 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
956                 NEW(abc_method_body_t,body);array_append(file->method_bodies, NO_KEY, body);
957                 // don't bother to set m->index
958                 body->method = m; m->body = body;
959                 __ returnvoid(body);
960                 c->constructor = m;
961             } else {
962                 NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
963                 c->constructor = m;
964             }
965         }
966         if(!c->static_constructor) {
967             NEW(abc_method_t,m);array_append(file->methods, NO_KEY, m);
968             NEW(abc_method_body_t,body);array_append(file->method_bodies, NO_KEY, body);
969             body->method = m; m->body = body;
970             __ returnvoid(body);
971             c->static_constructor = m;
972         }
973     }
974
975
976     swf_SetU30(tag, file->methods->num);
977     /* enumerate classes, methods and method bodies */
978     for(t=0;t<file->methods->num;t++) {
979         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
980         m->index = t;
981     }
982     for(t=0;t<file->classes->num;t++) {
983         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
984         c->index = t;
985     }
986     for(t=0;t<file->method_bodies->num;t++) {
987         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
988         m->index = t;
989     }
990     
991     /* generate code statistics */
992     for(t=0;t<file->method_bodies->num;t++) {
993         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
994         m->stats = code_get_statistics(m->code, m->exceptions);
995     }
996     
997     /* level init scope depths: The init scope depth of a method is
998        always as least as high as the init scope depth of it's surrounding
999        class.
1000        A method has it's own init_scope_depth if it's an init method 
1001        (then its init scope depth is zero), or if it's used as a closure.
1002
1003        Not sure yet what to do with methods which are used at different
1004        locations- e.g. the nullmethod is used all over the place.
1005        EDIT: flashplayer doesn't allow this anyway- a method can only
1006              be used once
1007
1008        Also, I have the strong suspicion that flash player uses only
1009        the difference between max_scope_stack and init_scope_stack, anyway.
1010      */
1011     for(t=0;t<file->classes->num;t++) {
1012         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1013         trait_list_t*traits = c->traits;
1014         if(c->constructor && c->constructor->body &&
1015            c->constructor->body->init_scope_depth < c->init_scope_depth) {
1016            c->constructor->body->init_scope_depth = c->init_scope_depth;
1017         }
1018         if(c->static_constructor && c->static_constructor->body &&
1019            c->static_constructor->body->init_scope_depth < c->init_scope_depth) {
1020            c->static_constructor->body->init_scope_depth = c->init_scope_depth;
1021         }
1022         while(traits) {
1023             trait_t*trait = traits->trait;
1024             if(trait_is_method(trait) && trait->method->body) {
1025                 abc_method_body_t*body = trait->method->body;
1026                 if(body->init_scope_depth < c->init_scope_depth) {
1027                    body->init_scope_depth = c->init_scope_depth;
1028                 }
1029             }
1030             traits = traits->next;
1031         }
1032     }
1033     
1034     for(t=0;t<file->methods->num;t++) {
1035         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1036         int n = 0;
1037         multiname_list_t*l = m->parameters;
1038         int num_params = list_length(m->parameters);
1039         swf_SetU30(tag, num_params);
1040         if(m->return_type) 
1041             swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
1042         else
1043             swf_SetU30(tag, 0);
1044         int s;
1045         while(l) {
1046             swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
1047             l = l->next;
1048         }
1049         if(m->name) {
1050             swf_SetU30(tag, pool_register_string(pool, m->name));
1051         } else {
1052             swf_SetU30(tag, 0);
1053         }
1054
1055         U8 flags = m->flags&(METHOD_NEED_REST|METHOD_NEED_ARGUMENTS);
1056         if(m->optional_parameters)
1057             flags |= METHOD_HAS_OPTIONAL;
1058         if(m->body) {
1059             flags |= m->body->stats->flags;
1060         }
1061
1062         swf_SetU8(tag, flags);
1063         if(flags&METHOD_HAS_OPTIONAL) {
1064             swf_SetU30(tag, list_length(m->optional_parameters));
1065             constant_list_t*l = m->optional_parameters;
1066             while(l) {
1067                 swf_SetU30(tag, constant_get_index(pool, l->constant));
1068                 swf_SetU8(tag, l->constant->type);
1069                 l = l->next;
1070             }
1071         }
1072     }
1073    
1074     /* write metadata */
1075     swf_SetU30(tag, file->metadata->num);
1076     for(t=0;t<file->metadata->num;t++) {
1077         const char*entry_name = array_getkey(file->metadata, t);
1078         swf_SetU30(tag, pool_register_string(pool, entry_name));
1079         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1080         swf_SetU30(tag, items->num);
1081         int s;
1082         for(s=0;s<items->num;s++) {
1083             int i1 = pool_register_string(pool, array_getkey(items, s));
1084             int i2 = pool_register_string(pool, array_getvalue(items, s));
1085             swf_SetU30(tag, i1);
1086             swf_SetU30(tag, i2);
1087         }
1088     }
1089
1090     swf_SetU30(tag, file->classes->num);
1091     for(t=0;t<file->classes->num;t++) {
1092         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1093    
1094         int classname_index = pool_register_multiname(pool, c->classname);
1095         int superclass_index = pool_register_multiname(pool, c->superclass);
1096
1097         swf_SetU30(tag, classname_index);
1098         swf_SetU30(tag, superclass_index);
1099
1100         swf_SetU8(tag, c->flags); // flags
1101         if(c->flags&0x08) {
1102             int ns_index = pool_register_namespace(pool, c->protectedNS);
1103             swf_SetU30(tag, ns_index);
1104         }
1105
1106         swf_SetU30(tag, list_length(c->interfaces));
1107         multiname_list_t*interface= c->interfaces;
1108         while(interface) {
1109             swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
1110             interface = interface->next;
1111         }
1112
1113         assert(c->constructor);
1114         swf_SetU30(tag, c->constructor->index);
1115
1116         traits_write(pool, tag, c->traits);
1117     }
1118     for(t=0;t<file->classes->num;t++) {
1119         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
1120         assert(c->static_constructor);
1121         swf_SetU30(tag, c->static_constructor->index);
1122         
1123         traits_write(pool, tag, c->static_traits);
1124     }
1125
1126     swf_SetU30(tag, file->scripts->num);
1127     for(t=0;t<file->scripts->num;t++) {
1128         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1129         swf_SetU30(tag, s->method->index); //!=t!
1130         traits_write(pool, tag, s->traits);
1131     }
1132
1133     swf_SetU30(tag, file->method_bodies->num);
1134     for(t=0;t<file->method_bodies->num;t++) {
1135         abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1136         abc_method_t*m = c->method;
1137         swf_SetU30(tag, m->index);
1138
1139         //swf_SetU30(tag, c->old.max_stack);
1140         //swf_SetU30(tag, c->old.local_count);
1141         //swf_SetU30(tag, c->old.init_scope_depth);
1142         //swf_SetU30(tag, c->old.max_scope_depth);
1143
1144         swf_SetU30(tag, c->stats->max_stack);
1145
1146         int param_num = list_length(c->method->parameters)+1;
1147         if(c->method->flags&METHOD_NEED_REST)
1148             param_num++;
1149         if(param_num <= c->stats->local_count)
1150             swf_SetU30(tag, c->stats->local_count);
1151         else
1152             swf_SetU30(tag, param_num);
1153
1154         swf_SetU30(tag, c->init_scope_depth);
1155         swf_SetU30(tag, c->stats->max_scope_depth+
1156                         c->init_scope_depth);
1157
1158         code_write(tag, c->code, pool, file);
1159
1160         swf_SetU30(tag, list_length(c->exceptions));
1161         abc_exception_list_t*l = c->exceptions;
1162         while(l) {
1163             // warning: assumes "pos" in each code_t is up-to-date
1164             swf_SetU30(tag, l->abc_exception->from->pos);
1165             swf_SetU30(tag, l->abc_exception->to->pos);
1166             swf_SetU30(tag, l->abc_exception->target->pos);
1167             swf_SetU30(tag, pool_register_multiname(pool, l->abc_exception->exc_type));
1168             swf_SetU30(tag, pool_register_multiname(pool, l->abc_exception->var_name));
1169             l = l->next;
1170         }
1171
1172         traits_write(pool, tag, c->traits);
1173     }
1174    
1175     /* free temporary codestat data again. Notice: If we were to write this
1176        file multiple times, this can also be shifted to abc_file_free() */
1177     for(t=0;t<file->method_bodies->num;t++) {
1178         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1179         codestats_free(m->stats);m->stats=0;
1180     }
1181
1182     // --- start to write real tag --
1183     
1184     tag = abctag;
1185
1186     if(tag->id == ST_DOABC) {
1187         swf_SetU32(tag, file->flags); // flags
1188         swf_SetString(tag, file->name);
1189     }
1190
1191     swf_SetU16(tag, 0x10); //version
1192     swf_SetU16(tag, 0x2e);
1193     
1194     pool_write(pool, tag);
1195     
1196     swf_SetBlock(tag, tmp->data, tmp->len);
1197
1198     swf_DeleteTag(0, tmp);
1199     pool_destroy(pool);
1200 }
1201
1202 void abc_file_free(abc_file_t*file)
1203 {
1204     int t;
1205     if(file->metadata) {
1206         for(t=0;t<file->metadata->num;t++) {
1207             array_t*items = (array_t*)array_getvalue(file->metadata, t);
1208             int s;
1209             for(s=0;s<items->num;s++) {
1210                 free(array_getvalue(items, s));
1211             }
1212             array_free(items);
1213         }
1214         array_free(file->metadata);file->metadata=0;
1215     }
1216
1217     for(t=0;t<file->methods->num;t++) {
1218         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1219
1220         multiname_list_t*param = m->parameters;
1221         while(param) {
1222             multiname_destroy(param->multiname);param->multiname=0;
1223             param = param->next;
1224         }
1225         list_free(m->parameters);m->parameters=0;
1226        
1227         constant_list_t*opt = m->optional_parameters;
1228         while(opt) {
1229             constant_free(opt->constant);opt->constant=0;
1230             opt = opt->next;
1231         }
1232         list_free(m->optional_parameters);m->optional_parameters=0;
1233
1234         if(m->name) {
1235             free((void*)m->name);m->name=0;
1236         }
1237         if(m->return_type) {
1238             multiname_destroy(m->return_type);
1239         }
1240         free(m);
1241     }
1242     array_free(file->methods);file->methods=0;
1243
1244     for(t=0;t<file->classes->num;t++) {
1245         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1246         traits_free(cls->traits);cls->traits=0;
1247         traits_free(cls->static_traits);cls->static_traits=0;
1248
1249         if(cls->classname) {
1250             multiname_destroy(cls->classname);
1251         }
1252         if(cls->superclass) {
1253             multiname_destroy(cls->superclass);
1254         }
1255
1256         multiname_list_t*i = cls->interfaces;
1257         while(i) {
1258             multiname_destroy(i->multiname);i->multiname=0;
1259             i = i->next;
1260         }
1261         list_free(cls->interfaces);cls->interfaces=0;
1262
1263         if(cls->protectedNS) {
1264             namespace_destroy(cls->protectedNS);
1265         }
1266         free(cls);
1267     }
1268     array_free(file->classes);file->classes=0;
1269
1270     for(t=0;t<file->scripts->num;t++) {
1271         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1272         traits_free(s->traits);s->traits=0;
1273         free(s);
1274     }
1275     array_free(file->scripts);file->scripts=0;
1276
1277     for(t=0;t<file->method_bodies->num;t++) {
1278         abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1279         code_free(body->code);body->code=0;
1280         traits_free(body->traits);body->traits=0;
1281
1282         abc_exception_list_t*ee = body->exceptions;
1283         while(ee) {
1284             abc_exception_t*e=ee->abc_exception;ee->abc_exception=0;
1285             e->from = e->to = e->target = 0;
1286             multiname_destroy(e->exc_type);e->exc_type=0;
1287             multiname_destroy(e->var_name);e->var_name=0;
1288             free(e);
1289             ee=ee->next;
1290         }
1291         list_free(body->exceptions);body->exceptions=0;
1292         
1293         free(body);
1294     }
1295     array_free(file->method_bodies);file->method_bodies=0;
1296
1297     if(file->name) {
1298         free((void*)file->name);file->name=0;
1299     }
1300
1301     free(file);
1302 }
1303
1304 void swf_FreeABC(void*code)
1305 {
1306     abc_file_t*file= (abc_file_t*)code;
1307     abc_file_free(file);
1308 }
1309
1310 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1311 {
1312     int num_frames = 0;
1313     int has_buttons = 0;
1314     TAG*tag=swf->firstTag;
1315     while(tag) {
1316         if(tag->id == ST_SHOWFRAME)
1317             num_frames++;
1318         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1319             has_buttons = 1;
1320         tag = tag->next;
1321     }
1322
1323     abc_file_t*file = abc_file_new();
1324     abc_method_body_t*c = 0;
1325    
1326     abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1327     abc_class_protectedNS(cls, "rfx:MainTimeline");
1328   
1329     TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1330     
1331     tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1332     swf_SetU16(tag, 1);
1333     swf_SetU16(tag, 0);
1334     swf_SetString(tag, "rfx.MainTimeline");
1335
1336     c = abc_class_getstaticconstructor(cls, 0)->body;
1337     c->old.max_stack = 1;
1338     c->old.local_count = 1;
1339     c->old.init_scope_depth = 9;
1340     c->old.max_scope_depth = 10;
1341
1342     __ getlocal_0(c);
1343     __ pushscope(c);
1344     __ returnvoid(c);
1345
1346     c = abc_class_getconstructor(cls, 0)->body;
1347     c->old.max_stack = 3;
1348     c->old.local_count = 1;
1349     c->old.init_scope_depth = 10;
1350     c->old.max_scope_depth = 11;
1351     
1352     debugfile(c, "constructor.as");
1353
1354     __ getlocal_0(c);
1355     __ pushscope(c);
1356
1357     __ getlocal_0(c);
1358     __ constructsuper(c,0);
1359
1360     __ getlex(c, "[package]flash.system::Security");
1361     __ pushstring(c, "*");
1362     __ callpropvoid(c, "[package]::allowDomain", 1);
1363     
1364     if(stop_each_frame || has_buttons) {
1365         int frame = 0;
1366         tag = swf->firstTag;
1367         abc_method_body_t*f = 0; //frame script
1368         while(tag && tag->id!=ST_END) {
1369             char framename[80];
1370             char needs_framescript=0;
1371             char buttonname[80];
1372             char functionname[80];
1373             sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1374             
1375             if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1376                 /* make the contructor add a frame script */
1377                 __ findpropstrict(c,"[package]::addFrameScript");
1378                 __ pushbyte(c,frame);
1379                 __ getlex(c,framename);
1380                 __ callpropvoid(c,"[package]::addFrameScript",2);
1381
1382                 f = abc_class_method(cls, 0, multiname_fromstring(framename))->body;
1383                 f->old.max_stack = 3;
1384                 f->old.local_count = 1;
1385                 f->old.init_scope_depth = 10;
1386                 f->old.max_scope_depth = 11;
1387                 __ debugfile(f, "framescript.as");
1388                 __ debugline(f, 1);
1389                 __ getlocal_0(f);
1390                 __ pushscope(f);
1391                 if(stop_each_frame) {
1392                     __ findpropstrict(f, "[package]::stop");
1393                     __ callpropvoid(f, "[package]::stop", 0);
1394                 }
1395             }
1396
1397             if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1398                 U16 id = swf_GetDefineID(tag);
1399                 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1400                 __ getlex(f,buttonname);
1401                 __ getlex(f,"flash.events::MouseEvent");
1402                 __ getproperty(f, "::CLICK");
1403                 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1404                 __ getlex(f,functionname);
1405                 __ callpropvoid(f, "::addEventListener" ,2);
1406
1407                 needs_framescript = 1;
1408
1409                 abc_method_body_t*h =
1410                     abc_class_method(cls, 0, multiname_fromstring(functionname))->body;
1411                 list_append(h->method->parameters, multiname_fromstring("flash.events::MouseEvent"));
1412
1413                 h->old.max_stack = 6;
1414                 h->old.local_count = 2;
1415                 h->old.init_scope_depth = 10;
1416                 h->old.max_scope_depth = 11;
1417                 __ getlocal_0(h);
1418                 __ pushscope(h);
1419
1420                 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1421                 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1422                     int framenr = GET16(oldaction->data);
1423                     if(framenr>254) {
1424                         fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1425                     }
1426                     if(!events) {
1427                         __ findpropstrict(h,"[package]::gotoAndStop");
1428                         __ pushbyte(h,framenr+1);
1429                         __ callpropvoid(h,"[package]::gotoAndStop", 1);
1430                     } else {
1431                         char framename[80];
1432                         sprintf(framename, "frame%d", framenr);
1433                         __ getlocal_0(h); //this
1434                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1435                         __ pushstring(h, "link");
1436                         __ pushtrue(h);
1437                         __ pushtrue(h);
1438                         __ pushstring(h, framename);
1439                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1440                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1441                     }
1442                 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1443                     if(!events) {
1444                         __ findpropstrict(h,"flash.net::navigateToURL");
1445                         __ findpropstrict(h,"flash.net::URLRequest");
1446                         // TODO: target _blank
1447                         __ pushstring(h,oldaction->data); //url
1448                         __ constructprop(h,"flash.net::URLRequest", 1);
1449                         __ callpropvoid(h,"flash.net::navigateToURL", 1);
1450                     } else {
1451                         __ getlocal_0(h); //this
1452                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1453                         __ pushstring(h, "link");
1454                         __ pushtrue(h);
1455                         __ pushtrue(h);
1456                         __ pushstring(h,oldaction->data); //url
1457                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1458                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1459                     }
1460                 } else if(oldaction) {
1461                     fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1462                 }
1463                 __ returnvoid(h);
1464                 swf_ActionFree(oldaction);
1465             }
1466             if(tag->id == ST_SHOWFRAME) {
1467                 if(f) {
1468                     __ returnvoid(f);
1469                     f = 0;
1470                 }
1471                 frame++;
1472             }
1473             tag = tag->next;
1474         }
1475         if(f) {
1476             __ returnvoid(f);
1477         }
1478     }
1479     __ returnvoid(c);
1480
1481     tag = swf->firstTag;
1482     while(tag) {
1483         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1484             char buttonname[80];
1485             sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1486             multiname_t*s = multiname_fromstring(buttonname);
1487             abc_class_slot(cls, multiname_fromstring(buttonname), s);
1488         }
1489         tag = tag->next;
1490     }
1491
1492
1493     abc_script_t*s = abc_initscript(file, 0);
1494     c = s->method->body;
1495     c->old.max_stack = 2;
1496     c->old.local_count = 1;
1497     c->old.init_scope_depth = 1;
1498     c->old.max_scope_depth = 9;
1499
1500     __ getlocal_0(c);
1501     __ pushscope(c);
1502     __ getscopeobject(c, 0);
1503     __ getlex(c,"::Object");
1504     __ pushscope(c);
1505     __ getlex(c,"flash.events::EventDispatcher");
1506     __ pushscope(c);
1507     __ getlex(c,"flash.display::DisplayObject");
1508     __ pushscope(c);
1509     __ getlex(c,"flash.display::InteractiveObject");
1510     __ pushscope(c);
1511     __ getlex(c,"flash.display::DisplayObjectContainer");
1512     __ pushscope(c);
1513     __ getlex(c,"flash.display::Sprite");
1514     __ pushscope(c);
1515     __ getlex(c,"flash.display::MovieClip");
1516     __ pushscope(c);
1517     __ getlex(c,"flash.display::MovieClip");
1518     __ newclass(c,cls);
1519     __ popscope(c);
1520     __ popscope(c);
1521     __ popscope(c);
1522     __ popscope(c);
1523     __ popscope(c);
1524     __ popscope(c);
1525     __ popscope(c);
1526     __ initproperty(c,"rfx::MainTimeline");
1527     __ returnvoid(c);
1528
1529     //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1530     multiname_t*classname = multiname_fromstring("rfx::MainTimeline");
1531     abc_initscript_addClassTrait(s, classname, cls);
1532     multiname_destroy(classname);
1533
1534     swf_WriteABC(abctag, file);
1535 }
1536