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