]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
d15b59f5 JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
d15b59f5 | 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 | **/ |
d15b59f5 JF |
20 | /* }}} */ |
21 | ||
3c1c3635 JF |
22 | #ifndef CYCRIPT_INTERNAL_HPP |
23 | #define CYCRIPT_INTERNAL_HPP | |
24 | ||
b799113b JF |
25 | #include <sig/parse.hpp> |
26 | #include <sig/ffi_type.hpp> | |
3c1c3635 JF |
27 | |
28 | #include <JavaScriptCore/JSBase.h> | |
bc60fb46 | 29 | #include <JavaScriptCore/JSContextRef.h> |
37954781 | 30 | #include <JavaScriptCore/JSObjectRef.h> |
3c1c3635 JF |
31 | #include <JavaScriptCore/JSValueRef.h> |
32 | ||
d6848e73 | 33 | #include "JavaScript.hpp" |
b799113b | 34 | #include "Pooling.hpp" |
d6848e73 | 35 | #include "Utility.hpp" |
3c1c3635 | 36 | |
6419a40f JF |
37 | struct CYPropertyName; |
38 | ||
bc60fb46 | 39 | JSGlobalContextRef CYGetJSContext(JSContextRef context); |
0559abf8 | 40 | sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate); |
8a23fb2e | 41 | |
d4222ffb | 42 | struct CYRoot : |
3c1c3635 JF |
43 | CYData |
44 | { | |
9e7cf36b JF |
45 | // XXX: without this, CYData is zero-initialized?! |
46 | CYRoot() : | |
47 | CYData() | |
48 | { | |
49 | } | |
50 | ||
dbf05bfd JF |
51 | _finline JSValueRef GetPrototype(JSContextRef context) const { |
52 | return NULL; | |
53 | } | |
d4222ffb JF |
54 | }; |
55 | ||
56 | template <typename Internal_, typename Base_ = CYRoot> | |
57 | struct CYPrivateOld : | |
58 | Base_ | |
59 | { | |
60 | static JSClassRef Class_; | |
dbf05bfd JF |
61 | |
62 | template <typename... Args_> | |
63 | _finline static JSClassRef GetClass(Args_ &&... args) { | |
64 | return Class_; | |
65 | } | |
66 | ||
67 | template <typename... Args_> | |
68 | static JSObjectRef Make(JSContextRef context, Args_ &&... args) { | |
69 | Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...)); | |
70 | JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal)); | |
71 | if (JSValueRef prototype = internal->GetPrototype(context)) | |
72 | CYSetPrototype(context, object, prototype); | |
73 | return object; | |
74 | } | |
0b5f88f6 JF |
75 | |
76 | static Internal_ *Get(JSContextRef context, JSObjectRef object) { | |
77 | _assert(JSValueIsObjectOfClass(context, object, Class_)); | |
78 | return static_cast<Internal_ *>(JSObjectGetPrivate(object)); | |
79 | } | |
dbf05bfd JF |
80 | }; |
81 | ||
d4222ffb JF |
82 | template <typename Internal_, typename Base_> |
83 | JSClassRef CYPrivateOld<Internal_, Base_>::Class_; | |
84 | ||
85 | template <typename Internal_> | |
86 | struct CYPrivate { | |
87 | static JSClassRef Class_; | |
88 | ||
89 | template <typename... Args_> | |
90 | static JSObjectRef Make(JSContextRef context, Args_ &&... args) { | |
91 | Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...)); | |
92 | JSObjectRef object(JSObjectMake(context, Class_, internal)); | |
93 | if (JSValueRef prototype = internal->GetPrototype(context)) | |
94 | CYSetPrototype(context, object, prototype); | |
95 | return object; | |
96 | } | |
97 | ||
3d2d95a0 JF |
98 | template <typename Arg_> |
99 | static JSObjectRef Cache(JSContextRef context, Arg_ *arg) { | |
100 | JSObjectRef global(CYGetGlobalObject(context)); | |
101 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); | |
102 | ||
103 | char label[32]; | |
104 | sprintf(label, "%s%p", Internal_::Cache_, arg); | |
105 | CYJSString name(label); | |
106 | ||
107 | JSValueRef value(CYGetProperty(context, cy, name)); | |
108 | if (!JSValueIsUndefined(context, value)) | |
109 | return CYCastJSObject(context, value); | |
110 | ||
111 | JSObjectRef object(Make(context, arg)); | |
112 | CYSetProperty(context, cy, name, object); | |
113 | return object; | |
114 | } | |
115 | ||
d4222ffb JF |
116 | static Internal_ *Get(JSContextRef context, JSObjectRef object) { |
117 | _assert(JSValueIsObjectOfClass(context, object, Class_)); | |
118 | return static_cast<Internal_ *>(JSObjectGetPrivate(object)); | |
119 | } | |
120 | }; | |
121 | ||
122 | template <typename Internal_> | |
123 | JSClassRef CYPrivate<Internal_>::Class_; | |
124 | ||
dbf05bfd | 125 | struct Type_privateData : |
d4222ffb | 126 | CYRoot |
dbf05bfd | 127 | { |
3c1c3635 JF |
128 | ffi_type *ffi_; |
129 | sig::Type *type_; | |
130 | ||
807a88be JF |
131 | Type_privateData(const char *type) : |
132 | ffi_(NULL) | |
133 | { | |
3c1c3635 | 134 | sig::Signature signature; |
b799113b | 135 | sig::Parse(*pool_, &signature, type, &Structor_); |
3c1c3635 JF |
136 | type_ = signature.elements[0].type; |
137 | } | |
138 | ||
0559abf8 JF |
139 | Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) : |
140 | type_(type.Copy(*pool_)) | |
baea6375 | 141 | { |
baea6375 | 142 | |
0559abf8 JF |
143 | if (ffi == NULL) |
144 | ffi_ = NULL; | |
145 | else { | |
146 | ffi_ = new(*pool_) ffi_type; | |
147 | sig::Copy(*pool_, *ffi_, *ffi); | |
148 | } | |
3c1c3635 JF |
149 | } |
150 | ||
151 | ffi_type *GetFFI() { | |
152 | if (ffi_ == NULL) { | |
3c1c3635 JF |
153 | sig::Element element; |
154 | element.name = NULL; | |
155 | element.type = type_; | |
156 | element.offset = 0; | |
157 | ||
158 | sig::Signature signature; | |
159 | signature.elements = &element; | |
160 | signature.count = 1; | |
161 | ||
162 | ffi_cif cif; | |
574d4720 | 163 | sig::sig_ffi_cif(*pool_, false, signature, &cif); |
446d46d2 JF |
164 | |
165 | ffi_ = new(*pool_) ffi_type; | |
3c1c3635 JF |
166 | *ffi_ = *cif.rtype; |
167 | } | |
168 | ||
169 | return ffi_; | |
170 | } | |
171 | }; | |
172 | ||
d6848e73 | 173 | struct CYProtect { |
3c1c3635 | 174 | private: |
bc60fb46 | 175 | JSGlobalContextRef context_; |
d6848e73 | 176 | JSObjectRef object_; |
3c1c3635 JF |
177 | |
178 | public: | |
d6848e73 | 179 | CYProtect(JSContextRef context, JSObjectRef object) : |
bc60fb46 | 180 | context_(CYGetJSContext(context)), |
d6848e73 | 181 | object_(object) |
3c1c3635 | 182 | { |
bc60fb46 | 183 | //XXX:JSGlobalContextRetain(context_); |
d6848e73 JF |
184 | if (object_ != NULL) |
185 | JSValueProtect(context_, object_); | |
3c1c3635 JF |
186 | } |
187 | ||
d6848e73 JF |
188 | ~CYProtect() { |
189 | if (object_ != NULL) | |
190 | JSValueUnprotect(context_, object_); | |
bc60fb46 | 191 | //XXX:JSGlobalContextRelease(context_); |
3c1c3635 JF |
192 | } |
193 | ||
5f7a1d38 JF |
194 | operator bool() const { |
195 | return object_ != NULL; | |
196 | } | |
197 | ||
198 | operator JSContextRef() const { | |
199 | return context_; | |
200 | } | |
201 | ||
d6848e73 JF |
202 | operator JSObjectRef() const { |
203 | return object_; | |
3c1c3635 JF |
204 | } |
205 | }; | |
206 | ||
9e7cf36b JF |
207 | class CYBuffer { |
208 | private: | |
209 | JSObjectRef owner_; | |
210 | CYPool *pool_; | |
211 | ||
212 | public: | |
213 | CYBuffer(JSContextRef context) : | |
214 | owner_(CYPrivate<CYRoot>::Make(context)), | |
215 | pool_(CYPrivate<CYRoot>::Get(context, owner_)->pool_) | |
216 | { | |
217 | auto internal(CYPrivate<CYRoot>::Get(context, owner_)); | |
218 | internal->pool_->malloc<int>(10); | |
219 | } | |
220 | ||
221 | operator JSObjectRef() const { | |
222 | return owner_; | |
223 | } | |
224 | ||
225 | operator CYPool *() const { | |
226 | return pool_; | |
227 | } | |
228 | ||
229 | CYPool *operator ->() const { | |
230 | return pool_; | |
231 | } | |
232 | }; | |
233 | ||
37954781 JF |
234 | namespace cy { |
235 | struct Functor : | |
d4222ffb | 236 | CYRoot |
37954781 | 237 | { |
6419a40f JF |
238 | public: |
239 | static JSClassRef Class_; | |
240 | ||
077756a4 JF |
241 | private: |
242 | void set() { | |
574d4720 | 243 | sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_); |
077756a4 JF |
244 | } |
245 | ||
246 | public: | |
88c31c1e | 247 | void (*value_)(); |
574d4720 | 248 | bool variadic_; |
37954781 JF |
249 | sig::Signature signature_; |
250 | ffi_cif cif_; | |
251 | ||
574d4720 | 252 | Functor(void (*value)(), bool variadic, const sig::Signature &signature) : |
88c31c1e | 253 | value_(value), |
574d4720 | 254 | variadic_(variadic) |
37954781 | 255 | { |
077756a4 JF |
256 | sig::Copy(*pool_, signature_, signature); |
257 | set(); | |
258 | } | |
259 | ||
574d4720 | 260 | Functor(void (*value)(), const char *encoding) : |
88c31c1e | 261 | value_(value), |
574d4720 | 262 | variadic_(false) |
077756a4 JF |
263 | { |
264 | sig::Parse(*pool_, &signature_, encoding, &Structor_); | |
265 | set(); | |
37954781 JF |
266 | } |
267 | ||
6419a40f | 268 | virtual CYPropertyName *GetName(CYPool &pool) const; |
37954781 JF |
269 | }; } |
270 | ||
271 | struct Closure_privateData : | |
272 | cy::Functor | |
273 | { | |
5f7a1d38 | 274 | CYProtect function_; |
24e7b1a6 | 275 | JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef); |
37954781 | 276 | |
24e7b1a6 | 277 | Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) : |
574d4720 | 278 | cy::Functor(NULL, false, signature), |
5f7a1d38 | 279 | function_(context, function), |
24e7b1a6 | 280 | adapter_(adapter) |
37954781 | 281 | { |
37954781 JF |
282 | } |
283 | }; | |
284 | ||
24e7b1a6 | 285 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
9ec7dd18 | 286 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
37954781 | 287 | |
3c1c3635 | 288 | #endif/*CYCRIPT_INTERNAL_HPP*/ |