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