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