more bugfixes
[swftools.git] / lib / as3 / registry.c
1 /* registry.c
2
3    Routines for compiling Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
9  
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.
14
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.
19
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 */
23
24 #include <assert.h>
25 #include "pool.h"
26 #include "registry.h"
27 #include "builtin.h"
28
29 dict_t*registry_classes=0;
30 asset_bundle_list_t*assets=0;
31
32 // ----------------------- class signature ------------------------------
33
34 char slotinfo_equals(slotinfo_t*c1, slotinfo_t*c2)
35 {
36     if(!!c1 != !!c2)
37         return 0;
38     /* notice: access right is *not* respected */
39     if(!strcmp(c1->name, c2->name) &&
40        !strcmp(c1->package, c2->package)) {
41         return 1;
42     }
43     return 0;
44 }
45 static unsigned int slotinfo_hash(slotinfo_t*c)
46 {
47     unsigned int hash = 0;
48     hash = crc32_add_string(hash, c->package);
49     hash = crc32_add_string(hash, c->name);
50     return hash;
51 }
52 static unsigned int memberinfo_hash(slotinfo_t*c)
53 {
54     unsigned int hash = 0;
55     hash = crc32_add_string(hash, c->name);
56     return hash;
57 }
58
59 static void* dummy_clone(void*other) {return other;}
60 static void dummy_destroy(slotinfo_t*c) {}
61
62 type_t slotinfo_type = {
63     hash: (hash_func)slotinfo_hash,
64     equals: (equals_func)slotinfo_equals,
65     dup: (dup_func)dummy_clone, // all signatures are static
66     free: (free_func)dummy_destroy,
67 };
68 type_t memberinfo_type = {
69     hash: (hash_func)memberinfo_hash,
70     equals: (equals_func)slotinfo_equals,
71     dup: (dup_func)dummy_clone, // all signatures are static
72     free: (free_func)dummy_destroy,
73 };
74
75 // ----------------------- assets -------------------------------------
76 void registry_use(slotinfo_t*s)
77 {
78     if(!s) return;
79     if(!(s->flags&FLAG_USED)) {
80         s->flags |= FLAG_USED;
81         if(s->kind == INFOTYPE_CLASS) {
82             classinfo_t*c=(classinfo_t*)s;
83             if(c->assets) c->assets->used = 1;
84             int t=0;
85             while(c->interfaces[t]) {
86                 registry_use((slotinfo_t*)c->interfaces[t]);
87                 t++;
88             }
89             while(c->superclass) {
90                 c = c->superclass;
91                 registry_use((slotinfo_t*)c);
92             }
93         } else if(s->kind == INFOTYPE_METHOD) {
94             methodinfo_t*m=(methodinfo_t*)s;
95             if(m->parent) {
96                 registry_use((slotinfo_t*)m->parent);
97             }
98         } else if(s->kind == INFOTYPE_VAR) {
99             varinfo_t*v=(varinfo_t*)s;
100             if(v->parent) {
101                 registry_use((slotinfo_t*)v->parent);
102             }
103         }
104     }
105 }
106 void registry_add_asset(asset_bundle_t*bundle)
107 {
108     list_append(assets, bundle);
109 }
110 asset_bundle_list_t*registry_getassets()
111 {
112     return assets;
113 }
114 // ----------------------- resolving ----------------------------------
115 slotinfo_t* registry_resolve(slotinfo_t*_s)
116 {
117     if(!_s || _s->kind != INFOTYPE_UNRESOLVED)
118         return _s;
119     unresolvedinfo_t*s = (unresolvedinfo_t*)_s;
120
121     if(s->package)
122         return registry_find(s->package, s->name);
123
124     namespace_list_t*l = s->nsset;
125     while(l) {
126         slotinfo_t* n = registry_find(l->namespace->name, s->name);
127         if(n) return n;
128         l = l->next;
129     }
130     return 0;
131 }
132
133 static slotinfo_list_t*unresolved = 0;
134 static void schedule_for_resolve(slotinfo_t*s)
135 {
136     list_append(unresolved, s);
137 }
138 static void resolve_on_slot(slotinfo_t*_member)
139 {
140     if(_member->kind == INFOTYPE_VAR) {
141         varinfo_t*member = (varinfo_t*)_member;
142         member->type = (classinfo_t*)registry_resolve((slotinfo_t*)member->type);
143     } else if(_member->kind == INFOTYPE_METHOD) {
144         methodinfo_t*member = (methodinfo_t*)_member;
145         member->return_type = (classinfo_t*)registry_resolve((slotinfo_t*)member->return_type);
146         classinfo_list_t*l = member->params;
147         while(l) {
148             l->classinfo = (classinfo_t*)registry_resolve((slotinfo_t*)l->classinfo);
149             l = l->next;
150         }
151     } else fprintf(stderr, "Internal Error: bad slot %s", _member->name);
152 }
153 static void resolve_on_class(slotinfo_t*_cls)
154 {
155     classinfo_t*cls = (classinfo_t*)_cls;
156     cls->superclass = (classinfo_t*)registry_resolve((slotinfo_t*)cls->superclass);
157         
158     DICT_ITERATE_DATA(&cls->members,slotinfo_t*,m) {
159         resolve_on_slot(m);
160     }
161     DICT_ITERATE_DATA(&cls->static_members,slotinfo_t*,m2) {
162         resolve_on_slot(m2);
163     }
164
165     int t=0;
166     while(cls->interfaces[t]) {
167         cls->interfaces[t] = (classinfo_t*)registry_resolve((slotinfo_t*)cls->interfaces[t]);
168         t++;
169     }
170 }
171 void registry_resolve_all()
172 {
173     while(unresolved) {
174         slotinfo_t*_s = unresolved->slotinfo;
175         if(_s->kind == INFOTYPE_CLASS) {
176             resolve_on_class(_s);
177         } else if(_s->kind == INFOTYPE_METHOD || _s->kind == INFOTYPE_VAR) {
178             resolve_on_slot(_s);
179         } else {
180             fprintf(stderr, "Internal Error: object %s.%s has bad type\n", _s->package, _s->name);
181         }
182         slotinfo_list_t*tofree = unresolved;
183         unresolved = unresolved->next;
184         free(tofree);
185     }
186 }
187 // ------------------------- constructors --------------------------------
188
189 #define AVERAGE_NUMBER_OF_MEMBERS 8
190 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
191 {
192     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
193     c->interfaces[0] = 0;
194     c->kind = INFOTYPE_CLASS;
195     c->access = access;
196     c->package = package;
197     c->name = name;
198     dict_put(registry_classes, c, c);
199     dict_init2(&c->members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
200     dict_init2(&c->static_members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
201
202     schedule_for_resolve((slotinfo_t*)c);
203     return c;
204 }
205 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name, char is_static)
206 {
207     NEW(methodinfo_t,m);
208     m->kind = INFOTYPE_METHOD;
209     m->access = access;
210     m->name = name;
211     m->package = ns;
212     m->parent = cls;
213     if(!is_static) 
214         dict_put(&cls->members, m, m);
215     else
216         dict_put(&cls->static_members, m, m);
217     return m;
218 }
219 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name, char is_static)
220 {
221     NEW(varinfo_t,m);
222     m->kind = INFOTYPE_VAR;
223     m->access = access;
224     m->name = name;
225     m->package = ns;
226     m->parent = cls;
227     if(!is_static) 
228         dict_put(&cls->members, m, m);
229     else
230         dict_put(&cls->static_members, m, m);
231     return m;
232 }
233 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
234 {
235     NEW(methodinfo_t, m);
236     m->kind = INFOTYPE_METHOD;
237     m->flags = FLAG_STATIC;
238     m->access = access;
239     m->package = package;
240     m->name = name;
241     m->parent = 0;
242     dict_put(registry_classes, m, m);
243     
244     schedule_for_resolve((slotinfo_t*)m);
245     return m;
246 }
247 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
248 {
249     NEW(varinfo_t, m);
250     m->kind = INFOTYPE_VAR;
251     m->flags = FLAG_STATIC;
252     m->access = access;
253     m->package = package;
254     m->name = name;
255     m->parent = 0;
256     dict_put(registry_classes, m, m);
257     
258     schedule_for_resolve((slotinfo_t*)m);
259     return m;
260 }
261
262 // --------------- builtin classes (from builtin.c) ----------------------
263
264 void registry_init()
265 {
266     if(!registry_classes)
267         registry_classes = builtin_getclasses();
268 }
269 slotinfo_t* registry_find(const char*package, const char*name)
270 {
271     assert(registry_classes);
272     slotinfo_t tmp;
273     tmp.package = package;
274     tmp.name = name;
275     slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
276     /*if(c)
277         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
278     return c;
279 }
280 slotinfo_t* registry_safefind(const char*package, const char*name)
281 {
282     slotinfo_t*c = registry_find(package, name);
283     if(!c) {
284         printf("%s.%s\n", package, name);
285     }
286     assert(c);
287     return c;
288 }
289 void registry_dump()
290 {
291     int t;
292     for(t=0;t<registry_classes->hashsize;t++) {
293         dictentry_t*e = registry_classes->slots[t];
294         while(e) {
295             slotinfo_t*i = (slotinfo_t*)e->key;
296             printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
297             e = e->next;
298         }
299     }
300 }
301
302 memberinfo_t* registry_findmember(classinfo_t*cls, const char*ns, const char*name, char recursive, char is_static)
303 {
304     memberinfo_t tmp;
305     tmp.name = name;
306     tmp.package = ns?ns:"";
307
308     if(!recursive) {
309         if(!is_static) 
310             return (memberinfo_t*)dict_lookup(&cls->members, &tmp);
311         else
312             return (memberinfo_t*)dict_lookup(&cls->static_members, &tmp);
313     }
314     /* look at classes directly extended by this class */
315     slotinfo_t*m = 0;
316     classinfo_t*s = cls;
317
318     if(recursive>1) // check *only* superclasses
319         s = s->superclass;
320
321     while(s) {
322         if(s->kind == INFOTYPE_UNRESOLVED)
323             break;
324
325         if(!is_static) {
326             m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
327             if(m) return (memberinfo_t*)m;
328         }
329         m = (slotinfo_t*)dict_lookup(&s->static_members, &tmp);
330         if(m) return (memberinfo_t*)m;
331
332         s = s->superclass;
333     }
334     /* look at interfaces, and parent interfaces */
335     int t=0;
336     while(cls->interfaces[t]) {
337         classinfo_t*s = cls->interfaces[t];
338         if(s->kind != INFOTYPE_UNRESOLVED) {
339             while(s) {
340                 if(!is_static) {
341                     m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
342                     if(m) return (memberinfo_t*)m;
343                 }
344                 m = (slotinfo_t*)dict_lookup(&s->static_members, &tmp);
345                 if(m) return (memberinfo_t*)m;
346
347                 s = s->superclass;
348             }
349         }
350         t++;
351     }
352     return 0;
353 }
354
355 memberinfo_t* registry_findmember_nsset(classinfo_t*cls, namespace_list_t*ns, const char*name, char superclasses, char is_static)
356 {
357     memberinfo_t*m = 0;
358     while(ns) {
359         m = registry_findmember(cls, ns->namespace->name, name, superclasses, is_static);
360         if(m) return m;
361         ns = ns->next;
362     }
363     m = registry_findmember(cls, "", name, superclasses, is_static);
364     if(m) return m;
365     /* TODO: it maybe would be faster to just store the builtin namespace as "" in
366              builtins.c (update: some members (e.g. XML.length) are present both for
367             "" and "http:...builtin") */
368     m = registry_findmember(cls, "http://adobe.com/AS3/2006/builtin", name, superclasses, is_static);
369     if(m) return m;
370     return 0;
371 }
372
373
374 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
375 {
376     m->type = QNAME;
377     m->ns = n;
378     m->ns->access = c->access;
379     m->ns->name = (char*)c->package;
380     m->name = c->name;
381     m->namespace_set = 0;
382 }
383 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
384 {
385     if(!cls)
386         return 0;
387     multiname_t*m=0;
388     namespace_t ns = {cls->access, (char*)cls->package};
389     return multiname_new(&ns,cls->name);
390 }
391
392 // ----------------------- memberinfo methods ------------------------------
393
394 /* hacky code to wrap a variable or function into a "type"
395    object, but keep a pointer to the "value" */
396 static dict_t* functionobjects = 0;
397 classinfo_t* slotinfo_asclass(slotinfo_t*f) {
398     if(!functionobjects) {
399         functionobjects = dict_new2(&ptr_type);
400     } else {
401         classinfo_t*c = dict_lookup(functionobjects, f);
402         if(c)
403             return c;
404     }
405
406     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
407     c->access = ACCESS_PUBLIC;
408     c->package = "";
409     if(f->kind == INFOTYPE_METHOD) {
410         c->name = "Function";
411         c->superclass = registry_getobjectclass();
412     } else if(f->kind == INFOTYPE_CLASS) {
413         c->name = "Class";
414         c->superclass = registry_getobjectclass();
415     } else if(f->kind == INFOTYPE_VAR) {
416         c->name = "Object";
417     } else {
418         c->name = "undefined";
419     }
420     
421     dict_init2(&c->members, &memberinfo_type, 1);
422     dict_init2(&c->static_members, &memberinfo_type, 1);
423     c->data = f;
424     dict_put(functionobjects, f, c);
425     return c;
426 }
427
428 classinfo_t* slotinfo_gettype(slotinfo_t*f)
429 {
430     if(f) {
431        if(f->kind == INFOTYPE_METHOD) {
432            return slotinfo_asclass(f);
433        } else if(f->kind == INFOTYPE_VAR) {
434            varinfo_t*v = (varinfo_t*)f;
435            return v->type;
436        } else 
437            return 0;
438     } else {
439        return TYPE_ANY;
440     }
441 }
442
443 // ----------------------- package handling ---------------------------
444 char registry_ispackage(const char*package)
445 {
446     /* crude approximation of "the real thing", but sufficient for now */
447     return !strncmp(package, "flash", 5);
448 }
449 // ----------------------- builtin types ------------------------------
450
451 char registry_isfunctionclass(classinfo_t*c) {
452     return (c && c->package && c->name && 
453             !strcmp(c->package, "") && !strcmp(c->name, "Function"));
454 }
455 char registry_isclassclass(classinfo_t*c) {
456     return (c && c->package && c->name && 
457             !strcmp(c->package, "") && !strcmp(c->name, "Class"));
458 }
459
460 classinfo_t* registry_getobjectclass() {
461     static classinfo_t*c = 0;
462     if(!c) c = (classinfo_t*)registry_safefind("", "Object");
463     return c;
464 }
465 classinfo_t* registry_getstringclass() {
466     static classinfo_t*c = 0;
467     if(!c) c = (classinfo_t*)registry_safefind("", "String");
468     return c;
469 }
470 classinfo_t* registry_getarrayclass() {
471     static classinfo_t*c = 0;
472     if(!c) c = (classinfo_t*)registry_safefind("", "Array");
473     return c;
474 }
475 classinfo_t* registry_getintclass() {
476     static classinfo_t*c = 0;
477     if(!c) c = (classinfo_t*)registry_safefind("", "int");
478     return c;
479 }
480 classinfo_t* registry_getuintclass() {
481     static classinfo_t*c = 0;
482     if(!c) c = (classinfo_t*)registry_safefind("", "uint");
483     return c;
484 }
485 classinfo_t* registry_getbooleanclass() {
486     static classinfo_t*c = 0;
487     if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
488     return c;
489 }
490 classinfo_t* registry_getnumberclass() {
491     static classinfo_t*c = 0;
492     if(!c) c = (classinfo_t*)registry_safefind("", "Number");
493     return c;
494 }
495 classinfo_t* registry_getregexpclass() {
496     static classinfo_t*c = 0;
497     if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
498     return c;
499 }
500 classinfo_t* registry_getdateclass() {
501     static classinfo_t*c = 0;
502     if(!c) c = (classinfo_t*)registry_safefind("", "Date");
503     return c;
504 }
505 classinfo_t* registry_getxmlclass() {
506     static classinfo_t*c = 0;
507     if(!c) c = (classinfo_t*)registry_safefind("", "XML");
508     return c;
509 }
510 classinfo_t* registry_getxmllistclass() {
511     static classinfo_t*c = 0;
512     if(!c) c = (classinfo_t*)registry_safefind("", "XMLList");
513     return c;
514 }
515 classinfo_t* registry_getnamespaceclass() {
516     static classinfo_t*c = 0;
517     if(!c) c = (classinfo_t*)registry_safefind("", "Namespace");
518     return c;
519 }
520 classinfo_t* registry_getMovieClip() {
521     static classinfo_t*c = 0;
522     if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
523     return c;
524 }
525
526 // ----------------------- builtin dummy types -------------------------
527 classinfo_t nullclass = {
528     INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
529 };
530 classinfo_t* registry_getnullclass() {
531     return &nullclass;
532 }
533 classinfo_t voidclass = {
534     INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "void", 0, 0, 0
535 };
536 classinfo_t* registry_getvoidclass() {
537     return &voidclass;
538 }
539
540 namespace_t access2namespace(U8 access, char*package)
541 {
542     namespace_t ns;
543     ns.access = access;
544     ns.name = package;
545     return ns;
546 }
547
548 char* infotypename(slotinfo_t*s)
549 {
550     if(s->kind == INFOTYPE_CLASS) return "class";
551     else if(s->kind == INFOTYPE_VAR) return "var";
552     else if(s->kind == INFOTYPE_METHOD) return "function";
553     else return "object";
554 }
555
556 void slotinfo_dump(slotinfo_t*s)
557 {
558     if(s->package[0]) {
559         printf("%s %s.%s", infotypename(s), s->package, s->name);
560     } else {
561         printf("%s %s", infotypename(s), s->name);
562     }
563     if(s->kind == INFOTYPE_CLASS) {
564         classinfo_t*c = (classinfo_t*)s;
565     }
566     else if(s->kind == INFOTYPE_VAR) {
567         varinfo_t*v = (varinfo_t*)s;
568         printf(":%s", v->type?v->type->name:"*");
569         if(v->value)
570             printf("=%s", constant_tostring(v->value));
571         if(v->slot)
572             printf(" (slot:%d)", v->slot);
573     }
574     else if(s->kind == INFOTYPE_METHOD) {
575         methodinfo_t*m = (methodinfo_t*)s;
576     }
577     else {
578     }
579     printf("\n");
580 }
581