]> git.saurik.com Git - cycript.git/blame - sig/ffi_type.cpp
Print NULL instead of crashing for CString.toCYON.
[cycript.git] / sig / ffi_type.cpp
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
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 32void sig_ffi_types(
b799113b
JF
33 CYPool &pool,
34 ffi_type *(*sig_ffi_type)(CYPool &, struct Type *),
b21525c7
JF
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
b799113b 45ffi_type *ObjectiveC(CYPool &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
0cbeddf8 67 ffi_type *aggregate(new(pool) ffi_type());
534fb6da
JF
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
0cbeddf8 75 aggregate->elements = new(pool) ffi_type *[size + 1];
534fb6da
JF
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
57f06434 90 case schar_P: return &ffi_type_schar;
ea2d184c
JF
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;
57f06434 99 case char_P: return &ffi_type_schar;
ea2d184c
JF
100
101 case struct_P: {
0cbeddf8 102 ffi_type *aggregate(new(pool) ffi_type());
ea2d184c
JF
103 aggregate->size = 0;
104 aggregate->alignment = 0;
105 aggregate->type = FFI_TYPE_STRUCT;
106
0cbeddf8 107 aggregate->elements = new(pool) ffi_type *[type->data.signature.count + 1];
b21525c7 108 sig_ffi_types(pool, &ObjectiveC, &type->data.signature, aggregate->elements);
ea2d184c
JF
109 aggregate->elements[type->data.signature.count] = NULL;
110
111 return aggregate;
112 } break;
113
114 default:
115 _assert(false);
116 break;
117 }
118}
119
b799113b 120ffi_type *Java(CYPool &pool, struct Type *type) {
ea2d184c
JF
121 switch (type->primitive) {
122 case typename_P: return &ffi_type_pointer;
549add83 123 case union_P: _assert(false); break;
ea2d184c
JF
124 case string_P: return &ffi_type_pointer;
125 case selector_P: return &ffi_type_pointer;
ecf94af8 126 case block_P: return &ffi_type_pointer;
ea2d184c
JF
127 case object_P: return &ffi_type_pointer;
128 case boolean_P: return &ffi_type_uchar;
129 case uchar_P: return &ffi_type_uchar;
130 case uint_P: return &ffi_type_uint;
131 case ulong_P: return &ffi_type_ulong;
5875f632 132 case ulonglong_P: return &ffi_type_ulonglong;
ea2d184c
JF
133 case ushort_P: return &ffi_type_ushort;
134 case array_P: return &ffi_type_pointer;
135 case pointer_P: return &ffi_type_pointer;
549add83 136 case bit_P: _assert(false); break;
57f06434 137 case schar_P: return &ffi_type_schar;
ea2d184c
JF
138 case double_P: return &ffi_type_double;
139 case float_P: return &ffi_type_double;
140 case int_P: return &ffi_type_sint;
5875f632
JF
141 case long_P: return &ffi_type_slong;
142 case longlong_P: return &ffi_type_slonglong;
ea2d184c
JF
143 case short_P: return &ffi_type_sshort;
144 case void_P: return &ffi_type_void;
57f06434 145 case char_P: return &ffi_type_schar;
ea2d184c
JF
146 case struct_P: return &ffi_type_pointer;
147
148 default:
149 _assert(false);
150 break;
151 }
152}
153
ea2d184c 154void sig_ffi_cif(
b799113b
JF
155 CYPool &pool,
156 ffi_type *(*sig_ffi_type)(CYPool &, struct Type *),
ea2d184c 157 struct Signature *signature,
b21525c7 158 ffi_cif *cif,
ea2d184c
JF
159 size_t skip,
160 ffi_type **types,
161 size_t offset
162) {
163 if (types == NULL)
0cbeddf8 164 types = new(pool) ffi_type *[signature->count - 1];
ea2d184c
JF
165 ffi_type *type = (*sig_ffi_type)(pool, signature->elements[0].type);
166 sig_ffi_types(pool, sig_ffi_type, signature, types, 1 + skip, offset);
b21525c7 167 ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, signature->count - 1 - skip + offset, type, types);
ea2d184c
JF
168 _assert(status == FFI_OK);
169}
170
171}