store global functions as classes, too
[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* mkpid(const char*package)
46 {
47     char*id = malloc(strlen(package)+20);
48     strcpy(id, "package_");
49     if(!*package) 
50         strcat(id, "global");
51     else
52         strcat(id, package);
53     fixstring(id);
54     return id;
55 }
56 char* mkid(const char*package, const char*name)
57 {
58     char*id = malloc(strlen(package)+strlen(name)+10);
59     strcpy(id, package);
60     strcat(id, "_");
61     strcat(id, name);
62     fixstring(id);
63     return id;
64 }
65
66 char*mkid2(multiname_t*m)
67 {
68     if(m->type == QNAME)
69         return mkid(m->ns->name, m->name);
70     else if(m->type == MULTINAME) {
71         namespace_list_t*l = m->namespace_set->namespaces;
72         while(l) {
73             if(l->namespace->name &&
74                l->namespace->name[0])
75                 break;
76             l = l->next;
77         }
78         return mkid(l->namespace->name, m->name);
79     }
80     else {
81         fprintf(stderr, "can't convert multiname type %d\n", m->type);
82     }
83 }
84
85 static array_t*tosort=0;
86 static int compare_classes(const void*v1, const void*v2)
87 {
88     int x1 = *(int*)v1;
89     int x2 = *(int*)v2;
90     abc_class_t*c1 = array_getvalue(tosort, x1);
91     abc_class_t*c2 = array_getvalue(tosort, x2);
92     int i = strcmp(c1->classname->ns->name, c2->classname->ns->name);
93     if(i)
94         return i;
95     return strcmp(c1->classname->name, c2->classname->name);
96 }
97
98 static int compare_traits(const void*v1, const void*v2)
99 {
100     trait_t* x1 = *(trait_t**)v1;
101     trait_t* x2 = *(trait_t**)v2;
102     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
103     if(i)
104         return i;
105     return strcmp(x1->name->name, x2->name->name);
106 }
107
108 char* kind2string(int kind)
109 {
110 }
111
112 void write_member_info(FILE*fi, char*id2, const char*name, int flags, trait_t*trait)
113 {
114     char*retvalue = 0;
115     char*type="0";
116     switch(trait->kind) {
117         case TRAIT_METHOD: {
118             multiname_t*ret = trait->method->return_type;
119
120             if(!ret)
121                 retvalue = 0;
122             else
123                 retvalue = mkid(ret->ns->name, ret->name);
124             if(ret && !strcmp(ret->name, "void"))
125                 retvalue = 0;
126         } //fallthrough
127         case TRAIT_FUNCTION:
128             type = "MEMBER_METHOD";
129             break;
130         case TRAIT_CONST:
131         case TRAIT_GETTER:
132         case TRAIT_SETTER:
133         case TRAIT_SLOT:
134             type = "MEMBER_SLOT";
135             break;
136         default:
137             fprintf(stderr, "Unknown trait type %d\n", trait->kind);
138     }
139     fprintf(fi, "static memberinfo_t %s = {%s, 0x%02x, \"%s\"", id2, type, flags, name);
140     if(!retvalue)
141         fprintf(fi, ", 0");
142     else
143         fprintf(fi, ", &%s", retvalue);
144     fprintf(fi, "};\n");
145 }
146
147 int access2flags(int access)
148 {
149     int flags=0;
150     if(access == ACCESS_PACKAGE) flags|=FLAG_PUBLIC;
151     if(access == ACCESS_PRIVATE) flags|=FLAG_PRIVATE;
152     if(access == ACCESS_PROTECTED) flags|=FLAG_PROTECTED;
153     if(access == ACCESS_PACKAGEINTERNAL) flags|=FLAG_INTERNAL;
154     return flags;
155 }
156
157 void load_libraries(char*filename, int pass, FILE*fi)
158 {
159     SWF swf;
160     memset(&swf, 0, sizeof(SWF));
161     TAG*tag = swf_InsertTag(0, ST_RAWABC);
162     memfile_t*file = memfile_open(filename);
163     tag->data = file->data;
164     tag->len = file->len;
165     abc_file_t*abc = swf_ReadABC(tag);
166     //swf_DumpABC(stdout, abc, "");
167
168     if(pass<=2) {
169         int*index = malloc(abc->classes->num*sizeof(int));
170         int t;
171         tosort=abc->classes;
172         for(t=0;t<abc->classes->num;t++) {index[t]=t;}
173         qsort(index, abc->classes->num, sizeof(int), compare_classes);
174
175         if(pass==-1) {
176             const char*last_package = "-*-";
177             for(t=0;t<abc->classes->num;t++) {
178                 abc_class_t*cls = array_getvalue(abc->classes, index[t]);
179                 const char*package = cls->classname->ns->name;
180                 char*pid = mkpid(package);
181                 if(strcmp(last_package, package)) {
182                     last_package = package;
183                     fprintf(fi, "static packageinfo_t %s = {\"%s\"};\n", pid, package);
184                 }
185             }
186         }
187
188         if(pass>=0)
189         for(t=0;t<abc->classes->num;t++) {
190             abc_class_t*cls = array_getvalue(abc->classes, index[t]);
191             int access = cls->classname->ns->access;
192             if(access==ACCESS_PRIVATE ||
193                access==ACCESS_PACKAGEINTERNAL)
194                 continue;
195             if(!strncmp(cls->classname->ns->name, "__AS3", 5))
196                 continue;
197
198             const char*package = cls->classname->ns->name;
199             const char*name = cls->classname->name;
200             const char*superpackage = 0;
201             const char*supername = 0;
202             char*superid = 0;
203             if(cls->superclass) {
204                 superpackage = cls->superclass->ns->name;
205                 supername = cls->superclass->name;
206                 superid = mkid(superpackage, supername);
207             }
208             char*id = mkid(package, name);
209             U8 flags = cls->flags;
210             
211             if(pass==0)  {
212                 fprintf(fi, "static classinfo_t %s;\n", id);
213             } else if(pass==1) {
214                 fprintf(fi, "static classinfo_t %s = {0x%02x, 0x%02x, \"%s\", \"%s\"", id, access, flags, package, name);
215                 fprintf(fi, ", 0"); //slot
216                 if(superid)
217                     fprintf(fi, ", &%s, interfaces:{", superid);
218                 else
219                     fprintf(fi, ", 0, {");
220                 if(cls->interfaces) {
221                     multiname_list_t*i=cls->interfaces;
222                     while(i) {
223                         char*iid = mkid2(i->multiname);
224                         fprintf(fi, "&%s, ", iid);
225                         i = i->next;
226                     }
227                 }
228                 fprintf(fi, "0}};\n");
229             } else if(pass==2) {
230                 trait_list_t*l=cls->traits;
231                 fprintf(fi, "    dict_put(d, &%s, &%s);\n", id, id);
232                 fprintf(fi, "    dict_init(&%s.members, %d);\n", id, list_length(l)+1);
233             }
234
235           
236             trait_list_t*l=0;
237             char is_static = 0;
238             dict_t*d = dict_new();
239             l = cls->traits;
240             while(l) {
241                 trait_t*trait = l->trait;
242                 if(trait->name->ns->access==ACCESS_PRIVATE)
243                     goto cont;
244                 const char*name = trait->name->name;
245                 char id2[1024];
246                 sprintf(id2, "%s_%s", id, name);
247                 
248                 if(dict_lookup(d, name)) {
249                     goto cont;
250                 } else {
251                     dict_put(d, (char*)name, (char*)name);
252                 }
253                 int flags = is_static?FLAG_STATIC:0;
254                 //flags |= access2flags(access);
255                 flags |= access2flags(trait->name->ns->access);
256
257                 if(pass==0) {
258                     fprintf(fi, "static memberinfo_t %s;\n", id2);
259                 } if(pass==1) {
260                     write_member_info(fi, id2, name, flags, trait);
261                 } else if(pass==2) {
262                     fprintf(fi, "    dict_put(&%s.members, \"%s\", &%s);\n", id, name, id2);
263                 }
264     cont:
265                 l = l->next;
266                 if(!l && !is_static) {
267                     l = cls->static_traits;
268                     is_static = 1;
269                 }
270             }
271             
272             dict_destroy(d);
273
274             if(id) free(id);
275             if(superid) free(superid);
276         }
277         free(index);
278     }
279
280     if(pass==0 || pass==1 || pass==3) {
281         int t;
282
283 #define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS)
284   
285         /* count public functions */
286         int num_methods=0;
287         for(t=0;t<abc->scripts->num;t++) {
288             trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
289             for(;l;l=l->next) {
290                 num_methods += IS_PUBLIC_MEMBER(l->trait);
291             }
292         }
293         trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
294         num_methods=0;
295         for(t=0;t<abc->scripts->num;t++) {
296             trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
297             for(;l;l=l->next) {
298                 if(IS_PUBLIC_MEMBER(l->trait)) {
299                     traits[num_methods++] = l->trait;
300                 }
301             }
302         }
303         qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
304         const char*last_package = "-xxx-";
305         for(t=0;t<num_methods;t++) {
306             trait_t*trait = traits[t];
307             if(IS_PUBLIC_MEMBER(trait)) {
308                 const char*package = trait->name->ns->name;
309                 const char*name = trait->name->name;
310                 char*pid = mkpid(package);
311                 char*id2 = mkid2(trait->name);
312                 int flags = FLAG_STATIC|access2flags(trait->name->ns->access);
313                 NEW(memberinfo_t,m);
314                 char np = 0;
315                 int clsflags = FLAG_STATIC | FLAG_METHOD;
316                 if(pass==0) {
317                     fprintf(fi, "static memberinfo_t %s;\n", id2);
318                     fprintf(fi, "static classinfo_t %s_class;\n", id2);
319                 } else if(pass==1) {
320                     write_member_info(fi, id2, name, flags, trait);
321                     fprintf(fi, "static classinfo_t %s_class = {0x%02x, 0x%02x, \"%s\", \"%s\", &%s, 0, members:{0}};\n", 
322                             id2,
323                             trait->name->ns->access, clsflags,
324                             package, name, 
325                             id2);
326                 } else if(pass==3) {
327                     /*if(np) {
328                         fprintf(fi, "    dict_init(%s.classes, 1); //not used yet\n", pid);
329                         fprintf(fi, "    dict_init(%s.functions, 1);\n", pid);
330                     }
331                     fprintf(fi, "    dict_put(&%s.functions, \"%s\", &%s);\n", pid, name, id2);*/
332                     fprintf(fi, "    dict_put(d, &%s_class, &%s_class);\n", id2, id2);
333                 }
334             } else if(trait->kind == TRAIT_CLASS) {
335                 // ignore classes, these are treated above
336             } else {
337                 printf("%02x %s\n", trait->kind, multiname_tostring(trait->name));
338             }
339         }
340     }
341
342     swf_FreeABC(abc);
343     memfile_close(file);tag->data=0;
344     swf_DeleteTag(0, tag);
345 }
346
347 int main()
348 {
349     FILE*fi = fopen("builtin.c", "wb");
350     fprintf(fi, "#include \"builtin.h\"\n");
351
352     //load_libraries("builtin.abc", -1, fi);
353     //load_libraries("playerglobal.abc", -1, fi);
354     //fprintf(fi, "static packageinfo_t package_flash_debugger = {\"flash.debugger\"};\n");
355     //fprintf(fi, "static packageinfo_t package_flash_profiler = {\"flash.profiler\"};\n");
356
357     load_libraries("builtin.abc", 0, fi);
358     load_libraries("playerglobal.abc", 0, fi);
359     
360     load_libraries("builtin.abc", 1, fi);
361     load_libraries("playerglobal.abc", 1, fi);
362    
363     fprintf(fi, "dict_t* builtin_getclasses()\n");
364     fprintf(fi, "{\n");
365     fprintf(fi, "    dict_t*d = dict_new2(&classinfo_type);\n");
366     load_libraries("builtin.abc", 2, fi);
367     load_libraries("playerglobal.abc", 2, fi);
368     load_libraries("builtin.abc", 3, fi);
369     load_libraries("playerglobal.abc", 3, fi);
370     fprintf(fi, "    return d;\n");
371     fprintf(fi, "}\n");
372     fprintf(fi, "\n");
373     
374     //fprintf(fi, "dict_t* builtin_getglobalfunctions()\n");
375     //fprintf(fi, "{\n");
376     //fprintf(fi, "    dict_t*d = dict_new();\n");
377     //load_libraries("builtin.abc", 3, fi);
378     //load_libraries("playerglobal.abc", 3, fi);
379     //fprintf(fi, "    return d;\n");
380     //fprintf(fi, "}\n");
381
382     fclose(fi);
383 }