3 File to generate builtin.c
5 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25 #include "../rfxswf.h"
28 #include "tokenizer.h"
29 #include "parser.tab.h"
32 void fixstring(char*s)
36 if(!((*s>='a' && *s<='z') || (*s>='A' && *s<='Z') ||
37 (*s>='0' && *s<='9' && !first))) {
44 char* mkid(const char*package, const char*name)
46 char*id = malloc(strlen(package)+strlen(name)+10);
54 char*mkid2(multiname_t*m)
57 return mkid(m->ns->name, m->name);
58 else if(m->type == MULTINAME) {
59 namespace_list_t*l = m->namespace_set->namespaces;
61 if(l->namespace->name &&
62 l->namespace->name[0])
66 return mkid(l->namespace->name, m->name);
69 fprintf(stderr, "can't convert multiname type %d\n", m->type);
73 static array_t*tosort=0;
74 static int compare_classes(const void*v1, const void*v2)
78 abc_class_t*c1 = array_getvalue(tosort, x1);
79 abc_class_t*c2 = array_getvalue(tosort, x2);
80 int i = strcmp(c1->classname->ns->name, c2->classname->ns->name);
83 return strcmp(c1->classname->name, c2->classname->name);
86 void load_libraries(char*filename, int pass, FILE*fi)
89 memset(&swf, 0, sizeof(SWF));
90 TAG*tag = swf_InsertTag(0, ST_RAWABC);
91 memfile_t*file = memfile_open(filename);
92 tag->data = file->data;
94 abc_file_t*abc = swf_ReadABC(tag);
95 //swf_DumpABC(stdout, abc, "");
97 int*index = malloc(abc->classes->num*sizeof(int));
100 for(t=0;t<abc->classes->num;t++) {index[t]=t;}
101 qsort(index, abc->classes->num, sizeof(int), compare_classes);
103 for(t=0;t<abc->classes->num;t++) {
104 abc_class_t*cls = array_getvalue(abc->classes, index[t]);
105 int access = cls->classname->ns->access;
106 if(access==ACCESS_PRIVATE ||
107 access==ACCESS_PACKAGEINTERNAL)
109 if(!strncmp(cls->classname->ns->name, "__AS3", 5))
112 const char*package = cls->classname->ns->name;
113 const char*name = cls->classname->name;
114 const char*superpackage = 0;
115 const char*supername = 0;
117 if(cls->superclass) {
118 superpackage = cls->superclass->ns->name;
119 supername = cls->superclass->name;
120 superid = mkid(superpackage, supername);
122 char*id = mkid(package, name);
125 fprintf(fi, "static class_signature_t %s;\n", id);
127 fprintf(fi, "static class_signature_t %s = {0x%02x, \"%s\", \"%s\"", id, access, package, name);
129 fprintf(fi, ", &%s, interfaces:{", superid);
131 fprintf(fi, ", 0, {");
132 if(cls->interfaces) {
133 multiname_list_t*i=cls->interfaces;
135 char*iid = mkid2(i->multiname);
136 fprintf(fi, "&%s, ", iid);
140 fprintf(fi, "0}};\n");
142 trait_list_t*l=cls->traits;
143 fprintf(fi, " dict_put(d, &%s, &%s);\n", id, id);
144 fprintf(fi, " dict_init(&%s.members, %d);\n", id, list_length(l)+1);
146 dict_t*d = dict_new();
148 trait_t*trait = l->trait;
149 if(trait->name->ns->access==ACCESS_PRIVATE)
151 char*type="something";
152 const char*name = trait->name->name;
153 if(dict_lookup(d, name)) {
156 dict_put(d, (char*)name, (char*)name);
158 switch(trait->kind) {
159 case TRAIT_METHOD: type="method";break;
160 case TRAIT_CONST: type="const";break;
161 case TRAIT_FUNCTION: type="function";break;
162 case TRAIT_GETTER: type="member";break;
163 case TRAIT_SETTER: type="member";break;
164 case TRAIT_SLOT: type="member";break;
165 case TRAIT_CLASS: type="subclass";break;
167 fprintf(fi, " dict_put(&%s.members, \"%s\", \"%s\");\n",
170 l=cls->static_traits;
172 trait_t*trait = l->trait;
173 if(trait->name->ns->access==ACCESS_PRIVATE)
175 char*type="static entity";
176 const char*name = trait->name->name;
177 if(dict_lookup(d, name)) {
180 dict_put(d, (char*)name, (char*)name);
182 switch(trait->kind) {
183 case TRAIT_METHOD: type="static method";break;
184 case TRAIT_CONST: type="constant";break;
185 case TRAIT_FUNCTION: type="static function";break;
186 case TRAIT_GETTER: type="static variable";break;
187 case TRAIT_SETTER: type="static variable";break;
188 case TRAIT_SLOT: type="static variable";break;
189 case TRAIT_CLASS: type="static subclass";break;
191 fprintf(fi, " dict_put(&%s.members, \"%s\", \"%s\");\n",
198 if(superid) free(superid);
202 memfile_close(file);tag->data=0;
203 swf_DeleteTag(0, tag);
208 FILE*fi = fopen("builtin.c", "wb");
209 fprintf(fi, "#include \"builtin.h\"\n");
210 load_libraries("builtin.abc", 0, fi);
211 load_libraries("playerglobal.abc", 0, fi);
213 load_libraries("builtin.abc", 1, fi);
214 load_libraries("playerglobal.abc", 1, fi);
216 fprintf(fi, "dict_t* builtin_getclasses()\n");
218 fprintf(fi, " dict_t*d = dict_new2(&class_signature_type);\n");
219 load_libraries("builtin.abc", 2, fi);
220 load_libraries("playerglobal.abc", 2, fi);
221 fprintf(fi, " return d;\n");