]>
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 | |
bc60fb46 | 37 | JSGlobalContextRef CYGetJSContext(JSContextRef context); |
0559abf8 | 38 | sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate); |
8a23fb2e | 39 | |
3f9ae37c JF |
40 | extern JSClassRef Functor_; |
41 | ||
dbf05bfd JF |
42 | template <typename Internal_> |
43 | struct CYPrivate : | |
3c1c3635 JF |
44 | CYData |
45 | { | |
46 | static JSClassRef Class_; | |
47 | ||
dbf05bfd JF |
48 | _finline JSValueRef GetPrototype(JSContextRef context) const { |
49 | return NULL; | |
50 | } | |
51 | ||
52 | template <typename... Args_> | |
53 | _finline static JSClassRef GetClass(Args_ &&... args) { | |
54 | return Class_; | |
55 | } | |
56 | ||
57 | template <typename... Args_> | |
58 | static JSObjectRef Make(JSContextRef context, Args_ &&... args) { | |
59 | Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...)); | |
60 | JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal)); | |
61 | if (JSValueRef prototype = internal->GetPrototype(context)) | |
62 | CYSetPrototype(context, object, prototype); | |
63 | return object; | |
64 | } | |
0b5f88f6 JF |
65 | |
66 | static Internal_ *Get(JSContextRef context, JSObjectRef object) { | |
67 | _assert(JSValueIsObjectOfClass(context, object, Class_)); | |
68 | return static_cast<Internal_ *>(JSObjectGetPrivate(object)); | |
69 | } | |
dbf05bfd JF |
70 | }; |
71 | ||
72 | struct Type_privateData : | |
73 | CYPrivate<Type_privateData> | |
74 | { | |
3c1c3635 JF |
75 | ffi_type *ffi_; |
76 | sig::Type *type_; | |
77 | ||
807a88be JF |
78 | Type_privateData(const char *type) : |
79 | ffi_(NULL) | |
80 | { | |
3c1c3635 | 81 | sig::Signature signature; |
b799113b | 82 | sig::Parse(*pool_, &signature, type, &Structor_); |
3c1c3635 JF |
83 | type_ = signature.elements[0].type; |
84 | } | |
85 | ||
0559abf8 JF |
86 | Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) : |
87 | type_(type.Copy(*pool_)) | |
baea6375 | 88 | { |
baea6375 | 89 | |
0559abf8 JF |
90 | if (ffi == NULL) |
91 | ffi_ = NULL; | |
92 | else { | |
93 | ffi_ = new(*pool_) ffi_type; | |
94 | sig::Copy(*pool_, *ffi_, *ffi); | |
95 | } | |
3c1c3635 JF |
96 | } |
97 | ||
98 | ffi_type *GetFFI() { | |
99 | if (ffi_ == NULL) { | |
3c1c3635 JF |
100 | sig::Element element; |
101 | element.name = NULL; | |
102 | element.type = type_; | |
103 | element.offset = 0; | |
104 | ||
105 | sig::Signature signature; | |
106 | signature.elements = &element; | |
107 | signature.count = 1; | |
108 | ||
109 | ffi_cif cif; | |
574d4720 | 110 | sig::sig_ffi_cif(*pool_, false, signature, &cif); |
446d46d2 JF |
111 | |
112 | ffi_ = new(*pool_) ffi_type; | |
3c1c3635 JF |
113 | *ffi_ = *cif.rtype; |
114 | } | |
115 | ||
116 | return ffi_; | |
117 | } | |
118 | }; | |
119 | ||
dbf05bfd | 120 | template <typename Internal_, typename Value_> |
3c1c3635 | 121 | struct CYValue : |
dbf05bfd | 122 | CYPrivate<Internal_> |
3c1c3635 | 123 | { |
dbf05bfd | 124 | Value_ value_; |
3c1c3635 JF |
125 | |
126 | CYValue() { | |
127 | } | |
128 | ||
dbf05bfd JF |
129 | CYValue(const Value_ &value) : |
130 | value_(value) | |
3c1c3635 JF |
131 | { |
132 | } | |
133 | ||
134 | CYValue(const CYValue &rhs) : | |
135 | value_(rhs.value_) | |
136 | { | |
137 | } | |
3c1c3635 JF |
138 | }; |
139 | ||
dbf05bfd JF |
140 | template <typename Internal_> |
141 | JSClassRef CYPrivate<Internal_>::Class_; | |
d6848e73 | 142 | |
d6848e73 | 143 | struct CYProtect { |
3c1c3635 | 144 | private: |
bc60fb46 | 145 | JSGlobalContextRef context_; |
d6848e73 | 146 | JSObjectRef object_; |
3c1c3635 JF |
147 | |
148 | public: | |
d6848e73 | 149 | CYProtect(JSContextRef context, JSObjectRef object) : |
bc60fb46 | 150 | context_(CYGetJSContext(context)), |
d6848e73 | 151 | object_(object) |
3c1c3635 | 152 | { |
bc60fb46 | 153 | //XXX:JSGlobalContextRetain(context_); |
d6848e73 JF |
154 | if (object_ != NULL) |
155 | JSValueProtect(context_, object_); | |
3c1c3635 JF |
156 | } |
157 | ||
d6848e73 JF |
158 | ~CYProtect() { |
159 | if (object_ != NULL) | |
160 | JSValueUnprotect(context_, object_); | |
bc60fb46 | 161 | //XXX:JSGlobalContextRelease(context_); |
3c1c3635 JF |
162 | } |
163 | ||
5f7a1d38 JF |
164 | operator bool() const { |
165 | return object_ != NULL; | |
166 | } | |
167 | ||
168 | operator JSContextRef() const { | |
169 | return context_; | |
170 | } | |
171 | ||
d6848e73 JF |
172 | operator JSObjectRef() const { |
173 | return object_; | |
3c1c3635 JF |
174 | } |
175 | }; | |
176 | ||
37954781 JF |
177 | namespace cy { |
178 | struct Functor : | |
dbf05bfd | 179 | CYValue<Functor, void (*)()> |
37954781 | 180 | { |
077756a4 JF |
181 | private: |
182 | void set() { | |
574d4720 | 183 | sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_); |
077756a4 JF |
184 | } |
185 | ||
186 | public: | |
574d4720 | 187 | bool variadic_; |
37954781 JF |
188 | sig::Signature signature_; |
189 | ffi_cif cif_; | |
190 | ||
574d4720 | 191 | Functor(void (*value)(), bool variadic, const sig::Signature &signature) : |
dbf05bfd | 192 | CYValue(value), |
574d4720 | 193 | variadic_(variadic) |
37954781 | 194 | { |
077756a4 JF |
195 | sig::Copy(*pool_, signature_, signature); |
196 | set(); | |
197 | } | |
198 | ||
574d4720 | 199 | Functor(void (*value)(), const char *encoding) : |
dbf05bfd | 200 | CYValue(value), |
574d4720 | 201 | variadic_(false) |
077756a4 JF |
202 | { |
203 | sig::Parse(*pool_, &signature_, encoding, &Structor_); | |
204 | set(); | |
37954781 JF |
205 | } |
206 | ||
37954781 | 207 | static JSStaticFunction const * const StaticFunctions; |
8493347d | 208 | static JSStaticValue const * const StaticValues; |
37954781 JF |
209 | }; } |
210 | ||
211 | struct Closure_privateData : | |
212 | cy::Functor | |
213 | { | |
5f7a1d38 | 214 | CYProtect function_; |
24e7b1a6 | 215 | JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef); |
37954781 | 216 | |
24e7b1a6 | 217 | Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) : |
574d4720 | 218 | cy::Functor(NULL, false, signature), |
5f7a1d38 | 219 | function_(context, function), |
24e7b1a6 | 220 | adapter_(adapter) |
37954781 | 221 | { |
37954781 JF |
222 | } |
223 | }; | |
224 | ||
24e7b1a6 | 225 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
9ec7dd18 | 226 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
37954781 | 227 | |
3c1c3635 | 228 | #endif/*CYCRIPT_INTERNAL_HPP*/ |