]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
b4aa79af JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
b4aa79af | 6 | /* |
f95d2598 JF |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 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 | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
b3378a02 | 19 | **/ |
b4aa79af JF |
20 | /* }}} */ |
21 | ||
ea2d184c JF |
22 | #ifndef SIG_TYPES_H |
23 | #define SIG_TYPES_H | |
24 | ||
b799113b JF |
25 | #include <cstdlib> |
26 | #include <stdint.h> | |
27 | ||
0559abf8 JF |
28 | #include <JavaScriptCore/JSBase.h> |
29 | ||
30 | #ifdef HAVE_FFI_FFI_H | |
31 | #include <ffi/ffi.h> | |
32 | #else | |
33 | #include <ffi.h> | |
34 | #endif | |
35 | ||
37954781 | 36 | #include "Standard.hpp" |
ea2d184c | 37 | |
0559abf8 | 38 | class CYPool; |
5b4dabb2 | 39 | struct CYType; |
574d4720 | 40 | struct CYTypedParameter; |
0559abf8 | 41 | |
ea2d184c JF |
42 | namespace sig { |
43 | ||
0559abf8 JF |
44 | #define JOC_TYPE_INOUT (1 << 0) |
45 | #define JOC_TYPE_IN (1 << 1) | |
46 | #define JOC_TYPE_BYCOPY (1 << 2) | |
47 | #define JOC_TYPE_OUT (1 << 3) | |
48 | #define JOC_TYPE_BYREF (1 << 4) | |
49 | #define JOC_TYPE_CONST (1 << 5) | |
50 | #define JOC_TYPE_ONEWAY (1 << 6) | |
51 | ||
52 | struct Type { | |
53 | uint8_t flags; | |
54 | ||
55 | Type() : | |
56 | flags(0) | |
57 | { | |
58 | } | |
59 | ||
0c172698 JF |
60 | template <typename Type_> |
61 | _finline Type_ *Flag(Type_ *type) const { | |
62 | type->flags = flags; | |
63 | return type; | |
64 | } | |
65 | ||
a109809e | 66 | virtual Type *Copy(CYPool &pool, const char *rename = NULL) const = 0; |
0559abf8 JF |
67 | virtual const char *GetName() const; |
68 | ||
69 | virtual const char *Encode(CYPool &pool) const = 0; | |
5b4dabb2 | 70 | virtual CYType *Decode(CYPool &pool) const = 0; |
0559abf8 JF |
71 | |
72 | virtual ffi_type *GetFFI(CYPool &pool) const = 0; | |
73 | virtual void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const = 0; | |
74 | virtual JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) const = 0; | |
0559abf8 JF |
75 | }; |
76 | ||
77 | template <typename Type_> | |
78 | struct Primitive : | |
79 | Type | |
80 | { | |
81 | Primitive *Copy(CYPool &pool, const char *name) const { | |
0c172698 | 82 | return Flag(new(pool) Primitive()); |
0559abf8 JF |
83 | } |
84 | ||
85 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 86 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
87 | |
88 | ffi_type *GetFFI(CYPool &pool) const override; | |
89 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
90 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
ea2d184c JF |
91 | }; |
92 | ||
93 | struct Element { | |
b799113b | 94 | const char *name; |
0559abf8 | 95 | Type *type; |
ea2d184c JF |
96 | size_t offset; |
97 | }; | |
98 | ||
99 | struct Signature { | |
0559abf8 | 100 | Element *elements; |
ea2d184c JF |
101 | size_t count; |
102 | }; | |
103 | ||
0559abf8 JF |
104 | struct Void : |
105 | Type | |
106 | { | |
a109809e | 107 | Void *Copy(CYPool &pool, const char *rename = NULL) const override; |
ea2d184c | 108 | |
0559abf8 | 109 | const char *Encode(CYPool &pool) const override; |
5b4dabb2 | 110 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
111 | |
112 | ffi_type *GetFFI(CYPool &pool) const override; | |
113 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
114 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
115 | }; | |
116 | ||
117 | struct Unknown : | |
118 | Type | |
119 | { | |
a109809e | 120 | Unknown *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
121 | |
122 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 123 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
124 | |
125 | ffi_type *GetFFI(CYPool &pool) const override; | |
126 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
127 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
128 | }; | |
129 | ||
130 | struct String : | |
131 | Type | |
132 | { | |
284f434e JF |
133 | String() { |
134 | } | |
135 | ||
136 | String(bool constant) { | |
137 | if (constant) | |
138 | flags |= JOC_TYPE_CONST; | |
139 | } | |
140 | ||
a109809e | 141 | String *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
142 | |
143 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 144 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
145 | |
146 | ffi_type *GetFFI(CYPool &pool) const override; | |
147 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
148 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
149 | }; | |
150 | ||
e2ce853b | 151 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
152 | struct Meta : |
153 | Type | |
154 | { | |
a109809e | 155 | Meta *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
156 | |
157 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 158 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
159 | |
160 | ffi_type *GetFFI(CYPool &pool) const override; | |
161 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
162 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
163 | }; | |
164 | ||
165 | struct Selector : | |
166 | Type | |
167 | { | |
a109809e | 168 | Selector *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
169 | |
170 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 171 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
172 | |
173 | ffi_type *GetFFI(CYPool &pool) const override; | |
174 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
175 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
176 | }; | |
e2ce853b | 177 | #endif |
0559abf8 JF |
178 | |
179 | struct Bits : | |
180 | Type | |
181 | { | |
182 | size_t size; | |
183 | ||
184 | Bits(size_t size) : | |
185 | size(size) | |
186 | { | |
187 | } | |
188 | ||
a109809e | 189 | Bits *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
190 | |
191 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 192 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
193 | |
194 | ffi_type *GetFFI(CYPool &pool) const override; | |
195 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
196 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
197 | }; | |
198 | ||
199 | struct Pointer : | |
200 | Type | |
201 | { | |
202 | Type &type; | |
203 | ||
204 | Pointer(Type &type) : | |
205 | type(type) | |
206 | { | |
207 | } | |
208 | ||
a109809e | 209 | Pointer *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
210 | |
211 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 212 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
213 | |
214 | ffi_type *GetFFI(CYPool &pool) const override; | |
215 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
216 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
217 | }; | |
218 | ||
219 | struct Array : | |
220 | Type | |
221 | { | |
222 | Type &type; | |
223 | size_t size; | |
224 | ||
225 | Array(Type &type, size_t size = _not(size_t)) : | |
226 | type(type), | |
227 | size(size) | |
228 | { | |
229 | } | |
230 | ||
a109809e | 231 | Array *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
232 | |
233 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 234 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
235 | |
236 | ffi_type *GetFFI(CYPool &pool) const override; | |
237 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
238 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
0559abf8 JF |
239 | }; |
240 | ||
e2ce853b | 241 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
242 | struct Object : |
243 | Type | |
244 | { | |
21d5f610 | 245 | const char *name; |
ea2d184c | 246 | |
0559abf8 JF |
247 | Object(const char *name = NULL) : |
248 | name(name) | |
249 | { | |
250 | } | |
ea2d184c | 251 | |
a109809e | 252 | Object *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
253 | |
254 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 255 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
256 | |
257 | ffi_type *GetFFI(CYPool &pool) const override; | |
258 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
259 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
ea2d184c | 260 | }; |
e2ce853b | 261 | #endif |
ea2d184c | 262 | |
aaa29c28 JF |
263 | struct Constant { |
264 | const char *name; | |
265 | double value; | |
266 | }; | |
267 | ||
268 | struct Enum : | |
269 | Type | |
270 | { | |
271 | Type &type; | |
272 | unsigned count; | |
273 | const char *name; | |
274 | ||
275 | Constant *constants; | |
276 | ||
277 | Enum(Type &type, unsigned count, const char *name = NULL) : | |
278 | type(type), | |
279 | count(count), | |
280 | name(name), | |
281 | constants(NULL) | |
282 | { | |
283 | } | |
284 | ||
285 | Enum *Copy(CYPool &pool, const char *rename = NULL) const override; | |
286 | const char *GetName() const override; | |
287 | ||
288 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 289 | CYType *Decode(CYPool &pool) const override; |
aaa29c28 JF |
290 | |
291 | ffi_type *GetFFI(CYPool &pool) const override; | |
292 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
293 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
294 | }; | |
295 | ||
0559abf8 JF |
296 | struct Aggregate : |
297 | Type | |
298 | { | |
299 | bool overlap; | |
300 | const char *name; | |
301 | Signature signature; | |
ea2d184c | 302 | |
0559abf8 JF |
303 | Aggregate(bool overlap, const char *name = NULL) : |
304 | overlap(overlap), | |
305 | name(name) | |
306 | { | |
307 | } | |
3fe16be7 | 308 | |
a109809e | 309 | Aggregate *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
310 | const char *GetName() const override; |
311 | ||
312 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 313 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
314 | |
315 | ffi_type *GetFFI(CYPool &pool) const override; | |
316 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
317 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
318 | }; | |
319 | ||
320 | struct Callable : | |
321 | Type | |
322 | { | |
323 | Signature signature; | |
574d4720 | 324 | |
5b4dabb2 JF |
325 | CYType *Decode(CYPool &pool) const override; |
326 | virtual CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const = 0; | |
0559abf8 JF |
327 | }; |
328 | ||
329 | struct Function : | |
330 | Callable | |
331 | { | |
574d4720 JF |
332 | bool variadic; |
333 | ||
334 | Function(bool variadic) : | |
335 | variadic(variadic) | |
336 | { | |
337 | } | |
338 | ||
a109809e | 339 | Function *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
340 | |
341 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 342 | CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override; |
0559abf8 JF |
343 | |
344 | ffi_type *GetFFI(CYPool &pool) const override; | |
345 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
346 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
347 | }; | |
348 | ||
e2ce853b | 349 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
350 | struct Block : |
351 | Callable | |
352 | { | |
a109809e | 353 | Block *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
354 | |
355 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 JF |
356 | CYType *Decode(CYPool &pool) const override; |
357 | CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override; | |
0559abf8 JF |
358 | |
359 | ffi_type *GetFFI(CYPool &pool) const override; | |
360 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
361 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
362 | }; | |
e2ce853b | 363 | #endif |
0559abf8 JF |
364 | |
365 | Type *joc_parse_type(char **name, char eos, bool variable, bool signature); | |
366 | void joc_parse_signature(Signature *signature, char **name, char eos, bool variable); | |
bd17e6f3 | 367 | |
ea2d184c JF |
368 | } |
369 | ||
370 | #endif/*SIG_TYPES_H*/ |