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