]> git.saurik.com Git - cycript.git/blob - sig/types.hpp
With -p on all platforms, we can't use asprintf().
[cycript.git] / sig / types.hpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
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
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
19 **/
20 /* }}} */
21
22 #ifndef SIG_TYPES_H
23 #define SIG_TYPES_H
24
25 #include <cstdlib>
26 #include <stdint.h>
27
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
36 #include "Standard.hpp"
37
38 class CYPool;
39 struct CYType;
40 struct CYTypedParameter;
41
42 namespace sig {
43
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
60 template <typename Type_>
61 _finline Type_ *Flag(Type_ *type) const {
62 type->flags = flags;
63 return type;
64 }
65
66 virtual Type *Copy(CYPool &pool, const char *rename = NULL) const = 0;
67 virtual const char *GetName() const;
68
69 virtual const char *Encode(CYPool &pool) const = 0;
70 virtual CYType *Decode(CYPool &pool) const = 0;
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;
75 };
76
77 template <typename Type_>
78 struct Primitive :
79 Type
80 {
81 Primitive *Copy(CYPool &pool, const char *name) const {
82 return Flag(new(pool) Primitive());
83 }
84
85 const char *Encode(CYPool &pool) const override;
86 CYType *Decode(CYPool &pool) const override;
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;
91 };
92
93 struct Element {
94 const char *name;
95 Type *type;
96 size_t offset;
97 };
98
99 struct Signature {
100 Element *elements;
101 size_t count;
102 };
103
104 struct Void :
105 Type
106 {
107 Void() {
108 }
109
110 Void(bool constant) {
111 if (constant)
112 flags |= JOC_TYPE_CONST;
113 }
114
115 Void *Copy(CYPool &pool, const char *rename = NULL) const override;
116
117 const char *Encode(CYPool &pool) const override;
118 CYType *Decode(CYPool &pool) const override;
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
125 struct Unknown :
126 Type
127 {
128 Unknown *Copy(CYPool &pool, const char *rename = NULL) const override;
129
130 const char *Encode(CYPool &pool) const override;
131 CYType *Decode(CYPool &pool) const override;
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
138 struct String :
139 Type
140 {
141 String() {
142 }
143
144 String(bool constant) {
145 if (constant)
146 flags |= JOC_TYPE_CONST;
147 }
148
149 String *Copy(CYPool &pool, const char *rename = NULL) const override;
150
151 const char *Encode(CYPool &pool) const override;
152 CYType *Decode(CYPool &pool) const override;
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
159 #ifdef CY_OBJECTIVEC
160 struct Meta :
161 Type
162 {
163 Meta *Copy(CYPool &pool, const char *rename = NULL) const override;
164
165 const char *Encode(CYPool &pool) const override;
166 CYType *Decode(CYPool &pool) const override;
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
173 struct Selector :
174 Type
175 {
176 Selector *Copy(CYPool &pool, const char *rename = NULL) const override;
177
178 const char *Encode(CYPool &pool) const override;
179 CYType *Decode(CYPool &pool) const override;
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 };
185 #endif
186
187 struct Bits :
188 Type
189 {
190 size_t size;
191
192 Bits(size_t size) :
193 size(size)
194 {
195 }
196
197 Bits *Copy(CYPool &pool, const char *rename = NULL) const override;
198
199 const char *Encode(CYPool &pool) const override;
200 CYType *Decode(CYPool &pool) const override;
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
207 struct Pointer :
208 Type
209 {
210 Type &type;
211
212 Pointer(Type &type) :
213 type(type)
214 {
215 }
216
217 Pointer *Copy(CYPool &pool, const char *rename = NULL) const override;
218
219 const char *Encode(CYPool &pool) const override;
220 CYType *Decode(CYPool &pool) const override;
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
227 struct 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
239 Array *Copy(CYPool &pool, const char *rename = NULL) const override;
240
241 const char *Encode(CYPool &pool) const override;
242 CYType *Decode(CYPool &pool) const override;
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;
247 };
248
249 #ifdef CY_OBJECTIVEC
250 struct Object :
251 Type
252 {
253 const char *name;
254
255 Object(const char *name = NULL) :
256 name(name)
257 {
258 }
259
260 Object *Copy(CYPool &pool, const char *rename = NULL) const override;
261
262 const char *Encode(CYPool &pool) const override;
263 CYType *Decode(CYPool &pool) const override;
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;
268 };
269 #endif
270
271 struct Constant {
272 const char *name;
273 double value;
274 };
275
276 struct 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;
297 CYType *Decode(CYPool &pool) const override;
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
304 struct Aggregate :
305 Type
306 {
307 bool overlap;
308 const char *name;
309 Signature signature;
310
311 Aggregate(bool overlap, const char *name = NULL) :
312 overlap(overlap),
313 name(name)
314 {
315 }
316
317 Aggregate *Copy(CYPool &pool, const char *rename = NULL) const override;
318 const char *GetName() const override;
319
320 const char *Encode(CYPool &pool) const override;
321 CYType *Decode(CYPool &pool) const override;
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
328 struct Callable :
329 Type
330 {
331 Signature signature;
332
333 CYType *Decode(CYPool &pool) const override;
334 virtual CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const = 0;
335 };
336
337 struct Function :
338 Callable
339 {
340 bool variadic;
341
342 Function(bool variadic) :
343 variadic(variadic)
344 {
345 }
346
347 Function *Copy(CYPool &pool, const char *rename = NULL) const override;
348
349 const char *Encode(CYPool &pool) const override;
350 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
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
357 #ifdef CY_OBJECTIVEC
358 struct Block :
359 Callable
360 {
361 Block *Copy(CYPool &pool, const char *rename = NULL) const override;
362
363 const char *Encode(CYPool &pool) const override;
364 CYType *Decode(CYPool &pool) const override;
365 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
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 };
371 #endif
372
373 Type *joc_parse_type(char **name, char eos, bool variable, bool signature);
374 void joc_parse_signature(Signature *signature, char **name, char eos, bool variable);
375
376 }
377
378 #endif/*SIG_TYPES_H*/