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