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