]>
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 | ||
d4222ffb JF |
56 | template <typename Internal_> |
57 | struct CYPrivate { | |
58 | static JSClassRef Class_; | |
59 | ||
60 | template <typename... Args_> | |
61 | static JSObjectRef Make(JSContextRef context, Args_ &&... args) { | |
62 | Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...)); | |
63 | JSObjectRef object(JSObjectMake(context, Class_, internal)); | |
64 | if (JSValueRef prototype = internal->GetPrototype(context)) | |
65 | CYSetPrototype(context, object, prototype); | |
66 | return object; | |
67 | } | |
68 | ||
3d2d95a0 JF |
69 | template <typename Arg_> |
70 | static JSObjectRef Cache(JSContextRef context, Arg_ *arg) { | |
71 | JSObjectRef global(CYGetGlobalObject(context)); | |
72 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); | |
73 | ||
74 | char label[32]; | |
75 | sprintf(label, "%s%p", Internal_::Cache_, arg); | |
76 | CYJSString name(label); | |
77 | ||
78 | JSValueRef value(CYGetProperty(context, cy, name)); | |
79 | if (!JSValueIsUndefined(context, value)) | |
80 | return CYCastJSObject(context, value); | |
81 | ||
82 | JSObjectRef object(Make(context, arg)); | |
83 | CYSetProperty(context, cy, name, object); | |
84 | return object; | |
85 | } | |
86 | ||
d4222ffb JF |
87 | static Internal_ *Get(JSContextRef context, JSObjectRef object) { |
88 | _assert(JSValueIsObjectOfClass(context, object, Class_)); | |
89 | return static_cast<Internal_ *>(JSObjectGetPrivate(object)); | |
90 | } | |
91 | }; | |
92 | ||
93 | template <typename Internal_> | |
94 | JSClassRef CYPrivate<Internal_>::Class_; | |
95 | ||
dbf05bfd | 96 | struct Type_privateData : |
d4222ffb | 97 | CYRoot |
dbf05bfd | 98 | { |
3c1c3635 JF |
99 | ffi_type *ffi_; |
100 | sig::Type *type_; | |
101 | ||
807a88be JF |
102 | Type_privateData(const char *type) : |
103 | ffi_(NULL) | |
104 | { | |
3c1c3635 | 105 | sig::Signature signature; |
b799113b | 106 | sig::Parse(*pool_, &signature, type, &Structor_); |
3c1c3635 JF |
107 | type_ = signature.elements[0].type; |
108 | } | |
109 | ||
0559abf8 JF |
110 | Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) : |
111 | type_(type.Copy(*pool_)) | |
baea6375 | 112 | { |
baea6375 | 113 | |
0559abf8 JF |
114 | if (ffi == NULL) |
115 | ffi_ = NULL; | |
116 | else { | |
117 | ffi_ = new(*pool_) ffi_type; | |
118 | sig::Copy(*pool_, *ffi_, *ffi); | |
119 | } | |
3c1c3635 JF |
120 | } |
121 | ||
122 | ffi_type *GetFFI() { | |
123 | if (ffi_ == NULL) { | |
3c1c3635 JF |
124 | sig::Element element; |
125 | element.name = NULL; | |
126 | element.type = type_; | |
127 | element.offset = 0; | |
128 | ||
129 | sig::Signature signature; | |
130 | signature.elements = &element; | |
131 | signature.count = 1; | |
132 | ||
133 | ffi_cif cif; | |
574d4720 | 134 | sig::sig_ffi_cif(*pool_, false, signature, &cif); |
446d46d2 JF |
135 | |
136 | ffi_ = new(*pool_) ffi_type; | |
3c1c3635 JF |
137 | *ffi_ = *cif.rtype; |
138 | } | |
139 | ||
140 | return ffi_; | |
141 | } | |
142 | }; | |
143 | ||
d6848e73 | 144 | struct CYProtect { |
3c1c3635 | 145 | private: |
bc60fb46 | 146 | JSGlobalContextRef context_; |
d6848e73 | 147 | JSObjectRef object_; |
3c1c3635 JF |
148 | |
149 | public: | |
d6848e73 | 150 | CYProtect(JSContextRef context, JSObjectRef object) : |
bc60fb46 | 151 | context_(CYGetJSContext(context)), |
d6848e73 | 152 | object_(object) |
3c1c3635 | 153 | { |
bc60fb46 | 154 | //XXX:JSGlobalContextRetain(context_); |
d6848e73 JF |
155 | if (object_ != NULL) |
156 | JSValueProtect(context_, object_); | |
3c1c3635 JF |
157 | } |
158 | ||
d6848e73 JF |
159 | ~CYProtect() { |
160 | if (object_ != NULL) | |
161 | JSValueUnprotect(context_, object_); | |
bc60fb46 | 162 | //XXX:JSGlobalContextRelease(context_); |
3c1c3635 JF |
163 | } |
164 | ||
5f7a1d38 JF |
165 | operator bool() const { |
166 | return object_ != NULL; | |
167 | } | |
168 | ||
169 | operator JSContextRef() const { | |
170 | return context_; | |
171 | } | |
172 | ||
d6848e73 JF |
173 | operator JSObjectRef() const { |
174 | return object_; | |
3c1c3635 JF |
175 | } |
176 | }; | |
177 | ||
9e7cf36b JF |
178 | class CYBuffer { |
179 | private: | |
180 | JSObjectRef owner_; | |
181 | CYPool *pool_; | |
182 | ||
183 | public: | |
184 | CYBuffer(JSContextRef context) : | |
185 | owner_(CYPrivate<CYRoot>::Make(context)), | |
186 | pool_(CYPrivate<CYRoot>::Get(context, owner_)->pool_) | |
187 | { | |
188 | auto internal(CYPrivate<CYRoot>::Get(context, owner_)); | |
189 | internal->pool_->malloc<int>(10); | |
190 | } | |
191 | ||
192 | operator JSObjectRef() const { | |
193 | return owner_; | |
194 | } | |
195 | ||
196 | operator CYPool *() const { | |
197 | return pool_; | |
198 | } | |
199 | ||
200 | CYPool *operator ->() const { | |
201 | return pool_; | |
202 | } | |
203 | }; | |
204 | ||
37954781 JF |
205 | namespace cy { |
206 | struct Functor : | |
d4222ffb | 207 | CYRoot |
37954781 | 208 | { |
6419a40f JF |
209 | public: |
210 | static JSClassRef Class_; | |
211 | ||
077756a4 JF |
212 | private: |
213 | void set() { | |
574d4720 | 214 | sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_); |
077756a4 JF |
215 | } |
216 | ||
217 | public: | |
88c31c1e | 218 | void (*value_)(); |
574d4720 | 219 | bool variadic_; |
37954781 JF |
220 | sig::Signature signature_; |
221 | ffi_cif cif_; | |
222 | ||
574d4720 | 223 | Functor(void (*value)(), bool variadic, const sig::Signature &signature) : |
88c31c1e | 224 | value_(value), |
574d4720 | 225 | variadic_(variadic) |
37954781 | 226 | { |
077756a4 JF |
227 | sig::Copy(*pool_, signature_, signature); |
228 | set(); | |
229 | } | |
230 | ||
574d4720 | 231 | Functor(void (*value)(), const char *encoding) : |
88c31c1e | 232 | value_(value), |
574d4720 | 233 | variadic_(false) |
077756a4 JF |
234 | { |
235 | sig::Parse(*pool_, &signature_, encoding, &Structor_); | |
236 | set(); | |
37954781 JF |
237 | } |
238 | ||
6419a40f | 239 | virtual CYPropertyName *GetName(CYPool &pool) const; |
37954781 JF |
240 | }; } |
241 | ||
242 | struct Closure_privateData : | |
243 | cy::Functor | |
244 | { | |
5f7a1d38 | 245 | CYProtect function_; |
24e7b1a6 | 246 | JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef); |
37954781 | 247 | |
24e7b1a6 | 248 | Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) : |
574d4720 | 249 | cy::Functor(NULL, false, signature), |
5f7a1d38 | 250 | function_(context, function), |
24e7b1a6 | 251 | adapter_(adapter) |
37954781 | 252 | { |
37954781 JF |
253 | } |
254 | }; | |
255 | ||
24e7b1a6 | 256 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
9ec7dd18 | 257 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
37954781 | 258 | |
3c1c3635 | 259 | #endif/*CYCRIPT_INTERNAL_HPP*/ |