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);
134 fprintf(fi, ", &%s, interfaces:{", superid);
136 fprintf(fi, ", 0, {");
137 if(cls->interfaces) {
138 multiname_list_t*i=cls->interfaces;
140 char*iid = mkid2(i->multiname);
141 fprintf(fi, "&%s, ", iid);
145 fprintf(fi, "0}};\n");
147 trait_list_t*l=cls->traits;
148 fprintf(fi, " dict_put(d, &%s, &%s);\n", id, id);
149 fprintf(fi, " dict_init(&%s.members, %d);\n", id, list_length(l)+1);
155 dict_t*d = dict_new();
158 trait_t*trait = l->trait;
159 if(trait->name->ns->access==ACCESS_PRIVATE)
161 const char*name = trait->name->name;
163 sprintf(id2, "%s_%s", id, name);
166 if(dict_lookup(d, name)) {
169 dict_put(d, (char*)name, (char*)name);
172 switch(trait->kind) {
174 multiname_t*ret = trait->method->return_type;
178 retvalue = mkid(ret->ns->name, ret->name);
179 if(ret && !strcmp(ret->name, "void"))
183 type = "MEMBER_METHOD";
189 type = "MEMBER_SLOT";
192 fprintf(stderr, "Unknown trait type %d\n", trait->kind);
194 int flags = is_static?FLAG_STATIC:0;
196 if(access == ACCESS_PACKAGE) flags|=FLAG_PUBLIC;
197 if(access == ACCESS_PRIVATE) flags|=FLAG_PRIVATE;
198 if(access == ACCESS_PROTECTED) flags|=FLAG_PROTECTED;
199 if(access == ACCESS_PACKAGEINTERNAL) flags|=FLAG_INTERNAL;
202 fprintf(fi, "static memberinfo_t %s;\n", id2);
204 fprintf(fi, "static memberinfo_t %s = {%s, 0x%02x, \"%s\"", id2, type, flags, name);
208 fprintf(fi, ", &%s", retvalue);
211 fprintf(fi, " dict_put(&%s.members, \"%s\", &%s);\n", id, name, id2);
215 if(!l && !is_static) {
216 l = cls->static_traits;
224 if(superid) free(superid);
227 for(t=0;t<abc->scripts->num;t++) {
228 abc_script_t*s = array_getvalue(abc->scripts, t);
230 for(l=s->traits;l;l=l->next) {
231 trait_t*trait = l->trait;
232 if(trait->kind == TRAIT_METHOD) {
233 printf("%s\n", multiname_tostring(trait->name));
240 memfile_close(file);tag->data=0;
241 swf_DeleteTag(0, tag);
246 FILE*fi = fopen("builtin.c", "wb");
247 fprintf(fi, "#include \"builtin.h\"\n");
248 load_libraries("builtin.abc", 0, fi);
249 load_libraries("playerglobal.abc", 0, fi);
251 load_libraries("builtin.abc", 1, fi);
252 load_libraries("playerglobal.abc", 1, fi);
254 fprintf(fi, "dict_t* builtin_getclasses()\n");
256 fprintf(fi, " dict_t*d = dict_new2(&classinfo_type);\n");
257 load_libraries("builtin.abc", 2, fi);
258 load_libraries("playerglobal.abc", 2, fi);
259 fprintf(fi, " return d;\n");