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 // ----------------------- resolving ----------------------------------
63 slotinfo_t* registry_resolve(slotinfo_t*_s)
65 if(!_s || _s->kind != INFOTYPE_UNRESOLVED)
67 unresolvedinfo_t*s = (unresolvedinfo_t*)_s;
70 return registry_find(s->package, s->name);
72 namespace_list_t*l = s->nsset;
74 slotinfo_t* n = registry_find(l->namespace->name, s->name);
81 static slotinfo_list_t*unresolved = 0;
82 static void schedule_for_resolve(slotinfo_t*s)
84 list_append(unresolved, s);
86 static void resolve_on_slot(slotinfo_t*_member)
88 if(_member->kind == INFOTYPE_SLOT) {
89 varinfo_t*member = (varinfo_t*)_member;
90 member->type = (classinfo_t*)registry_resolve((slotinfo_t*)member->type);
91 } else if(_member->kind == INFOTYPE_METHOD) {
92 methodinfo_t*member = (methodinfo_t*)_member;
93 member->return_type = (classinfo_t*)registry_resolve((slotinfo_t*)member->return_type);
94 classinfo_list_t*l = member->params;
96 l->classinfo = (classinfo_t*)registry_resolve((slotinfo_t*)l->classinfo);
99 } else fprintf(stderr, "Internal Error: bad slot %s", _member->name);
101 static void resolve_on_class(slotinfo_t*_cls)
103 classinfo_t*cls = (classinfo_t*)_cls;
104 cls->superclass = (classinfo_t*)registry_resolve((slotinfo_t*)cls->superclass);
105 DICT_ITERATE_DATA(&cls->members,slotinfo_t*,m) {
109 while(cls->interfaces[t]) {
110 cls->interfaces[t] = (classinfo_t*)registry_resolve((slotinfo_t*)cls->interfaces[t]);
114 void registry_resolve_all()
117 slotinfo_t*_s = unresolved->slotinfo;
118 if(_s->kind == INFOTYPE_CLASS) {
119 resolve_on_class(_s);
120 } else if(_s->kind == INFOTYPE_METHOD || _s->kind == INFOTYPE_SLOT) {
123 fprintf(stderr, "Internal Error: object %s.%s has bad type\n", _s->package, _s->name);
125 slotinfo_list_t*tofree = unresolved;
126 unresolved = unresolved->next;
130 // ------------------------- constructors --------------------------------
132 #define AVERAGE_NUMBER_OF_MEMBERS 8
133 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
135 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
136 c->interfaces[0] = 0;
137 c->kind = INFOTYPE_CLASS;
139 c->package = package;
141 dict_put(registry_classes, c, c);
142 dict_init2(&c->members, &slotinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
144 schedule_for_resolve((slotinfo_t*)c);
147 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
150 m->kind = INFOTYPE_METHOD;
155 dict_put(&cls->members, m, m);
158 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
161 m->kind = INFOTYPE_SLOT;
166 dict_put(&cls->members, m, m);
169 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
171 NEW(methodinfo_t, m);
172 m->kind = INFOTYPE_METHOD;
173 m->flags = FLAG_STATIC;
175 m->package = package;
178 dict_put(registry_classes, m, m);
180 schedule_for_resolve((slotinfo_t*)m);
183 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
186 m->kind = INFOTYPE_SLOT;
187 m->flags = FLAG_STATIC;
189 m->package = package;
192 dict_put(registry_classes, m, m);
194 schedule_for_resolve((slotinfo_t*)m);
198 // --------------- builtin classes (from builtin.c) ----------------------
202 if(!registry_classes)
203 registry_classes = builtin_getclasses();
205 slotinfo_t* registry_find(const char*package, const char*name)
207 assert(registry_classes);
209 tmp.package = package;
211 slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
213 printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
216 slotinfo_t* registry_safefind(const char*package, const char*name)
218 slotinfo_t*c = registry_find(package, name);
220 printf("%s.%s\n", package, name);
228 for(t=0;t<registry_classes->hashsize;t++) {
229 dictentry_t*e = registry_classes->slots[t];
231 slotinfo_t*i = (slotinfo_t*)e->key;
232 printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
238 memberinfo_t* registry_findmember(classinfo_t*cls, const char*ns, const char*name, char recursive)
242 tmp.package = ns?ns:"";
245 return (memberinfo_t*)dict_lookup(&cls->members, &tmp);
247 /* look at classes directly extended by this class */
251 if(recursive>1) // check *only* superclasses
255 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
257 return (memberinfo_t*)m;
261 /* look at interfaces, and parent interfaces */
263 while(cls->interfaces[t]) {
264 classinfo_t*s = cls->interfaces[t];
265 if(s->kind != INFOTYPE_UNRESOLVED) {
267 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
269 return (memberinfo_t*)m;
279 memberinfo_t* registry_findmember_nsset(classinfo_t*cls, namespace_list_t*ns, const char*name, char superclasses)
282 memberinfo_t*m = registry_findmember(cls, ns->namespace->name, name, superclasses);
286 return registry_findmember(cls, "", name, superclasses);
290 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
294 m->ns->access = c->access;
295 m->ns->name = (char*)c->package;
297 m->namespace_set = 0;
299 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
304 namespace_t ns = {cls->access, (char*)cls->package};
305 return multiname_new(&ns,cls->name);
308 // ----------------------- memberinfo methods ------------------------------
310 /* hacky code to wrap a variable or function into a "type"
311 object, but keep a pointer to the "value" */
312 static dict_t* functionobjects = 0;
313 classinfo_t* slotinfo_asclass(slotinfo_t*f) {
314 if(!functionobjects) {
315 functionobjects = dict_new2(&ptr_type);
317 classinfo_t*c = dict_lookup(functionobjects, f);
322 classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
323 c->access = ACCESS_PUBLIC;
325 if(f->kind == INFOTYPE_METHOD)
326 c->name = "Function";
327 else if(f->kind == INFOTYPE_CLASS)
329 else if(f->kind == INFOTYPE_SLOT)
332 c->name = "undefined";
335 dict_init2(&c->members, &slotinfo_type, 1);
337 dict_put(functionobjects, f, c);
341 classinfo_t* slotinfo_gettype(slotinfo_t*f)
344 if(f->kind == INFOTYPE_METHOD) {
345 return slotinfo_asclass(f);
346 } else if(f->kind == INFOTYPE_SLOT) {
347 varinfo_t*v = (varinfo_t*)f;
352 return registry_getanytype();
355 // ----------------------- builtin types ------------------------------
356 classinfo_t* registry_getanytype() {return 0;}
358 char registry_isfunctionclass(classinfo_t*c) {
359 return (c && c->package && c->name &&
360 !strcmp(c->package, "") && !strcmp(c->name, "Function"));
362 char registry_isclassclass(classinfo_t*c) {
363 return (c && c->package && c->name &&
364 !strcmp(c->package, "") && !strcmp(c->name, "Class"));
367 classinfo_t* registry_getobjectclass() {
368 static classinfo_t*c = 0;
369 if(!c) c = (classinfo_t*)registry_safefind("", "Object");
372 classinfo_t* registry_getstringclass() {
373 static classinfo_t*c = 0;
374 if(!c) c = (classinfo_t*)registry_safefind("", "String");
377 classinfo_t* registry_getarrayclass() {
378 static classinfo_t*c = 0;
379 if(!c) c = (classinfo_t*)registry_safefind("", "Array");
382 classinfo_t* registry_getintclass() {
383 static classinfo_t*c = 0;
384 if(!c) c = (classinfo_t*)registry_safefind("", "int");
387 classinfo_t* registry_getuintclass() {
388 static classinfo_t*c = 0;
389 if(!c) c = (classinfo_t*)registry_safefind("", "uint");
392 classinfo_t* registry_getbooleanclass() {
393 static classinfo_t*c = 0;
394 if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
397 classinfo_t* registry_getnumberclass() {
398 static classinfo_t*c = 0;
399 if(!c) c = (classinfo_t*)registry_safefind("", "Number");
402 classinfo_t* registry_getregexpclass() {
403 static classinfo_t*c = 0;
404 if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
407 classinfo_t* registry_getMovieClip() {
408 static classinfo_t*c = 0;
409 if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
413 // ----------------------- builtin dummy types -------------------------
414 classinfo_t nullclass = {
415 INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
417 classinfo_t* registry_getnullclass() {
421 namespace_t access2namespace(U8 access, char*package)
429 char* infotypename(slotinfo_t*s)
431 if(s->kind == INFOTYPE_CLASS) return "class";
432 else if(s->kind == INFOTYPE_SLOT) return "member";
433 else if(s->kind == INFOTYPE_METHOD) return "method";
434 else return "object";