8ef8fe7669f7d153006f51dc753fb5d638113d50
[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*parent, 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 = "INFOTYPE_METHOD";
129             break;
130         case TRAIT_CONST:
131         case TRAIT_GETTER:
132         case TRAIT_SETTER:
133         case TRAIT_SLOT:
134             type = "INFOTYPE_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\"", type, flags, id2, name);
140     if(!retvalue)
141         fprintf(fi, ", 0");
142     else
143         fprintf(fi, ", &%s", retvalue);
144   
145     if(!parent) 
146         fprintf(fi, ", 0");
147     else
148         fprintf(fi, ", &%s", parent); // parent
149
150     fprintf(fi, ", 0,0,0};\n");
151 }
152
153 int access2flags(multiname_t*m)
154 {
155     int access = m->ns->access;
156     int flags=0;
157     return flags;
158 }
159
160 void load_libraries(char*filename, int pass, FILE*fi)
161 {
162     SWF swf;
163     memset(&swf, 0, sizeof(SWF));
164     TAG*tag = swf_InsertTag(0, ST_RAWABC);
165     memfile_t*file = memfile_open(filename);
166     tag->data = file->data;
167     tag->len = file->len;
168     abc_file_t*abc = swf_ReadABC(tag);
169     //swf_DumpABC(stdout, abc, "");
170
171     if(pass<=2) {
172         int*index = malloc(abc->classes->num*sizeof(int));
173         int t;
174         tosort=abc->classes;
175         for(t=0;t<abc->classes->num;t++) {index[t]=t;}
176         qsort(index, abc->classes->num, sizeof(int), compare_classes);
177
178         if(pass==-1) {
179             const char*last_package = "-*-";
180             for(t=0;t<abc->classes->num;t++) {
181                 abc_class_t*cls = array_getvalue(abc->classes, index[t]);
182                 const char*package = cls->classname->ns->name;
183                 char*pid = mkpid(package);
184                 if(strcmp(last_package, package)) {
185                     last_package = package;
186                     fprintf(fi, "static packageinfo_t %s = {\"%s\"};\n", pid, package);
187                 }
188             }
189         }
190
191         if(pass>=0)
192         for(t=0;t<abc->classes->num;t++) {
193             abc_class_t*cls = array_getvalue(abc->classes, index[t]);
194             int access = cls->classname->ns->access;
195             if(access==ACCESS_PRIVATE ||
196                access==ACCESS_PACKAGEINTERNAL)
197                 continue;
198             if(!strncmp(cls->classname->ns->name, "__AS3", 5))
199                 continue;
200
201             const char*package = cls->classname->ns->name;
202             const char*name = cls->classname->name;
203             const char*superpackage = 0;
204             const char*supername = 0;
205             char*superid = 0;
206             if(cls->superclass) {
207                 superpackage = cls->superclass->ns->name;
208                 supername = cls->superclass->name;
209                 superid = mkid(superpackage, supername);
210             }
211             char*id = mkid(package, name);
212             U8 flags = cls->flags;
213             
214             if(pass==0)  {
215                 fprintf(fi, "static classinfo_t %s;\n", id);
216             } else if(pass==1) {
217                 fprintf(fi, "static classinfo_t %s = {0x%02x, 0x%02x, \"%s\", \"%s\"", id, access, flags, package, name);
218                 fprintf(fi, ", (void*)0"); //slot
219                 if(superid)
220                     fprintf(fi, ", &%s, interfaces:{", superid);
221                 else
222                     fprintf(fi, ", (void*)0, interfaces:{");
223                 if(cls->interfaces) {
224                     multiname_list_t*i=cls->interfaces;
225                     while(i) {
226                         char*iid = mkid2(i->multiname);
227                         fprintf(fi, "&%s, ", iid);
228                         i = i->next;
229                     }
230                 }
231                 fprintf(fi, "(void*)0}};\n");
232             } else if(pass==2) {
233                 trait_list_t*l=cls->traits;
234                 fprintf(fi, "    dict_put(d, &%s, &%s);\n", id, id);
235                 fprintf(fi, "    dict_init(&%s.members, %d);\n", id, list_length(l)+1);
236             }
237
238           
239             trait_list_t*l=0;
240             char is_static = 0;
241             dict_t*d = dict_new();
242             l = cls->traits;
243             while(l) {
244                 trait_t*trait = l->trait;
245                 if(trait->name->ns->access==ACCESS_PRIVATE)
246                     goto cont;
247                 const char*name = trait->name->name;
248                 char id2[1024];
249                 sprintf(id2, "%s_%s", id, name);
250                 
251                 if(dict_lookup(d, name)) {
252                     goto cont;
253                 } else {
254                     dict_put(d, (char*)name, (char*)name);
255                 }
256                 int flags = is_static?FLAG_STATIC:0;
257                 //flags |= access2flags(access);
258                 flags |= access2flags(trait->name);
259
260                 if(pass==0) {
261                     fprintf(fi, "static memberinfo_t %s;\n", id2);
262                 } if(pass==1) {
263                     write_member_info(fi, id, id2, name, flags, trait);
264                 } else if(pass==2) {
265                     fprintf(fi, "    dict_put(&%s.members, \"%s\", &%s);\n", id, name, id2);
266                 }
267     cont:
268                 l = l->next;
269                 if(!l && !is_static) {
270                     l = cls->static_traits;
271                     is_static = 1;
272                 }
273             }
274             
275             dict_destroy(d);
276
277             if(id) free(id);
278             if(superid) free(superid);
279         }
280         free(index);
281     }
282
283     if(pass==0 || pass==1 || pass==3) {
284         int t;
285
286 #define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS)
287   
288         /* count public functions */
289         int num_methods=0;
290         for(t=0;t<abc->scripts->num;t++) {
291             trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
292             for(;l;l=l->next) {
293                 num_methods += IS_PUBLIC_MEMBER(l->trait);
294             }
295         }
296         trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
297         num_methods=0;
298         for(t=0;t<abc->scripts->num;t++) {
299             trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
300             for(;l;l=l->next) {
301                 if(IS_PUBLIC_MEMBER(l->trait)) {
302                     traits[num_methods++] = l->trait;
303                 }
304             }
305         }
306         qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
307         const char*last_package = "-xxx-";
308         for(t=0;t<num_methods;t++) {
309             trait_t*trait = traits[t];
310             if(IS_PUBLIC_MEMBER(trait)) {
311                 const char*package = trait->name->ns->name;
312                 const char*name = trait->name->name;
313                 char*pid = mkpid(package);
314                 char*id2 = mkid2(trait->name);
315                 int flags = FLAG_STATIC|access2flags(trait->name);
316                 NEW(memberinfo_t,m);
317                 char np = 0;
318                 int clsflags = FLAG_STATIC;
319                 if(pass==0) {
320                     fprintf(fi, "static memberinfo_t %s;\n", id2);
321                     fprintf(fi, "static classinfo_t %s_class;\n", id2);
322                 } else if(pass==1) {
323                     write_member_info(fi, 0, id2, name, flags, trait);
324                     fprintf(fi, "static classinfo_t %s_class = {0x%02x, 0x%02x, \"%s\", \"%s\", &%s, (void*)0, members:{(void*)0}};\n", 
325                             id2,
326                             trait->name->ns->access, clsflags,
327                             package, name, 
328                             id2);
329                 } else if(pass==3) {
330                     /*if(np) {
331                         fprintf(fi, "    dict_init(%s.classes, 1); //not used yet\n", pid);
332                         fprintf(fi, "    dict_init(%s.functions, 1);\n", pid);
333                     }
334                     fprintf(fi, "    dict_put(&%s.functions, \"%s\", &%s);\n", pid, name, id2);*/
335                     fprintf(fi, "    dict_put(d, &%s_class, &%s_class);\n", id2, id2);
336                 }
337             } else if(trait->kind == TRAIT_CLASS) {
338                 // ignore classes, these are treated above
339             } else {
340                 printf("%02x %s\n", trait->kind, multiname_tostring(trait->name));
341             }
342         }
343     }
344
345     swf_FreeABC(abc);
346     memfile_close(file);tag->data=0;
347     swf_DeleteTag(0, tag);
348 }
349
350 int main()
351 {
352     FILE*fi = fopen("builtin.c", "wb");
353     fprintf(fi, "#include \"builtin.h\"\n");
354
355     //load_libraries("builtin.abc", -1, fi);
356     //load_libraries("playerglobal.abc", -1, fi);
357     //fprintf(fi, "static packageinfo_t package_flash_debugger = {\"flash.debugger\"};\n");
358     //fprintf(fi, "static packageinfo_t package_flash_profiler = {\"flash.profiler\"};\n");
359
360     load_libraries("builtin.abc", 0, fi);
361     load_libraries("playerglobal.abc", 0, fi);
362     
363     load_libraries("builtin.abc", 1, fi);
364     load_libraries("playerglobal.abc", 1, fi);
365    
366     fprintf(fi, "dict_t* builtin_getclasses()\n");
367     fprintf(fi, "{\n");
368     fprintf(fi, "    dict_t*d = dict_new2(&slotinfo_type);\n");
369     load_libraries("builtin.abc", 2, fi);
370     load_libraries("playerglobal.abc", 2, fi);
371     load_libraries("builtin.abc", 3, fi);
372     load_libraries("playerglobal.abc", 3, fi);
373     fprintf(fi, "    return d;\n");
374     fprintf(fi, "}\n");
375     fprintf(fi, "\n");
376     
377     //fprintf(fi, "dict_t* builtin_getglobalfunctions()\n");
378     //fprintf(fi, "{\n");
379     //fprintf(fi, "    dict_t*d = dict_new();\n");
380     //load_libraries("builtin.abc", 3, fi);
381     //load_libraries("playerglobal.abc", 3, fi);
382     //fprintf(fi, "    return d;\n");
383     //fprintf(fi, "}\n");
384
385     fclose(fi);
386 }