]> git.saurik.com Git - cycript.git/blob - Internal.hpp
9819b53352ef6e9b165e014dd1488bff8600163a
[cycript.git] / Internal.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 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 "JavaScript.hpp"
34 #include "Pooling.hpp"
35 #include "Utility.hpp"
36
37 JSGlobalContextRef CYGetJSContext(JSContextRef context);
38 sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
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 Type_privateData(const char *type) :
51 ffi_(NULL)
52 {
53 sig::Signature signature;
54 sig::Parse(*pool_, &signature, type, &Structor_);
55 type_ = signature.elements[0].type;
56 }
57
58 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
59 type_(type.Copy(*pool_))
60 {
61
62 if (ffi == NULL)
63 ffi_ = NULL;
64 else {
65 ffi_ = new(*pool_) ffi_type;
66 sig::Copy(*pool_, *ffi_, *ffi);
67 }
68 }
69
70 ffi_type *GetFFI() {
71 if (ffi_ == NULL) {
72 sig::Element element;
73 element.name = NULL;
74 element.type = type_;
75 element.offset = 0;
76
77 sig::Signature signature;
78 signature.elements = &element;
79 signature.count = 1;
80
81 ffi_cif cif;
82 sig::sig_ffi_cif(*pool_, false, signature, &cif);
83
84 ffi_ = new(*pool_) ffi_type;
85 *ffi_ = *cif.rtype;
86 }
87
88 return ffi_;
89 }
90 };
91
92 struct CYValue :
93 CYData
94 {
95 void *value_;
96
97 CYValue() {
98 }
99
100 CYValue(const void *value) :
101 value_(const_cast<void *>(value))
102 {
103 }
104
105 CYValue(const CYValue &rhs) :
106 value_(rhs.value_)
107 {
108 }
109
110 virtual Type_privateData *GetType() const {
111 return NULL;
112 }
113 };
114
115 template <typename Internal_, typename Value_>
116 struct CYValue_ :
117 CYValue
118 {
119 static JSClassRef Class_;
120 static Type_privateData *Type_;
121
122 using CYValue::CYValue;
123
124 _finline Value_ GetValue() const {
125 return reinterpret_cast<Value_>(value_);
126 }
127
128 virtual Type_privateData *GetType() const {
129 return Type_;
130 }
131
132 _finline JSValueRef GetPrototype(JSContextRef context) const {
133 return NULL;
134 }
135
136 template <typename... Args_>
137 _finline static JSClassRef GetClass(Args_ &&... args) {
138 return Class_;
139 }
140
141 template <typename... Args_>
142 static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
143 Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
144 JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal));
145 if (JSValueRef prototype = internal->GetPrototype(context))
146 CYSetPrototype(context, object, prototype);
147 return object;
148 }
149 };
150
151 template <typename Internal_, typename Value_>
152 JSClassRef CYValue_<Internal_, Value_>::Class_;
153
154 template <typename Internal_, typename Value_>
155 Type_privateData *CYValue_<Internal_, Value_>::Type_;
156
157 struct CYProtect {
158 private:
159 JSGlobalContextRef context_;
160 JSObjectRef object_;
161
162 public:
163 CYProtect(JSContextRef context, JSObjectRef object) :
164 context_(CYGetJSContext(context)),
165 object_(object)
166 {
167 //XXX:JSGlobalContextRetain(context_);
168 if (object_ != NULL)
169 JSValueProtect(context_, object_);
170 }
171
172 ~CYProtect() {
173 if (object_ != NULL)
174 JSValueUnprotect(context_, object_);
175 //XXX:JSGlobalContextRelease(context_);
176 }
177
178 operator JSObjectRef() const {
179 return object_;
180 }
181 };
182
183 namespace cy {
184 struct Functor :
185 CYValue
186 {
187 private:
188 void set() {
189 sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_);
190 }
191
192 public:
193 bool variadic_;
194 sig::Signature signature_;
195 ffi_cif cif_;
196
197 Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
198 CYValue(reinterpret_cast<void *>(value)),
199 variadic_(variadic)
200 {
201 sig::Copy(*pool_, signature_, signature);
202 set();
203 }
204
205 Functor(void (*value)(), const char *encoding) :
206 CYValue(reinterpret_cast<void *>(value)),
207 variadic_(false)
208 {
209 sig::Parse(*pool_, &signature_, encoding, &Structor_);
210 set();
211 }
212
213 void (*GetValue() const)() {
214 return reinterpret_cast<void (*)()>(value_);
215 }
216
217 static JSStaticFunction const * const StaticFunctions;
218 static JSStaticValue const * const StaticValues;
219 }; }
220
221 struct Closure_privateData :
222 cy::Functor
223 {
224 JSGlobalContextRef context_;
225 JSObjectRef function_;
226 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
227
228 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
229 cy::Functor(NULL, false, signature),
230 context_(CYGetJSContext(context)),
231 function_(function),
232 adapter_(adapter)
233 {
234 //XXX:JSGlobalContextRetain(context_);
235 JSValueProtect(context_, function_);
236 }
237
238 virtual ~Closure_privateData() {
239 JSValueUnprotect(context_, function_);
240 //XXX:JSGlobalContextRelease(context_);
241 }
242 };
243
244 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
245 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
246
247 #endif/*CYCRIPT_INTERNAL_HPP*/