3 Extension module for the rfxswf library.
4 Part of the swftools package.
6 Copyright (c) 2009 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
27 #include "tokenizer.h"
30 static void import_code(void*_abc, char*filename, int pass);
32 void as3_import_abc(char*filename)
34 TAG*tag = swf_InsertTag(0, ST_RAWABC);
35 memfile_t*file = memfile_open(filename);
36 tag->data = file->data;
38 abc_file_t*abc = swf_ReadABC(tag);
39 import_code(abc, filename, 0);
40 import_code(abc, filename, 1);
46 void as3_import_swf(char*filename)
48 SWF* swf = swf_OpenSWF(filename);
51 TAG*tag = swf->firstTag;
55 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
56 abc_file_t*abc = swf_ReadABC(tag);
57 import_code(abc, filename, 0);
66 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
67 abc_file_t*abc = swf_ReadABC(tag);
68 import_code(abc, filename, 1);
78 void as3_import_file(char*filename)
80 FILE*fi = fopen(filename, "rb");
83 fread(head, 3, 1, fi);
85 if(!strncmp(head, "FWS", 3) ||
86 !strncmp(head, "CWS", 3)) {
87 as3_import_swf(filename);
89 as3_import_abc(filename);
93 static int compare_traits(const void*v1, const void*v2)
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);
100 return strcmp(x1->name->name, x2->name->name);
103 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
106 if(!n->name[0]) return 0;
107 if(!strcmp(n->name, "void"))
111 if(n->ns && n->ns->name) {
112 c = (classinfo_t*)registry_find(n->ns->name, n->name);
113 } else if(n->namespace_set) {
114 namespace_list_t*s = n->namespace_set->namespaces;
116 c = (classinfo_t*)registry_find(s->namespace->name, n->name);
124 as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
127 if(c->kind != INFOTYPE_CLASS)
128 as3_warning("import %s: %s %s resolves to something that's not a class", filename, what, n->name);
132 static void import_code(void*_abc, char*filename, int pass)
134 abc_file_t*abc = _abc;
137 for(t=0;t<abc->classes->num;t++) {
138 abc_class_t*cls = array_getvalue(abc->classes, t);
139 U8 access = cls->classname->ns->access;
140 if(access==ACCESS_PRIVATE ||
141 access==ACCESS_PACKAGEINTERNAL)
143 //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
146 const char*package = strdup(cls->classname->ns->name);
147 const char*name = strdup(cls->classname->name);
149 multiname_list_t*i=cls->interfaces;
150 classinfo_t*c = classinfo_register(access, package, name, list_length(i));
151 c->flags|=FLAG_BUILTIN;
153 if(cls->flags & CLASS_FINAL)
154 c->flags |= FLAG_FINAL;
155 if(cls->flags & CLASS_INTERFACE)
156 c->flags |= FLAG_INTERFACE;
157 if(!(cls->flags & CLASS_SEALED))
158 c->flags |= FLAG_DYNAMIC;
163 for(t=0;t<abc->classes->num;t++) {
164 abc_class_t*cls = array_getvalue(abc->classes, t);
165 const char*package = strdup(cls->classname->ns->name);
166 const char*name = strdup(cls->classname->name);
167 classinfo_t*c = (classinfo_t*)registry_find(package, name);
171 multiname_list_t*i = cls->interfaces;
173 c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
176 c->superclass = resolve_class(filename, "superclass", cls->superclass);
182 l = cls->static_traits;
185 dict_t*names = dict_new();
187 trait_t*trait = l->trait;
188 U8 access = trait->name->ns->access;
190 if(access==ACCESS_PRIVATE)
192 const char*name = trait->name->name;
193 char* ns = access==ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
195 if(registry_findmember(c, ns, name, 0, is_static))
201 if(trait->kind == TRAIT_METHOD) {
202 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name, is_static);
203 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
204 dict_put(names, name, 0);
205 } else if(trait->kind == TRAIT_SLOT) {
206 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
207 s->type = resolve_class(filename, "type", trait->type_name);
208 dict_put(names, name, 0);
209 } else if(trait->kind == TRAIT_GETTER) {
210 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
211 s->type = resolve_class(filename, "type", trait->method->return_type);
212 dict_put(names, name, 0);
213 } else if(trait->kind == TRAIT_CONST) {
214 /* some variables (e.g. XML.length) are apparently both a method and a slot.
215 needs split of static/non-static first */
216 if(!dict_contains(names, name)) {
217 varinfo_t*v = (varinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
218 v->type = resolve_class(filename, "type", trait->type_name);
219 v->flags |= FLAG_CONST;
220 /* leave this alone for now- it blows up the file too much
221 v->value = constant_clone(trait->value);*/
222 s = (memberinfo_t*)v;
223 dict_put(names, name, 0);
231 s->flags = is_static?FLAG_STATIC:0;
232 s->flags |= FLAG_BUILTIN;
237 if(!l && !is_static) {
238 l = cls->static_traits;
245 # define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
247 /* count public functions */
249 for(t=0;t<abc->scripts->num;t++) {
250 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
252 num_methods += IS_PUBLIC_MEMBER(l->trait);
255 trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
257 for(t=0;t<abc->scripts->num;t++) {
258 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
260 if(IS_PUBLIC_MEMBER(l->trait)) {
261 traits[num_methods++] = l->trait;
265 qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
266 for(t=0;t<num_methods;t++) {
267 trait_t*trait = traits[t];
268 if(IS_PUBLIC_MEMBER(trait)) {
269 U8 access = trait->name->ns->access;
270 const char*package = strdup(trait->name->ns->name);
271 const char*name = strdup(trait->name->name);
274 if(trait->kind == TRAIT_METHOD) {
275 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
276 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
278 varinfo_t*v = varinfo_register_global(access, package, name);
279 v->type = resolve_class(filename, "type", trait->type_name);
280 v->value = constant_clone(trait->value);
281 v->flags |= trait->kind==TRAIT_CONST?FLAG_CONST:0;
282 m = (memberinfo_t*)v;
284 m->flags |= FLAG_BUILTIN;
290 void as3_import_code(void*_abc)
292 import_code(_abc, "", 0);
293 import_code(_abc, "", 1);