]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
b4aa79af JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
b4aa79af | 6 | /* |
c15969fd JF |
7 | * Cycript is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
b4aa79af | 11 | * |
c15969fd JF |
12 | * Cycript is distributed in the hope that it will be useful, but |
13 | * 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. | |
b4aa79af | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
b4aa79af JF |
20 | /* }}} */ |
21 | ||
ea2d184c | 22 | #include "sig/parse.hpp" |
37954781 JF |
23 | #include "Error.hpp" |
24 | ||
25 | #include <cstdio> | |
26 | #include <cstdlib> | |
27 | #include <cstring> | |
ea2d184c JF |
28 | |
29 | namespace sig { | |
30 | ||
b799113b JF |
31 | void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback); |
32 | struct Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback); | |
b21525c7 JF |
33 | |
34 | ||
ea2d184c | 35 | /* XXX: I really screwed up this time */ |
b799113b | 36 | void *prealloc_(CYPool &pool, void *odata, size_t osize, size_t nsize) { |
0cbeddf8 | 37 | void *ndata(pool.malloc<void>(nsize)); |
ea2d184c JF |
38 | memcpy(ndata, odata, osize); |
39 | return ndata; | |
40 | } | |
41 | ||
b799113b | 42 | void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback) { |
ea2d184c JF |
43 | _assert(*name != NULL); |
44 | ||
f33b048a JF |
45 | // XXX: this is just a stupid check :( |
46 | bool named(**name == '"'); | |
ea2d184c JF |
47 | |
48 | signature->elements = NULL; | |
49 | signature->count = 0; | |
50 | ||
51 | for (;;) { | |
52 | if (**name == eos) { | |
53 | ++*name; | |
54 | return; | |
55 | } | |
56 | ||
57 | signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element)); | |
58 | _assert(signature->elements != NULL); | |
59 | ||
60 | struct Element *element = &signature->elements[signature->count++]; | |
61 | ||
62 | if (**name != '"') | |
63 | element->name = NULL; | |
64 | else { | |
a815a4d6 | 65 | const char *quote = strchr(++*name, '"'); |
b799113b | 66 | element->name = pool.strmemdup(*name, quote - *name); |
ea2d184c JF |
67 | *name = quote + 1; |
68 | } | |
69 | ||
f33b048a | 70 | element->type = Parse_(pool, name, eos, named, callback); |
ea2d184c JF |
71 | |
72 | if (**name < '0' || **name > '9') | |
73 | element->offset = _not(size_t); | |
74 | else { | |
75 | element->offset = 0; | |
76 | ||
77 | do | |
78 | element->offset = element->offset * 10 + (*(*name)++ - '0'); | |
79 | while (**name >= '0' && **name <= '9'); | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
b799113b | 84 | Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback) { |
ea2d184c JF |
85 | char next = *(*name)++; |
86 | if (next == '?') | |
87 | return NULL; | |
88 | ||
b799113b | 89 | Type *type(new(pool) Type()); |
ea2d184c | 90 | _assert(type != NULL); |
b799113b | 91 | memset(type, 0, sizeof(Type)); |
ea2d184c JF |
92 | |
93 | parse: | |
94 | switch (next) { | |
95 | case '#': type->primitive = typename_P; break; | |
96 | ||
97 | case '(': | |
d63c39cc JF |
98 | if (type->data.signature.count < 2) |
99 | type->primitive = struct_P; | |
100 | else | |
101 | type->primitive = union_P; | |
ea2d184c JF |
102 | next = ')'; |
103 | goto aggregate; | |
104 | ||
105 | case '*': type->primitive = string_P; break; | |
106 | case ':': type->primitive = selector_P; break; | |
107 | ||
ecf94af8 JF |
108 | case '@': { |
109 | char next(**name); | |
110 | ||
111 | if (next == '?') { | |
112 | type->primitive = block_P; | |
113 | ++*name; | |
114 | } else { | |
115 | type->primitive = object_P; | |
116 | ||
117 | if (next == '"') { | |
118 | const char *quote = strchr(*name + 1, '"'); | |
506aad76 JF |
119 | if (quote == NULL) { |
120 | printf("unterminated specific id type {%s}\n", *name - 10); | |
121 | _assert(false); | |
122 | } else if (!named || quote[1] == eos || quote[1] == '"') { | |
b799113b | 123 | type->name = pool.strmemdup(*name + 1, quote - *name - 1); |
ecf94af8 JF |
124 | *name = quote + 1; |
125 | } | |
ea2d184c JF |
126 | } |
127 | } | |
128 | ||
ecf94af8 | 129 | } break; |
ea2d184c JF |
130 | |
131 | case 'B': type->primitive = boolean_P; break; | |
132 | case 'C': type->primitive = uchar_P; break; | |
133 | case 'I': type->primitive = uint_P; break; | |
134 | case 'L': type->primitive = ulong_P; break; | |
135 | case 'Q': type->primitive = ulonglong_P; break; | |
136 | case 'S': type->primitive = ushort_P; break; | |
137 | ||
138 | case '[': | |
139 | type->primitive = array_P; | |
140 | type->data.data.size = strtoul(*name, (char **) name, 10); | |
f33b048a | 141 | type->data.data.type = Parse_(pool, name, eos, false, callback); |
ea2d184c JF |
142 | if (**name != ']') { |
143 | printf("']' != \"%s\"\n", *name); | |
144 | _assert(false); | |
145 | } | |
146 | ++*name; | |
147 | break; | |
148 | ||
149 | case '^': | |
150 | type->primitive = pointer_P; | |
61933e16 | 151 | if (**name == '"') { |
ea2d184c JF |
152 | type->data.data.type = NULL; |
153 | } else { | |
f33b048a | 154 | type->data.data.type = Parse_(pool, name, eos, named, callback); |
3a1b79a7 JF |
155 | sig::Type *&target(type->data.data.type); |
156 | if (target != NULL && target->primitive == void_P) | |
157 | target = NULL; | |
ea2d184c JF |
158 | } |
159 | break; | |
160 | ||
161 | case 'b': | |
162 | type->primitive = bit_P; | |
163 | type->data.data.size = strtoul(*name, (char **) name, 10); | |
164 | break; | |
165 | ||
166 | case 'c': type->primitive = char_P; break; | |
167 | case 'd': type->primitive = double_P; break; | |
168 | case 'f': type->primitive = float_P; break; | |
169 | case 'i': type->primitive = int_P; break; | |
170 | case 'l': type->primitive = long_P; break; | |
171 | case 'q': type->primitive = longlong_P; break; | |
172 | case 's': type->primitive = short_P; break; | |
173 | case 'v': type->primitive = void_P; break; | |
174 | ||
f61f9da6 JF |
175 | #ifdef __LP64__ |
176 | case 'F': type->primitive = double_P; break; | |
177 | #else | |
178 | case 'F': type->primitive = float_P; break; | |
179 | #endif | |
180 | ||
ea2d184c JF |
181 | case '{': |
182 | type->primitive = struct_P; | |
183 | next = '}'; | |
184 | goto aggregate; | |
185 | ||
186 | aggregate: { | |
187 | char end = next; | |
188 | const char *begin = *name; | |
189 | do next = *(*name)++; | |
190 | while ( | |
191 | next != '=' && | |
192 | next != '}' | |
193 | ); | |
194 | size_t length = *name - begin - 1; | |
195 | if (strncmp(begin, "?", length) != 0) | |
b799113b | 196 | type->name = (char *) pool.strmemdup(begin, length); |
ea2d184c JF |
197 | else |
198 | type->name = NULL; | |
199 | ||
f33b048a JF |
200 | // XXX: this types thing is a throwback to JocStrap |
201 | ||
9814ec39 | 202 | if (next == '=') |
f33b048a | 203 | Parse_(pool, &type->data.signature, name, end, callback); |
ea2d184c JF |
204 | } break; |
205 | ||
206 | case 'N': type->flags |= JOC_TYPE_INOUT; goto next; | |
207 | case 'n': type->flags |= JOC_TYPE_IN; goto next; | |
208 | case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next; | |
209 | case 'o': type->flags |= JOC_TYPE_OUT; goto next; | |
210 | case 'R': type->flags |= JOC_TYPE_BYREF; goto next; | |
211 | case 'r': type->flags |= JOC_TYPE_CONST; goto next; | |
212 | case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next; | |
213 | ||
214 | next: | |
215 | next = *(*name)++; | |
216 | goto parse; | |
217 | break; | |
218 | ||
219 | default: | |
220 | printf("invalid type character: '%c' {%s}\n", next, *name - 10); | |
221 | _assert(false); | |
222 | } | |
223 | ||
9814ec39 JF |
224 | if (callback != NULL) |
225 | (*callback)(pool, type); | |
226 | ||
ea2d184c JF |
227 | return type; |
228 | } | |
229 | ||
b799113b | 230 | void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback) { |
ea2d184c | 231 | const char *temp = name; |
f33b048a | 232 | Parse_(pool, signature, &temp, '\0', callback); |
ea2d184c JF |
233 | _assert(temp[-1] == '\0'); |
234 | } | |
235 | ||
b799113b | 236 | const char *Unparse(CYPool &pool, struct Signature *signature) { |
ea2d184c JF |
237 | const char *value = ""; |
238 | size_t offset; | |
239 | ||
240 | for (offset = 0; offset != signature->count; ++offset) { | |
b21525c7 | 241 | const char *type = Unparse(pool, signature->elements[offset].type); |
0cbeddf8 | 242 | value = pool.strcat(value, type, NULL); |
ea2d184c JF |
243 | } |
244 | ||
245 | return value; | |
246 | } | |
247 | ||
b799113b | 248 | const char *Unparse_(CYPool &pool, struct Type *type) { |
51714815 | 249 | switch (type->primitive) { |
ea2d184c | 250 | case typename_P: return "#"; |
0cbeddf8 | 251 | case union_P: return pool.strcat("(", Unparse(pool, &type->data.signature), ")", NULL); |
ea2d184c JF |
252 | case string_P: return "*"; |
253 | case selector_P: return ":"; | |
ecf94af8 | 254 | case block_P: return "@?"; |
0cbeddf8 | 255 | case object_P: return type->name == NULL ? "@" : pool.strcat("@\"", type->name, "\"", NULL); |
ea2d184c JF |
256 | case boolean_P: return "B"; |
257 | case uchar_P: return "C"; | |
258 | case uint_P: return "I"; | |
259 | case ulong_P: return "L"; | |
260 | case ulonglong_P: return "Q"; | |
261 | case ushort_P: return "S"; | |
262 | ||
263 | case array_P: { | |
b21525c7 | 264 | const char *value = Unparse(pool, type->data.data.type); |
0cbeddf8 | 265 | return pool.strcat("[", pool.itoa(type->data.data.size), value, "]", NULL); |
ea2d184c JF |
266 | } break; |
267 | ||
0cbeddf8 JF |
268 | case pointer_P: return pool.strcat("^", type->data.data.type == NULL ? "v" : Unparse(pool, type->data.data.type), NULL); |
269 | case bit_P: return pool.strcat("b", pool.itoa(type->data.data.size), NULL); | |
ea2d184c JF |
270 | case char_P: return "c"; |
271 | case double_P: return "d"; | |
272 | case float_P: return "f"; | |
273 | case int_P: return "i"; | |
274 | case long_P: return "l"; | |
275 | case longlong_P: return "q"; | |
276 | case short_P: return "s"; | |
277 | case void_P: return "v"; | |
0cbeddf8 | 278 | case struct_P: return pool.strcat("{", type->name == NULL ? "?" : type->name, "=", Unparse(pool, &type->data.signature), "}", NULL); |
ea2d184c JF |
279 | } |
280 | ||
281 | _assert(false); | |
282 | return NULL; | |
283 | } | |
284 | ||
b799113b | 285 | const char *Unparse(CYPool &pool, struct Type *type) { |
51714815 JF |
286 | if (type == NULL) |
287 | return "?"; | |
288 | ||
289 | const char *base(Unparse_(pool, type)); | |
290 | if (type->flags == 0) | |
291 | return base; | |
292 | ||
293 | #define iovec_(base, size) \ | |
294 | (struct iovec) {const_cast<char *>(base), size} | |
295 | ||
0cbeddf8 JF |
296 | size_t size(strlen(base)); |
297 | char buffer[7 + size]; | |
298 | size_t offset(0); | |
51714815 JF |
299 | |
300 | if ((type->flags & JOC_TYPE_INOUT) != 0) | |
0cbeddf8 | 301 | buffer[offset++] = 'N'; |
51714815 | 302 | if ((type->flags & JOC_TYPE_IN) != 0) |
0cbeddf8 | 303 | buffer[offset++] = 'n'; |
51714815 | 304 | if ((type->flags & JOC_TYPE_BYCOPY) != 0) |
0cbeddf8 | 305 | buffer[offset++] = 'O'; |
51714815 | 306 | if ((type->flags & JOC_TYPE_OUT) != 0) |
0cbeddf8 | 307 | buffer[offset++] = 'o'; |
51714815 | 308 | if ((type->flags & JOC_TYPE_BYREF) != 0) |
0cbeddf8 | 309 | buffer[offset++] = 'R'; |
51714815 | 310 | if ((type->flags & JOC_TYPE_CONST) != 0) |
0cbeddf8 | 311 | buffer[offset++] = 'r'; |
51714815 | 312 | if ((type->flags & JOC_TYPE_ONEWAY) != 0) |
0cbeddf8 | 313 | buffer[offset++] = 'V'; |
51714815 | 314 | |
0cbeddf8 JF |
315 | memcpy(buffer + offset, base, size); |
316 | return pool.strmemdup(buffer, offset + size); | |
51714815 JF |
317 | } |
318 | ||
ea2d184c | 319 | } |