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