]> git.saurik.com Git - cycript.git/blame - sig/types.hpp
Replace sig::Primitive with full object hierarchy.
[cycript.git] / sig / types.hpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
b4aa79af
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
b4aa79af 6/*
f95d2598
JF
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
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
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/>.
b3378a02 19**/
b4aa79af
JF
20/* }}} */
21
ea2d184c
JF
22#ifndef SIG_TYPES_H
23#define SIG_TYPES_H
24
b799113b
JF
25#include <cstdlib>
26#include <stdint.h>
27
0559abf8
JF
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
37954781 36#include "Standard.hpp"
ea2d184c 37
0559abf8
JF
38class CYPool;
39struct CYTypedIdentifier;
40
ea2d184c
JF
41namespace sig {
42
0559abf8
JF
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
51struct 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
74template <typename Type_>
75struct 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;
ea2d184c
JF
88};
89
90struct Element {
b799113b 91 const char *name;
0559abf8 92 Type *type;
ea2d184c
JF
93 size_t offset;
94};
95
96struct Signature {
0559abf8 97 Element *elements;
ea2d184c
JF
98 size_t count;
99};
100
0559abf8
JF
101struct Void :
102 Type
103{
104 Void *Copy(CYPool &pool, const char *name = NULL) const override;
ea2d184c 105
0559abf8
JF
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
114struct 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
127struct 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
140struct 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
153struct 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
166struct 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
186struct 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
206struct 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
233struct Object :
234 Type
235{
21d5f610 236 const char *name;
ea2d184c 237
0559abf8
JF
238 Object(const char *name = NULL) :
239 name(name)
240 {
241 }
ea2d184c 242
0559abf8
JF
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;
ea2d184c
JF
252};
253
0559abf8
JF
254struct Aggregate :
255 Type
256{
257 bool overlap;
258 const char *name;
259 Signature signature;
ea2d184c 260
0559abf8
JF
261 Aggregate(bool overlap, const char *name = NULL) :
262 overlap(overlap),
263 name(name)
264 {
265 }
3fe16be7 266
0559abf8
JF
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
278struct Callable :
279 Type
280{
281 Signature signature;
282};
283
284struct 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
297struct 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
310Type *joc_parse_type(char **name, char eos, bool variable, bool signature);
311void joc_parse_signature(Signature *signature, char **name, char eos, bool variable);
bd17e6f3 312
ea2d184c
JF
313}
314
315#endif/*SIG_TYPES_H*/