switched from multiname to own types
[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 class_signature_equals(class_signature_t*c1, class_signature_t*c2)
34 {
35     /* notice: access right is *not* respected */
36     if(!strcmp(c1->name, c2->name) &&
37        !strcmp(c1->package, c2->package)) {
38         return 1;
39     }
40     return 0;
41 }
42 static unsigned int class_signature_hash(class_signature_t*c)
43 {
44     unsigned int hash = 0;
45     hash = crc32_add_string(hash, c->package);
46     hash = crc32_add_string(hash, c->name);
47     return hash;
48 }
49
50 static void* dummy_clone(void*other) {return other;}
51 static void dummy_destroy(class_signature_t*c) {}
52
53 type_t class_signature_type = {
54     hash: (hash_func)class_signature_hash,
55     equals: (equals_func)class_signature_equals,
56     /* all signatures are static */
57     dup: (dup_func)dummy_clone,
58     free: (free_func)dummy_destroy,
59 };
60
61 // ----------------------- function signature ------------------------------
62
63 static char function_signature_equals(function_signature_t*f1, function_signature_t*f2)
64 {
65     return !strcmp(f1->name, f2->name);
66 }
67 static unsigned int function_signature_hash(function_signature_t*f)
68 {
69     return crc32_add_string(0, f->name);
70 }
71 type_t function_signature_type = {
72     hash: (hash_func)function_signature_hash,
73     equals: (equals_func)function_signature_equals,
74     /* all signatures are static */
75     dup: (dup_func)dummy_clone,
76     free: (free_func)dummy_destroy,
77 };
78
79 // ------------------------- constructors --------------------------------
80
81 class_signature_t* class_signature_register(int access, char*package, char*name)
82 {
83     class_signature_t*c = malloc(sizeof(class_signature_t));
84     c->access = access;
85     c->package = package;
86     c->name = name;
87     dict_put(classes, c, c);
88     return c;
89 }
90
91 // --------------- builtin classes (from builtin.c) ----------------------
92
93 void registry_init()
94 {
95     classes = builtin_getclasses();
96 }
97 class_signature_t* registry_safefindclass(const char*package, const char*name)
98 {
99     class_signature_t*c = registry_findclass(package, name);
100     if(!c)
101         printf("%s.%s\n", package, name);
102     assert(c);
103     return c;
104 }
105 class_signature_t* registry_findclass(const char*package, const char*name)
106 {
107     assert(classes);
108     class_signature_t tmp;
109     tmp.package = package;
110     tmp.name = name;
111     class_signature_t* c = (class_signature_t*)dict_lookup(classes, &tmp);
112     return c;
113 }
114 void registry_fill_multiname(multiname_t*m, namespace_t*n, class_signature_t*c)
115 {
116     m->type = QNAME;
117     m->ns = n;
118     m->ns->access = c->access;
119     m->ns->name = (char*)c->package;
120     m->name = c->name;
121     m->namespace_set = 0;
122 }
123 multiname_t* class_signature_to_multiname(class_signature_t*cls)
124 {
125     if(!cls)
126         return 0;
127     multiname_t*m=0;
128     namespace_t*ns = namespace_new(cls->access, cls->package);
129     return multiname_new(ns,cls->name);
130 }
131
132 // ----------------------- builtin types ------------------------------
133 class_signature_t* registry_getanytype() {return 0;/*FIXME*/}
134
135 class_signature_t* registry_getobjectclass() {return registry_safefindclass("", "Object");}
136 class_signature_t* registry_getstringclass() {return registry_safefindclass("", "String");}
137 class_signature_t* registry_getintclass() {return registry_safefindclass("", "int");}
138 class_signature_t* registry_getuintclass() {return registry_safefindclass("", "uint");}
139 class_signature_t* registry_getbooleanclass() {return registry_safefindclass("", "Boolean");}
140 class_signature_t* registry_getnumberclass() {return registry_safefindclass("", "Number");}
141 class_signature_t* registry_getMovieClip() {return registry_safefindclass("flash.display", "MovieClip");}
142
143 // ----------------------- builtin dummy types -------------------------
144 class_signature_t nullclass = {
145     ACCESS_PACKAGE, "", "null", 0, 0, 0,
146 };
147 class_signature_t* registry_getnullclass() {
148     return &nullclass;
149 }
150