]> git.saurik.com Git - cycript.git/blob - sig/types.hpp
Improve CString/Pointer consistency, using CArray.
[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
70 template <typename Type_>
71 struct Primitive :
72 Type
73 {
74 Primitive *Copy(CYPool &pool, const char *name) const {
75 return new(pool) Primitive();
76 }
77
78 const char *Encode(CYPool &pool) const override;
79 CYTypedIdentifier *Decode(CYPool &pool) const override;
80
81 ffi_type *GetFFI(CYPool &pool) const override;
82 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
83 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
84 };
85
86 struct Element {
87 const char *name;
88 Type *type;
89 size_t offset;
90 };
91
92 struct Signature {
93 Element *elements;
94 size_t count;
95 };
96
97 struct Void :
98 Type
99 {
100 Void *Copy(CYPool &pool, const char *name = NULL) const override;
101
102 const char *Encode(CYPool &pool) const override;
103 CYTypedIdentifier *Decode(CYPool &pool) const override;
104
105 ffi_type *GetFFI(CYPool &pool) const override;
106 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
107 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
108 };
109
110 struct Unknown :
111 Type
112 {
113 Unknown *Copy(CYPool &pool, const char *name = NULL) const override;
114
115 const char *Encode(CYPool &pool) const override;
116 CYTypedIdentifier *Decode(CYPool &pool) const override;
117
118 ffi_type *GetFFI(CYPool &pool) const override;
119 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
120 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
121 };
122
123 struct String :
124 Type
125 {
126 String *Copy(CYPool &pool, const char *name = NULL) const override;
127
128 const char *Encode(CYPool &pool) const override;
129 CYTypedIdentifier *Decode(CYPool &pool) const override;
130
131 ffi_type *GetFFI(CYPool &pool) const override;
132 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
133 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
134 };
135
136 struct Meta :
137 Type
138 {
139 Meta *Copy(CYPool &pool, const char *name = NULL) const override;
140
141 const char *Encode(CYPool &pool) const override;
142 CYTypedIdentifier *Decode(CYPool &pool) const override;
143
144 ffi_type *GetFFI(CYPool &pool) const override;
145 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
146 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
147 };
148
149 struct Selector :
150 Type
151 {
152 Selector *Copy(CYPool &pool, const char *name = NULL) const override;
153
154 const char *Encode(CYPool &pool) const override;
155 CYTypedIdentifier *Decode(CYPool &pool) const override;
156
157 ffi_type *GetFFI(CYPool &pool) const override;
158 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
159 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
160 };
161
162 struct Bits :
163 Type
164 {
165 size_t size;
166
167 Bits(size_t size) :
168 size(size)
169 {
170 }
171
172 Bits *Copy(CYPool &pool, const char *name = NULL) const override;
173
174 const char *Encode(CYPool &pool) const override;
175 CYTypedIdentifier *Decode(CYPool &pool) const override;
176
177 ffi_type *GetFFI(CYPool &pool) const override;
178 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
179 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
180 };
181
182 struct Pointer :
183 Type
184 {
185 Type &type;
186
187 Pointer(Type &type) :
188 type(type)
189 {
190 }
191
192 Pointer *Copy(CYPool &pool, const char *name = NULL) const override;
193
194 const char *Encode(CYPool &pool) const override;
195 CYTypedIdentifier *Decode(CYPool &pool) const override;
196
197 ffi_type *GetFFI(CYPool &pool) const override;
198 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
199 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
200 };
201
202 struct Array :
203 Type
204 {
205 Type &type;
206 size_t size;
207
208 Array(Type &type, size_t size = _not(size_t)) :
209 type(type),
210 size(size)
211 {
212 }
213
214 Array *Copy(CYPool &pool, const char *name = NULL) const override;
215
216 const char *Encode(CYPool &pool) const override;
217 CYTypedIdentifier *Decode(CYPool &pool) const override;
218
219 ffi_type *GetFFI(CYPool &pool) const override;
220 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
221 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
222 };
223
224 struct Object :
225 Type
226 {
227 const char *name;
228
229 Object(const char *name = NULL) :
230 name(name)
231 {
232 }
233
234 Object *Copy(CYPool &pool, const char *name = NULL) const override;
235 const char *GetName() const override;
236
237 const char *Encode(CYPool &pool) const override;
238 CYTypedIdentifier *Decode(CYPool &pool) const override;
239
240 ffi_type *GetFFI(CYPool &pool) const override;
241 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
242 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
243 };
244
245 struct Aggregate :
246 Type
247 {
248 bool overlap;
249 const char *name;
250 Signature signature;
251
252 Aggregate(bool overlap, const char *name = NULL) :
253 overlap(overlap),
254 name(name)
255 {
256 }
257
258 Aggregate *Copy(CYPool &pool, const char *name = NULL) const override;
259 const char *GetName() const override;
260
261 const char *Encode(CYPool &pool) const override;
262 CYTypedIdentifier *Decode(CYPool &pool) const override;
263
264 ffi_type *GetFFI(CYPool &pool) const override;
265 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
266 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
267 };
268
269 struct Callable :
270 Type
271 {
272 Signature signature;
273 };
274
275 struct Function :
276 Callable
277 {
278 Function *Copy(CYPool &pool, const char *name = NULL) const override;
279
280 const char *Encode(CYPool &pool) const override;
281 CYTypedIdentifier *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 Block :
289 Callable
290 {
291 Block *Copy(CYPool &pool, const char *name = NULL) const override;
292
293 const char *Encode(CYPool &pool) const override;
294 CYTypedIdentifier *Decode(CYPool &pool) const override;
295
296 ffi_type *GetFFI(CYPool &pool) const override;
297 void PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const override;
298 JSValueRef FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const override;
299 };
300
301 Type *joc_parse_type(char **name, char eos, bool variable, bool signature);
302 void joc_parse_signature(Signature *signature, char **name, char eos, bool variable);
303
304 }
305
306 #endif/*SIG_TYPES_H*/