43532021ddec46a025e5c30490d6c9214f69f01e
[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 "common.h"
26 #include "common.h"
27 #include "tokenizer.h"
28 #include "../os.h"
29
30 static void import_code(void*_abc, char*filename, int pass);
31
32 void as3_import_abc(char*filename)
33 {
34     TAG*tag = swf_InsertTag(0, ST_RAWABC);
35     memfile_t*file = memfile_open(filename);
36     tag->data = file->data;
37     tag->len = file->len;
38     abc_file_t*abc = swf_ReadABC(tag);
39     import_code(abc, filename, 0);
40     import_code(abc, filename, 1);
41     swf_FreeABC(abc);
42     memfile_close(file);
43     free(tag);
44 }
45
46 void as3_import_swf(char*filename)
47 {
48     SWF* swf = swf_OpenSWF(filename);
49     if(!swf)
50         return;
51     TAG*tag = swf->firstTag;
52
53     /* pass 1 */
54     while(tag) {
55         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
56             abc_file_t*abc = swf_ReadABC(tag);
57             import_code(abc, filename, 0);
58             swf_FreeABC(abc);
59         }
60         tag = tag->next;
61     }
62
63     tag = swf->firstTag;
64     /* pass 2 */
65     while(tag) {
66         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
67             abc_file_t*abc = swf_ReadABC(tag);
68             import_code(abc, filename, 1);
69             swf_FreeABC(abc);
70         }
71         tag = tag->next;
72     }
73
74     swf_FreeTags(swf);
75     free(swf);
76 }
77
78 void as3_import_file(char*filename)
79 {
80     FILE*fi = fopen(filename, "rb");
81     if(!fi) return;
82     char head[3];
83     fread(head, 3, 1, fi);
84     fclose(fi);
85     if(!strncmp(head, "FWS", 3) ||
86        !strncmp(head, "CWS", 3)) {
87         as3_import_swf(filename);
88     } else {
89         as3_import_abc(filename);
90     }
91 }
92
93 static int compare_traits(const void*v1, const void*v2)
94 {
95     trait_t* x1 = *(trait_t**)v1;
96     trait_t* x2 = *(trait_t**)v2;
97     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
98     if(i)
99         return i;
100     return strcmp(x1->name->name, x2->name->name);
101 }
102
103 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
104 {
105     if(!n) return 0;
106     if(!n->name[0] || !strcmp(n->name, "void")) return 0;
107
108     classinfo_t*c = 0;
109     if(n->ns && n->ns->name) {
110         c = (classinfo_t*)registry_find(n->ns->name, n->name);
111     } else if(n->namespace_set) {
112         namespace_list_t*s = n->namespace_set->namespaces;
113         while(s) {
114             c = (classinfo_t*)registry_find(s->namespace->name, n->name);
115             if(c)
116                 break;
117             s = s->next;
118         }
119     }
120
121     if(!c) {
122         as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
123         return 0;
124     }
125     if(c->kind != INFOTYPE_CLASS)
126         as3_warning("import %s: %s %s resolves to something that's not a class", filename, what, n->name);
127     return c;
128 }
129
130 static void import_code(void*_abc, char*filename, int pass)
131 {
132     abc_file_t*abc = _abc;
133     int t;
134     if(pass==0) {
135         for(t=0;t<abc->classes->num;t++) {
136             abc_class_t*cls = array_getvalue(abc->classes, t);
137             U8 access = cls->classname->ns->access;
138             if(access==ACCESS_PRIVATE ||
139                access==ACCESS_PACKAGEINTERNAL)
140                 continue;
141             //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
142             //    continue;
143
144             const char*package = strdup(cls->classname->ns->name);
145             const char*name = strdup(cls->classname->name);
146
147             multiname_list_t*i=cls->interfaces;
148             classinfo_t*c = classinfo_register(access, package, name, list_length(i));
149             c->flags|=FLAG_BUILTIN;
150
151             if(cls->flags & CLASS_FINAL)
152                 c->flags |= FLAG_FINAL;
153             if(cls->flags & CLASS_INTERFACE)
154                 c->flags |= FLAG_INTERFACE;
155             if(!(cls->flags & CLASS_SEALED))
156                 c->flags |= FLAG_DYNAMIC;
157         }
158         return;
159     }
160     
161     for(t=0;t<abc->classes->num;t++) {
162         abc_class_t*cls = array_getvalue(abc->classes, t);
163         const char*package = strdup(cls->classname->ns->name);
164         const char*name = strdup(cls->classname->name);
165         classinfo_t*c = (classinfo_t*)registry_find(package, name);
166         if(!c) continue;
167
168         int nr = 0;
169         multiname_list_t*i = cls->interfaces;
170         while(i) {
171             c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
172             i = i->next;
173         }
174         c->superclass = resolve_class(filename, "superclass", cls->superclass);
175       
176         trait_list_t*l=0;
177         char is_static = 0;
178         l = cls->traits;
179         if(!l) {
180             l = cls->static_traits;
181             is_static = 1;
182         }
183         dict_t*names = dict_new();
184         while(l) {
185             trait_t*trait = l->trait;
186             U8 access = trait->name->ns->access;
187
188             if(access==ACCESS_PRIVATE)
189                 goto cont;
190             const char*name = trait->name->name;
191             char* ns= ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
192             if(registry_findmember(c, ns, name, 0))
193                 goto cont;
194             name = strdup(name);
195
196             memberinfo_t*s = 0;
197             if(trait->kind == TRAIT_METHOD) {
198                 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name);
199                 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
200                 dict_put(names, name, 0);
201             } else if(trait->kind == TRAIT_SLOT) {
202                 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name);
203                 s->type = resolve_class(filename, "type", trait->type_name);
204                 dict_put(names, name, 0);
205             } else if(trait->kind == TRAIT_GETTER) {
206                 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name);
207                 s->type = resolve_class(filename, "type", trait->method->return_type);
208                 dict_put(names, name, 0);
209             } else if(trait->kind == TRAIT_CONST) {
210                 /* some variables (e.g. XML.length) are apparently both a method and a slot.
211                    needs split of static/non-static first */
212                 if(!dict_contains(names, name)) {
213                     varinfo_t*v = (varinfo_t*)varinfo_register_onclass(c, access, ns, name);
214                     v->type = resolve_class(filename, "type", trait->type_name);
215                     v->flags |= FLAG_CONST;
216                     /* leave this alone for now- it blows up the file too much 
217                     v->value = constant_clone(trait->value);*/
218                     s = (memberinfo_t*)v;
219                     dict_put(names, name, 0);
220                 } else 
221                     goto cont;
222
223             } else {
224                 goto cont;
225             }
226
227             s->flags = is_static?FLAG_STATIC:0;
228             s->flags |= FLAG_BUILTIN;
229             s->parent = c;
230
231             cont:
232             l = l->next;
233             if(!l && !is_static) {
234                 l = cls->static_traits;
235                 is_static = 1;
236             }
237         }
238         dict_destroy(names);
239     }
240
241 #   define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
242
243     /* count public functions */
244     int num_methods=0;
245     for(t=0;t<abc->scripts->num;t++) {
246         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
247         for(;l;l=l->next) {
248             num_methods += IS_PUBLIC_MEMBER(l->trait);
249         }
250     }
251     trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
252     num_methods=0;
253     for(t=0;t<abc->scripts->num;t++) {
254         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
255         for(;l;l=l->next) {
256             if(IS_PUBLIC_MEMBER(l->trait)) {
257                 traits[num_methods++] = l->trait;
258             }
259         }
260     }
261     qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
262     for(t=0;t<num_methods;t++) {
263         trait_t*trait = traits[t];
264         if(IS_PUBLIC_MEMBER(trait)) {
265             U8 access = trait->name->ns->access;
266             const char*package = strdup(trait->name->ns->name);
267             const char*name = strdup(trait->name->name);
268             char np = 0;
269             memberinfo_t*m = 0;
270             if(trait->kind == TRAIT_METHOD) {
271                 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
272                 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
273             } else {
274                 varinfo_t*v = varinfo_register_global(access, package, name);
275                 v->type = resolve_class(filename, "type", trait->type_name);
276                 v->value = constant_clone(trait->value);
277                 v->flags |= trait->kind==TRAIT_CONST?FLAG_CONST:0;
278                 m = (memberinfo_t*)v;
279             }
280             m->flags |= FLAG_BUILTIN;
281             m->parent = 0;
282         }
283     }
284 }
285
286 void as3_import_code(void*_abc)
287 {
288     import_code(_abc, "", 0);
289     import_code(_abc, "", 1);
290 }