mem leak fixes
[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_to_string(multiname_list_t*list)
44 {
45     multiname_list_t*l;
46     int n;
47
48     l = list;
49     n = 0;
50     while(list) {
51         n++;list=list->next;
52     }
53
54     char**names = (char**)malloc(sizeof(char*)*n);
55     
56     l = list;
57     n = 0;
58     int size = 0;
59     while(list) {
60         names[n] = multiname_to_string(list->multiname);
61         size += strlen(names[n]) + 2;
62         n++;list=list->next;
63     }
64
65     char* params = malloc(size+5);
66     params[0]='(';
67     params[1]=0;
68     l = list;
69     int s=0;
70     n = 0;
71     while(list) {
72         if(s)
73             strcat(params, ", ");
74         strcat(params, names[n]);
75         free(names[n]);
76         n++;
77         s=1;
78     }
79     free(names);
80     strcat(params, ")");
81     int t;
82     return params;
83 }
84
85 //#define DEBUG
86 #define DEBUG if(0)
87
88 static void parse_metadata(TAG*tag, abc_file_t*file, pool_t*pool)
89 {
90     int t;
91     int num_metadata = swf_GetU30(tag);
92
93     DEBUG printf("%d metadata\n");
94     for(t=0;t<num_metadata;t++) {
95         const char*entry_name = pool_lookup_string(pool, swf_GetU30(tag));
96         int num = swf_GetU30(tag);
97         int s;
98         DEBUG printf("  %s\n", entry_name);
99         array_t*items = array_new();
100         for(s=0;s<num;s++) {
101             int i1 = swf_GetU30(tag);
102             int i2 = swf_GetU30(tag);
103             char*key = i1?pool_lookup_string(pool, i1):"";
104             char*value = i2?pool_lookup_string(pool, i2):"";
105             DEBUG printf("    %s=%s\n", key, value);
106             array_append(items, key, strdup(value));
107         }
108         array_append(file->metadata, entry_name, items);
109     }
110 }
111
112 void swf_CopyData(TAG*to, TAG*from, int len)
113 {
114     unsigned char*data = malloc(len);
115     swf_GetBlock(from, data, len);
116     swf_SetBlock(to, data, len);
117     free(data);
118 }
119
120 abc_file_t*abc_file_new()
121 {
122     abc_file_t*f = malloc(sizeof(abc_file_t));
123     memset(f, 0, sizeof(abc_file_t));
124     f->metadata = array_new();
125
126     f->methods = array_new();
127     f->classes = array_new();
128     f->scripts = array_new();
129     f->method_bodies = array_new();
130
131     return f;
132 }
133
134 #define CLASS_SEALED 1
135 #define CLASS_FINAL 2
136 #define CLASS_INTERFACE 4
137 #define CLASS_PROTECTED_NS 8
138
139 abc_class_t* abc_class_new(abc_file_t*pool, multiname_t*classname, multiname_t*superclass) {
140     
141     NEW(abc_class_t,c);
142     array_append(pool->classes, NO_KEY, c);
143
144     c->pool = pool;
145     c->classname = classname;
146     c->superclass = superclass;
147     c->flags = 0;
148     c->constructor = 0;
149     c->static_constructor = 0;
150     c->traits = list_new();
151     return c;
152 }
153 abc_class_t* abc_class_new2(abc_file_t*pool, char*classname, char*superclass) 
154 {
155     return abc_class_new(pool, multiname_fromstring(classname), multiname_fromstring(superclass));
156 }
157
158 void abc_class_sealed(abc_class_t*c)
159 {
160     c->flags |= CLASS_SEALED;
161 }
162 void abc_class_final(abc_class_t*c)
163 {
164     c->flags |= CLASS_FINAL;
165 }
166 void abc_class_interface(abc_class_t*c)
167 {
168     c->flags |= CLASS_INTERFACE;
169 }
170 void abc_class_protectedNS(abc_class_t*c, char*namespace)
171 {
172     c->protectedNS = namespace_new_protected(namespace);
173     c->flags |= CLASS_PROTECTED_NS;
174 }
175 void abc_class_add_interface(abc_class_t*c, multiname_t*interface)
176 {
177     list_append(c->interfaces, interface);
178 }
179
180 abc_method_body_t* add_method(abc_file_t*pool, abc_class_t*cls, char*returntype, int num_params, va_list va)
181 {
182     /* construct code (method body) object */
183     NEW(abc_method_body_t,c);
184     array_append(pool->method_bodies, NO_KEY, c);
185     c->pool = pool;
186     c->traits = list_new();
187     c->code = 0;
188
189     /* construct method object */
190     NEW(abc_method_t,m);
191     array_append(pool->methods, NO_KEY, m);
192
193     if(returntype && strcmp(returntype, "void")) {
194         m->return_type = multiname_fromstring(returntype);
195     } else {
196         m->return_type = 0;
197     }
198     int t;
199     for(t=0;t<num_params;t++) {
200         const char*param = va_arg(va, const char*);
201         list_append(m->parameters, multiname_fromstring(param));
202     }
203
204     /* crosslink the two objects */
205     m->body = c;
206     c->method = m;
207
208     return c;
209 }
210
211 abc_method_body_t* abc_class_constructor(abc_class_t*cls, char*returntype, int num_params, ...) 
212 {
213     va_list va;
214     va_start(va, num_params);
215     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
216     va_end(va);
217     cls->constructor = c->method;
218     return c;
219 }
220
221 abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, char*returntype, int num_params, ...) 
222 {
223     va_list va;
224     va_start(va, num_params);
225     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
226     va_end(va);
227     cls->static_constructor = c->method;
228     return c;
229 }
230
231 trait_t*trait_new(int type, multiname_t*name, int data1, int data2, int vindex, int vkind)
232 {
233     trait_t*trait = malloc(sizeof(trait_t));
234     memset(trait, 0, sizeof(trait_t));
235     trait->kind = type&0x0f;
236     trait->attributes = type&0xf0;
237     trait->name = name;
238     trait->data1 = data1;
239     trait->data2 = data2;
240     trait->vindex = vindex;
241     trait->vkind = vkind;
242     return trait;
243 }
244 trait_t*trait_new_method(multiname_t*name, abc_method_t*m)
245 {
246     int type = TRAIT_METHOD;
247     trait_t*trait = malloc(sizeof(trait_t));
248     memset(trait, 0, sizeof(trait_t));
249     trait->kind = type&0x0f;
250     trait->attributes = type&0xf0;
251     trait->name = name;
252     trait->method = m;
253     return trait;
254 }
255
256 abc_method_body_t* abc_class_method(abc_class_t*cls, char*returntype, char*name, int num_params, ...)
257 {
258     abc_file_t*pool = cls->pool;
259     va_list va;
260     va_start(va, num_params);
261     abc_method_body_t* c = add_method(cls->pool, cls, returntype, num_params, va);
262     va_end(va);
263     list_append(cls->traits, trait_new_method(multiname_fromstring(name), c->method));
264     return c;
265 }
266
267 void abc_AddSlot(abc_class_t*cls, char*name, int slot, char*multiname)
268 {
269     abc_file_t*pool = cls->pool;
270     multiname_t*m = multiname_fromstring(multiname);
271     list_append(cls->traits, trait_new(TRAIT_SLOT, m, slot, 0, 0, 0));
272 }
273
274 void abc_method_body_addClassTrait(abc_method_body_t*code, char*multiname, int slotid, abc_class_t*cls)
275 {
276     abc_file_t*pool = code->pool;
277     multiname_t*m = multiname_fromstring(multiname);
278     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
279     trait->cls = cls;
280     list_append(code->traits, trait);
281 }
282
283 /* notice: traits of a method (body) belonging to an init script
284    and traits of the init script are *not* the same thing */
285 void abc_initscript_addClassTrait(abc_script_t*script, char*multiname, int slotid, abc_class_t*cls)
286 {
287     abc_file_t*pool = script->pool;
288     multiname_t*m = multiname_fromstring(multiname);
289     trait_t*trait = trait_new(TRAIT_CLASS, m, slotid, 0, 0, 0);
290     trait->cls = cls;
291     list_append(script->traits, trait);
292 }
293
294 abc_script_t* abc_initscript(abc_file_t*pool, char*returntype, int num_params, ...) 
295 {
296     va_list va;
297     va_start(va, num_params);
298     abc_method_body_t* c = add_method(pool, 0, returntype, num_params, va);
299     abc_script_t* s = malloc(sizeof(abc_script_t));
300     s->method = c->method;
301     s->traits = list_new();
302     s->pool = pool;
303     array_append(pool->scripts, NO_KEY, s);
304     va_end(va);
305     return s;
306 }
307
308 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file);
309
310 static void dump_method(FILE*fo, const char*prefix, const char*type, const char*name, abc_method_t*m, abc_file_t*file)
311 {
312     const char*return_type = "void";
313     if(m->return_type)
314         return_type = multiname_to_string(m->return_type);
315
316     char*paramstr = params_to_string(m->parameters);
317
318     fprintf(fo, "%s%s %s %s=%s %s\n", prefix, type, return_type, name, m->name, paramstr);
319
320     abc_method_body_t*c = m->body;
321     if(!c) {
322         return;
323     }
324     
325     fprintf(fo, "%s[%d %d %d %d %d]\n", prefix, c->max_stack, c->local_count, c->init_scope_depth, c->max_scope_depth, list_length(c->exceptions));
326
327     char prefix2[80];
328     sprintf(prefix2, "%s    ", prefix);
329     if(c->traits)
330         dump_traits(fo, prefix, c->traits, file);
331     fprintf(fo, "%s{\n", prefix);
332     code_dump(c->code, c->exceptions, file, prefix2, fo);
333     fprintf(fo, "%s}\n\n", prefix);
334 }
335
336 static void traits_free(trait_list_t*traits) 
337 {
338     trait_list_t*t = traits;
339     while(t) {
340         if(t->trait->name) {
341             multiname_destroy(t->trait->name);t->trait->name = 0;
342         }
343         if(t->trait->kind == TRAIT_SLOT || t->trait->kind == TRAIT_CONST) {
344             multiname_destroy(t->trait->type_name);
345         }
346         free(t->trait);t->trait = 0;
347         t = t->next;
348     }
349     list_free(traits);
350 }
351
352 static trait_list_t* traits_parse(TAG*tag, pool_t*pool, abc_file_t*file)
353 {
354     int num_traits = swf_GetU30(tag);
355     trait_list_t*traits = list_new();
356     int t;
357     if(num_traits) {
358         DEBUG printf("%d traits\n", num_traits);
359     }
360     
361     for(t=0;t<num_traits;t++) {
362         trait_t*trait = malloc(sizeof(trait_t));
363         memset(trait, 0, sizeof(trait_t));
364         list_append(traits, trait);
365
366         trait->name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag))); // always a QName (ns,name)
367
368         const char*name = 0;
369         DEBUG name = multiname_to_string(trait->name);
370         U8 kind = swf_GetU8(tag);
371         U8 attributes = kind&0xf0;
372         kind&=0x0f;
373         trait->kind = kind;
374         trait->attributes = attributes;
375         DEBUG printf("  trait %d) %s type=%02x\n", t, name, kind);
376         if(kind == TRAIT_METHOD || kind == TRAIT_GETTER || kind == TRAIT_SETTER) { // method / getter / setter
377             trait->disp_id = swf_GetU30(tag);
378             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
379             DEBUG printf("  method/getter/setter\n");
380         } else if(kind == TRAIT_FUNCTION) { // function
381             trait->slot_id =  swf_GetU30(tag);
382             trait->method = (abc_method_t*)array_getvalue(file->methods, swf_GetU30(tag));
383         } else if(kind == TRAIT_CLASS) { // class
384             trait->slot_id = swf_GetU30(tag);
385             trait->cls = (abc_class_t*)array_getvalue(file->classes, swf_GetU30(tag));
386             DEBUG printf("  class %s %d %d\n", name, trait->slot_id, trait->cls);
387         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
388             /* a slot is a variable in a class that is shared amonst all instances
389                of the same type, but which has a unique location in each object 
390                (in other words, slots are non-static, traits are static)
391              */
392             trait->slot_id = swf_GetU30(tag);
393             trait->type_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
394             trait->vindex = swf_GetU30(tag);
395             if(trait->vindex) {
396                 trait->vkind = swf_GetU8(tag);
397             }
398             DEBUG printf("  slot %s %d %s (vindex=%d)\n", name, trait->slot_id, trait->type_name->name, trait->vindex);
399         } else {
400             fprintf(stderr, "Can't parse trait type %d\n", kind);
401         }
402         if(attributes&0x40) {
403             int num = swf_GetU30(tag);
404             int s;
405             for(s=0;s<num;s++) {
406                 swf_GetU30(tag); //index into metadata array
407             }
408         }
409     }
410     return traits;
411 }
412
413 void traits_skip(TAG*tag)
414 {
415     int num_traits = swf_GetU30(tag);
416     int t;
417     for(t=0;t<num_traits;t++) {
418         swf_GetU30(tag);
419         U8 kind = swf_GetU8(tag);
420         U8 attributes = kind&0xf0;
421         kind&=0x0f;
422         swf_GetU30(tag);
423         swf_GetU30(tag);
424         if(kind == TRAIT_SLOT || kind == TRAIT_CONST) {
425             if(swf_GetU30(tag)) swf_GetU8(tag);
426         } else if(kind>TRAIT_CONST) {
427             fprintf(stderr, "Can't parse trait type %d\n", kind);
428         }
429         if(attributes&0x40) {
430             int s, num = swf_GetU30(tag);
431             for(s=0;s<num;s++) swf_GetU30(tag);
432         }
433     }
434 }
435
436
437 static void traits_write(pool_t*pool, TAG*tag, trait_list_t*traits)
438 {
439     if(!traits) {
440         swf_SetU30(tag, 0);
441         return;
442     }
443     swf_SetU30(tag, list_length(traits));
444     int s;
445
446     while(traits) {
447         trait_t*trait = traits->trait;
448
449         swf_SetU30(tag, pool_register_multiname(pool, trait->name));
450         swf_SetU8(tag, trait->kind|trait->attributes);
451
452         swf_SetU30(tag, trait->data1);
453
454         if(trait->kind == TRAIT_CLASS) {
455             swf_SetU30(tag, trait->cls->index);
456         } else if(trait->kind == TRAIT_GETTER ||
457                   trait->kind == TRAIT_SETTER ||
458                   trait->kind == TRAIT_METHOD) {
459             swf_SetU30(tag, trait->method->index);
460         } else if(trait->kind == TRAIT_SLOT ||
461                   trait->kind == TRAIT_CONST) {
462             int index = pool_register_multiname(pool, trait->type_name);
463             swf_SetU30(tag, index);
464         } else  {
465             swf_SetU30(tag, trait->data2);
466         }
467
468         if(trait->kind == TRAIT_SLOT || trait->kind == TRAIT_CONST) {
469             swf_SetU30(tag, trait->vindex);
470             if(trait->vindex) {
471                 swf_SetU8(tag, trait->vkind);
472             }
473         }
474         if(trait->attributes&0x40) {
475             // metadata
476             swf_SetU30(tag, 0);
477         }
478         traits = traits->next;
479     }
480 }
481
482
483 static void dump_traits(FILE*fo, const char*prefix, trait_list_t*traits, abc_file_t*file)
484 {
485     int t;
486     while(traits) {
487         trait_t*trait = traits->trait;
488         char*name = multiname_to_string(trait->name);
489         U8 kind = trait->kind;
490         U8 attributes = trait->attributes;
491         if(kind == TRAIT_METHOD) {
492             abc_method_t*m = trait->method;
493             dump_method(fo, prefix, "method", name, m, file);
494         } else if(kind == TRAIT_GETTER) {
495             abc_method_t*m = trait->method;
496             dump_method(fo, prefix, "getter", name, m, file);
497         } else if(kind == TRAIT_SETTER) {
498             abc_method_t*m = trait->method;
499             dump_method(fo, prefix, "setter", name, m, file);
500         } else if(kind == TRAIT_FUNCTION) { // function
501             abc_method_t*m = trait->method;
502             dump_method(fo, prefix, "function", name, m, file);
503         } else if(kind == TRAIT_CLASS) { // class
504             abc_class_t*cls = trait->cls;
505             if(!cls) {
506                 fprintf(fo, "%sslot %d: class %s=class%d\n", prefix, trait->slot_id, name);
507             } else {
508                 fprintf(fo, "%sslot %d: class %s=%s\n", prefix, trait->slot_id, name, cls->classname->name);
509             }
510         } else if(kind == TRAIT_SLOT || kind == TRAIT_CONST) { // slot, const
511             int slot_id = trait->slot_id;
512             char*type_name = multiname_to_string(trait->type_name);
513             fprintf(fo, "%sslot %s %d %s (vindex=%d)\n", prefix, name, trait->slot_id, type_name, trait->vindex);
514             free(type_name);
515         } else {
516             fprintf(fo, "%s    can't dump trait type %d\n", prefix, kind);
517         }
518         free(name);
519         traits=traits->next;
520     }
521 }
522
523 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
524 {
525     abc_file_t* file = (abc_file_t*)code;
526         
527     if(file->name) {
528         fprintf(fo, "%s#\n", prefix);
529         fprintf(fo, "%s#name: %s\n", prefix, file->name);
530         fprintf(fo, "%s#\n", prefix);
531     }
532
533     int t;
534     for(t=0;t<file->metadata->num;t++) {
535         const char*entry_name = array_getkey(file->metadata, t);
536         fprintf(fo, "%s#Metadata \"%s\":\n", prefix, entry_name);
537         int s;
538         array_t*items = (array_t*)array_getvalue(file->metadata, t);
539         for(s=0;s<items->num;s++) {
540             fprintf(fo, "%s#  %s=%s\n", prefix, array_getkey(items, s), array_getvalue(items,s));
541         }
542         fprintf(fo, "%s#\n", prefix);
543     }
544
545     for(t=0;t<file->classes->num;t++) {
546         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
547         char prefix2[80];
548         sprintf(prefix2, "%s    ", prefix);
549
550         fprintf(fo, "%s", prefix);
551         if(cls->flags&1) fprintf(fo, "sealed ");
552         if(cls->flags&2) fprintf(fo, "final ");
553         if(cls->flags&4) fprintf(fo, "interface ");
554         if(cls->flags&8) {
555             char*s = namespace_to_string(cls->protectedNS);
556             fprintf(fo, "protectedNS(%s) ", s);
557             free(s);
558         }
559
560         char*classname = multiname_to_string(cls->classname);
561         fprintf(fo, "class %s", classname);
562         free(classname);
563         if(cls->superclass) {
564             char*supername = multiname_to_string(cls->superclass);
565             fprintf(fo, " extends %s", supername);
566             free(supername);
567             multiname_list_t*ilist = cls->interfaces;
568             if(ilist)
569                 fprintf(fo, " implements");
570             while(ilist) {
571                 char*s = multiname_to_string(ilist->multiname);
572                 fprintf(fo, " %s", s);
573                 free(s);
574                 ilist = ilist->next;
575             }
576             ilist->next;
577         }
578         if(cls->flags&0xf0) 
579             fprintf(fo, "extra flags=%02x\n", cls->flags&0xf0);
580         fprintf(fo, "%s{\n", prefix);
581
582         if(cls->static_constructor)
583             dump_method(fo, prefix2,"staticconstructor", "", cls->static_constructor, file);
584         dump_traits(fo, prefix2, cls->static_constructor_traits, file);
585         
586         char*n = multiname_to_string(cls->classname);
587         if(cls->constructor)
588             dump_method(fo, prefix2, "constructor", n, cls->constructor, file);
589         free(n);
590         dump_traits(fo, prefix2,cls->traits, file);
591         fprintf(fo, "%s}\n", prefix);
592     }
593     fprintf(fo, "%s\n", prefix);
594
595     for(t=0;t<file->scripts->num;t++) {
596         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
597         dump_method(fo, prefix,"initmethod", "init", s->method, file);
598         dump_traits(fo, prefix, s->traits, file);
599     }
600     return file;
601 }
602
603 void* swf_ReadABC(TAG*tag)
604 {
605     abc_file_t* file = abc_file_new();
606     pool_t*pool = pool_new();
607
608     swf_SetTagPos(tag, 0);
609     int t;
610     if(tag->id == ST_DOABC) {
611         U32 abcflags = swf_GetU32(tag);
612         DEBUG printf("flags=%08x\n", abcflags);
613         char*name= swf_GetString(tag);
614         file->name = name?strdup(name):0;
615     }
616     U32 version = swf_GetU32(tag);
617     if(version!=0x002e0010) {
618         fprintf(stderr, "Warning: unknown AVM2 version %08x\n", version);
619     }
620
621     pool_read(pool, tag);
622
623     int num_methods = swf_GetU30(tag);
624     DEBUG printf("%d methods\n", num_methods);
625     for(t=0;t<num_methods;t++) {
626         NEW(abc_method_t,m);
627         int param_count = swf_GetU30(tag);
628         int return_type_index = swf_GetU30(tag);
629         if(return_type_index)
630             m->return_type = multiname_clone(pool_lookup_multiname(pool, return_type_index));
631         else
632             m->return_type = 0;
633
634         int s;
635         for(s=0;s<param_count;s++) {
636             int type_index = swf_GetU30(tag);
637             
638             /* type_index might be 0, which probably means "..." (varargs) */
639             multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
640             list_append(m->parameters, param);
641         }
642
643         int namenr = swf_GetU30(tag);
644         if(namenr)
645             m->name = strdup(pool_lookup_string(pool, namenr));
646         else
647             m->name = strdup("");
648
649         m->flags = swf_GetU8(tag);
650         
651         DEBUG printf("method %d) %s flags=%02x\n", t, params_to_string(m->parameters), m->flags);
652
653         if(m->flags&0x08) {
654             /* optional parameters */
655             int num = swf_GetU30(tag);
656             int s;
657             for(s=0;s<num;s++) {
658                 int val = swf_GetU30(tag);
659                 U8 kind = swf_GetU8(tag); // specifies index type for "val"
660             }
661         }
662         if(m->flags&0x80) {
663             /* debug information- not used by avm2 */
664             multiname_list_t*l = m->parameters;
665             while(l) {
666                 char*name = pool_lookup_string(pool, swf_GetU30(tag));
667                 l = l->next;
668             }
669         }
670         array_append(file->methods, NO_KEY, m);
671     }
672             
673     parse_metadata(tag, file, pool);
674         
675     /* skip classes, and scripts for now, and do the real parsing later */
676     int num_classes = swf_GetU30(tag);
677     int classes_pos = tag->pos;
678     DEBUG printf("%d classes\n", num_classes);
679     for(t=0;t<num_classes;t++) {
680         abc_class_t*cls = malloc(sizeof(abc_class_t));
681         memset(cls, 0, sizeof(abc_class_t));
682         
683         DEBUG printf("class %d\n", t);
684         swf_GetU30(tag); //classname
685         swf_GetU30(tag); //supername
686
687         array_append(file->classes, NO_KEY, cls);
688
689         cls->flags = swf_GetU8(tag);
690         if(cls->flags&8) 
691             swf_GetU30(tag); //protectedNS
692         int s;
693         int inum = swf_GetU30(tag); //interface count
694         cls->interfaces = 0;
695         for(s=0;s<inum;s++) {
696             int interface_index = swf_GetU30(tag);
697             multiname_t* m = multiname_clone(pool_lookup_multiname(pool, interface_index));
698             list_append(cls->interfaces, m);
699             DEBUG printf("  class %d interface: %s\n", t, m->name);
700         }
701
702         swf_GetU30(tag); //iinit
703         traits_skip(tag);
704     }
705     for(t=0;t<num_classes;t++) {
706         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
707         int cinit = swf_GetU30(tag);
708         cls->static_constructor = (abc_method_t*)array_getvalue(file->methods, cinit);
709         traits_skip(tag);
710     }
711     int num_scripts = swf_GetU30(tag);
712     DEBUG printf("%d scripts\n", num_scripts);
713     for(t=0;t<num_scripts;t++) {
714         int init = swf_GetU30(tag);
715         traits_skip(tag);
716     }
717
718     int num_method_bodies = swf_GetU30(tag);
719     DEBUG printf("%d method bodies\n", num_method_bodies);
720     for(t=0;t<num_method_bodies;t++) {
721         int methodnr = swf_GetU30(tag);
722         if(methodnr >= file->methods->num) {
723             printf("Invalid method number: %d\n", methodnr);
724             return 0;
725         }
726         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, methodnr);
727         abc_method_body_t*c = malloc(sizeof(abc_method_body_t));
728         memset(c, 0, sizeof(abc_method_body_t));
729         c->max_stack = swf_GetU30(tag);
730         c->local_count = swf_GetU30(tag);
731         c->init_scope_depth = swf_GetU30(tag);
732         c->max_scope_depth = swf_GetU30(tag);
733         int code_length = swf_GetU30(tag);
734
735         c->method = m;
736         m->body = c;
737
738         int pos = tag->pos + code_length;
739         codelookup_t*codelookup = 0;
740         c->code = code_parse(tag, code_length, file, pool, &codelookup);
741         tag->pos = pos;
742
743         int exception_count = swf_GetU30(tag);
744         int s;
745         c->exceptions = list_new();
746         for(s=0;s<exception_count;s++) {
747             exception_t*e = malloc(sizeof(exception_t));
748
749             e->from = code_atposition(codelookup, swf_GetU30(tag));
750             e->to = code_atposition(codelookup, swf_GetU30(tag));
751             e->target = code_atposition(codelookup, swf_GetU30(tag));
752
753             e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
754             e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
755             //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
756             //if(e->var_name) e->var_name = strdup(e->var_name);
757             list_append(c->exceptions, e);
758         }
759         codelookup_free(codelookup);
760         c->traits = traits_parse(tag, pool, file);
761
762         DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
763
764         array_append(file->method_bodies, NO_KEY, c);
765     }
766     if(tag->len - tag->pos) {
767         fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
768         return 0;
769     }
770
771     swf_SetTagPos(tag, classes_pos);
772     for(t=0;t<num_classes;t++) {
773         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
774
775         int classname_index = swf_GetU30(tag);
776         int superclass_index = swf_GetU30(tag);
777         cls->classname = multiname_clone(pool_lookup_multiname(pool, classname_index));
778         cls->superclass = multiname_clone(pool_lookup_multiname(pool, superclass_index));
779         cls->flags = swf_GetU8(tag);
780         const char*ns = "";
781         if(cls->flags&8) {
782             int ns_index = swf_GetU30(tag);
783             cls->protectedNS = namespace_clone(pool_lookup_namespace(pool, ns_index));
784         }
785         
786         int num_interfaces = swf_GetU30(tag); //interface count
787         int s;
788         for(s=0;s<num_interfaces;s++) {
789             swf_GetU30(tag); // multiname index TODO
790         }
791         int iinit = swf_GetU30(tag);
792         cls->constructor = (abc_method_t*)array_getvalue(file->methods, iinit);
793         cls->traits = traits_parse(tag, pool, file);
794     }
795     for(t=0;t<num_classes;t++) {
796         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
797         /* SKIP */
798         swf_GetU30(tag); // cindex
799         cls->static_constructor_traits = traits_parse(tag, pool, file);
800     }
801     int num_scripts2 = swf_GetU30(tag);
802     for(t=0;t<num_scripts2;t++) {
803         int init = swf_GetU30(tag);
804         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, init);
805         
806         abc_script_t*s = malloc(sizeof(abc_script_t));
807         memset(s, 0, sizeof(abc_script_t));
808         s->method = m;
809         s->traits = traits_parse(tag, pool, file);
810         array_append(file->scripts, NO_KEY, s);
811         if(!s->traits) {
812             fprintf(stderr, "Can't parse script traits\n");
813             return 0;
814         }
815     }
816
817     pool_destroy(pool);
818     return file;
819 }
820
821 void swf_WriteABC(TAG*abctag, void*code)
822 {
823     abc_file_t*file = (abc_file_t*)code;
824     pool_t*pool = pool_new();
825
826     TAG*tmp = swf_InsertTag(0,0);
827     TAG*tag = tmp;
828     int t;
829    
830     char need_null_method=0;
831     for(t=0;t<file->classes->num;t++) {
832         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
833         if(!c->constructor || !c->static_constructor) {
834             need_null_method=1;
835             break;
836         }
837     }
838
839     abc_method_t*nullmethod = 0;
840     if(need_null_method) {
841         nullmethod = malloc(sizeof(abc_method_t));
842         memset(nullmethod, 0, sizeof(abc_method_t));
843         /*TODO: might be more efficient to have this at the beginning */
844         array_append(file->methods, NO_KEY, nullmethod);
845     }
846    
847     swf_SetU30(tag, file->methods->num);
848     /* enumerate classes, methods and method bodies */
849     for(t=0;t<file->methods->num;t++) {
850         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
851         m->index = t;
852     }
853     for(t=0;t<file->classes->num;t++) {
854         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
855         c->index = t;
856     }
857     for(t=0;t<file->method_bodies->num;t++) {
858         abc_method_body_t*m = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
859         m->index = t;
860         exception_list_t*ee = m->exceptions;
861         while(ee) {
862             exception_t*e=ee->exception;ee->exception=0;
863             e->from = e->to = e->target = 0;
864             multiname_destroy(e->exc_type);e->exc_type=0;
865             multiname_destroy(e->var_name);e->var_name=0;
866             free(e);
867             ee=ee->next;
868         }
869         list_free(m->exceptions);m->exceptions=0;
870     }
871
872     for(t=0;t<file->methods->num;t++) {
873         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
874         int n = 0;
875         multiname_list_t*l = m->parameters;
876         int num_params = list_length(m->parameters);
877         swf_SetU30(tag, num_params);
878         if(m->return_type) 
879             swf_SetU30(tag, pool_register_multiname(pool, m->return_type));
880         else
881             swf_SetU30(tag, 0);
882         int s;
883         while(l) {
884             swf_SetU30(tag, pool_register_multiname(pool, l->multiname));
885             l = l->next;
886         }
887         if(m->name) {
888             swf_SetU30(tag, pool_register_string(pool, m->name));
889         } else {
890             swf_SetU30(tag, 0);
891         }
892
893         swf_SetU8(tag, 0); //flags
894     }
895    
896     /* write metadata */
897     swf_SetU30(tag, file->metadata->num);
898     for(t=0;t<file->metadata->num;t++) {
899         const char*entry_name = array_getkey(file->metadata, t);
900         swf_SetU30(tag, pool_register_string(pool, entry_name));
901         array_t*items = (array_t*)array_getvalue(file->metadata, t);
902         swf_SetU30(tag, items->num);
903         int s;
904         for(s=0;s<items->num;s++) {
905             int i1 = pool_register_string(pool, array_getkey(items, s));
906             int i2 = pool_register_string(pool, array_getvalue(items, s));
907             swf_SetU30(tag, i1);
908             swf_SetU30(tag, i2);
909         }
910     }
911
912     swf_SetU30(tag, file->classes->num);
913     for(t=0;t<file->classes->num;t++) {
914         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
915    
916         int classname_index = pool_register_multiname(pool, c->classname);
917         int superclass_index = pool_register_multiname(pool, c->superclass);
918
919         swf_SetU30(tag, classname_index);
920         swf_SetU30(tag, superclass_index);
921
922         swf_SetU8(tag, c->flags); // flags
923         if(c->flags&0x08) {
924             int ns_index = pool_register_namespace(pool, c->protectedNS);
925             swf_SetU30(tag, ns_index);
926         }
927
928         swf_SetU30(tag, list_length(c->interfaces));
929         multiname_list_t*interface= c->interfaces;
930         while(interface) {
931             swf_SetU30(tag, pool_register_multiname(pool, interface->multiname));
932             interface = interface->next;
933         }
934
935         if(!c->constructor) {
936             swf_SetU30(tag, nullmethod->index);
937         } else {
938             swf_SetU30(tag, c->constructor->index);
939         }
940         traits_write(pool, tag, c->traits);
941     }
942     for(t=0;t<file->classes->num;t++) {
943         abc_class_t*c = (abc_class_t*)array_getvalue(file->classes, t);
944         if(!c->static_constructor) {
945             swf_SetU30(tag, nullmethod->index);
946         } else {
947             swf_SetU30(tag, c->static_constructor->index);
948         }
949         traits_write(pool, tag, c->static_constructor_traits);
950     }
951
952     swf_SetU30(tag, file->scripts->num);
953     for(t=0;t<file->scripts->num;t++) {
954         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
955         swf_SetU30(tag, s->method->index); //!=t!
956         traits_write(pool, tag, s->traits);
957     }
958
959     swf_SetU30(tag, file->method_bodies->num);
960     for(t=0;t<file->method_bodies->num;t++) {
961         abc_method_body_t*c = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
962         abc_method_t*m = c->method;
963         swf_SetU30(tag, m->index);
964         swf_SetU30(tag, c->max_stack);
965         swf_SetU30(tag, c->local_count);
966         swf_SetU30(tag, c->init_scope_depth);
967         swf_SetU30(tag, c->max_scope_depth);
968
969         code_write(tag, c->code, pool, file);
970
971         swf_SetU30(tag, list_length(c->exceptions));
972         exception_list_t*l = c->exceptions;
973         while(l) {
974             // warning: assumes "pos" in each code_t is up-to-date
975             swf_SetU30(tag, l->exception->from->pos);
976             swf_SetU30(tag, l->exception->to->pos);
977             swf_SetU30(tag, l->exception->target->pos);
978             swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
979             swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
980             l = l->next;
981         }
982
983         traits_write(pool, tag, c->traits);
984     }
985
986     // --- start to write real tag --
987     
988     tag = abctag;
989
990     if(tag->id == ST_DOABC) {
991         swf_SetU32(tag, 1); // flags
992         swf_SetString(tag, file->name);
993     }
994
995     swf_SetU16(tag, 0x10); //version
996     swf_SetU16(tag, 0x2e);
997     
998     pool_write(pool, tag);
999     
1000     swf_SetBlock(tag, tmp->data, tmp->len);
1001
1002     swf_DeleteTag(0, tmp);
1003     pool_destroy(pool);
1004 }
1005
1006 void abc_file_free(abc_file_t*file)
1007 {
1008
1009     int t;
1010     for(t=0;t<file->metadata->num;t++) {
1011         array_t*items = (array_t*)array_getvalue(file->metadata, t);
1012         int s;
1013         for(s=0;s<items->num;s++) {
1014             free(array_getvalue(items, s));
1015         }
1016         array_free(items);
1017     }
1018     array_free(file->metadata);
1019
1020     for(t=0;t<file->methods->num;t++) {
1021         abc_method_t*m = (abc_method_t*)array_getvalue(file->methods, t);
1022
1023         multiname_list_t*param = m->parameters;
1024         while(param) {
1025             multiname_destroy(param->multiname);param->multiname=0;
1026             param = param->next;
1027         }
1028         list_free(m->parameters);m->parameters=0;
1029
1030         if(m->name) {
1031             free((void*)m->name);m->name=0;
1032         }
1033         if(m->return_type) {
1034             multiname_destroy(m->return_type);
1035         }
1036         free(m);
1037     }
1038     array_free(file->methods);
1039
1040     for(t=0;t<file->classes->num;t++) {
1041         abc_class_t*cls = (abc_class_t*)array_getvalue(file->classes, t);
1042         traits_free(cls->traits);cls->traits=0;
1043         traits_free(cls->static_constructor_traits);cls->static_constructor_traits=0;
1044
1045         if(cls->classname) {
1046             multiname_destroy(cls->classname);
1047         }
1048         if(cls->superclass) {
1049             multiname_destroy(cls->superclass);
1050         }
1051
1052         multiname_list_t*i = cls->interfaces;
1053         while(i) {
1054             multiname_destroy(i->multiname);i->multiname=0;
1055             i = i->next;
1056         }
1057         list_free(cls->interfaces);cls->interfaces=0;
1058
1059         if(cls->protectedNS) {
1060             namespace_destroy(cls->protectedNS);
1061         }
1062         free(cls);
1063     }
1064     array_free(file->classes);
1065
1066     for(t=0;t<file->scripts->num;t++) {
1067         abc_script_t*s = (abc_script_t*)array_getvalue(file->scripts, t);
1068         traits_free(s->traits);s->traits=0;
1069         free(s);
1070     }
1071     array_free(file->scripts);
1072
1073     for(t=0;t<file->method_bodies->num;t++) {
1074         abc_method_body_t*body = (abc_method_body_t*)array_getvalue(file->method_bodies, t);
1075         code_free(body->code);body->code=0;
1076         traits_free(body->traits);body->traits=0;
1077         free(body);
1078     }
1079     array_free(file->method_bodies);
1080
1081     if(file->name) {
1082         free((void*)file->name);file->name=0;
1083     }
1084
1085     free(file);
1086 }
1087
1088 void swf_FreeABC(void*code)
1089 {
1090     abc_file_t*file= (abc_file_t*)code;
1091     abc_file_free(file);
1092 }
1093
1094 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
1095 {
1096     int num_frames = 0;
1097     int has_buttons = 0;
1098     TAG*tag=swf->firstTag;
1099     while(tag) {
1100         if(tag->id == ST_SHOWFRAME)
1101             num_frames++;
1102         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2)
1103             has_buttons = 1;
1104         tag = tag->next;
1105     }
1106
1107     abc_file_t*file = abc_file_new();
1108     abc_method_body_t*c = 0;
1109    
1110     abc_class_t*cls = abc_class_new2(file, "rfx::MainTimeline", "flash.display::MovieClip");
1111     abc_class_protectedNS(cls, "rfx:MainTimeline");
1112   
1113     TAG*abctag = swf_InsertTagBefore(swf, swf->firstTag, ST_DOABC);
1114     
1115     tag = swf_InsertTag(abctag, ST_SYMBOLCLASS);
1116     swf_SetU16(tag, 1);
1117     swf_SetU16(tag, 0);
1118     swf_SetString(tag, "rfx.MainTimeline");
1119
1120     c = abc_class_staticconstructor(cls, 0, 0);
1121     c->max_stack = 1;
1122     c->local_count = 1;
1123     c->init_scope_depth = 9;
1124     c->max_scope_depth = 10;
1125
1126     __ getlocal_0(c);
1127     __ pushscope(c);
1128     __ returnvoid(c);
1129
1130     c = abc_class_constructor(cls, 0, 0);
1131     c->max_stack = 3;
1132     c->local_count = 1;
1133     c->init_scope_depth = 10;
1134     c->max_scope_depth = 11;
1135     
1136     debugfile(c, "constructor.as");
1137
1138     __ getlocal_0(c);
1139     __ pushscope(c);
1140
1141     __ getlocal_0(c);
1142     __ constructsuper(c,0);
1143
1144     __ getlex(c, "[package]flash.system::Security");
1145     __ pushstring(c, "*");
1146     __ callpropvoid(c, "[package]::allowDomain", 1);
1147     
1148     if(stop_each_frame || has_buttons) {
1149         int frame = 0;
1150         tag = swf->firstTag;
1151         abc_method_body_t*f = 0; //frame script
1152         while(tag && tag->id!=ST_END) {
1153             char framename[80];
1154             char needs_framescript=0;
1155             char buttonname[80];
1156             char functionname[80];
1157             sprintf(framename, "[packageinternal]rfx::frame%d", frame);
1158             
1159             if(!f && (tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2 || stop_each_frame)) {
1160                 /* make the contructor add a frame script */
1161                 __ findpropstrict(c,"[package]::addFrameScript");
1162                 __ pushbyte(c,frame);
1163                 __ getlex(c,framename);
1164                 __ callpropvoid(c,"[package]::addFrameScript",2);
1165
1166                 f = abc_class_method(cls, 0, framename, 0);
1167                 f->max_stack = 3;
1168                 f->local_count = 1;
1169                 f->init_scope_depth = 10;
1170                 f->max_scope_depth = 11;
1171                 __ debugfile(f, "framescript.as");
1172                 __ debugline(f, 1);
1173                 __ getlocal_0(f);
1174                 __ pushscope(f);
1175             }
1176
1177             if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1178                 U16 id = swf_GetDefineID(tag);
1179                 sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1180                 __ getlex(f,buttonname);
1181                 __ getlex(f,"flash.events::MouseEvent");
1182                 __ getproperty(f, "::CLICK");
1183                 sprintf(functionname, "::clickbutton%d", swf_GetDefineID(tag));
1184                 __ getlex(f,functionname);
1185                 __ callpropvoid(f, "::addEventListener" ,2);
1186
1187                 if(stop_each_frame) {
1188                     __ findpropstrict(f, "[package]::stop");
1189                     __ callpropvoid(f, "[package]::stop", 0);
1190                 }
1191                 needs_framescript = 1;
1192
1193                 abc_method_body_t*h =
1194                     abc_class_method(cls, "::void", functionname, 1, "flash.events::MouseEvent");
1195                 h->max_stack = 6;
1196                 h->local_count = 2;
1197                 h->init_scope_depth = 10;
1198                 h->max_scope_depth = 11;
1199                 __ getlocal_0(h);
1200                 __ pushscope(h);
1201
1202                 ActionTAG*oldaction = swf_ButtonGetAction(tag);
1203                 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
1204                     int framenr = GET16(oldaction->data);
1205                     if(framenr>254) {
1206                         fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
1207                     }
1208                     if(!events) {
1209                         __ findpropstrict(h,"[package]::gotoAndStop");
1210                         __ pushbyte(h,framenr+1);
1211                         __ callpropvoid(h,"[package]::gotoAndStop", 1);
1212                     } else {
1213                         char framename[80];
1214                         sprintf(framename, "frame%d", framenr);
1215                         __ getlocal_0(h); //this
1216                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1217                         __ pushstring(h, "link");
1218                         __ pushtrue(h);
1219                         __ pushtrue(h);
1220                         __ pushstring(h, framename);
1221                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1222                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1223                     }
1224                 } else if(oldaction && oldaction->op == ACTION__GETURL) {
1225                     if(!events) {
1226                         __ findpropstrict(h,"flash.net::navigateToURL");
1227                         __ findpropstrict(h,"flash.net::URLRequest");
1228                         // TODO: target _blank
1229                         __ pushstring(h,oldaction->data); //url
1230                         __ constructprop(h,"flash.net::URLRequest", 1);
1231                         __ callpropvoid(h,"flash.net::navigateToURL", 1);
1232                     } else {
1233                         __ getlocal_0(h); //this
1234                         __ findpropstrict(h, "[package]flash.events::TextEvent");
1235                         __ pushstring(h, "link");
1236                         __ pushtrue(h);
1237                         __ pushtrue(h);
1238                         __ pushstring(h,oldaction->data); //url
1239                         __ constructprop(h,"[package]flash.events::TextEvent", 4);
1240                         __ callpropvoid(h,"[package]::dispatchEvent", 1);
1241                     }
1242                 } else if(oldaction) {
1243                     fprintf(stderr, "Warning: Couldn't translate button code of button %d to flash 9 abc action\n", id);
1244                 }
1245                 __ returnvoid(h);
1246                 swf_ActionFree(oldaction);
1247             }
1248             if(tag->id == ST_SHOWFRAME) {
1249                 if(f) {
1250                     __ returnvoid(f);
1251                     f = 0;
1252                 }
1253                 frame++;
1254             }
1255             tag = tag->next;
1256         }
1257         if(f) {
1258             __ returnvoid(f);
1259         }
1260     }
1261     __ returnvoid(c);
1262
1263     tag = swf->firstTag;
1264     while(tag) {
1265         if(tag->id == ST_DEFINEBUTTON || tag->id == ST_DEFINEBUTTON2) {
1266             char buttonname[80];
1267             sprintf(buttonname, "::button%d", swf_GetDefineID(tag));
1268             abc_AddSlot(cls, buttonname, 0, "flash.display::SimpleButton");
1269         }
1270         tag = tag->next;
1271     }
1272
1273
1274     abc_script_t*s = abc_initscript(file, 0, 0);
1275     c = s->method->body;
1276     c->max_stack = 2;
1277     c->local_count = 1;
1278     c->init_scope_depth = 1;
1279     c->max_scope_depth = 9;
1280
1281     __ getlocal_0(c);
1282     __ pushscope(c);
1283     __ getscopeobject(c, 0);
1284     __ getlex(c,"::Object");
1285     __ pushscope(c);
1286     __ getlex(c,"flash.events::EventDispatcher");
1287     __ pushscope(c);
1288     __ getlex(c,"flash.display::DisplayObject");
1289     __ pushscope(c);
1290     __ getlex(c,"flash.display::InteractiveObject");
1291     __ pushscope(c);
1292     __ getlex(c,"flash.display::DisplayObjectContainer");
1293     __ pushscope(c);
1294     __ getlex(c,"flash.display::Sprite");
1295     __ pushscope(c);
1296     __ getlex(c,"flash.display::MovieClip");
1297     __ pushscope(c);
1298     __ getlex(c,"flash.display::MovieClip");
1299     __ newclass(c,cls);
1300     __ popscope(c);
1301     __ popscope(c);
1302     __ popscope(c);
1303     __ popscope(c);
1304     __ popscope(c);
1305     __ popscope(c);
1306     __ popscope(c);
1307     __ initproperty(c,"rfx::MainTimeline");
1308     __ returnvoid(c);
1309
1310     //abc_method_body_addClassTrait(c, "rfx:MainTimeline", 1, cls);
1311     abc_initscript_addClassTrait(s, "rfx::MainTimeline", 1, cls);
1312
1313     swf_WriteABC(abctag, file);
1314 }
1315