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