]> git.saurik.com Git - cycript.git/blob - sig/types.hpp
CYUTF8String cannot have a negative size, even -1.
[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() {
134 }
135
136 String(bool constant) {
137 if (constant)
138 flags |= JOC_TYPE_CONST;
139 }
140
141 String *Copy(CYPool &pool, const char *rename = NULL) const override;
142
143 const char *Encode(CYPool &pool) const override;
144 CYType *Decode(CYPool &pool) const override;
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 #ifdef CY_OBJECTIVEC
152 struct Meta :
153 Type
154 {
155 Meta *Copy(CYPool &pool, const char *rename = NULL) const override;
156
157 const char *Encode(CYPool &pool) const override;
158 CYType *Decode(CYPool &pool) const override;
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 {
168 Selector *Copy(CYPool &pool, const char *rename = NULL) const override;
169
170 const char *Encode(CYPool &pool) const override;
171 CYType *Decode(CYPool &pool) const override;
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 };
177 #endif
178
179 struct Bits :
180 Type
181 {
182 size_t size;
183
184 Bits(size_t size) :
185 size(size)
186 {
187 }
188
189 Bits *Copy(CYPool &pool, const char *rename = NULL) const override;
190
191 const char *Encode(CYPool &pool) const override;
192 CYType *Decode(CYPool &pool) const override;
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
209 Pointer *Copy(CYPool &pool, const char *rename = NULL) const override;
210
211 const char *Encode(CYPool &pool) const override;
212 CYType *Decode(CYPool &pool) const override;
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
231 Array *Copy(CYPool &pool, const char *rename = NULL) const override;
232
233 const char *Encode(CYPool &pool) const override;
234 CYType *Decode(CYPool &pool) const override;
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;
239 };
240
241 #ifdef CY_OBJECTIVEC
242 struct Object :
243 Type
244 {
245 const char *name;
246
247 Object(const char *name = NULL) :
248 name(name)
249 {
250 }
251
252 Object *Copy(CYPool &pool, const char *rename = NULL) const override;
253
254 const char *Encode(CYPool &pool) const override;
255 CYType *Decode(CYPool &pool) const override;
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;
260 };
261 #endif
262
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;
289 CYType *Decode(CYPool &pool) const override;
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
296 struct Aggregate :
297 Type
298 {
299 bool overlap;
300 const char *name;
301 Signature signature;
302
303 Aggregate(bool overlap, const char *name = NULL) :
304 overlap(overlap),
305 name(name)
306 {
307 }
308
309 Aggregate *Copy(CYPool &pool, const char *rename = NULL) const override;
310 const char *GetName() const override;
311
312 const char *Encode(CYPool &pool) const override;
313 CYType *Decode(CYPool &pool) const override;
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;
324
325 CYType *Decode(CYPool &pool) const override;
326 virtual CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const = 0;
327 };
328
329 struct Function :
330 Callable
331 {
332 bool variadic;
333
334 Function(bool variadic) :
335 variadic(variadic)
336 {
337 }
338
339 Function *Copy(CYPool &pool, const char *rename = NULL) const override;
340
341 const char *Encode(CYPool &pool) const override;
342 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
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
349 #ifdef CY_OBJECTIVEC
350 struct Block :
351 Callable
352 {
353 Block *Copy(CYPool &pool, const char *rename = NULL) const override;
354
355 const char *Encode(CYPool &pool) const override;
356 CYType *Decode(CYPool &pool) const override;
357 CYType *Modify(CYPool &pool, CYType *result, CYTypedParameter *parameters) const override;
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 };
363 #endif
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);
367
368 }
369
370 #endif/*SIG_TYPES_H*/