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