added support for \0 in strings
[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 "files.h"
28 #include "tokenizer.h"
29 #include "parser.tab.h"
30 #include "parser.h"
31
32 void fixstring(char*s)
33 {
34     char first=1;
35     for(;*s;s++) {
36         if(!((*s>='a' && *s<='z') || (*s>='A' && *s<='Z') || 
37                     (*s>='0' && *s<='9' && !first))) {
38             *s = '_';
39         }
40         first = 0;
41     }
42 }
43
44 char* mkid(const char*package, const char*name)
45 {
46     char*id = malloc(strlen(package)+strlen(name)+10);
47     strcpy(id, package);
48     strcat(id, "_");
49     strcat(id, name);
50     fixstring(id);
51     return id;
52 }
53
54 char*mkid2(multiname_t*m)
55 {
56     if(m->type == QNAME)
57         return mkid(m->ns->name, m->name);
58     else if(m->type == MULTINAME) {
59         namespace_list_t*l = m->namespace_set->namespaces;
60         while(l) {
61             if(l->namespace->name &&
62                l->namespace->name[0])
63                 break;
64             l = l->next;
65         }
66         return mkid(l->namespace->name, m->name);
67     }
68     else {
69         fprintf(stderr, "can't convert multiname type %d\n", m->type);
70     }
71 }
72
73 static array_t*tosort=0;
74 static int compare_classes(const void*v1, const void*v2)
75 {
76     int x1 = *(int*)v1;
77     int x2 = *(int*)v2;
78     abc_class_t*c1 = array_getvalue(tosort, x1);
79     abc_class_t*c2 = array_getvalue(tosort, x2);
80     int i = strcmp(c1->classname->ns->name, c2->classname->ns->name);
81     if(i)
82         return i;
83     return strcmp(c1->classname->name, c2->classname->name);
84 }
85
86 void load_libraries(char*filename, int pass, FILE*fi)
87 {
88     SWF swf;
89     memset(&swf, 0, sizeof(SWF));
90     TAG*tag = swf_InsertTag(0, ST_RAWABC);
91     memfile_t*file = memfile_open(filename);
92     tag->data = file->data;
93     tag->len = file->len;
94     abc_file_t*abc = swf_ReadABC(tag);
95     //swf_DumpABC(stdout, abc, "");
96
97     int*index = malloc(abc->classes->num*sizeof(int));
98     int t;
99     tosort=abc->classes;
100     for(t=0;t<abc->classes->num;t++) {index[t]=t;}
101     qsort(index, abc->classes->num, sizeof(int), compare_classes);
102
103     for(t=0;t<abc->classes->num;t++) {
104         abc_class_t*cls = array_getvalue(abc->classes, index[t]);
105         int access = cls->classname->ns->access;
106         if(access==ACCESS_PRIVATE ||
107            access==ACCESS_PACKAGEINTERNAL)
108             continue;
109         if(!strncmp(cls->classname->ns->name, "__AS3", 5))
110             continue;
111
112         const char*package = cls->classname->ns->name;
113         const char*name = cls->classname->name;
114         const char*superpackage = 0;
115         const char*supername = 0;
116         char*superid = 0;
117         if(cls->superclass) {
118             superpackage = cls->superclass->ns->name;
119             supername = cls->superclass->name;
120             superid = mkid(superpackage, supername);
121         }
122         char*id = mkid(package, name);
123       
124         if(pass==0) 
125             fprintf(fi, "static class_signature_t %s;\n", id);
126         else if(pass==1) {
127             fprintf(fi, "static class_signature_t %s = {0x%02x, \"%s\", \"%s\"", id, access, package, name);
128             if(superid)
129                 fprintf(fi, ", &%s, interfaces:{", superid);
130             else
131                 fprintf(fi, ", 0, {");
132             if(cls->interfaces) {
133                 multiname_list_t*i=cls->interfaces;
134                 while(i) {
135                     char*iid = mkid2(i->multiname);
136                     fprintf(fi, "&%s, ", iid);
137                     i = i->next;
138                 }
139             }
140             fprintf(fi, "0}};\n");
141         } else if(pass==2) {
142             trait_list_t*l=cls->traits;
143             fprintf(fi, "    dict_put(d, &%s, &%s);\n", id, id);
144             fprintf(fi, "    dict_init(&%s.members, %d);\n", id, list_length(l)+1);
145             int j;
146             dict_t*d = dict_new();
147             for(;l;l=l->next) {
148                 trait_t*trait = l->trait;
149                 if(trait->name->ns->access==ACCESS_PRIVATE)
150                     continue;
151                 char*type="something";
152                 const char*name = trait->name->name;
153                 if(dict_lookup(d, name)) {
154                     continue;
155                 } else {
156                     dict_put(d, (char*)name, (char*)name);
157                 }
158                 switch(trait->kind) {
159                     case TRAIT_METHOD: type="method";break;
160                     case TRAIT_CONST: type="const";break;
161                     case TRAIT_FUNCTION: type="function";break;
162                     case TRAIT_GETTER: type="member";break;
163                     case TRAIT_SETTER: type="member";break;
164                     case TRAIT_SLOT: type="member";break;
165                     case TRAIT_CLASS: type="subclass";break;
166                 }
167                 fprintf(fi, "    dict_put(&%s.members, \"%s\", \"%s\");\n", 
168                         id, name, type);
169             }
170             l=cls->static_traits;
171             for(;l;l=l->next) {
172                 trait_t*trait = l->trait;
173                 if(trait->name->ns->access==ACCESS_PRIVATE)
174                     continue;
175                 char*type="static entity";
176                 const char*name = trait->name->name;
177                 if(dict_lookup(d, name)) {
178                     continue;
179                 } else {
180                     dict_put(d, (char*)name, (char*)name);
181                 }
182                 switch(trait->kind) {
183                     case TRAIT_METHOD: type="static method";break;
184                     case TRAIT_CONST: type="constant";break;
185                     case TRAIT_FUNCTION: type="static function";break;
186                     case TRAIT_GETTER: type="static variable";break;
187                     case TRAIT_SETTER: type="static variable";break;
188                     case TRAIT_SLOT: type="static variable";break;
189                     case TRAIT_CLASS: type="static subclass";break;
190                 }
191                 fprintf(fi, "    dict_put(&%s.members, \"%s\", \"%s\");\n", 
192                         id, name, type);
193             }
194             dict_destroy(d);
195         }
196
197         if(id) free(id);
198         if(superid) free(superid);
199     }
200
201     swf_FreeABC(abc);
202     memfile_close(file);tag->data=0;
203     swf_DeleteTag(0, tag);
204 }
205
206 int main()
207 {
208     FILE*fi = fopen("builtin.c", "wb");
209     fprintf(fi, "#include \"builtin.h\"\n");
210     load_libraries("builtin.abc", 0, fi);
211     load_libraries("playerglobal.abc", 0, fi);
212     
213     load_libraries("builtin.abc", 1, fi);
214     load_libraries("playerglobal.abc", 1, fi);
215    
216     fprintf(fi, "dict_t* builtin_getclasses()\n");
217     fprintf(fi, "{\n");
218     fprintf(fi, "    dict_t*d = dict_new2(&class_signature_type);\n");
219     load_libraries("builtin.abc", 2, fi);
220     load_libraries("playerglobal.abc", 2, fi);
221     fprintf(fi, "    return d;\n");
222     fprintf(fi, "}\n");
223     fclose(fi);
224 }