added access codes to member flags
[swftools.git] / lib / as3 / mklib.c
1 /* code.c
2
3    File to generate builtin.c
4
5    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
6  
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.
11
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.
16
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 */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <memory.h>
25 #include "../rfxswf.h"
26 #include "../os.h"
27 #include "pool.h"
28 #include "files.h"
29 #include "tokenizer.h"
30 #include "parser.tab.h"
31 #include "parser.h"
32
33 void fixstring(char*s)
34 {
35     char first=1;
36     for(;*s;s++) {
37         if(!((*s>='a' && *s<='z') || (*s>='A' && *s<='Z') || 
38                     (*s>='0' && *s<='9' && !first))) {
39             *s = '_';
40         }
41         first = 0;
42     }
43 }
44
45 char* mkid(const char*package, const char*name)
46 {
47     char*id = malloc(strlen(package)+strlen(name)+10);
48     strcpy(id, package);
49     strcat(id, "_");
50     strcat(id, name);
51     fixstring(id);
52     return id;
53 }
54
55 char*mkid2(multiname_t*m)
56 {
57     if(m->type == QNAME)
58         return mkid(m->ns->name, m->name);
59     else if(m->type == MULTINAME) {
60         namespace_list_t*l = m->namespace_set->namespaces;
61         while(l) {
62             if(l->namespace->name &&
63                l->namespace->name[0])
64                 break;
65             l = l->next;
66         }
67         return mkid(l->namespace->name, m->name);
68     }
69     else {
70         fprintf(stderr, "can't convert multiname type %d\n", m->type);
71     }
72 }
73
74 static array_t*tosort=0;
75 static int compare_classes(const void*v1, const void*v2)
76 {
77     int x1 = *(int*)v1;
78     int x2 = *(int*)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);
82     if(i)
83         return i;
84     return strcmp(c1->classname->name, c2->classname->name);
85 }
86
87 char* kind2string(int kind)
88 {
89 }
90
91 void load_libraries(char*filename, int pass, FILE*fi)
92 {
93     SWF swf;
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;
98     tag->len = file->len;
99     abc_file_t*abc = swf_ReadABC(tag);
100     //swf_DumpABC(stdout, abc, "");
101
102     int*index = malloc(abc->classes->num*sizeof(int));
103     int t;
104     tosort=abc->classes;
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)
112             continue;
113         if(!strncmp(cls->classname->ns->name, "__AS3", 5))
114             continue;
115
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;
120         char*superid = 0;
121         if(cls->superclass) {
122             superpackage = cls->superclass->ns->name;
123             supername = cls->superclass->name;
124             superid = mkid(superpackage, supername);
125         }
126         char*id = mkid(package, name);
127         U8 flags = cls->flags;
128         
129         if(pass==0)  {
130             fprintf(fi, "static classinfo_t %s;\n", id);
131         } else if(pass==1) {
132             fprintf(fi, "static classinfo_t %s = {0x%02x, 0x%02x, \"%s\", \"%s\"", id, access, flags, package, name);
133             if(superid)
134                 fprintf(fi, ", &%s, interfaces:{", superid);
135             else
136                 fprintf(fi, ", 0, {");
137             if(cls->interfaces) {
138                 multiname_list_t*i=cls->interfaces;
139                 while(i) {
140                     char*iid = mkid2(i->multiname);
141                     fprintf(fi, "&%s, ", iid);
142                     i = i->next;
143                 }
144             }
145             fprintf(fi, "0}};\n");
146         } else if(pass==2) {
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);
150         }
151
152       
153         trait_list_t*l=0;
154         char is_static = 0;
155         dict_t*d = dict_new();
156         l = cls->traits;
157         while(l) {
158             trait_t*trait = l->trait;
159             if(trait->name->ns->access==ACCESS_PRIVATE)
160                 goto cont;
161             const char*name = trait->name->name;
162             char id2[1024];
163             sprintf(id2, "%s_%s", id, name);
164             char*retvalue = 0;
165
166             if(dict_lookup(d, name)) {
167                 goto cont;
168             } else {
169                 dict_put(d, (char*)name, (char*)name);
170             }
171             char*type="0";
172             switch(trait->kind) {
173                 case TRAIT_METHOD: {
174                     multiname_t*ret = trait->method->return_type;
175                     if(!ret)
176                         retvalue = 0;
177                     else
178                         retvalue = mkid(ret->ns->name, ret->name);
179                     if(ret && !strcmp(ret->name, "void"))
180                         retvalue = 0;
181                 } //fallthrough
182                 case TRAIT_FUNCTION:
183                     type = "MEMBER_METHOD";
184                     break;
185                 case TRAIT_CONST:
186                 case TRAIT_GETTER:
187                 case TRAIT_SETTER:
188                 case TRAIT_SLOT:
189                     type = "MEMBER_SLOT";
190                     break;
191                 default:
192                     fprintf(stderr, "Unknown trait type %d\n", trait->kind);
193             }
194             int flags = is_static?FLAG_STATIC:0;
195
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;
200
201             if(pass==0) {
202                 fprintf(fi, "static memberinfo_t %s;\n", id2);
203             } if(pass==1) {
204                 fprintf(fi, "static memberinfo_t %s = {%s, 0x%02x, \"%s\"", id2, type, flags, name);
205                 if(!retvalue)
206                     fprintf(fi, ", 0");
207                 else
208                     fprintf(fi, ", &%s", retvalue);
209                 fprintf(fi, "};\n");
210             } else if(pass==2) {
211                 fprintf(fi, "    dict_put(&%s.members, \"%s\", &%s);\n", id, name, id2);
212             }
213 cont:
214             l = l->next;
215             if(!l && !is_static) {
216                 l = cls->static_traits;
217                 is_static = 1;
218             }
219         }
220         
221         dict_destroy(d);
222
223         if(id) free(id);
224         if(superid) free(superid);
225     }
226     
227     for(t=0;t<abc->scripts->num;t++) {
228         abc_script_t*s = array_getvalue(abc->scripts, t);
229         trait_list_t*l=0;
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));
234             }
235         }
236     }
237
238
239     swf_FreeABC(abc);
240     memfile_close(file);tag->data=0;
241     swf_DeleteTag(0, tag);
242 }
243
244 int main()
245 {
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);
250     
251     load_libraries("builtin.abc", 1, fi);
252     load_libraries("playerglobal.abc", 1, fi);
253    
254     fprintf(fi, "dict_t* builtin_getclasses()\n");
255     fprintf(fi, "{\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");
260     fprintf(fi, "}\n");
261     fclose(fi);
262 }