]>
git.saurik.com Git - cycript.git/blob - sig/ffi_type.cpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include "sig/ffi_type.hpp"
43 #include "sig/types.hpp"
45 #define ffi_type_slonglong ffi_type_sint64
46 #define ffi_type_ulonglong ffi_type_uint64
52 ffi_type
*(*sig_ffi_type
)(apr_pool_t
*, struct Type
*),
53 struct Signature
*signature
,
58 _assert(signature
->count
>= skip
);
59 for (size_t index
= skip
; index
!= signature
->count
; ++index
)
60 types
[index
- skip
+ offset
] = (*sig_ffi_type
)(pool
, signature
->elements
[index
].type
);
63 ffi_type
*ObjectiveC(apr_pool_t
*pool
, struct Type
*type
) {
64 switch (type
->primitive
) {
65 case typename_P
: return &ffi_type_pointer
;
68 /* XXX: we can totally make this work */
72 case string_P
: return &ffi_type_pointer
;
73 case selector_P
: return &ffi_type_pointer
;
74 case object_P
: return &ffi_type_pointer
;
75 case boolean_P
: return &ffi_type_uchar
;
76 case uchar_P
: return &ffi_type_uchar
;
77 case uint_P
: return &ffi_type_uint
;
78 case ulong_P
: return &ffi_type_ulong
;
79 case ulonglong_P
: return &ffi_type_ulonglong
;
80 case ushort_P
: return &ffi_type_ushort
;
83 // XXX: this is really lame
84 ffi_type
*aggregate(reinterpret_cast<ffi_type
*>(apr_palloc(pool
, sizeof(ffi_type
))));
86 aggregate
->alignment
= 0;
87 aggregate
->type
= FFI_TYPE_STRUCT
;
89 ffi_type
*element(ObjectiveC(pool
, type
->data
.data
.type
));
90 size_t size(type
->data
.data
.size
);
92 aggregate
->elements
= reinterpret_cast<ffi_type
**>(apr_palloc(pool
, (size
+ 1) * sizeof(ffi_type
*)));
93 for (size_t i(0); i
!= size
; ++i
)
94 aggregate
->elements
[i
] = element
;
95 aggregate
->elements
[size
] = NULL
;
100 case pointer_P
: return &ffi_type_pointer
;
103 /* XXX: we can totally make this work */
107 case char_P
: return &ffi_type_schar
;
108 case double_P
: return &ffi_type_double
;
109 case float_P
: return &ffi_type_float
;
110 case int_P
: return &ffi_type_sint
;
111 case long_P
: return &ffi_type_slong
;
112 case longlong_P
: return &ffi_type_slonglong
;
113 case short_P
: return &ffi_type_sshort
;
115 case void_P
: return &ffi_type_void
;
118 ffi_type
*aggregate(reinterpret_cast<ffi_type
*>(apr_palloc(pool
, sizeof(ffi_type
))));
120 aggregate
->alignment
= 0;
121 aggregate
->type
= FFI_TYPE_STRUCT
;
123 aggregate
->elements
= reinterpret_cast<ffi_type
**>(apr_palloc(pool
, (type
->data
.signature
.count
+ 1) * sizeof(ffi_type
*)));
124 sig_ffi_types(pool
, &ObjectiveC
, &type
->data
.signature
, aggregate
->elements
);
125 aggregate
->elements
[type
->data
.signature
.count
] = NULL
;
136 ffi_type
*Java(apr_pool_t
*pool
, struct Type
*type
) {
137 switch (type
->primitive
) {
138 case typename_P
: return &ffi_type_pointer
;
139 case union_P
: return &ffi_type_pointer
;
140 case string_P
: return &ffi_type_pointer
;
141 case selector_P
: return &ffi_type_pointer
;
142 case object_P
: return &ffi_type_pointer
;
143 case boolean_P
: return &ffi_type_uchar
;
144 case uchar_P
: return &ffi_type_uchar
;
145 case uint_P
: return &ffi_type_uint
;
146 case ulong_P
: return &ffi_type_ulong
;
147 case ulonglong_P
: return &ffi_type_ulonglong
;
148 case ushort_P
: return &ffi_type_ushort
;
149 case array_P
: return &ffi_type_pointer
;
150 case pointer_P
: return &ffi_type_pointer
;
153 case bit_P
: return &ffi_type_uint
;
155 case char_P
: return &ffi_type_schar
;
156 case double_P
: return &ffi_type_double
;
157 case float_P
: return &ffi_type_double
;
158 case int_P
: return &ffi_type_sint
;
159 case long_P
: return &ffi_type_slong
;
160 case longlong_P
: return &ffi_type_slonglong
;
161 case short_P
: return &ffi_type_sshort
;
162 case void_P
: return &ffi_type_void
;
163 case struct_P
: return &ffi_type_pointer
;
173 ffi_type
*(*sig_ffi_type
)(apr_pool_t
*, struct Type
*),
174 struct Signature
*signature
,
181 types
= reinterpret_cast<ffi_type
**>(apr_palloc(pool
, (signature
->count
- 1) * sizeof(ffi_type
*)));
182 ffi_type
*type
= (*sig_ffi_type
)(pool
, signature
->elements
[0].type
);
183 sig_ffi_types(pool
, sig_ffi_type
, signature
, types
, 1 + skip
, offset
);
184 ffi_status status
= ffi_prep_cif(cif
, FFI_DEFAULT_ABI
, signature
->count
- 1 - skip
+ offset
, type
, types
);
185 _assert(status
== FFI_OK
);