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