]>
Commit | Line | Data |
---|---|---|
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 | struct CYPropertyName; | |
38 | ||
39 | JSGlobalContextRef CYGetJSContext(JSContextRef context); | |
40 | sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate); | |
41 | ||
42 | struct CYRoot : | |
43 | CYData | |
44 | { | |
45 | // XXX: without this, CYData is zero-initialized?! | |
46 | CYRoot() : | |
47 | CYData() | |
48 | { | |
49 | } | |
50 | ||
51 | _finline JSValueRef GetPrototype(JSContextRef context) const { | |
52 | return NULL; | |
53 | } | |
54 | }; | |
55 | ||
56 | template <typename Internal_, typename Base_ = CYRoot> | |
57 | struct CYPrivateOld : | |
58 | Base_ | |
59 | { | |
60 | static JSClassRef Class_; | |
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 | } | |
75 | ||
76 | static Internal_ *Get(JSContextRef context, JSObjectRef object) { | |
77 | _assert(JSValueIsObjectOfClass(context, object, Class_)); | |
78 | return static_cast<Internal_ *>(JSObjectGetPrivate(object)); | |
79 | } | |
80 | }; | |
81 | ||
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 | ||
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 | ||
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 | ||
125 | struct Type_privateData : | |
126 | CYRoot | |
127 | { | |
128 | ffi_type *ffi_; | |
129 | sig::Type *type_; | |
130 | ||
131 | Type_privateData(const char *type) : | |
132 | ffi_(NULL) | |
133 | { | |
134 | sig::Signature signature; | |
135 | sig::Parse(*pool_, &signature, type, &Structor_); | |
136 | type_ = signature.elements[0].type; | |
137 | } | |
138 | ||
139 | Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) : | |
140 | type_(type.Copy(*pool_)) | |
141 | { | |
142 | ||
143 | if (ffi == NULL) | |
144 | ffi_ = NULL; | |
145 | else { | |
146 | ffi_ = new(*pool_) ffi_type; | |
147 | sig::Copy(*pool_, *ffi_, *ffi); | |
148 | } | |
149 | } | |
150 | ||
151 | ffi_type *GetFFI() { | |
152 | if (ffi_ == NULL) { | |
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; | |
163 | sig::sig_ffi_cif(*pool_, false, signature, &cif); | |
164 | ||
165 | ffi_ = new(*pool_) ffi_type; | |
166 | *ffi_ = *cif.rtype; | |
167 | } | |
168 | ||
169 | return ffi_; | |
170 | } | |
171 | }; | |
172 | ||
173 | struct CYProtect { | |
174 | private: | |
175 | JSGlobalContextRef context_; | |
176 | JSObjectRef object_; | |
177 | ||
178 | public: | |
179 | CYProtect(JSContextRef context, JSObjectRef object) : | |
180 | context_(CYGetJSContext(context)), | |
181 | object_(object) | |
182 | { | |
183 | //XXX:JSGlobalContextRetain(context_); | |
184 | if (object_ != NULL) | |
185 | JSValueProtect(context_, object_); | |
186 | } | |
187 | ||
188 | ~CYProtect() { | |
189 | if (object_ != NULL) | |
190 | JSValueUnprotect(context_, object_); | |
191 | //XXX:JSGlobalContextRelease(context_); | |
192 | } | |
193 | ||
194 | operator bool() const { | |
195 | return object_ != NULL; | |
196 | } | |
197 | ||
198 | operator JSContextRef() const { | |
199 | return context_; | |
200 | } | |
201 | ||
202 | operator JSObjectRef() const { | |
203 | return object_; | |
204 | } | |
205 | }; | |
206 | ||
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 | ||
234 | namespace cy { | |
235 | struct Functor : | |
236 | CYRoot | |
237 | { | |
238 | public: | |
239 | static JSClassRef Class_; | |
240 | ||
241 | private: | |
242 | void set() { | |
243 | sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_); | |
244 | } | |
245 | ||
246 | public: | |
247 | void (*value_)(); | |
248 | bool variadic_; | |
249 | sig::Signature signature_; | |
250 | ffi_cif cif_; | |
251 | ||
252 | Functor(void (*value)(), bool variadic, const sig::Signature &signature) : | |
253 | value_(value), | |
254 | variadic_(variadic) | |
255 | { | |
256 | sig::Copy(*pool_, signature_, signature); | |
257 | set(); | |
258 | } | |
259 | ||
260 | Functor(void (*value)(), const char *encoding) : | |
261 | value_(value), | |
262 | variadic_(false) | |
263 | { | |
264 | sig::Parse(*pool_, &signature_, encoding, &Structor_); | |
265 | set(); | |
266 | } | |
267 | ||
268 | virtual CYPropertyName *GetName(CYPool &pool) const; | |
269 | }; } | |
270 | ||
271 | struct Closure_privateData : | |
272 | cy::Functor | |
273 | { | |
274 | CYProtect function_; | |
275 | JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef); | |
276 | ||
277 | Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) : | |
278 | cy::Functor(NULL, false, signature), | |
279 | function_(context, function), | |
280 | adapter_(adapter) | |
281 | { | |
282 | } | |
283 | }; | |
284 | ||
285 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); | |
286 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); | |
287 | ||
288 | #endif/*CYCRIPT_INTERNAL_HPP*/ |