bugfixes
[swftools.git] / lib / as3 / import.c
1 /* import.c
2
3    Extension module for the rfxswf library.
4    Part of the swftools package.
5
6    Copyright (c) 2009 Matthias Kramm <kramm@quiss.org>
7  
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
21
22 #include "import.h"
23 #include "abc.h"
24 #include "registry.h"
25 #include "tokenizer.h"
26 #include "../os.h"
27
28 static void import_code(void*_abc, char*filename, int pass);
29
30 void as3_import_abc(char*filename)
31 {
32     TAG*tag = swf_InsertTag(0, ST_RAWABC);
33     memfile_t*file = memfile_open(filename);
34     tag->data = file->data;
35     tag->len = file->len;
36     abc_file_t*abc = swf_ReadABC(tag);
37     import_code(abc, filename, 0);
38     import_code(abc, filename, 1);
39     swf_FreeABC(abc);
40     memfile_close(file);
41     free(tag);
42 }
43
44 void as3_import_swf(char*filename)
45 {
46     SWF* swf = swf_OpenSWF(filename);
47     if(!swf)
48         return;
49     TAG*tag = swf->firstTag;
50
51     /* pass 1 */
52     while(tag) {
53         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
54             abc_file_t*abc = swf_ReadABC(tag);
55             import_code(abc, filename, 0);
56             swf_FreeABC(abc);
57         }
58         tag = tag->next;
59     }
60
61     tag = swf->firstTag;
62     /* pass 2 */
63     while(tag) {
64         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
65             abc_file_t*abc = swf_ReadABC(tag);
66             import_code(abc, filename, 1);
67             swf_FreeABC(abc);
68         }
69         tag = tag->next;
70     }
71
72     swf_FreeTags(swf);
73     free(swf);
74 }
75
76 void as3_import_file(char*filename)
77 {
78     FILE*fi = fopen(filename, "rb");
79     if(!fi) return;
80     char head[3];
81     fread(head, 3, 1, fi);
82     fclose(fi);
83     if(!strncmp(head, "FWS", 3) ||
84        !strncmp(head, "CWS", 3)) {
85         as3_import_swf(filename);
86     } else {
87         as3_import_abc(filename);
88     }
89 }
90
91 static int compare_traits(const void*v1, const void*v2)
92 {
93     trait_t* x1 = *(trait_t**)v1;
94     trait_t* x2 = *(trait_t**)v2;
95     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
96     if(i)
97         return i;
98     return strcmp(x1->name->name, x2->name->name);
99 }
100
101 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
102 {
103     if(!n) return 0;
104     if(!n->name[0] || !strcmp(n->name, "void")) return 0;
105
106     classinfo_t*c = 0;
107     if(n->ns && n->ns->name) {
108         c = (classinfo_t*)registry_find(n->ns->name, n->name);
109     } else if(n->namespace_set) {
110         namespace_list_t*s = n->namespace_set->namespaces;
111         while(s) {
112             c = (classinfo_t*)registry_find(s->namespace->name, n->name);
113             if(c)
114                 break;
115             s = s->next;
116         }
117     }
118
119     if(!c) {
120         as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
121         return 0;
122     }
123     if(c->kind != INFOTYPE_CLASS)
124         as3_warning("import %s: %s %s resolves to something that's not a class", filename, what, n->name);
125     return c;
126 }
127
128 static void import_code(void*_abc, char*filename, int pass)
129 {
130     abc_file_t*abc = _abc;
131     int t;
132     if(pass==0) {
133         for(t=0;t<abc->classes->num;t++) {
134             abc_class_t*cls = array_getvalue(abc->classes, t);
135             U8 access = cls->classname->ns->access;
136             if(access==ACCESS_PRIVATE ||
137                access==ACCESS_PACKAGEINTERNAL)
138                 continue;
139             //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
140             //    continue;
141
142             const char*package = strdup(cls->classname->ns->name);
143             const char*name = strdup(cls->classname->name);
144                     
145             multiname_list_t*i=cls->interfaces;
146             classinfo_t*c = classinfo_register(access, package, name, list_length(i));
147             c->flags|=FLAG_BUILTIN;
148
149             if(cls->flags & CLASS_FINAL)
150                 c->flags |= FLAG_FINAL;
151             if(cls->flags & CLASS_INTERFACE)
152                 c->flags |= FLAG_INTERFACE;
153             if(!(cls->flags & CLASS_SEALED))
154                 c->flags |= FLAG_DYNAMIC;
155         }
156         return;
157     }
158     
159     for(t=0;t<abc->classes->num;t++) {
160         abc_class_t*cls = array_getvalue(abc->classes, t);
161         const char*package = strdup(cls->classname->ns->name);
162         const char*name = strdup(cls->classname->name);
163         classinfo_t*c = (classinfo_t*)registry_find(package, name);
164         if(!c) continue;
165
166         int nr = 0;
167         multiname_list_t*i = cls->interfaces;
168         while(i) {
169             c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
170             i = i->next;
171         }
172         c->superclass = resolve_class(filename, "superclass", cls->superclass);
173       
174         trait_list_t*l=0;
175         char is_static = 0;
176         l = cls->traits;
177         while(l) {
178             trait_t*trait = l->trait;
179             U8 access = trait->name->ns->access;
180             if(access==ACCESS_PRIVATE)
181                 goto cont;
182             const char*name = trait->name->name;
183             char* ns= ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
184             if(registry_findmember(c, ns, name, 0))
185                 goto cont;
186             name = strdup(name);
187
188             memberinfo_t*s = 0;
189             if(trait->kind == TRAIT_METHOD) {
190                 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name);
191                 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
192             } else if(trait->kind == TRAIT_SLOT ||
193                       trait->kind == TRAIT_GETTER) {
194                 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name);
195                 s->type = resolve_class(filename, "type", trait->type_name);
196             } else {
197                 goto cont;
198             }
199
200             s->flags = is_static?FLAG_STATIC:0;
201             s->flags |= FLAG_BUILTIN;
202             s->parent = c;
203
204             cont:
205             l = l->next;
206             if(!l && !is_static) {
207                 l = cls->static_traits;
208                 is_static = 1;
209             }
210         }
211     }
212
213 #   define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
214
215     /* count public functions */
216     int num_methods=0;
217     for(t=0;t<abc->scripts->num;t++) {
218         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
219         for(;l;l=l->next) {
220             num_methods += IS_PUBLIC_MEMBER(l->trait);
221         }
222     }
223     trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
224     num_methods=0;
225     for(t=0;t<abc->scripts->num;t++) {
226         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
227         for(;l;l=l->next) {
228             if(IS_PUBLIC_MEMBER(l->trait)) {
229                 traits[num_methods++] = l->trait;
230             }
231         }
232     }
233     qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
234     for(t=0;t<num_methods;t++) {
235         trait_t*trait = traits[t];
236         if(IS_PUBLIC_MEMBER(trait)) {
237             U8 access = trait->name->ns->access;
238             const char*package = strdup(trait->name->ns->name);
239             const char*name = strdup(trait->name->name);
240             char np = 0;
241             memberinfo_t*m = 0;
242             if(trait->kind == TRAIT_METHOD) {
243                 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
244                 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
245             } else {
246                 m = (memberinfo_t*)varinfo_register_global(access, package, name);
247                 m->type = resolve_class(filename, "type", trait->type_name);
248             }
249             m->flags |= FLAG_BUILTIN;
250             m->parent = 0;
251         }
252     }
253 }
254
255 void as3_import_code(void*_abc)
256 {
257     import_code(_abc, "", 0);
258     import_code(_abc, "", 1);
259 }