implemented url resolving in namespaces
[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
52 static void* dummy_clone(void*other) {return other;}
53 static void dummy_destroy(slotinfo_t*c) {}
54
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,
60 };
61
62 // ----------------------- resolving ----------------------------------
63 slotinfo_t* registry_resolve(slotinfo_t*_s)
64 {
65     if(!_s || _s->kind != INFOTYPE_UNRESOLVED)
66         return _s;
67     unresolvedinfo_t*s = (unresolvedinfo_t*)_s;
68
69     if(s->package)
70         return registry_find(s->package, s->name);
71
72     namespace_list_t*l = s->nsset;
73     while(l) {
74         slotinfo_t* n = registry_find(l->namespace->name, s->name);
75         if(n) return n;
76         l = l->next;
77     }
78     return 0;
79 }
80
81 static slotinfo_list_t*unresolved = 0;
82 static void schedule_for_resolve(slotinfo_t*s)
83 {
84     list_append(unresolved, s);
85 }
86 static void resolve_on_slot(slotinfo_t*_member)
87 {
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;
95         while(l) {
96             l->classinfo = (classinfo_t*)registry_resolve((slotinfo_t*)l->classinfo);
97             l = l->next;
98         }
99     } else fprintf(stderr, "Internal Error: bad slot %s", _member->name);
100 }
101 static void resolve_on_class(slotinfo_t*_cls)
102 {
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) {
106         resolve_on_slot(m);
107     }
108     int t=0;
109     while(cls->interfaces[t]) {
110         cls->interfaces[t] = (classinfo_t*)registry_resolve((slotinfo_t*)cls->interfaces[t]);
111         t++;
112     }
113 }
114 void registry_resolve_all()
115 {
116     while(unresolved) {
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) {
121             resolve_on_slot(_s);
122         } else {
123             fprintf(stderr, "Internal Error: object %s.%s has bad type\n", _s->package, _s->name);
124         }
125         slotinfo_list_t*tofree = unresolved;
126         unresolved = unresolved->next;
127         free(tofree);
128     }
129 }
130 // ------------------------- constructors --------------------------------
131
132 #define AVERAGE_NUMBER_OF_MEMBERS 8
133 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
134 {
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;
138     c->access = access;
139     c->package = package;
140     c->name = name;
141     dict_put(registry_classes, c, c);
142     dict_init2(&c->members, &slotinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
143
144     schedule_for_resolve((slotinfo_t*)c);
145     return c;
146 }
147 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
148 {
149     NEW(methodinfo_t,m);
150     m->kind = INFOTYPE_METHOD;
151     m->access = access;
152     m->name = name;
153     m->package = ns;
154     m->parent = cls;
155     dict_put(&cls->members, m, m);
156     return m;
157 }
158 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name)
159 {
160     NEW(varinfo_t,m);
161     m->kind = INFOTYPE_SLOT;
162     m->access = access;
163     m->name = name;
164     m->package = ns;
165     m->parent = cls;
166     dict_put(&cls->members, m, m);
167     return m;
168 }
169 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
170 {
171     NEW(methodinfo_t, m);
172     m->kind = INFOTYPE_METHOD;
173     m->flags = FLAG_STATIC;
174     m->access = access;
175     m->package = package;
176     m->name = name;
177     m->parent = 0;
178     dict_put(registry_classes, m, m);
179     
180     schedule_for_resolve((slotinfo_t*)m);
181     return m;
182 }
183 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
184 {
185     NEW(varinfo_t, m);
186     m->kind = INFOTYPE_SLOT;
187     m->flags = FLAG_STATIC;
188     m->access = access;
189     m->package = package;
190     m->name = name;
191     m->parent = 0;
192     dict_put(registry_classes, m, m);
193     
194     schedule_for_resolve((slotinfo_t*)m);
195     return m;
196 }
197
198 // --------------- builtin classes (from builtin.c) ----------------------
199
200 void registry_init()
201 {
202     if(!registry_classes)
203         registry_classes = builtin_getclasses();
204 }
205 slotinfo_t* registry_find(const char*package, const char*name)
206 {
207     assert(registry_classes);
208     slotinfo_t tmp;
209     tmp.package = package;
210     tmp.name = name;
211     slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
212     /*if(c)
213         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
214     return c;
215 }
216 slotinfo_t* registry_safefind(const char*package, const char*name)
217 {
218     slotinfo_t*c = registry_find(package, name);
219     if(!c) {
220         printf("%s.%s\n", package, name);
221     }
222     assert(c);
223     return c;
224 }
225 void registry_dump()
226 {
227     int t;
228     for(t=0;t<registry_classes->hashsize;t++) {
229         dictentry_t*e = registry_classes->slots[t];
230         while(e) {
231             slotinfo_t*i = (slotinfo_t*)e->key;
232             printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
233             e = e->next;
234         }
235     }
236 }
237
238 memberinfo_t* registry_findmember(classinfo_t*cls, const char*ns, const char*name, char recursive)
239 {
240     memberinfo_t tmp;
241     tmp.name = name;
242     tmp.package = ns?ns:"";
243
244     if(!recursive) {
245         return (memberinfo_t*)dict_lookup(&cls->members, &tmp);
246     }
247     /* look at classes directly extended by this class */
248     slotinfo_t*m = 0;
249     classinfo_t*s = cls;
250
251     if(recursive>1) // check *only* superclasses
252         s = s->superclass;
253
254     while(s) {
255         m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
256         if(m) {
257             return (memberinfo_t*)m;
258         }
259         s = s->superclass;
260     }
261     /* look at interfaces, and parent interfaces */
262     int t=0;
263     while(cls->interfaces[t]) {
264         classinfo_t*s = cls->interfaces[t];
265         if(s->kind != INFOTYPE_UNRESOLVED) {
266             while(s) {
267                 m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
268                 if(m) {
269                     return (memberinfo_t*)m;
270                 }
271                 s = s->superclass;
272             }
273         }
274         t++;
275     }
276     return 0;
277 }
278
279 memberinfo_t* registry_findmember_nsset(classinfo_t*cls, namespace_list_t*ns, const char*name, char superclasses)
280 {
281     while(ns) {
282         memberinfo_t*m = registry_findmember(cls, ns->namespace->name, name, superclasses);
283         if(m) return m;
284         ns = ns->next;
285     }
286     return registry_findmember(cls, "", name, superclasses);
287 }
288
289
290 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
291 {
292     m->type = QNAME;
293     m->ns = n;
294     m->ns->access = c->access;
295     m->ns->name = (char*)c->package;
296     m->name = c->name;
297     m->namespace_set = 0;
298 }
299 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
300 {
301     if(!cls)
302         return 0;
303     multiname_t*m=0;
304     namespace_t ns = {cls->access, (char*)cls->package};
305     return multiname_new(&ns,cls->name);
306 }
307
308 // ----------------------- memberinfo methods ------------------------------
309
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);
316     } else {
317         classinfo_t*c = dict_lookup(functionobjects, f);
318         if(c)
319             return c;
320     }
321
322     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
323     c->access = ACCESS_PUBLIC;
324     c->package = "";
325     if(f->kind == INFOTYPE_METHOD)
326         c->name = "Function";
327     else if(f->kind == INFOTYPE_CLASS)
328         c->name = "Class";
329     else if(f->kind == INFOTYPE_SLOT)
330         c->name = "Object";
331     else {
332         c->name = "undefined";
333     }
334     
335     dict_init2(&c->members, &slotinfo_type, 1);
336     c->data = f;
337     dict_put(functionobjects, f, c);
338     return c;
339 }
340
341 classinfo_t* slotinfo_gettype(slotinfo_t*f)
342 {
343     if(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;
348            return v->type;
349        } else 
350            return 0;
351     } else {
352        return registry_getanytype();
353     }
354 }
355 // ----------------------- builtin types ------------------------------
356 classinfo_t* registry_getanytype() {return 0;}
357
358 char registry_isfunctionclass(classinfo_t*c) {
359     return (c && c->package && c->name && 
360             !strcmp(c->package, "") && !strcmp(c->name, "Function"));
361 }
362 char registry_isclassclass(classinfo_t*c) {
363     return (c && c->package && c->name && 
364             !strcmp(c->package, "") && !strcmp(c->name, "Class"));
365 }
366
367 classinfo_t* registry_getobjectclass() {
368     static classinfo_t*c = 0;
369     if(!c) c = (classinfo_t*)registry_safefind("", "Object");
370     return c;
371 }
372 classinfo_t* registry_getstringclass() {
373     static classinfo_t*c = 0;
374     if(!c) c = (classinfo_t*)registry_safefind("", "String");
375     return c;
376 }
377 classinfo_t* registry_getarrayclass() {
378     static classinfo_t*c = 0;
379     if(!c) c = (classinfo_t*)registry_safefind("", "Array");
380     return c;
381 }
382 classinfo_t* registry_getintclass() {
383     static classinfo_t*c = 0;
384     if(!c) c = (classinfo_t*)registry_safefind("", "int");
385     return c;
386 }
387 classinfo_t* registry_getuintclass() {
388     static classinfo_t*c = 0;
389     if(!c) c = (classinfo_t*)registry_safefind("", "uint");
390     return c;
391 }
392 classinfo_t* registry_getbooleanclass() {
393     static classinfo_t*c = 0;
394     if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
395     return c;
396 }
397 classinfo_t* registry_getnumberclass() {
398     static classinfo_t*c = 0;
399     if(!c) c = (classinfo_t*)registry_safefind("", "Number");
400     return c;
401 }
402 classinfo_t* registry_getregexpclass() {
403     static classinfo_t*c = 0;
404     if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
405     return c;
406 }
407 classinfo_t* registry_getnamespaceclass() {
408     static classinfo_t*c = 0;
409     if(!c) c = (classinfo_t*)registry_safefind("", "Namespace");
410     return c;
411 }
412 classinfo_t* registry_getMovieClip() {
413     static classinfo_t*c = 0;
414     if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
415     return c;
416 }
417
418 // ----------------------- builtin dummy types -------------------------
419 classinfo_t nullclass = {
420     INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
421 };
422 classinfo_t* registry_getnullclass() {
423     return &nullclass;
424 }
425
426 namespace_t access2namespace(U8 access, char*package)
427 {
428     namespace_t ns;
429     ns.access = access;
430     ns.name = package;
431     return ns;
432 }
433
434 char* infotypename(slotinfo_t*s)
435 {
436     if(s->kind == INFOTYPE_CLASS) return "class";
437     else if(s->kind == INFOTYPE_SLOT) return "member";
438     else if(s->kind == INFOTYPE_METHOD) return "method";
439     else return "object";
440 }
441