]> git.saurik.com Git - cycript.git/blob - sig/types.hpp
d437475091e978f8aa9c72179cfe2544f6a3c97d
[cycript.git] / sig / types.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 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 CYTypedIdentifier;
40
41 namespace sig {
42
43 #define JOC_TYPE_INOUT (1 << 0)
44 #define JOC_TYPE_IN (1 << 1)
45 #define JOC_TYPE_BYCOPY (1 << 2)
46 #define JOC_TYPE_OUT (1 << 3)
47 #define JOC_TYPE_BYREF (1 << 4)
48 #define JOC_TYPE_CONST (1 << 5)
49 #define JOC_TYPE_ONEWAY (1 << 6)
50
51 struct Type {
52 uint8_t flags;
53
54 Type() :
55 flags(0)
56 {
57 }
58
59 virtual Type *Copy(CYPool &pool, const char *name = NULL) const = 0;
60 virtual const char *GetName() const;
61
62 virtual const char *Encode(CYPool &pool) const = 0;
63 virtual CYTypedIdentifier *Decode(CYPool &pool) const = 0;
64
65 virtual ffi_type *GetFFI(CYPool &pool) const = 0;
66 virtual void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const = 0;
67 virtual JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL) const = 0;
68
69 virtual size_t Translate(Type *&type) const {
70 return _not(size_t);
71 }
72 };
73
74 template <typename Type_>
75 struct Primitive :
76 Type
77 {
78 Primitive *Copy(CYPool &pool, const char *name) const {
79 return new(pool) Primitive();
80 }
81
82 const char *Encode(CYPool &pool) const override;
83 CYTypedIdentifier *Decode(CYPool &pool) const override;
84
85 ffi_type *GetFFI(CYPool &pool) const override;
86 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
87 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
88 };
89
90 struct Element {
91 const char *name;
92 Type *type;
93 size_t offset;
94 };
95
96 struct Signature {
97 Element *elements;
98 size_t count;
99 };
100
101 struct Void :
102 Type
103 {
104 Void *Copy(CYPool &pool, const char *name = NULL) const override;
105
106 const char *Encode(CYPool &pool) const override;
107 CYTypedIdentifier *Decode(CYPool &pool) const override;
108
109 ffi_type *GetFFI(CYPool &pool) const override;
110 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
111 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
112 };
113
114 struct Unknown :
115 Type
116 {
117 Unknown *Copy(CYPool &pool, const char *name = NULL) const override;
118
119 const char *Encode(CYPool &pool) const override;
120 CYTypedIdentifier *Decode(CYPool &pool) const override;
121
122 ffi_type *GetFFI(CYPool &pool) const override;
123 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
124 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
125 };
126
127 struct String :
128 Type
129 {
130 String *Copy(CYPool &pool, const char *name = NULL) const override;
131
132 const char *Encode(CYPool &pool) const override;
133 CYTypedIdentifier *Decode(CYPool &pool) const override;
134
135 ffi_type *GetFFI(CYPool &pool) const override;
136 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
137 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
138 };
139
140 struct Meta :
141 Type
142 {
143 Meta *Copy(CYPool &pool, const char *name = NULL) const override;
144
145 const char *Encode(CYPool &pool) const override;
146 CYTypedIdentifier *Decode(CYPool &pool) const override;
147
148 ffi_type *GetFFI(CYPool &pool) const override;
149 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
150 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
151 };
152
153 struct Selector :
154 Type
155 {
156 Selector *Copy(CYPool &pool, const char *name = NULL) const override;
157
158 const char *Encode(CYPool &pool) const override;
159 CYTypedIdentifier *Decode(CYPool &pool) const override;
160
161 ffi_type *GetFFI(CYPool &pool) const override;
162 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
163 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
164 };
165
166 struct Bits :
167 Type
168 {
169 size_t size;
170
171 Bits(size_t size) :
172 size(size)
173 {
174 }
175
176 Bits *Copy(CYPool &pool, const char *name = NULL) const override;
177
178 const char *Encode(CYPool &pool) const override;
179 CYTypedIdentifier *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
186 struct Pointer :
187 Type
188 {
189 Type &type;
190
191 Pointer(Type &type) :
192 type(type)
193 {
194 }
195
196 Pointer *Copy(CYPool &pool, const char *name = NULL) const override;
197
198 const char *Encode(CYPool &pool) const override;
199 CYTypedIdentifier *Decode(CYPool &pool) const override;
200
201 ffi_type *GetFFI(CYPool &pool) const override;
202 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
203 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
204 };
205
206 struct Array :
207 Type
208 {
209 Type &type;
210 size_t size;
211
212 Array(Type &type, size_t size = _not(size_t)) :
213 type(type),
214 size(size)
215 {
216 }
217
218 Array *Copy(CYPool &pool, const char *name = NULL) const override;
219
220 const char *Encode(CYPool &pool) const override;
221 CYTypedIdentifier *Decode(CYPool &pool) const override;
222
223 ffi_type *GetFFI(CYPool &pool) const override;
224 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
225 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
226
227 size_t Translate(Type *&type) const override {
228 type = &this->type;
229 return size;
230 }
231 };
232
233 struct Object :
234 Type
235 {
236 const char *name;
237
238 Object(const char *name = NULL) :
239 name(name)
240 {
241 }
242
243 Object *Copy(CYPool &pool, const char *name = NULL) const override;
244 const char *GetName() const override;
245
246 const char *Encode(CYPool &pool) const override;
247 CYTypedIdentifier *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
254 struct Aggregate :
255 Type
256 {
257 bool overlap;
258 const char *name;
259 Signature signature;
260
261 Aggregate(bool overlap, const char *name = NULL) :
262 overlap(overlap),
263 name(name)
264 {
265 }
266
267 Aggregate *Copy(CYPool &pool, const char *name = NULL) const override;
268 const char *GetName() const override;
269
270 const char *Encode(CYPool &pool) const override;
271 CYTypedIdentifier *Decode(CYPool &pool) const override;
272
273 ffi_type *GetFFI(CYPool &pool) const override;
274 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
275 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
276 };
277
278 struct Callable :
279 Type
280 {
281 Signature signature;
282 };
283
284 struct Function :
285 Callable
286 {
287 Function *Copy(CYPool &pool, const char *name = NULL) const override;
288
289 const char *Encode(CYPool &pool) const override;
290 CYTypedIdentifier *Decode(CYPool &pool) const override;
291
292 ffi_type *GetFFI(CYPool &pool) const override;
293 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
294 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
295 };
296
297 struct Block :
298 Callable
299 {
300 Block *Copy(CYPool &pool, const char *name = NULL) const override;
301
302 const char *Encode(CYPool &pool) const override;
303 CYTypedIdentifier *Decode(CYPool &pool) const override;
304
305 ffi_type *GetFFI(CYPool &pool) const override;
306 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
307 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
308 };
309
310 Type *joc_parse_type(char **name, char eos, bool variable, bool signature);
311 void joc_parse_signature(Signature *signature, char **name, char eos, bool variable);
312
313 }
314
315 #endif/*SIG_TYPES_H*/