]> git.saurik.com Git - cycript.git/blob - Internal.hpp
Directly create types for Objective-C field types.
[cycript.git] / Internal.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2014 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 CYCRIPT_INTERNAL_HPP
23 #define CYCRIPT_INTERNAL_HPP
24
25 #include <sig/parse.hpp>
26 #include <sig/ffi_type.hpp>
27
28 #include <JavaScriptCore/JSBase.h>
29 #include <JavaScriptCore/JSContextRef.h>
30 #include <JavaScriptCore/JSObjectRef.h>
31 #include <JavaScriptCore/JSValueRef.h>
32
33 #include "Pooling.hpp"
34
35 JSGlobalContextRef CYGetJSContext(JSContextRef context);
36 void Structor_(CYPool &pool, sig::Type *&type);
37
38 JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
39
40 extern JSClassRef Functor_;
41
42 struct Type_privateData :
43 CYData
44 {
45 static JSClassRef Class_;
46
47 ffi_type *ffi_;
48 sig::Type *type_;
49
50 void Set(sig::Type *type) {
51 type_ = new(*pool_) sig::Type;
52 sig::Copy(*pool_, *type_, *type);
53 }
54
55 Type_privateData(const char *type) :
56 ffi_(NULL)
57 {
58 sig::Signature signature;
59 sig::Parse(*pool_, &signature, type, &Structor_);
60 type_ = signature.elements[0].type;
61 }
62
63 Type_privateData(sig::Type *type) :
64 ffi_(NULL)
65 {
66 // XXX: just in case I messed up migrating
67 _assert(type != NULL);
68 Set(type);
69 }
70
71 Type_privateData(sig::Type *type, ffi_type *ffi) {
72 ffi_ = new(*pool_) ffi_type;
73 sig::Copy(*pool_, *ffi_, *ffi);
74 Set(type);
75 }
76
77 ffi_type *GetFFI() {
78 if (ffi_ == NULL) {
79 sig::Element element;
80 element.name = NULL;
81 element.type = type_;
82 element.offset = 0;
83
84 sig::Signature signature;
85 signature.elements = &element;
86 signature.count = 1;
87
88 ffi_cif cif;
89 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
90
91 ffi_ = new(*pool_) ffi_type;
92 *ffi_ = *cif.rtype;
93 }
94
95 return ffi_;
96 }
97 };
98
99 struct CYValue :
100 CYData
101 {
102 void *value_;
103
104 CYValue() {
105 }
106
107 CYValue(const void *value) :
108 value_(const_cast<void *>(value))
109 {
110 }
111
112 CYValue(const CYValue &rhs) :
113 value_(rhs.value_)
114 {
115 }
116
117 virtual Type_privateData *GetType() const {
118 return NULL;
119 }
120 };
121
122 struct CYOwned :
123 CYValue
124 {
125 private:
126 JSGlobalContextRef context_;
127 JSObjectRef owner_;
128
129 public:
130 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
131 CYValue(value),
132 context_(CYGetJSContext(context)),
133 owner_(owner)
134 {
135 //XXX:JSGlobalContextRetain(context_);
136 if (owner_ != NULL)
137 JSValueProtect(context_, owner_);
138 }
139
140 virtual ~CYOwned() {
141 if (owner_ != NULL)
142 JSValueUnprotect(context_, owner_);
143 //XXX:JSGlobalContextRelease(context_);
144 }
145
146 JSObjectRef GetOwner() const {
147 return owner_;
148 }
149 };
150
151 namespace cy {
152 struct Functor :
153 CYValue
154 {
155 private:
156 void set() {
157 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
158 }
159
160 public:
161 sig::Signature signature_;
162 ffi_cif cif_;
163
164 Functor(const sig::Signature &signature, void (*value)()) :
165 CYValue(reinterpret_cast<void *>(value))
166 {
167 sig::Copy(*pool_, signature_, signature);
168 set();
169 }
170
171 Functor(const char *encoding, void (*value)()) :
172 CYValue(reinterpret_cast<void *>(value))
173 {
174 sig::Parse(*pool_, &signature_, encoding, &Structor_);
175 set();
176 }
177
178 void (*GetValue() const)() {
179 return reinterpret_cast<void (*)()>(value_);
180 }
181
182 static JSStaticFunction const * const StaticFunctions;
183 static JSStaticValue const * const StaticValues;
184 }; }
185
186 struct Closure_privateData :
187 cy::Functor
188 {
189 JSGlobalContextRef context_;
190 JSObjectRef function_;
191
192 Closure_privateData(JSContextRef context, JSObjectRef function, const sig::Signature &signature) :
193 cy::Functor(signature, NULL),
194 context_(CYGetJSContext(context)),
195 function_(function)
196 {
197 //XXX:JSGlobalContextRetain(context_);
198 JSValueProtect(context_, function_);
199 }
200
201 virtual ~Closure_privateData() {
202 JSValueUnprotect(context_, function_);
203 //XXX:JSGlobalContextRelease(context_);
204 }
205 };
206
207 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, void (*callback)(ffi_cif *, void *, void **, void *));
208 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
209
210 #endif/*CYCRIPT_INTERNAL_HPP*/