renamed class_signature to classinfo
[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 classinfo_t* classinfo_register(int access, char*package, char*name)
84 {
85     NEW(classinfo_t,c);
86     c->access = access;
87     c->package = package;
88     c->name = name;
89     dict_put(classes, c, c);
90     return c;
91 }
92
93 // --------------- builtin classes (from builtin.c) ----------------------
94
95 void registry_init()
96 {
97     classes = builtin_getclasses();
98 }
99 classinfo_t* registry_safefindclass(const char*package, const char*name)
100 {
101     classinfo_t*c = registry_findclass(package, name);
102     assert(c);
103     return c;
104 }
105 classinfo_t* registry_findclass(const char*package, const char*name)
106 {
107     assert(classes);
108     classinfo_t tmp;
109     tmp.package = package;
110     tmp.name = name;
111     classinfo_t* c = (classinfo_t*)dict_lookup(classes, &tmp);
112     /*if(c)
113         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
114     return c;
115 }
116 void registry_fill_multiname(multiname_t*m, namespace_t*n, classinfo_t*c)
117 {
118     m->type = QNAME;
119     m->ns = n;
120     m->ns->access = c->access;
121     m->ns->name = (char*)c->package;
122     m->name = c->name;
123     m->namespace_set = 0;
124 }
125 multiname_t* classinfo_to_multiname(classinfo_t*cls)
126 {
127     if(!cls)
128         return 0;
129     multiname_t*m=0;
130     namespace_t*ns = namespace_new(cls->access, cls->package);
131     return multiname_new(ns,cls->name);
132 }
133
134 // ----------------------- builtin types ------------------------------
135 classinfo_t* registry_getanytype() {return 0;/*FIXME*/}
136
137 classinfo_t* registry_getobjectclass() {return registry_safefindclass("", "Object");}
138 classinfo_t* registry_getstringclass() {return registry_safefindclass("", "String");}
139 classinfo_t* registry_getintclass() {return registry_safefindclass("", "int");}
140 classinfo_t* registry_getuintclass() {return registry_safefindclass("", "uint");}
141 classinfo_t* registry_getbooleanclass() {return registry_safefindclass("", "Boolean");}
142 classinfo_t* registry_getnumberclass() {return registry_safefindclass("", "Number");}
143 classinfo_t* registry_getMovieClip() {return registry_safefindclass("flash.display", "MovieClip");}
144
145 // ----------------------- builtin dummy types -------------------------
146 classinfo_t nullclass = {
147     ACCESS_PACKAGE, 0, "", "null", 0, 0, 0,
148 };
149 classinfo_t* registry_getnullclass() {
150     return &nullclass;
151 }
152