as3: various bugfixes
[swftools.git] / lib / as3 / initcode.c
1 #include "abc.h"
2 #include "code.h"
3 #include "registry.h"
4 #include "initcode.h"
5
6 void initcode_add_classlist(abc_script_t*init, parsedclass_list_t*classes)
7 {
8     code_t*c = 0;
9
10     c = abc_getlocal_0(c);
11     c = abc_pushscope(c);
12
13     for(;classes;classes=classes->next) {
14         abc_class_t*abc = classes->parsedclass->abc;
15         classinfo_t*cls = classes->parsedclass->cls;
16
17         /* write the construction code for this class to the global init
18            function */
19         MULTINAME(classname2,cls);
20         int slotindex = abc_initscript_addClassTrait(init, &classname2, abc);
21
22         c = abc_getglobalscope(c);
23         classinfo_t*s = cls->superclass;
24
25         int count=0;
26
27         while(s) {
28             //TODO: take a look at the current scope stack, maybe 
29             //      we can re-use something
30             s = s->superclass;
31             if(!s) 
32             break;
33            
34             multiname_t*s2 = sig2mname(s);
35             c = abc_getlex2(c, s2);
36             multiname_destroy(s2);
37
38             c = abc_pushscope(c); count++;
39             c = c->prev->prev; // invert
40         }
41         /* continue appending after last op end */
42         while(c && c->next) c = c->next; 
43
44         multiname_t*extends2 = sig2mname(cls->superclass);
45         /* TODO: if this is one of *our* classes, we can also 
46                  do a getglobalscope/getslot <nr> (which references
47                  the init function's slots) */
48         if(extends2) {
49             c = abc_getlex2(c, extends2);
50             c = abc_dup(c);
51             /* notice: we get a Verify Error #1107 if the top elemnt on the scope
52                stack is not the superclass */
53             c = abc_pushscope(c);count++;
54         } else {
55             c = abc_pushnull(c);
56             /* notice: we get a verify error #1107 if the top element on the scope 
57                stack is not the global object */
58             c = abc_getlocal_0(c);
59             c = abc_pushscope(c);count++;
60         }
61         c = abc_newclass(c,abc);
62         while(count--) {
63             c = abc_popscope(c);
64         }
65         c = abc_setslot(c, slotindex);
66         multiname_destroy(extends2);
67     }
68     c = abc_returnvoid(c);
69
70     init->method->body->code = c;
71 }
72