]> git.saurik.com Git - cycript.git/blame - sig/types.hpp
With -p on all platforms, we can't use asprintf().
[cycript.git] / sig / types.hpp
CommitLineData
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 38class CYPool;
5b4dabb2 39struct CYType;
574d4720 40struct CYTypedParameter;
0559abf8 41
ea2d184c
JF
42namespace 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
52struct 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
77template <typename Type_>
78struct 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
93struct Element {
b799113b 94 const char *name;
0559abf8 95 Type *type;
ea2d184c
JF
96 size_t offset;
97};
98
99struct Signature {
0559abf8 100 Element *elements;
ea2d184c
JF
101 size_t count;
102};
103
0559abf8
JF
104struct Void :
105 Type
106{
2c4a8bb6
JF
107 Void() {
108 }
109
110 Void(bool constant) {
111 if (constant)
112 flags |= JOC_TYPE_CONST;
113 }
114
a109809e 115 Void *Copy(CYPool &pool, const char *rename = NULL) const override;
ea2d184c 116
0559abf8 117 const char *Encode(CYPool &pool) const override;
5b4dabb2 118 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
119
120 ffi_type *GetFFI(CYPool &pool) const override;
121 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
122 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
123};
124
125struct Unknown :
126 Type
127{
a109809e 128 Unknown *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
129
130 const char *Encode(CYPool &pool) const override;
5b4dabb2 131 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
132
133 ffi_type *GetFFI(CYPool &pool) const override;
134 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
135 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
136};
137
138struct String :
139 Type
140{
284f434e
JF
141 String() {
142 }
143
144 String(bool constant) {
145 if (constant)
146 flags |= JOC_TYPE_CONST;
147 }
148
a109809e 149 String *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
150
151 const char *Encode(CYPool &pool) const override;
5b4dabb2 152 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
153
154 ffi_type *GetFFI(CYPool &pool) const override;
155 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
156 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
157};
158
e2ce853b 159#ifdef CY_OBJECTIVEC
0559abf8
JF
160struct Meta :
161 Type
162{
a109809e 163 Meta *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
164
165 const char *Encode(CYPool &pool) const override;
5b4dabb2 166 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
167
168 ffi_type *GetFFI(CYPool &pool) const override;
169 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
170 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
171};
172
173struct Selector :
174 Type
175{
a109809e 176 Selector *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
177
178 const char *Encode(CYPool &pool) const override;
5b4dabb2 179 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
180
181 ffi_type *GetFFI(CYPool &pool) const override;
182 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
183 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
184};
e2ce853b 185#endif
0559abf8
JF
186
187struct Bits :
188 Type
189{
190 size_t size;
191
192 Bits(size_t size) :
193 size(size)
194 {
195 }
196
a109809e 197 Bits *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
198
199 const char *Encode(CYPool &pool) const override;
5b4dabb2 200 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
201
202 ffi_type *GetFFI(CYPool &pool) const override;
203 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
204 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
205};
206
207struct Pointer :
208 Type
209{
210 Type &type;
211
212 Pointer(Type &type) :
213 type(type)
214 {
215 }
216
a109809e 217 Pointer *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;
225};
226
227struct Array :
228 Type
229{
230 Type &type;
231 size_t size;
232
233 Array(Type &type, size_t size = _not(size_t)) :
234 type(type),
235 size(size)
236 {
237 }
238
a109809e 239 Array *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
240
241 const char *Encode(CYPool &pool) const override;
5b4dabb2 242 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
243
244 ffi_type *GetFFI(CYPool &pool) const override;
245 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
246 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
0559abf8
JF
247};
248
e2ce853b 249#ifdef CY_OBJECTIVEC
0559abf8
JF
250struct Object :
251 Type
252{
21d5f610 253 const char *name;
ea2d184c 254
0559abf8
JF
255 Object(const char *name = NULL) :
256 name(name)
257 {
258 }
ea2d184c 259
a109809e 260 Object *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
261
262 const char *Encode(CYPool &pool) const override;
5b4dabb2 263 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
264
265 ffi_type *GetFFI(CYPool &pool) const override;
266 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
267 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
ea2d184c 268};
e2ce853b 269#endif
ea2d184c 270
aaa29c28
JF
271struct Constant {
272 const char *name;
273 double value;
274};
275
276struct Enum :
277 Type
278{
279 Type &type;
280 unsigned count;
281 const char *name;
282
283 Constant *constants;
284
285 Enum(Type &type, unsigned count, const char *name = NULL) :
286 type(type),
287 count(count),
288 name(name),
289 constants(NULL)
290 {
291 }
292
293 Enum *Copy(CYPool &pool, const char *rename = NULL) const override;
294 const char *GetName() const override;
295
296 const char *Encode(CYPool &pool) const override;
5b4dabb2 297 CYType *Decode(CYPool &pool) const override;
aaa29c28
JF
298
299 ffi_type *GetFFI(CYPool &pool) const override;
300 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
301 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
302};
303
0559abf8
JF
304struct Aggregate :
305 Type
306{
307 bool overlap;
308 const char *name;
309 Signature signature;
ea2d184c 310
0559abf8
JF
311 Aggregate(bool overlap, const char *name = NULL) :
312 overlap(overlap),
313 name(name)
314 {
315 }
3fe16be7 316
a109809e 317 Aggregate *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
318 const char *GetName() const override;
319
320 const char *Encode(CYPool &pool) const override;
5b4dabb2 321 CYType *Decode(CYPool &pool) const override;
0559abf8
JF
322
323 ffi_type *GetFFI(CYPool &pool) const override;
324 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
325 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
326};
327
328struct Callable :
329 Type
330{
331 Signature signature;
574d4720 332
5b4dabb2
JF
333 CYType *Decode(CYPool &pool) const override;
334 virtual CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const = 0;
0559abf8
JF
335};
336
337struct Function :
338 Callable
339{
574d4720
JF
340 bool variadic;
341
342 Function(bool variadic) :
343 variadic(variadic)
344 {
345 }
346
a109809e 347 Function *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
348
349 const char *Encode(CYPool &pool) const override;
5b4dabb2 350 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
0559abf8
JF
351
352 ffi_type *GetFFI(CYPool &pool) const override;
353 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
354 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
355};
356
e2ce853b 357#ifdef CY_OBJECTIVEC
0559abf8
JF
358struct Block :
359 Callable
360{
a109809e 361 Block *Copy(CYPool &pool, const char *rename = NULL) const override;
0559abf8
JF
362
363 const char *Encode(CYPool &pool) const override;
5b4dabb2
JF
364 CYType *Decode(CYPool &pool) const override;
365 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
0559abf8
JF
366
367 ffi_type *GetFFI(CYPool &pool) const override;
368 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
369 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
370};
e2ce853b 371#endif
0559abf8
JF
372
373Type *joc_parse_type(char **name, char eos, bool variable, bool signature);
374void joc_parse_signature(Signature *signature, char **name, char eos, bool variable);
bd17e6f3 375
ea2d184c
JF
376}
377
378#endif/*SIG_TYPES_H*/