3 Routines for compiling Flash2 AVM2 ABC Actionscript
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
29 dict_t*registry_classes=0;
31 // ----------------------- class signature ------------------------------
33 char slotinfo_equals(slotinfo_t*c1, slotinfo_t*c2)
37 /* notice: access right is *not* respected */
38 if(!strcmp(c1->name, c2->name) &&
39 !strcmp(c1->package, c2->package)) {
44 static unsigned int slotinfo_hash(slotinfo_t*c)
46 unsigned int hash = 0;
47 hash = crc32_add_string(hash, c->package);
48 hash = crc32_add_string(hash, c->name);
52 static void* dummy_clone(void*other) {return other;}
53 static void dummy_destroy(slotinfo_t*c) {}
55 type_t slotinfo_type = {
56 hash: (hash_func)slotinfo_hash,
57 equals: (equals_func)slotinfo_equals,
58 dup: (dup_func)dummy_clone, // all signatures are static
59 free: (free_func)dummy_destroy,
62 // ------------------------- constructors --------------------------------
64 #define AVERAGE_NUMBER_OF_MEMBERS 8
65 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
67 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
69 c->kind = INFOTYPE_CLASS;
73 dict_put(registry_classes, c, c);
74 dict_init2(&c->members, &slotinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
77 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
80 m->kind = INFOTYPE_METHOD;
85 dict_put(&cls->members, m, m);
88 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
91 m->kind = INFOTYPE_SLOT;
96 dict_put(&cls->members, m, m);
99 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
101 NEW(methodinfo_t, m);
102 m->kind = INFOTYPE_METHOD;
103 m->flags = FLAG_STATIC;
105 m->package = package;
108 dict_put(registry_classes, m, m);
111 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
114 m->kind = INFOTYPE_SLOT;
115 m->flags = FLAG_STATIC;
117 m->package = package;
120 dict_put(registry_classes, m, m);
124 // --------------- builtin classes (from builtin.c) ----------------------
128 if(!registry_classes)
129 registry_classes = builtin_getclasses();
131 slotinfo_t* registry_find(const char*package, const char*name)
133 assert(registry_classes);
135 tmp.package = package;
137 slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
139 printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
142 slotinfo_t* registry_safefind(const char*package, const char*name)
144 slotinfo_t*c = registry_find(package, name);
146 printf("%s.%s\n", package, name);
154 for(t=0;t<registry_classes->hashsize;t++) {
155 dictentry_t*e = registry_classes->slots[t];
157 slotinfo_t*i = (slotinfo_t*)e->key;
158 printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
164 memberinfo_t* registry_findmember(classinfo_t*cls, const char*ns, const char*name, char recursive)
168 tmp.package = ns?ns:"";
171 return (memberinfo_t*)dict_lookup(&cls->members, &tmp);
173 /* look at classes directly extended by this class */
177 if(recursive>1) // check *only* superclasses
181 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
183 return (memberinfo_t*)m;
187 /* look at interfaces, and parent interfaces */
189 while(cls->interfaces[t]) {
190 classinfo_t*s = cls->interfaces[t];
192 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
194 return (memberinfo_t*)m;
203 memberinfo_t* registry_findmember_nsset(classinfo_t*cls, namespace_list_t*ns, const char*name, char superclasses)
206 memberinfo_t*m = registry_findmember(cls, ns->namespace->name, name, superclasses);
210 return registry_findmember(cls, "", name, superclasses);
214 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
218 m->ns->access = c->access;
219 m->ns->name = (char*)c->package;
221 m->namespace_set = 0;
223 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
228 namespace_t ns = {cls->access, (char*)cls->package};
229 return multiname_new(&ns,cls->name);
232 // ----------------------- memberinfo methods ------------------------------
234 /* hacky code to wrap a variable or function into a "type"
235 object, but keep a pointer to the "value" */
236 static dict_t* functionobjects = 0;
237 classinfo_t* slotinfo_asclass(slotinfo_t*f) {
238 if(!functionobjects) {
239 functionobjects = dict_new2(&ptr_type);
241 classinfo_t*c = dict_lookup(functionobjects, f);
246 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
247 c->access = ACCESS_PUBLIC;
249 if(f->kind == INFOTYPE_METHOD)
250 c->name = "Function";
251 else if(f->kind == INFOTYPE_CLASS)
253 else if(f->kind == INFOTYPE_SLOT)
256 c->name = "undefined";
259 dict_init2(&c->members, &slotinfo_type, 1);
261 dict_put(functionobjects, f, c);
265 classinfo_t* slotinfo_gettype(slotinfo_t*f)
268 if(f->kind == INFOTYPE_METHOD) {
269 return slotinfo_asclass(f);
270 } else if(f->kind == INFOTYPE_SLOT) {
271 varinfo_t*v = (varinfo_t*)f;
276 return registry_getanytype();
279 // ----------------------- builtin types ------------------------------
280 classinfo_t* registry_getanytype() {return 0;}
282 char registry_isfunctionclass(classinfo_t*c) {
283 return (c && c->package && c->name &&
284 !strcmp(c->package, "") && !strcmp(c->name, "Function"));
286 char registry_isclassclass(classinfo_t*c) {
287 return (c && c->package && c->name &&
288 !strcmp(c->package, "") && !strcmp(c->name, "Class"));
291 classinfo_t* registry_getobjectclass() {
292 static classinfo_t*c = 0;
293 if(!c) c = (classinfo_t*)registry_safefind("", "Object");
296 classinfo_t* registry_getstringclass() {
297 static classinfo_t*c = 0;
298 if(!c) c = (classinfo_t*)registry_safefind("", "String");
301 classinfo_t* registry_getarrayclass() {
302 static classinfo_t*c = 0;
303 if(!c) c = (classinfo_t*)registry_safefind("", "Array");
306 classinfo_t* registry_getintclass() {
307 static classinfo_t*c = 0;
308 if(!c) c = (classinfo_t*)registry_safefind("", "int");
311 classinfo_t* registry_getuintclass() {
312 static classinfo_t*c = 0;
313 if(!c) c = (classinfo_t*)registry_safefind("", "uint");
316 classinfo_t* registry_getbooleanclass() {
317 static classinfo_t*c = 0;
318 if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
321 classinfo_t* registry_getnumberclass() {
322 static classinfo_t*c = 0;
323 if(!c) c = (classinfo_t*)registry_safefind("", "Number");
326 classinfo_t* registry_getregexpclass() {
327 static classinfo_t*c = 0;
328 if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
331 classinfo_t* registry_getMovieClip() {
332 static classinfo_t*c = 0;
333 if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
337 // ----------------------- builtin dummy types -------------------------
338 classinfo_t nullclass = {
339 INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
341 classinfo_t* registry_getnullclass() {
345 namespace_t access2namespace(U8 access, char*package)
353 char* infotypename(slotinfo_t*s)
355 if(s->kind == INFOTYPE_CLASS) return "class";
356 else if(s->kind == INFOTYPE_SLOT) return "member";
357 else if(s->kind == INFOTYPE_METHOD) return "method";
358 else return "object";