reworked member architecture
[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
108     for(t=0;t<abc->classes->num;t++) {
109         abc_class_t*cls = array_getvalue(abc->classes, index[t]);
110         int access = cls->classname->ns->access;
111         if(access==ACCESS_PRIVATE ||
112            access==ACCESS_PACKAGEINTERNAL)
113             continue;
114         if(!strncmp(cls->classname->ns->name, "__AS3", 5))
115             continue;
116
117         const char*package = cls->classname->ns->name;
118         const char*name = cls->classname->name;
119         const char*superpackage = 0;
120         const char*supername = 0;
121         char*superid = 0;
122         if(cls->superclass) {
123             superpackage = cls->superclass->ns->name;
124             supername = cls->superclass->name;
125             superid = mkid(superpackage, supername);
126         }
127         char*id = mkid(package, name);
128         U8 flags = cls->flags;
129         
130         if(pass==0)  {
131             fprintf(fi, "static classinfo_t %s;\n", id);
132         } else if(pass==1) {
133             fprintf(fi, "static classinfo_t %s = {0x%02x, 0x%02x, \"%s\", \"%s\"", id, access, flags, package, name);
134             if(superid)
135                 fprintf(fi, ", &%s, interfaces:{", superid);
136             else
137                 fprintf(fi, ", 0, {");
138             if(cls->interfaces) {
139                 multiname_list_t*i=cls->interfaces;
140                 while(i) {
141                     char*iid = mkid2(i->multiname);
142                     fprintf(fi, "&%s, ", iid);
143                     i = i->next;
144                 }
145             }
146             fprintf(fi, "0}};\n");
147         } else if(pass==2) {
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);
151         }
152
153       
154         trait_list_t*l=0;
155         char is_static = 0;
156         dict_t*d = dict_new();
157         l = cls->traits;
158         while(l) {
159             trait_t*trait = l->trait;
160             if(trait->name->ns->access==ACCESS_PRIVATE)
161                 goto cont;
162             const char*name = trait->name->name;
163             char id2[1024];
164             sprintf(id2, "%s_%s", id, name);
165             char*retvalue = 0;
166
167             if(dict_lookup(d, name)) {
168                 goto cont;
169             } else {
170                 dict_put(d, (char*)name, (char*)name);
171             }
172             char*type="0";
173             switch(trait->kind) {
174                 case TRAIT_METHOD: {
175                     multiname_t*ret = trait->method->return_type;
176                     if(!ret)
177                         retvalue = 0;
178                     else
179                         retvalue = mkid(ret->ns->name, ret->name);
180                     if(ret && !strcmp(ret->name, "void"))
181                         retvalue = 0;
182                 } //fallthrough
183                 case TRAIT_FUNCTION:
184                     type = "MEMBER_METHOD";
185                     break;
186                 case TRAIT_CONST:
187                 case TRAIT_GETTER:
188                 case TRAIT_SETTER:
189                 case TRAIT_SLOT:
190                     type = "MEMBER_SLOT";
191                     break;
192                 default:
193                     fprintf(stderr, "Unknown trait type %d\n", trait->kind);
194             }
195             if(pass==0) {
196                 fprintf(fi, "static memberinfo_t %s;\n", id2);
197             } if(pass==1) {
198                 fprintf(fi, "static memberinfo_t %s = {%s, \"%s\"", id2, type, name);
199                 if(!retvalue)
200                     fprintf(fi, ", 0");
201                 else
202                     fprintf(fi, ", &%s", retvalue);
203                 fprintf(fi, "};\n");
204             } else if(pass==2) {
205                 fprintf(fi, "    dict_put(&%s.members, \"%s\", &%s);\n", id, name, id2);
206             }
207 cont:
208             l = l->next;
209             if(!l && !is_static) {
210                 l = cls->static_traits;
211                 is_static = 1;
212             }
213         }
214         
215         dict_destroy(d);
216
217         if(id) free(id);
218         if(superid) free(superid);
219     }
220
221     swf_FreeABC(abc);
222     memfile_close(file);tag->data=0;
223     swf_DeleteTag(0, tag);
224 }
225
226 int main()
227 {
228     FILE*fi = fopen("builtin.c", "wb");
229     fprintf(fi, "#include \"builtin.h\"\n");
230     load_libraries("builtin.abc", 0, fi);
231     load_libraries("playerglobal.abc", 0, fi);
232     
233     load_libraries("builtin.abc", 1, fi);
234     load_libraries("playerglobal.abc", 1, fi);
235    
236     fprintf(fi, "dict_t* builtin_getclasses()\n");
237     fprintf(fi, "{\n");
238     fprintf(fi, "    dict_t*d = dict_new2(&classinfo_type);\n");
239     load_libraries("builtin.abc", 2, fi);
240     load_libraries("playerglobal.abc", 2, fi);
241     fprintf(fi, "    return d;\n");
242     fprintf(fi, "}\n");
243     fclose(fi);
244 }