]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime | |
2 | * Copyright (C) 2009-2015 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 | template <typename Internal_> | |
43 | struct CYPrivate : | |
44 | CYData | |
45 | { | |
46 | static JSClassRef Class_; | |
47 | ||
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 | } | |
65 | }; | |
66 | ||
67 | struct Type_privateData : | |
68 | CYPrivate<Type_privateData> | |
69 | { | |
70 | ffi_type *ffi_; | |
71 | sig::Type *type_; | |
72 | ||
73 | Type_privateData(const char *type) : | |
74 | ffi_(NULL) | |
75 | { | |
76 | sig::Signature signature; | |
77 | sig::Parse(*pool_, &signature, type, &Structor_); | |
78 | type_ = signature.elements[0].type; | |
79 | } | |
80 | ||
81 | Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) : | |
82 | type_(type.Copy(*pool_)) | |
83 | { | |
84 | ||
85 | if (ffi == NULL) | |
86 | ffi_ = NULL; | |
87 | else { | |
88 | ffi_ = new(*pool_) ffi_type; | |
89 | sig::Copy(*pool_, *ffi_, *ffi); | |
90 | } | |
91 | } | |
92 | ||
93 | ffi_type *GetFFI() { | |
94 | if (ffi_ == NULL) { | |
95 | sig::Element element; | |
96 | element.name = NULL; | |
97 | element.type = type_; | |
98 | element.offset = 0; | |
99 | ||
100 | sig::Signature signature; | |
101 | signature.elements = &element; | |
102 | signature.count = 1; | |
103 | ||
104 | ffi_cif cif; | |
105 | sig::sig_ffi_cif(*pool_, false, signature, &cif); | |
106 | ||
107 | ffi_ = new(*pool_) ffi_type; | |
108 | *ffi_ = *cif.rtype; | |
109 | } | |
110 | ||
111 | return ffi_; | |
112 | } | |
113 | }; | |
114 | ||
115 | template <typename Internal_, typename Value_> | |
116 | struct CYValue : | |
117 | CYPrivate<Internal_> | |
118 | { | |
119 | Value_ value_; | |
120 | ||
121 | CYValue() { | |
122 | } | |
123 | ||
124 | CYValue(const Value_ &value) : | |
125 | value_(value) | |
126 | { | |
127 | } | |
128 | ||
129 | CYValue(const CYValue &rhs) : | |
130 | value_(rhs.value_) | |
131 | { | |
132 | } | |
133 | }; | |
134 | ||
135 | template <typename Internal_> | |
136 | JSClassRef CYPrivate<Internal_>::Class_; | |
137 | ||
138 | struct CYProtect { | |
139 | private: | |
140 | JSGlobalContextRef context_; | |
141 | JSObjectRef object_; | |
142 | ||
143 | public: | |
144 | CYProtect(JSContextRef context, JSObjectRef object) : | |
145 | context_(CYGetJSContext(context)), | |
146 | object_(object) | |
147 | { | |
148 | //XXX:JSGlobalContextRetain(context_); | |
149 | if (object_ != NULL) | |
150 | JSValueProtect(context_, object_); | |
151 | } | |
152 | ||
153 | ~CYProtect() { | |
154 | if (object_ != NULL) | |
155 | JSValueUnprotect(context_, object_); | |
156 | //XXX:JSGlobalContextRelease(context_); | |
157 | } | |
158 | ||
159 | operator bool() const { | |
160 | return object_ != NULL; | |
161 | } | |
162 | ||
163 | operator JSContextRef() const { | |
164 | return context_; | |
165 | } | |
166 | ||
167 | operator JSObjectRef() const { | |
168 | return object_; | |
169 | } | |
170 | }; | |
171 | ||
172 | namespace cy { | |
173 | struct Functor : | |
174 | CYValue<Functor, void (*)()> | |
175 | { | |
176 | private: | |
177 | void set() { | |
178 | sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_); | |
179 | } | |
180 | ||
181 | public: | |
182 | bool variadic_; | |
183 | sig::Signature signature_; | |
184 | ffi_cif cif_; | |
185 | ||
186 | Functor(void (*value)(), bool variadic, const sig::Signature &signature) : | |
187 | CYValue(value), | |
188 | variadic_(variadic) | |
189 | { | |
190 | sig::Copy(*pool_, signature_, signature); | |
191 | set(); | |
192 | } | |
193 | ||
194 | Functor(void (*value)(), const char *encoding) : | |
195 | CYValue(value), | |
196 | variadic_(false) | |
197 | { | |
198 | sig::Parse(*pool_, &signature_, encoding, &Structor_); | |
199 | set(); | |
200 | } | |
201 | ||
202 | static JSStaticFunction const * const StaticFunctions; | |
203 | static JSStaticValue const * const StaticValues; | |
204 | }; } | |
205 | ||
206 | struct Closure_privateData : | |
207 | cy::Functor | |
208 | { | |
209 | CYProtect function_; | |
210 | JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef); | |
211 | ||
212 | Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) : | |
213 | cy::Functor(NULL, false, signature), | |
214 | function_(context, function), | |
215 | adapter_(adapter) | |
216 | { | |
217 | } | |
218 | }; | |
219 | ||
220 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); | |
221 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); | |
222 | ||
223 | #endif/*CYCRIPT_INTERNAL_HPP*/ |