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 */
25 #include "tokenizer.h"
28 void as3_import_abc(char*filename)
30 TAG*tag = swf_InsertTag(0, ST_RAWABC);
31 memfile_t*file = memfile_open(filename);
32 tag->data = file->data;
34 abc_file_t*abc = swf_ReadABC(tag);
41 void as3_import_swf(char*filename)
43 SWF* swf = swf_OpenSWF(filename);
46 TAG*tag = swf->firstTag;
48 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
49 abc_file_t*abc = swf_ReadABC(tag);
59 void as3_import_file(char*filename)
61 FILE*fi = fopen(filename, "rb");
64 fread(head, 3, 1, fi);
66 if(!strncmp(head, "FWS", 3) ||
67 !strncmp(head, "CWS", 3)) {
68 as3_import_swf(filename);
70 as3_import_abc(filename);
74 static int compare_traits(const void*v1, const void*v2)
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);
81 return strcmp(x1->name->name, x2->name->name);
84 static classinfo_t*resolve_class(char*what, multiname_t*n)
87 if(!n->name[0] || !strcmp(n->name, "void")) return 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;
95 c = (classinfo_t*)registry_find(s->namespace->name, n->name);
103 as3_warning("import: couldn't resolve %s %s", what, n->name);
106 if(c->kind != INFOTYPE_CLASS)
107 as3_warning("import: %s %s resolves to something that's not a class", what, n->name);
111 void as3_import_code(void*_abc)
113 abc_file_t*abc = _abc;
115 for(t=0;t<abc->classes->num;t++) {
116 abc_class_t*cls = array_getvalue(abc->classes, t);
117 U8 access = cls->classname->ns->access;
118 if(access==ACCESS_PRIVATE ||
119 access==ACCESS_PACKAGEINTERNAL)
121 //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
124 const char*package = strdup(cls->classname->ns->name);
125 const char*name = strdup(cls->classname->name);
127 multiname_list_t*i=cls->interfaces;
128 classinfo_t*c = classinfo_register(access, package, name, list_length(i));
129 c->flags|=FLAG_BUILTIN;
131 if(cls->flags & CLASS_FINAL)
132 c->flags |= FLAG_FINAL;
133 if(cls->flags & CLASS_INTERFACE)
134 c->flags |= FLAG_INTERFACE;
135 if(!(cls->flags & CLASS_SEALED))
136 c->flags |= FLAG_DYNAMIC;
139 for(t=0;t<abc->classes->num;t++) {
140 abc_class_t*cls = array_getvalue(abc->classes, t);
141 const char*package = strdup(cls->classname->ns->name);
142 const char*name = strdup(cls->classname->name);
143 classinfo_t*c = (classinfo_t*)registry_find(package, name);
147 multiname_list_t*i = cls->interfaces;
149 c->interfaces[nr++] = resolve_class("interface", i->multiname);
152 c->superclass = resolve_class("superclass", cls->superclass);
158 trait_t*trait = l->trait;
159 U8 access = trait->name->ns->access;
160 if(access==ACCESS_PRIVATE)
162 const char*name = trait->name->name;
163 char* ns= ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
164 if(registry_findmember(c, ns, name, 0))
169 if(trait->kind == TRAIT_METHOD) {
170 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name);
171 s->return_type = resolve_class("return type", trait->method->return_type);
172 } else if(trait->kind == TRAIT_SLOT ||
173 trait->kind == TRAIT_GETTER) {
174 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name);
175 s->type = resolve_class("type", trait->type_name);
180 s->flags = is_static?FLAG_STATIC:0;
181 s->flags |= FLAG_BUILTIN;
186 if(!l && !is_static) {
187 l = cls->static_traits;
193 # define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
195 /* count public functions */
197 for(t=0;t<abc->scripts->num;t++) {
198 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
200 num_methods += IS_PUBLIC_MEMBER(l->trait);
203 trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
205 for(t=0;t<abc->scripts->num;t++) {
206 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
208 if(IS_PUBLIC_MEMBER(l->trait)) {
209 traits[num_methods++] = l->trait;
213 qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
214 for(t=0;t<num_methods;t++) {
215 trait_t*trait = traits[t];
216 if(IS_PUBLIC_MEMBER(trait)) {
217 U8 access = trait->name->ns->access;
218 const char*package = strdup(trait->name->ns->name);
219 const char*name = strdup(trait->name->name);
222 if(trait->kind == TRAIT_METHOD) {
223 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
224 m->return_type = resolve_class("return type", trait->method->return_type);
226 m = (memberinfo_t*)varinfo_register_global(access, package, name);
227 m->type = resolve_class("type", trait->type_name);
229 m->flags |= FLAG_BUILTIN;