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