added class type
[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, char*package, 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 /* function and class pointers get their own type class */
97 static dict_t* functionobjects = 0;
98 classinfo_t* registry_getfunctionclass(memberinfo_t*f) {
99     if(!functionobjects) {
100         functionobjects = dict_new2(&ptr_type);
101     } else {
102         classinfo_t*c = dict_lookup(functionobjects, f);
103         if(c)
104             return c;
105     }
106
107     NEW(classinfo_t,c);
108     c->access = ACCESS_PUBLIC;
109     c->package = "";
110     c->name = "Function";
111     dict_init(&c->members,1);
112     dict_put(&c->members, "call", f);
113
114     dict_put(functionobjects, f, c);
115     return c;
116 }
117 static dict_t* classobjects = 0;
118 classinfo_t* registry_getclassclass(classinfo_t*a) {
119     if(!classobjects) {
120         classobjects = dict_new2(&ptr_type);
121     } else {
122         classinfo_t*c = dict_lookup(classobjects, a);
123         if(c)
124             return c;
125     }
126
127     NEW(classinfo_t,c);
128     c->access = ACCESS_PUBLIC;
129     c->package = "";
130     c->name = "Class";
131     
132     NEW(memberinfo_t,m);
133     m->kind = MEMBER_SLOT;
134     m->name = "prototype";
135     m->type = a;
136
137     dict_init(&c->members,1);
138     dict_put(&c->members, "prototype", m);
139
140     dict_put(classobjects, a, c);
141     return c;
142 }
143
144 memberinfo_t* memberinfo_register(classinfo_t*cls, const char*name, U8 kind)
145 {
146     NEW(memberinfo_t,m);
147     m->kind = kind;
148     m->name = strdup(name);
149     dict_put(&cls->members, name, m);
150     return m;
151 }
152
153 // --------------- builtin classes (from builtin.c) ----------------------
154
155 void registry_init()
156 {
157     classes = builtin_getclasses();
158 }
159 classinfo_t* registry_safefindclass(const char*package, const char*name)
160 {
161     classinfo_t*c = registry_findclass(package, name);
162     assert(c);
163     return c;
164 }
165 classinfo_t* registry_findclass(const char*package, const char*name)
166 {
167     assert(classes);
168     classinfo_t tmp;
169     tmp.package = package;
170     tmp.name = name;
171     classinfo_t* c = (classinfo_t*)dict_lookup(classes, &tmp);
172     /*if(c)
173         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
174     return c;
175 }
176 memberinfo_t* registry_findmember(classinfo_t*cls, const char*name)
177 {
178     return (memberinfo_t*)dict_lookup(&cls->members, name);
179 }
180 void registry_fill_multiname(multiname_t*m, namespace_t*n, classinfo_t*c)
181 {
182     m->type = QNAME;
183     m->ns = n;
184     m->ns->access = c->access;
185     m->ns->name = (char*)c->package;
186     m->name = c->name;
187     m->namespace_set = 0;
188 }
189 multiname_t* classinfo_to_multiname(classinfo_t*cls)
190 {
191     if(!cls)
192         return 0;
193     multiname_t*m=0;
194     namespace_t*ns = namespace_new(cls->access, cls->package);
195     return multiname_new(ns,cls->name);
196 }
197
198 // ----------------------- builtin types ------------------------------
199 classinfo_t* registry_getanytype() {return 0;}
200
201 char registry_isfunctionclass(classinfo_t*c) {
202     return (c && c->package && c->name && 
203             !strcmp(c->package, "") && !strcmp(c->name, "Function"));
204 }
205 char registry_isclassclass(classinfo_t*c) {
206     return (c && c->package && c->name && 
207             !strcmp(c->package, "") && !strcmp(c->name, "Class"));
208 }
209
210 classinfo_t* registry_getobjectclass() {
211     static classinfo_t*c = 0;
212     if(!c) c = registry_safefindclass("", "Object");
213     return c;
214 }
215 classinfo_t* registry_getstringclass() {
216     static classinfo_t*c = 0;
217     if(!c) c = registry_safefindclass("", "String");
218     return c;
219 }
220 classinfo_t* registry_getintclass() {
221     static classinfo_t*c = 0;
222     if(!c) c = registry_safefindclass("", "int");
223     return c;
224 }
225 classinfo_t* registry_getuintclass() {
226     static classinfo_t*c = 0;
227     if(!c) c = registry_safefindclass("", "uint");
228     return c;
229 }
230 classinfo_t* registry_getbooleanclass() {
231     static classinfo_t*c = 0;
232     if(!c) c = registry_safefindclass("", "Boolean");
233     return c;
234 }
235 classinfo_t* registry_getnumberclass() {
236     static classinfo_t*c = 0;
237     if(!c) c = registry_safefindclass("", "Number");
238     return c;
239 }
240 classinfo_t* registry_getMovieClip() {
241     static classinfo_t*c = 0;
242     if(!c) c = registry_safefindclass("flash.display", "MovieClip");
243     return c;
244 }
245
246 // ----------------------- builtin dummy types -------------------------
247 classinfo_t nullclass = {
248     ACCESS_PACKAGE, 0, "", "null", 0, 0, 0,
249 };
250 classinfo_t* registry_getnullclass() {
251     return &nullclass;
252 }
253