]> git.saurik.com Git - cycript.git/blame - sig/ffi_type.cpp
New syntax: let declarations, initialised arguments.
[cycript.git] / sig / ffi_type.cpp
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
b4aa79af
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
b4aa79af 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
b4aa79af 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
b4aa79af 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
b4aa79af
JF
20/* }}} */
21
37954781 22#include "Error.hpp"
ea2d184c
JF
23
24#include "sig/ffi_type.hpp"
25#include "sig/types.hpp"
26
5875f632
JF
27#define ffi_type_slonglong ffi_type_sint64
28#define ffi_type_ulonglong ffi_type_uint64
29
ea2d184c
JF
30namespace sig {
31
b21525c7
JF
32void sig_ffi_types(
33 apr_pool_t *pool,
34 ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *),
35 struct Signature *signature,
36 ffi_type **types,
37 size_t skip = 0,
38 size_t offset = 0
39) {
40 _assert(signature->count >= skip);
41 for (size_t index = skip; index != signature->count; ++index)
42 types[index - skip + offset] = (*sig_ffi_type)(pool, signature->elements[index].type);
43}
44
45ffi_type *ObjectiveC(apr_pool_t *pool, struct Type *type) {
ea2d184c
JF
46 switch (type->primitive) {
47 case typename_P: return &ffi_type_pointer;
48
49 case union_P:
50 /* XXX: we can totally make this work */
51 _assert(false);
52 break;
53
54 case string_P: return &ffi_type_pointer;
55 case selector_P: return &ffi_type_pointer;
ecf94af8 56 case block_P: return &ffi_type_pointer;
ea2d184c
JF
57 case object_P: return &ffi_type_pointer;
58 case boolean_P: return &ffi_type_uchar;
59 case uchar_P: return &ffi_type_uchar;
60 case uint_P: return &ffi_type_uint;
61 case ulong_P: return &ffi_type_ulong;
5875f632 62 case ulonglong_P: return &ffi_type_ulonglong;
ea2d184c
JF
63 case ushort_P: return &ffi_type_ushort;
64
534fb6da
JF
65 case array_P: {
66 // XXX: this is really lame
67 ffi_type *aggregate(reinterpret_cast<ffi_type *>(apr_palloc(pool, sizeof(ffi_type))));
68 aggregate->size = 0;
69 aggregate->alignment = 0;
70 aggregate->type = FFI_TYPE_STRUCT;
71
72 ffi_type *element(ObjectiveC(pool, type->data.data.type));
73 size_t size(type->data.data.size);
74
75 aggregate->elements = reinterpret_cast<ffi_type **>(apr_palloc(pool, (size + 1) * sizeof(ffi_type *)));
76 for (size_t i(0); i != size; ++i)
77 aggregate->elements[i] = element;
78 aggregate->elements[size] = NULL;
79
80 return aggregate;
81 } break;
ea2d184c
JF
82
83 case pointer_P: return &ffi_type_pointer;
84
85 case bit_P:
86 /* XXX: we can totally make this work */
87 _assert(false);
88 break;
89
90 case char_P: return &ffi_type_schar;
91 case double_P: return &ffi_type_double;
92 case float_P: return &ffi_type_float;
93 case int_P: return &ffi_type_sint;
5875f632
JF
94 case long_P: return &ffi_type_slong;
95 case longlong_P: return &ffi_type_slonglong;
ea2d184c
JF
96 case short_P: return &ffi_type_sshort;
97
98 case void_P: return &ffi_type_void;
99
100 case struct_P: {
534fb6da 101 ffi_type *aggregate(reinterpret_cast<ffi_type *>(apr_palloc(pool, sizeof(ffi_type))));
ea2d184c
JF
102 aggregate->size = 0;
103 aggregate->alignment = 0;
104 aggregate->type = FFI_TYPE_STRUCT;
105
106 aggregate->elements = reinterpret_cast<ffi_type **>(apr_palloc(pool, (type->data.signature.count + 1) * sizeof(ffi_type *)));
b21525c7 107 sig_ffi_types(pool, &ObjectiveC, &type->data.signature, aggregate->elements);
ea2d184c
JF
108 aggregate->elements[type->data.signature.count] = NULL;
109
110 return aggregate;
111 } break;
112
113 default:
114 _assert(false);
115 break;
116 }
117}
118
b21525c7 119ffi_type *Java(apr_pool_t *pool, struct Type *type) {
ea2d184c
JF
120 switch (type->primitive) {
121 case typename_P: return &ffi_type_pointer;
122 case union_P: return &ffi_type_pointer;
123 case string_P: return &ffi_type_pointer;
124 case selector_P: return &ffi_type_pointer;
ecf94af8 125 case block_P: return &ffi_type_pointer;
ea2d184c
JF
126 case object_P: return &ffi_type_pointer;
127 case boolean_P: return &ffi_type_uchar;
128 case uchar_P: return &ffi_type_uchar;
129 case uint_P: return &ffi_type_uint;
130 case ulong_P: return &ffi_type_ulong;
5875f632 131 case ulonglong_P: return &ffi_type_ulonglong;
ea2d184c
JF
132 case ushort_P: return &ffi_type_ushort;
133 case array_P: return &ffi_type_pointer;
134 case pointer_P: return &ffi_type_pointer;
135
136 /* XXX: bit type */
137 case bit_P: return &ffi_type_uint;
138
139 case char_P: return &ffi_type_schar;
140 case double_P: return &ffi_type_double;
141 case float_P: return &ffi_type_double;
142 case int_P: return &ffi_type_sint;
5875f632
JF
143 case long_P: return &ffi_type_slong;
144 case longlong_P: return &ffi_type_slonglong;
ea2d184c
JF
145 case short_P: return &ffi_type_sshort;
146 case void_P: return &ffi_type_void;
147 case struct_P: return &ffi_type_pointer;
148
149 default:
150 _assert(false);
151 break;
152 }
153}
154
ea2d184c
JF
155void sig_ffi_cif(
156 apr_pool_t *pool,
157 ffi_type *(*sig_ffi_type)(apr_pool_t *, struct Type *),
158 struct Signature *signature,
b21525c7 159 ffi_cif *cif,
ea2d184c
JF
160 size_t skip,
161 ffi_type **types,
162 size_t offset
163) {
164 if (types == NULL)
165 types = reinterpret_cast<ffi_type **>(apr_palloc(pool, (signature->count - 1) * sizeof(ffi_type *)));
166 ffi_type *type = (*sig_ffi_type)(pool, signature->elements[0].type);
167 sig_ffi_types(pool, sig_ffi_type, signature, types, 1 + skip, offset);
b21525c7 168 ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, signature->count - 1 - skip + offset, type, types);
ea2d184c
JF
169 _assert(status == FFI_OK);
170}
171
172}