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"
29 #include "tokenizer.h"
30 #include "parser.tab.h"
33 void fixstring(char*s)
37 if(!((*s>='a' && *s<='z') || (*s>='A' && *s<='Z') ||
38 (*s>='0' && *s<='9' && !first))) {
45 char* mkid(const char*package, const char*name)
47 char*id = malloc(strlen(package)+strlen(name)+10);
55 char*mkid2(multiname_t*m)
58 return mkid(m->ns->name, m->name);
59 else if(m->type == MULTINAME) {
60 namespace_list_t*l = m->namespace_set->namespaces;
62 if(l->namespace->name &&
63 l->namespace->name[0])
67 return mkid(l->namespace->name, m->name);
70 fprintf(stderr, "can't convert multiname type %d\n", m->type);
74 static array_t*tosort=0;
75 static int compare_classes(const void*v1, const void*v2)
79 abc_class_t*c1 = array_getvalue(tosort, x1);
80 abc_class_t*c2 = array_getvalue(tosort, x2);
81 int i = strcmp(c1->classname->ns->name, c2->classname->ns->name);
84 return strcmp(c1->classname->name, c2->classname->name);
87 char* kind2string(int kind)
91 void load_libraries(char*filename, int pass, FILE*fi)
94 memset(&swf, 0, sizeof(SWF));
95 TAG*tag = swf_InsertTag(0, ST_RAWABC);
96 memfile_t*file = memfile_open(filename);
97 tag->data = file->data;
99 abc_file_t*abc = swf_ReadABC(tag);
100 //swf_DumpABC(stdout, abc, "");
102 int*index = malloc(abc->classes->num*sizeof(int));
105 for(t=0;t<abc->classes->num;t++) {index[t]=t;}
106 qsort(index, abc->classes->num, sizeof(int), compare_classes);
107 for(t=0;t<abc->classes->num;t++) {
108 abc_class_t*cls = array_getvalue(abc->classes, index[t]);
109 int access = cls->classname->ns->access;
110 if(access==ACCESS_PRIVATE ||
111 access==ACCESS_PACKAGEINTERNAL)
113 if(!strncmp(cls->classname->ns->name, "__AS3", 5))
116 const char*package = cls->classname->ns->name;
117 const char*name = cls->classname->name;
118 const char*superpackage = 0;
119 const char*supername = 0;
121 if(cls->superclass) {
122 superpackage = cls->superclass->ns->name;
123 supername = cls->superclass->name;
124 superid = mkid(superpackage, supername);
126 char*id = mkid(package, name);
127 U8 flags = cls->flags;
130 fprintf(fi, "static classinfo_t %s;\n", id);
132 fprintf(fi, "static classinfo_t %s = {0x%02x, 0x%02x, \"%s\", \"%s\"", id, access, flags, package, name);
133 fprintf(fi, ", 0"); //slot
135 fprintf(fi, ", &%s, interfaces:{", superid);
137 fprintf(fi, ", 0, {");
138 if(cls->interfaces) {
139 multiname_list_t*i=cls->interfaces;
141 char*iid = mkid2(i->multiname);
142 fprintf(fi, "&%s, ", iid);
146 fprintf(fi, "0}};\n");
148 trait_list_t*l=cls->traits;
149 fprintf(fi, " dict_put(d, &%s, &%s);\n", id, id);
150 fprintf(fi, " dict_init(&%s.members, %d);\n", id, list_length(l)+1);
156 dict_t*d = dict_new();
159 trait_t*trait = l->trait;
160 if(trait->name->ns->access==ACCESS_PRIVATE)
162 const char*name = trait->name->name;
164 sprintf(id2, "%s_%s", id, name);
167 if(dict_lookup(d, name)) {
170 dict_put(d, (char*)name, (char*)name);
173 switch(trait->kind) {
175 multiname_t*ret = trait->method->return_type;
179 retvalue = mkid(ret->ns->name, ret->name);
180 if(ret && !strcmp(ret->name, "void"))
184 type = "MEMBER_METHOD";
190 type = "MEMBER_SLOT";
193 fprintf(stderr, "Unknown trait type %d\n", trait->kind);
195 int flags = is_static?FLAG_STATIC:0;
197 if(access == ACCESS_PACKAGE) flags|=FLAG_PUBLIC;
198 if(access == ACCESS_PRIVATE) flags|=FLAG_PRIVATE;
199 if(access == ACCESS_PROTECTED) flags|=FLAG_PROTECTED;
200 if(access == ACCESS_PACKAGEINTERNAL) flags|=FLAG_INTERNAL;
203 fprintf(fi, "static memberinfo_t %s;\n", id2);
205 fprintf(fi, "static memberinfo_t %s = {%s, 0x%02x, \"%s\"", id2, type, flags, name);
209 fprintf(fi, ", &%s", retvalue);
212 fprintf(fi, " dict_put(&%s.members, \"%s\", &%s);\n", id, name, id2);
216 if(!l && !is_static) {
217 l = cls->static_traits;
225 if(superid) free(superid);
228 for(t=0;t<abc->scripts->num;t++) {
229 abc_script_t*s = array_getvalue(abc->scripts, t);
231 for(l=s->traits;l;l=l->next) {
232 trait_t*trait = l->trait;
233 if(trait->kind == TRAIT_METHOD) {
234 printf("%s\n", multiname_tostring(trait->name));
241 memfile_close(file);tag->data=0;
242 swf_DeleteTag(0, tag);
247 FILE*fi = fopen("builtin.c", "wb");
248 fprintf(fi, "#include \"builtin.h\"\n");
249 load_libraries("builtin.abc", 0, fi);
250 load_libraries("playerglobal.abc", 0, fi);
252 load_libraries("builtin.abc", 1, fi);
253 load_libraries("playerglobal.abc", 1, fi);
255 fprintf(fi, "dict_t* builtin_getclasses()\n");
257 fprintf(fi, " dict_t*d = dict_new2(&classinfo_type);\n");
258 load_libraries("builtin.abc", 2, fi);
259 load_libraries("playerglobal.abc", 2, fi);
260 fprintf(fi, " return d;\n");