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