continued namespace member implementation
[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 void as3_import_abc(char*filename)
29 {
30     TAG*tag = swf_InsertTag(0, ST_RAWABC);
31     memfile_t*file = memfile_open(filename);
32     tag->data = file->data;
33     tag->len = file->len;
34     abc_file_t*abc = swf_ReadABC(tag);
35     as3_import_code(abc);
36     swf_FreeABC(abc);
37     memfile_close(file);
38     free(tag);
39 }
40
41 void as3_import_swf(char*filename)
42 {
43     SWF* swf = swf_OpenSWF(filename);
44     if(!swf)
45         return;
46     TAG*tag = swf->firstTag;
47     while(tag) {
48         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
49             abc_file_t*abc = swf_ReadABC(tag);
50             as3_import_code(abc);
51             swf_FreeABC(abc);
52         }
53         tag = tag->next;
54     }
55     swf_FreeTags(swf);
56     free(swf);
57 }
58
59 void as3_import_file(char*filename)
60 {
61     FILE*fi = fopen(filename, "rb");
62     if(!fi) return;
63     char head[3];
64     fread(head, 3, 1, fi);
65     fclose(fi);
66     if(!strncmp(head, "FWS", 3) ||
67        !strncmp(head, "CWS", 3)) {
68         as3_import_swf(filename);
69     } else {
70         as3_import_abc(filename);
71     }
72 }
73
74 static int compare_traits(const void*v1, const void*v2)
75 {
76     trait_t* x1 = *(trait_t**)v1;
77     trait_t* x2 = *(trait_t**)v2;
78     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
79     if(i)
80         return i;
81     return strcmp(x1->name->name, x2->name->name);
82 }
83
84 static classinfo_t*resolve_class(char*what, multiname_t*n)
85 {
86     if(!n) return 0;
87     if(!n->name[0] || !strcmp(n->name, "void")) return 0;
88
89     classinfo_t*c = 0;
90     if(n->ns && n->ns->name) {
91         c = (classinfo_t*)registry_find(n->ns->name, n->name);
92     } else if(n->namespace_set) {
93         namespace_list_t*s = n->namespace_set->namespaces;
94         while(s) {
95             c = (classinfo_t*)registry_find(s->namespace->name, n->name);
96             if(c)
97                 break;
98             s = s->next;
99         }
100     }
101
102     if(!c) {
103         as3_warning("import: couldn't resolve %s %s", what, n->name);
104         return 0;
105     }
106     if(c->kind != INFOTYPE_CLASS)
107         as3_warning("import: %s %s resolves to something that's not a class", what, n->name);
108     return c;
109 }
110
111 void as3_import_code(abc_file_t*abc)
112 {
113     int t;
114     for(t=0;t<abc->classes->num;t++) {
115         abc_class_t*cls = array_getvalue(abc->classes, t);
116         U8 access = cls->classname->ns->access;
117         if(access==ACCESS_PRIVATE ||
118            access==ACCESS_PACKAGEINTERNAL)
119             continue;
120         //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
121         //    continue;
122
123         const char*package = strdup(cls->classname->ns->name);
124         const char*name = strdup(cls->classname->name);
125                 
126         multiname_list_t*i=cls->interfaces;
127         classinfo_t*c = classinfo_register(access, package, name, list_length(i));
128         c->flags|=FLAG_BUILTIN;
129
130         if(cls->flags & CLASS_FINAL)
131             c->flags |= FLAG_FINAL;
132         if(cls->flags & CLASS_INTERFACE)
133             c->flags |= FLAG_INTERFACE;
134         if(!(cls->flags & CLASS_SEALED))
135             c->flags |= FLAG_DYNAMIC;
136     }
137     
138     for(t=0;t<abc->classes->num;t++) {
139         abc_class_t*cls = array_getvalue(abc->classes, t);
140         const char*package = strdup(cls->classname->ns->name);
141         const char*name = strdup(cls->classname->name);
142         classinfo_t*c = (classinfo_t*)registry_find(package, name);
143         if(!c) continue;
144
145         int nr = 0;
146         multiname_list_t*i = cls->interfaces;
147         while(i) {
148             c->interfaces[nr++] = resolve_class("interface", i->multiname);
149             i = i->next;
150         }
151         c->superclass = resolve_class("superclass", cls->superclass);
152       
153         trait_list_t*l=0;
154         char is_static = 0;
155         l = cls->traits;
156         while(l) {
157             trait_t*trait = l->trait;
158             U8 access = trait->name->ns->access;
159             if(access==ACCESS_PRIVATE)
160                 goto cont;
161             const char*name = trait->name->name;
162             char* ns= ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
163             if(registry_findmember(c, ns, name, 0))
164                 goto cont;
165             name = strdup(name);
166
167             memberinfo_t*s = 0;
168             if(trait->kind == TRAIT_METHOD) {
169                 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name);
170                 s->return_type = resolve_class("return type", trait->method->return_type);
171             } else if(trait->kind == TRAIT_SLOT ||
172                       trait->kind == TRAIT_GETTER) {
173                 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name);
174                 s->type = resolve_class("type", trait->type_name);
175             } else {
176                 goto cont;
177             }
178
179             s->flags = is_static?FLAG_STATIC:0;
180             s->flags |= FLAG_BUILTIN;
181             s->parent = c;
182
183             cont:
184             l = l->next;
185             if(!l && !is_static) {
186                 l = cls->static_traits;
187                 is_static = 1;
188             }
189         }
190     }
191
192 #   define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
193
194     /* count public functions */
195     int num_methods=0;
196     for(t=0;t<abc->scripts->num;t++) {
197         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
198         for(;l;l=l->next) {
199             num_methods += IS_PUBLIC_MEMBER(l->trait);
200         }
201     }
202     trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
203     num_methods=0;
204     for(t=0;t<abc->scripts->num;t++) {
205         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
206         for(;l;l=l->next) {
207             if(IS_PUBLIC_MEMBER(l->trait)) {
208                 traits[num_methods++] = l->trait;
209             }
210         }
211     }
212     qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
213     for(t=0;t<num_methods;t++) {
214         trait_t*trait = traits[t];
215         if(IS_PUBLIC_MEMBER(trait)) {
216             U8 access = trait->name->ns->access;
217             const char*package = strdup(trait->name->ns->name);
218             const char*name = strdup(trait->name->name);
219             char np = 0;
220             memberinfo_t*m = 0;
221             if(trait->kind == TRAIT_METHOD) {
222                 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
223                 m->return_type = resolve_class("return type", trait->method->return_type);
224             } else {
225                 m = (memberinfo_t*)varinfo_register_global(access, package, name);
226                 m->type = resolve_class("type", trait->type_name);
227             }
228             m->flags |= FLAG_BUILTIN;
229             m->parent = 0;
230         }
231     }
232 }