]>
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 | ||
a109809e | 60 | virtual Type *Copy(CYPool &pool, const char *rename = NULL) const = 0; |
0559abf8 JF |
61 | virtual const char *GetName() const; |
62 | ||
63 | virtual const char *Encode(CYPool &pool) const = 0; | |
5b4dabb2 | 64 | virtual CYType *Decode(CYPool &pool) const = 0; |
0559abf8 JF |
65 | |
66 | virtual ffi_type *GetFFI(CYPool &pool) const = 0; | |
67 | virtual void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const = 0; | |
68 | virtual JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) const = 0; | |
0559abf8 JF |
69 | }; |
70 | ||
71 | template <typename Type_> | |
72 | struct Primitive : | |
73 | Type | |
74 | { | |
75 | Primitive *Copy(CYPool &pool, const char *name) const { | |
76 | return new(pool) Primitive(); | |
77 | } | |
78 | ||
79 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 80 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
81 | |
82 | ffi_type *GetFFI(CYPool &pool) const override; | |
83 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
84 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
ea2d184c JF |
85 | }; |
86 | ||
87 | struct Element { | |
b799113b | 88 | const char *name; |
0559abf8 | 89 | Type *type; |
ea2d184c JF |
90 | size_t offset; |
91 | }; | |
92 | ||
93 | struct Signature { | |
0559abf8 | 94 | Element *elements; |
ea2d184c JF |
95 | size_t count; |
96 | }; | |
97 | ||
0559abf8 JF |
98 | struct Void : |
99 | Type | |
100 | { | |
a109809e | 101 | Void *Copy(CYPool &pool, const char *rename = NULL) const override; |
ea2d184c | 102 | |
0559abf8 | 103 | const char *Encode(CYPool &pool) const override; |
5b4dabb2 | 104 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
105 | |
106 | ffi_type *GetFFI(CYPool &pool) const override; | |
107 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
108 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
109 | }; | |
110 | ||
111 | struct Unknown : | |
112 | Type | |
113 | { | |
a109809e | 114 | Unknown *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
115 | |
116 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 117 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
118 | |
119 | ffi_type *GetFFI(CYPool &pool) const override; | |
120 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
121 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
122 | }; | |
123 | ||
124 | struct String : | |
125 | Type | |
126 | { | |
a109809e | 127 | String *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
128 | |
129 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 130 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
131 | |
132 | ffi_type *GetFFI(CYPool &pool) const override; | |
133 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
134 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
135 | }; | |
136 | ||
e2ce853b | 137 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
138 | struct Meta : |
139 | Type | |
140 | { | |
a109809e | 141 | Meta *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 | ||
151 | struct Selector : | |
152 | Type | |
153 | { | |
a109809e | 154 | Selector *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
155 | |
156 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 157 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
158 | |
159 | ffi_type *GetFFI(CYPool &pool) const override; | |
160 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
161 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
162 | }; | |
e2ce853b | 163 | #endif |
0559abf8 JF |
164 | |
165 | struct Bits : | |
166 | Type | |
167 | { | |
168 | size_t size; | |
169 | ||
170 | Bits(size_t size) : | |
171 | size(size) | |
172 | { | |
173 | } | |
174 | ||
a109809e | 175 | Bits *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
176 | |
177 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 178 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
179 | |
180 | ffi_type *GetFFI(CYPool &pool) const override; | |
181 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
182 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
183 | }; | |
184 | ||
185 | struct Pointer : | |
186 | Type | |
187 | { | |
188 | Type &type; | |
189 | ||
190 | Pointer(Type &type) : | |
191 | type(type) | |
192 | { | |
193 | } | |
194 | ||
a109809e | 195 | Pointer *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
196 | |
197 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 198 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
199 | |
200 | ffi_type *GetFFI(CYPool &pool) const override; | |
201 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
202 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
203 | }; | |
204 | ||
205 | struct Array : | |
206 | Type | |
207 | { | |
208 | Type &type; | |
209 | size_t size; | |
210 | ||
211 | Array(Type &type, size_t size = _not(size_t)) : | |
212 | type(type), | |
213 | size(size) | |
214 | { | |
215 | } | |
216 | ||
a109809e | 217 | Array *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
218 | |
219 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 220 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
221 | |
222 | ffi_type *GetFFI(CYPool &pool) const override; | |
223 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
224 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
0559abf8 JF |
225 | }; |
226 | ||
e2ce853b | 227 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
228 | struct Object : |
229 | Type | |
230 | { | |
21d5f610 | 231 | const char *name; |
ea2d184c | 232 | |
0559abf8 JF |
233 | Object(const char *name = NULL) : |
234 | name(name) | |
235 | { | |
236 | } | |
ea2d184c | 237 | |
a109809e | 238 | Object *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
239 | |
240 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 241 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
242 | |
243 | ffi_type *GetFFI(CYPool &pool) const override; | |
244 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
245 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
ea2d184c | 246 | }; |
e2ce853b | 247 | #endif |
ea2d184c | 248 | |
aaa29c28 JF |
249 | struct Constant { |
250 | const char *name; | |
251 | double value; | |
252 | }; | |
253 | ||
254 | struct Enum : | |
255 | Type | |
256 | { | |
257 | Type &type; | |
258 | unsigned count; | |
259 | const char *name; | |
260 | ||
261 | Constant *constants; | |
262 | ||
263 | Enum(Type &type, unsigned count, const char *name = NULL) : | |
264 | type(type), | |
265 | count(count), | |
266 | name(name), | |
267 | constants(NULL) | |
268 | { | |
269 | } | |
270 | ||
271 | Enum *Copy(CYPool &pool, const char *rename = NULL) const override; | |
272 | const char *GetName() const override; | |
273 | ||
274 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 275 | CYType *Decode(CYPool &pool) const override; |
aaa29c28 JF |
276 | |
277 | ffi_type *GetFFI(CYPool &pool) const override; | |
278 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
279 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
280 | }; | |
281 | ||
0559abf8 JF |
282 | struct Aggregate : |
283 | Type | |
284 | { | |
285 | bool overlap; | |
286 | const char *name; | |
287 | Signature signature; | |
ea2d184c | 288 | |
0559abf8 JF |
289 | Aggregate(bool overlap, const char *name = NULL) : |
290 | overlap(overlap), | |
291 | name(name) | |
292 | { | |
293 | } | |
3fe16be7 | 294 | |
a109809e | 295 | Aggregate *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
296 | const char *GetName() const override; |
297 | ||
298 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 299 | CYType *Decode(CYPool &pool) const override; |
0559abf8 JF |
300 | |
301 | ffi_type *GetFFI(CYPool &pool) const override; | |
302 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
303 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
304 | }; | |
305 | ||
306 | struct Callable : | |
307 | Type | |
308 | { | |
309 | Signature signature; | |
574d4720 | 310 | |
5b4dabb2 JF |
311 | CYType *Decode(CYPool &pool) const override; |
312 | virtual CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const = 0; | |
0559abf8 JF |
313 | }; |
314 | ||
315 | struct Function : | |
316 | Callable | |
317 | { | |
574d4720 JF |
318 | bool variadic; |
319 | ||
320 | Function(bool variadic) : | |
321 | variadic(variadic) | |
322 | { | |
323 | } | |
324 | ||
a109809e | 325 | Function *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
326 | |
327 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 | 328 | CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override; |
0559abf8 JF |
329 | |
330 | ffi_type *GetFFI(CYPool &pool) const override; | |
331 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
332 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
333 | }; | |
334 | ||
e2ce853b | 335 | #ifdef CY_OBJECTIVEC |
0559abf8 JF |
336 | struct Block : |
337 | Callable | |
338 | { | |
a109809e | 339 | Block *Copy(CYPool &pool, const char *rename = NULL) const override; |
0559abf8 JF |
340 | |
341 | const char *Encode(CYPool &pool) const override; | |
5b4dabb2 JF |
342 | CYType *Decode(CYPool &pool) const override; |
343 | CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override; | |
0559abf8 JF |
344 | |
345 | ffi_type *GetFFI(CYPool &pool) const override; | |
346 | void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override; | |
347 | JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override; | |
348 | }; | |
e2ce853b | 349 | #endif |
0559abf8 JF |
350 | |
351 | Type *joc_parse_type(char **name, char eos, bool variable, bool signature); | |
352 | void joc_parse_signature(Signature *signature, char **name, char eos, bool variable); | |
bd17e6f3 | 353 | |
ea2d184c JF |
354 | } |
355 | ||
356 | #endif/*SIG_TYPES_H*/ |