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);
51 static unsigned int memberinfo_hash(slotinfo_t*c)
53 unsigned int hash = 0;
54 hash = crc32_add_string(hash, c->name);
58 static void* dummy_clone(void*other) {return other;}
59 static void dummy_destroy(slotinfo_t*c) {}
61 type_t slotinfo_type = {
62 hash: (hash_func)slotinfo_hash,
63 equals: (equals_func)slotinfo_equals,
64 dup: (dup_func)dummy_clone, // all signatures are static
65 free: (free_func)dummy_destroy,
67 type_t memberinfo_type = {
68 hash: (hash_func)memberinfo_hash,
69 equals: (equals_func)slotinfo_equals,
70 dup: (dup_func)dummy_clone, // all signatures are static
71 free: (free_func)dummy_destroy,
74 // ----------------------- resolving ----------------------------------
75 slotinfo_t* registry_resolve(slotinfo_t*_s)
77 if(!_s || _s->kind != INFOTYPE_UNRESOLVED)
79 unresolvedinfo_t*s = (unresolvedinfo_t*)_s;
82 return registry_find(s->package, s->name);
84 namespace_list_t*l = s->nsset;
86 slotinfo_t* n = registry_find(l->namespace->name, s->name);
93 static slotinfo_list_t*unresolved = 0;
94 static void schedule_for_resolve(slotinfo_t*s)
96 list_append(unresolved, s);
98 static void resolve_on_slot(slotinfo_t*_member)
100 if(_member->kind == INFOTYPE_SLOT) {
101 varinfo_t*member = (varinfo_t*)_member;
102 member->type = (classinfo_t*)registry_resolve((slotinfo_t*)member->type);
103 } else if(_member->kind == INFOTYPE_METHOD) {
104 methodinfo_t*member = (methodinfo_t*)_member;
105 member->return_type = (classinfo_t*)registry_resolve((slotinfo_t*)member->return_type);
106 classinfo_list_t*l = member->params;
108 l->classinfo = (classinfo_t*)registry_resolve((slotinfo_t*)l->classinfo);
111 } else fprintf(stderr, "Internal Error: bad slot %s", _member->name);
113 static void resolve_on_class(slotinfo_t*_cls)
115 classinfo_t*cls = (classinfo_t*)_cls;
116 cls->superclass = (classinfo_t*)registry_resolve((slotinfo_t*)cls->superclass);
117 DICT_ITERATE_DATA(&cls->members,slotinfo_t*,m) {
121 while(cls->interfaces[t]) {
122 cls->interfaces[t] = (classinfo_t*)registry_resolve((slotinfo_t*)cls->interfaces[t]);
126 void registry_resolve_all()
129 slotinfo_t*_s = unresolved->slotinfo;
130 if(_s->kind == INFOTYPE_CLASS) {
131 resolve_on_class(_s);
132 } else if(_s->kind == INFOTYPE_METHOD || _s->kind == INFOTYPE_SLOT) {
135 fprintf(stderr, "Internal Error: object %s.%s has bad type\n", _s->package, _s->name);
137 slotinfo_list_t*tofree = unresolved;
138 unresolved = unresolved->next;
142 // ------------------------- constructors --------------------------------
144 #define AVERAGE_NUMBER_OF_MEMBERS 8
145 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
147 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
148 c->interfaces[0] = 0;
149 c->kind = INFOTYPE_CLASS;
151 c->package = package;
153 dict_put(registry_classes, c, c);
154 dict_init2(&c->members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
156 schedule_for_resolve((slotinfo_t*)c);
159 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
162 m->kind = INFOTYPE_METHOD;
167 dict_put(&cls->members, m, m);
170 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
173 m->kind = INFOTYPE_SLOT;
178 dict_put(&cls->members, m, m);
181 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
183 NEW(methodinfo_t, m);
184 m->kind = INFOTYPE_METHOD;
185 m->flags = FLAG_STATIC;
187 m->package = package;
190 dict_put(registry_classes, m, m);
192 schedule_for_resolve((slotinfo_t*)m);
195 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
198 m->kind = INFOTYPE_SLOT;
199 m->flags = FLAG_STATIC;
201 m->package = package;
204 dict_put(registry_classes, m, m);
206 schedule_for_resolve((slotinfo_t*)m);
210 // --------------- builtin classes (from builtin.c) ----------------------
214 if(!registry_classes)
215 registry_classes = builtin_getclasses();
217 slotinfo_t* registry_find(const char*package, const char*name)
219 assert(registry_classes);
221 tmp.package = package;
223 slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
225 printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
228 slotinfo_t* registry_safefind(const char*package, const char*name)
230 slotinfo_t*c = registry_find(package, name);
232 printf("%s.%s\n", package, name);
240 for(t=0;t<registry_classes->hashsize;t++) {
241 dictentry_t*e = registry_classes->slots[t];
243 slotinfo_t*i = (slotinfo_t*)e->key;
244 printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
250 memberinfo_t* registry_findmember(classinfo_t*cls, const char*ns, const char*name, char recursive)
254 tmp.package = ns?ns:"";
257 return (memberinfo_t*)dict_lookup(&cls->members, &tmp);
259 /* look at classes directly extended by this class */
263 if(recursive>1) // check *only* superclasses
267 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
269 return (memberinfo_t*)m;
273 /* look at interfaces, and parent interfaces */
275 while(cls->interfaces[t]) {
276 classinfo_t*s = cls->interfaces[t];
277 if(s->kind != INFOTYPE_UNRESOLVED) {
279 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
281 return (memberinfo_t*)m;
291 memberinfo_t* registry_findmember_nsset(classinfo_t*cls, namespace_list_t*ns, const char*name, char superclasses)
294 memberinfo_t*m = registry_findmember(cls, ns->namespace->name, name, superclasses);
298 return registry_findmember(cls, "", name, superclasses);
302 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
306 m->ns->access = c->access;
307 m->ns->name = (char*)c->package;
309 m->namespace_set = 0;
311 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
316 namespace_t ns = {cls->access, (char*)cls->package};
317 return multiname_new(&ns,cls->name);
320 // ----------------------- memberinfo methods ------------------------------
322 /* hacky code to wrap a variable or function into a "type"
323 object, but keep a pointer to the "value" */
324 static dict_t* functionobjects = 0;
325 classinfo_t* slotinfo_asclass(slotinfo_t*f) {
326 if(!functionobjects) {
327 functionobjects = dict_new2(&ptr_type);
329 classinfo_t*c = dict_lookup(functionobjects, f);
334 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
335 c->access = ACCESS_PUBLIC;
337 if(f->kind == INFOTYPE_METHOD)
338 c->name = "Function";
339 else if(f->kind == INFOTYPE_CLASS)
341 else if(f->kind == INFOTYPE_SLOT)
344 c->name = "undefined";
347 dict_init2(&c->members, &memberinfo_type, 1);
349 dict_put(functionobjects, f, c);
353 classinfo_t* slotinfo_gettype(slotinfo_t*f)
356 if(f->kind == INFOTYPE_METHOD) {
357 return slotinfo_asclass(f);
358 } else if(f->kind == INFOTYPE_SLOT) {
359 varinfo_t*v = (varinfo_t*)f;
364 return registry_getanytype();
368 // ----------------------- package handling ---------------------------
369 char registry_ispackage(char*package)
371 /* crude approximation of "the real thing", but sufficient for now */
372 return !strncmp(package, "flash", 5);
374 // ----------------------- builtin types ------------------------------
375 classinfo_t* registry_getanytype() {return 0;}
377 char registry_isfunctionclass(classinfo_t*c) {
378 return (c && c->package && c->name &&
379 !strcmp(c->package, "") && !strcmp(c->name, "Function"));
381 char registry_isclassclass(classinfo_t*c) {
382 return (c && c->package && c->name &&
383 !strcmp(c->package, "") && !strcmp(c->name, "Class"));
386 classinfo_t* registry_getobjectclass() {
387 static classinfo_t*c = 0;
388 if(!c) c = (classinfo_t*)registry_safefind("", "Object");
391 classinfo_t* registry_getstringclass() {
392 static classinfo_t*c = 0;
393 if(!c) c = (classinfo_t*)registry_safefind("", "String");
396 classinfo_t* registry_getarrayclass() {
397 static classinfo_t*c = 0;
398 if(!c) c = (classinfo_t*)registry_safefind("", "Array");
401 classinfo_t* registry_getintclass() {
402 static classinfo_t*c = 0;
403 if(!c) c = (classinfo_t*)registry_safefind("", "int");
406 classinfo_t* registry_getuintclass() {
407 static classinfo_t*c = 0;
408 if(!c) c = (classinfo_t*)registry_safefind("", "uint");
411 classinfo_t* registry_getbooleanclass() {
412 static classinfo_t*c = 0;
413 if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
416 classinfo_t* registry_getnumberclass() {
417 static classinfo_t*c = 0;
418 if(!c) c = (classinfo_t*)registry_safefind("", "Number");
421 classinfo_t* registry_getregexpclass() {
422 static classinfo_t*c = 0;
423 if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
426 classinfo_t* registry_getnamespaceclass() {
427 static classinfo_t*c = 0;
428 if(!c) c = (classinfo_t*)registry_safefind("", "Namespace");
431 classinfo_t* registry_getMovieClip() {
432 static classinfo_t*c = 0;
433 if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
437 // ----------------------- builtin dummy types -------------------------
438 classinfo_t nullclass = {
439 INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
441 classinfo_t* registry_getnullclass() {
445 namespace_t access2namespace(U8 access, char*package)
453 char* infotypename(slotinfo_t*s)
455 if(s->kind == INFOTYPE_CLASS) return "class";
456 else if(s->kind == INFOTYPE_SLOT) return "member";
457 else if(s->kind == INFOTYPE_METHOD) return "method";
458 else return "object";