new function memberinfo_register_global
[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 static dict_t*classes=0;
30
31 // ----------------------- class signature ------------------------------
32
33 char classinfo_equals(classinfo_t*c1, classinfo_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 classinfo_hash(classinfo_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(classinfo_t*c) {}
54
55 type_t classinfo_type = {
56     hash: (hash_func)classinfo_hash,
57     equals: (equals_func)classinfo_equals,
58     /* all signatures are static */
59     dup: (dup_func)dummy_clone,
60     free: (free_func)dummy_destroy,
61 };
62
63 // ----------------------- function signature ------------------------------
64
65 static char memberinfo_equals(memberinfo_t*f1, memberinfo_t*f2)
66 {
67     return !strcmp(f1->name, f2->name);
68 }
69 static unsigned int memberinfo_hash(memberinfo_t*f)
70 {
71     return crc32_add_string(0, f->name);
72 }
73 type_t memberinfo_type = {
74     hash: (hash_func)memberinfo_hash,
75     equals: (equals_func)memberinfo_equals,
76     /* all signatures are static */
77     dup: (dup_func)dummy_clone,
78     free: (free_func)dummy_destroy,
79 };
80
81 // ------------------------- constructors --------------------------------
82
83 #define AVERAGE_NUMBER_OF_MEMBERS 8
84 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
85 {
86     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
87     c->interfaces[0] = 0;
88     c->access = access;
89     c->package = package;
90     c->name = name;
91     dict_put(classes, c, c);
92     dict_init(&c->members,AVERAGE_NUMBER_OF_MEMBERS);
93     return c;
94 }
95
96 static dict_t* classobjects = 0;
97 classinfo_t* registry_getclassclass(classinfo_t*a) {
98     if(!classobjects) {
99         classobjects = dict_new2(&ptr_type);
100     } else {
101         classinfo_t*c = dict_lookup(classobjects, a);
102         if(c)
103             return c;
104     }
105
106     NEW(classinfo_t,c);
107     c->access = ACCESS_PUBLIC;
108     c->package = "";
109     c->name = "Class";
110     dict_init(&c->members,1);
111     c->cls = a;
112
113     dict_put(classobjects, a, c);
114     return c;
115 }
116
117 memberinfo_t* memberinfo_register(classinfo_t*cls, const char*name, U8 kind)
118 {
119     NEW(memberinfo_t,m);
120     m->kind = kind;
121     m->name = strdup(name);
122     m->parent = cls;
123     dict_put(&cls->members, name, m);
124     return m;
125 }
126 memberinfo_t* memberinfo_register_global(U8 access, const char*package, const char*name, U8 kind)
127 {
128     NEW(memberinfo_t, m);
129     m->kind = kind;
130     m->flags = FLAG_STATIC;
131     m->name = name;
132     m->parent = 0;
133
134     classinfo_t*c = classinfo_register(access, package, name, 0);
135     c->function = m;
136     c->flags |= FLAG_METHOD;
137     return m;
138 }
139
140
141 // --------------- builtin classes (from builtin.c) ----------------------
142
143 void registry_init()
144 {
145     classes = builtin_getclasses();
146 }
147 classinfo_t* registry_safefindclass(const char*package, const char*name)
148 {
149     classinfo_t*c = registry_findclass(package, name);
150     assert(c);
151     return c;
152 }
153 classinfo_t* registry_findclass(const char*package, const char*name)
154 {
155     assert(classes);
156     classinfo_t tmp;
157     tmp.package = package;
158     tmp.name = name;
159     classinfo_t* c = (classinfo_t*)dict_lookup(classes, &tmp);
160     /*if(c)
161         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
162     return c;
163 }
164 memberinfo_t* registry_findmember(classinfo_t*cls, const char*name, char recursive)
165 {
166     if(!recursive) {
167         return (memberinfo_t*)dict_lookup(&cls->members, name);
168     }
169     /* look at classes directly extended by this class */
170     memberinfo_t*m = 0;
171     classinfo_t*s = cls;
172     while(s) {
173         m = (memberinfo_t*)dict_lookup(&s->members, name);
174         if(m) return m;
175         s = s->superclass;
176     }
177     /* look at interfaces, and parent interfaces */
178     int t=0;
179     while(cls->interfaces[t]) {
180         classinfo_t*s = cls->interfaces[t];
181         while(s) {
182             m = (memberinfo_t*)dict_lookup(&s->members, name);
183             if(m) return m;
184             s = s->superclass;
185         }
186         t++;
187     }
188     return 0;
189
190 }
191 void registry_fill_multiname(multiname_t*m, namespace_t*n, classinfo_t*c)
192 {
193     m->type = QNAME;
194     m->ns = n;
195     m->ns->access = c->access;
196     m->ns->name = (char*)c->package;
197     m->name = c->name;
198     m->namespace_set = 0;
199 }
200 multiname_t* classinfo_to_multiname(classinfo_t*cls)
201 {
202     if(!cls)
203         return 0;
204     multiname_t*m=0;
205     namespace_t ns = {cls->access, (char*)cls->package};
206     return multiname_new(&ns,cls->name);
207 }
208
209 // ----------------------- memberinfo methods ------------------------------
210
211 /* function and class pointers get their own type class */
212 static dict_t* functionobjects = 0;
213 classinfo_t* memberinfo_asclass(memberinfo_t*f) {
214     if(!functionobjects) {
215         functionobjects = dict_new2(&ptr_type);
216     } else {
217         classinfo_t*c = dict_lookup(functionobjects, f);
218         if(c)
219             return c;
220     }
221
222     NEW(classinfo_t,c);
223     c->access = ACCESS_PUBLIC;
224     c->package = "";
225     c->name = "Function";
226     
227     dict_init(&c->members,1);
228     c->function = f;
229
230     dict_put(functionobjects, f, c);
231     return c;
232 }
233
234 classinfo_t* memberinfo_gettype(memberinfo_t*f)
235 {
236     if(f) {
237        if(f->kind == MEMBER_METHOD) {
238            return memberinfo_asclass(f);
239        } else {
240            return f->type;
241        }
242     } else {
243        return registry_getanytype();
244     }
245 }
246 // ----------------------- builtin types ------------------------------
247 classinfo_t* registry_getanytype() {return 0;}
248
249 char registry_isfunctionclass(classinfo_t*c) {
250     return (c && c->package && c->name && 
251             !strcmp(c->package, "") && !strcmp(c->name, "Function"));
252 }
253 char registry_isclassclass(classinfo_t*c) {
254     return (c && c->package && c->name && 
255             !strcmp(c->package, "") && !strcmp(c->name, "Class"));
256 }
257
258 classinfo_t* registry_getobjectclass() {
259     static classinfo_t*c = 0;
260     if(!c) c = registry_safefindclass("", "Object");
261     return c;
262 }
263 classinfo_t* registry_getstringclass() {
264     static classinfo_t*c = 0;
265     if(!c) c = registry_safefindclass("", "String");
266     return c;
267 }
268 classinfo_t* registry_getarrayclass() {
269     static classinfo_t*c = 0;
270     if(!c) c = registry_safefindclass("", "Array");
271     return c;
272 }
273 classinfo_t* registry_getintclass() {
274     static classinfo_t*c = 0;
275     if(!c) c = registry_safefindclass("", "int");
276     return c;
277 }
278 classinfo_t* registry_getuintclass() {
279     static classinfo_t*c = 0;
280     if(!c) c = registry_safefindclass("", "uint");
281     return c;
282 }
283 classinfo_t* registry_getbooleanclass() {
284     static classinfo_t*c = 0;
285     if(!c) c = registry_safefindclass("", "Boolean");
286     return c;
287 }
288 classinfo_t* registry_getnumberclass() {
289     static classinfo_t*c = 0;
290     if(!c) c = registry_safefindclass("", "Number");
291     return c;
292 }
293 classinfo_t* registry_getMovieClip() {
294     static classinfo_t*c = 0;
295     if(!c) c = registry_safefindclass("flash.display", "MovieClip");
296     return c;
297 }
298
299 // ----------------------- builtin dummy types -------------------------
300 classinfo_t nullclass = {
301     ACCESS_PACKAGE, 0, "", "null", 0, 0, 0,
302 };
303 classinfo_t* registry_getnullclass() {
304     return &nullclass;
305 }
306