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_init(&c->members,AVERAGE_NUMBER_OF_MEMBERS);
77 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*name)
80 m->kind = INFOTYPE_METHOD;
84 dict_put(&cls->members, name, m);
87 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*name)
90 m->kind = INFOTYPE_SLOT;
94 dict_put(&cls->members, name, m);
97 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
100 m->kind = INFOTYPE_METHOD;
101 m->flags = FLAG_STATIC;
103 m->package = package;
106 dict_put(registry_classes, m, m);
109 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
112 m->kind = INFOTYPE_SLOT;
113 m->flags = FLAG_STATIC;
115 m->package = package;
118 dict_put(registry_classes, m, m);
122 // --------------- builtin classes (from builtin.c) ----------------------
126 if(!registry_classes)
127 registry_classes = builtin_getclasses();
129 slotinfo_t* registry_find(const char*package, const char*name)
131 assert(registry_classes);
133 tmp.package = package;
135 slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
137 printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
140 slotinfo_t* registry_safefind(const char*package, const char*name)
142 slotinfo_t*c = registry_find(package, name);
144 printf("%s.%s\n", package, name);
152 for(t=0;t<registry_classes->hashsize;t++) {
153 dictentry_t*e = registry_classes->slots[t];
155 slotinfo_t*i = (slotinfo_t*)e->key;
156 printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
162 memberinfo_t* registry_findmember(classinfo_t*cls, const char*name, char recursive)
165 return (memberinfo_t*)dict_lookup(&cls->members, name);
167 /* look at classes directly extended by this class */
171 if(recursive>1) // check *only* superclasses
175 m = (slotinfo_t*)dict_lookup(&s->members, name);
177 return (memberinfo_t*)m;
181 /* look at interfaces, and parent interfaces */
183 while(cls->interfaces[t]) {
184 classinfo_t*s = cls->interfaces[t];
186 m = (slotinfo_t*)dict_lookup(&s->members, name);
188 return (memberinfo_t*)m;
197 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
201 m->ns->access = c->access;
202 m->ns->name = (char*)c->package;
204 m->namespace_set = 0;
206 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
211 namespace_t ns = {cls->access, (char*)cls->package};
212 return multiname_new(&ns,cls->name);
215 // ----------------------- memberinfo methods ------------------------------
217 /* hacky code to wrap a variable or function into a "type"
218 object, but keep a pointer to the "value" */
219 static dict_t* functionobjects = 0;
220 classinfo_t* slotinfo_asclass(slotinfo_t*f) {
221 if(!functionobjects) {
222 functionobjects = dict_new2(&ptr_type);
224 classinfo_t*c = dict_lookup(functionobjects, f);
229 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
230 c->access = ACCESS_PUBLIC;
232 if(f->kind == INFOTYPE_METHOD)
233 c->name = "Function";
234 else if(f->kind == INFOTYPE_CLASS)
236 else if(f->kind == INFOTYPE_SLOT)
239 c->name = "undefined";
242 dict_init(&c->members,1);
244 dict_put(functionobjects, f, c);
248 classinfo_t* slotinfo_gettype(slotinfo_t*f)
251 if(f->kind == INFOTYPE_METHOD) {
252 return slotinfo_asclass(f);
253 } else if(f->kind == INFOTYPE_SLOT) {
254 varinfo_t*v = (varinfo_t*)f;
259 return registry_getanytype();
262 // ----------------------- builtin types ------------------------------
263 classinfo_t* registry_getanytype() {return 0;}
265 char registry_isfunctionclass(classinfo_t*c) {
266 return (c && c->package && c->name &&
267 !strcmp(c->package, "") && !strcmp(c->name, "Function"));
269 char registry_isclassclass(classinfo_t*c) {
270 return (c && c->package && c->name &&
271 !strcmp(c->package, "") && !strcmp(c->name, "Class"));
274 classinfo_t* registry_getobjectclass() {
275 static classinfo_t*c = 0;
276 if(!c) c = (classinfo_t*)registry_safefind("", "Object");
279 classinfo_t* registry_getstringclass() {
280 static classinfo_t*c = 0;
281 if(!c) c = (classinfo_t*)registry_safefind("", "String");
284 classinfo_t* registry_getarrayclass() {
285 static classinfo_t*c = 0;
286 if(!c) c = (classinfo_t*)registry_safefind("", "Array");
289 classinfo_t* registry_getintclass() {
290 static classinfo_t*c = 0;
291 if(!c) c = (classinfo_t*)registry_safefind("", "int");
294 classinfo_t* registry_getuintclass() {
295 static classinfo_t*c = 0;
296 if(!c) c = (classinfo_t*)registry_safefind("", "uint");
299 classinfo_t* registry_getbooleanclass() {
300 static classinfo_t*c = 0;
301 if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
304 classinfo_t* registry_getnumberclass() {
305 static classinfo_t*c = 0;
306 if(!c) c = (classinfo_t*)registry_safefind("", "Number");
309 classinfo_t* registry_getregexpclass() {
310 static classinfo_t*c = 0;
311 if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
314 classinfo_t* registry_getMovieClip() {
315 static classinfo_t*c = 0;
316 if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
320 // ----------------------- builtin dummy types -------------------------
321 classinfo_t nullclass = {
322 INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
324 classinfo_t* registry_getnullclass() {
328 namespace_t access2namespace(U8 access, char*package)
336 char* infotypename(slotinfo_t*s)
338 if(s->kind == INFOTYPE_CLASS) return "class";
339 else if(s->kind == INFOTYPE_SLOT) return "member";
340 else if(s->kind == INFOTYPE_METHOD) return "method";
341 else return "object";