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