]> git.saurik.com Git - cycript.git/blob - sig/types.hpp
Move number's typeid set to FromFFI from Type_new.
[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 *Copy(CYPool &pool, const char *rename = NULL) const override;
108
109 const char *Encode(CYPool &pool) const override;
110 CYType *Decode(CYPool &pool) const override;
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 {
120 Unknown *Copy(CYPool &pool, const char *rename = NULL) const override;
121
122 const char *Encode(CYPool &pool) const override;
123 CYType *Decode(CYPool &pool) const override;
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 {
133 String *Copy(CYPool &pool, const char *rename = NULL) const override;
134
135 const char *Encode(CYPool &pool) const override;
136 CYType *Decode(CYPool &pool) const override;
137
138 ffi_type *GetFFI(CYPool &pool) const override;
139 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
140 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
141 };
142
143 #ifdef CY_OBJECTIVEC
144 struct Meta :
145 Type
146 {
147 Meta *Copy(CYPool &pool, const char *rename = NULL) const override;
148
149 const char *Encode(CYPool &pool) const override;
150 CYType *Decode(CYPool &pool) const override;
151
152 ffi_type *GetFFI(CYPool &pool) const override;
153 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
154 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
155 };
156
157 struct Selector :
158 Type
159 {
160 Selector *Copy(CYPool &pool, const char *rename = NULL) const override;
161
162 const char *Encode(CYPool &pool) const override;
163 CYType *Decode(CYPool &pool) const override;
164
165 ffi_type *GetFFI(CYPool &pool) const override;
166 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
167 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
168 };
169 #endif
170
171 struct Bits :
172 Type
173 {
174 size_t size;
175
176 Bits(size_t size) :
177 size(size)
178 {
179 }
180
181 Bits *Copy(CYPool &pool, const char *rename = NULL) const override;
182
183 const char *Encode(CYPool &pool) const override;
184 CYType *Decode(CYPool &pool) const override;
185
186 ffi_type *GetFFI(CYPool &pool) const override;
187 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
188 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
189 };
190
191 struct Pointer :
192 Type
193 {
194 Type &type;
195
196 Pointer(Type &type) :
197 type(type)
198 {
199 }
200
201 Pointer *Copy(CYPool &pool, const char *rename = NULL) const override;
202
203 const char *Encode(CYPool &pool) const override;
204 CYType *Decode(CYPool &pool) const override;
205
206 ffi_type *GetFFI(CYPool &pool) const override;
207 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
208 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
209 };
210
211 struct Array :
212 Type
213 {
214 Type &type;
215 size_t size;
216
217 Array(Type &type, size_t size = _not(size_t)) :
218 type(type),
219 size(size)
220 {
221 }
222
223 Array *Copy(CYPool &pool, const char *rename = NULL) const override;
224
225 const char *Encode(CYPool &pool) const override;
226 CYType *Decode(CYPool &pool) const override;
227
228 ffi_type *GetFFI(CYPool &pool) const override;
229 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
230 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
231 };
232
233 #ifdef CY_OBJECTIVEC
234 struct Object :
235 Type
236 {
237 const char *name;
238
239 Object(const char *name = NULL) :
240 name(name)
241 {
242 }
243
244 Object *Copy(CYPool &pool, const char *rename = NULL) const override;
245
246 const char *Encode(CYPool &pool) const override;
247 CYType *Decode(CYPool &pool) const override;
248
249 ffi_type *GetFFI(CYPool &pool) const override;
250 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
251 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
252 };
253 #endif
254
255 struct Constant {
256 const char *name;
257 double value;
258 };
259
260 struct Enum :
261 Type
262 {
263 Type &type;
264 unsigned count;
265 const char *name;
266
267 Constant *constants;
268
269 Enum(Type &type, unsigned count, const char *name = NULL) :
270 type(type),
271 count(count),
272 name(name),
273 constants(NULL)
274 {
275 }
276
277 Enum *Copy(CYPool &pool, const char *rename = NULL) const override;
278 const char *GetName() const override;
279
280 const char *Encode(CYPool &pool) const override;
281 CYType *Decode(CYPool &pool) const override;
282
283 ffi_type *GetFFI(CYPool &pool) const override;
284 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
285 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
286 };
287
288 struct Aggregate :
289 Type
290 {
291 bool overlap;
292 const char *name;
293 Signature signature;
294
295 Aggregate(bool overlap, const char *name = NULL) :
296 overlap(overlap),
297 name(name)
298 {
299 }
300
301 Aggregate *Copy(CYPool &pool, const char *rename = NULL) const override;
302 const char *GetName() const override;
303
304 const char *Encode(CYPool &pool) const override;
305 CYType *Decode(CYPool &pool) const override;
306
307 ffi_type *GetFFI(CYPool &pool) const override;
308 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
309 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
310 };
311
312 struct Callable :
313 Type
314 {
315 Signature signature;
316
317 CYType *Decode(CYPool &pool) const override;
318 virtual CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const = 0;
319 };
320
321 struct Function :
322 Callable
323 {
324 bool variadic;
325
326 Function(bool variadic) :
327 variadic(variadic)
328 {
329 }
330
331 Function *Copy(CYPool &pool, const char *rename = NULL) const override;
332
333 const char *Encode(CYPool &pool) const override;
334 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
335
336 ffi_type *GetFFI(CYPool &pool) const override;
337 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
338 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
339 };
340
341 #ifdef CY_OBJECTIVEC
342 struct Block :
343 Callable
344 {
345 Block *Copy(CYPool &pool, const char *rename = NULL) const override;
346
347 const char *Encode(CYPool &pool) const override;
348 CYType *Decode(CYPool &pool) const override;
349 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
350
351 ffi_type *GetFFI(CYPool &pool) const override;
352 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
353 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
354 };
355 #endif
356
357 Type *joc_parse_type(char **name, char eos, bool variable, bool signature);
358 void joc_parse_signature(Signature *signature, char **name, char eos, bool variable);
359
360 }
361
362 #endif/*SIG_TYPES_H*/