]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
f95d2598 | 2 | * Copyright (C) 2009-2014 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 | ||
b799113b | 33 | #include "Pooling.hpp" |
3c1c3635 | 34 | |
bc60fb46 | 35 | JSGlobalContextRef CYGetJSContext(JSContextRef context); |
b799113b | 36 | void Structor_(CYPool &pool, sig::Type *&type); |
3c1c3635 | 37 | |
8a23fb2e JF |
38 | JSObjectRef CYMakeType(JSContextRef context, sig::Type *type); |
39 | ||
3f9ae37c JF |
40 | extern JSClassRef Functor_; |
41 | ||
3c1c3635 JF |
42 | struct Type_privateData : |
43 | CYData | |
44 | { | |
45 | static JSClassRef Class_; | |
46 | ||
47 | ffi_type *ffi_; | |
48 | sig::Type *type_; | |
49 | ||
50 | void Set(sig::Type *type) { | |
b799113b JF |
51 | type_ = new(*pool_) sig::Type; |
52 | sig::Copy(*pool_, *type_, *type); | |
3c1c3635 JF |
53 | } |
54 | ||
807a88be JF |
55 | Type_privateData(const char *type) : |
56 | ffi_(NULL) | |
57 | { | |
3c1c3635 | 58 | sig::Signature signature; |
b799113b | 59 | sig::Parse(*pool_, &signature, type, &Structor_); |
3c1c3635 JF |
60 | type_ = signature.elements[0].type; |
61 | } | |
62 | ||
baea6375 JF |
63 | Type_privateData(sig::Primitive primitive) : |
64 | ffi_(NULL) | |
65 | { | |
66 | sig::Type type; | |
67 | memset(&type, 0, sizeof(type)); | |
68 | type.primitive = primitive; | |
69 | Set(&type); | |
70 | } | |
71 | ||
3c1c3635 JF |
72 | Type_privateData(sig::Type *type) : |
73 | ffi_(NULL) | |
74 | { | |
a2f3ecab JF |
75 | // XXX: just in case I messed up migrating |
76 | _assert(type != NULL); | |
77 | Set(type); | |
3c1c3635 JF |
78 | } |
79 | ||
80 | Type_privateData(sig::Type *type, ffi_type *ffi) { | |
b799113b JF |
81 | ffi_ = new(*pool_) ffi_type; |
82 | sig::Copy(*pool_, *ffi_, *ffi); | |
3c1c3635 JF |
83 | Set(type); |
84 | } | |
85 | ||
86 | ffi_type *GetFFI() { | |
87 | if (ffi_ == NULL) { | |
3c1c3635 JF |
88 | sig::Element element; |
89 | element.name = NULL; | |
90 | element.type = type_; | |
91 | element.offset = 0; | |
92 | ||
93 | sig::Signature signature; | |
94 | signature.elements = &element; | |
95 | signature.count = 1; | |
96 | ||
97 | ffi_cif cif; | |
b799113b | 98 | sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif); |
446d46d2 JF |
99 | |
100 | ffi_ = new(*pool_) ffi_type; | |
3c1c3635 JF |
101 | *ffi_ = *cif.rtype; |
102 | } | |
103 | ||
104 | return ffi_; | |
105 | } | |
106 | }; | |
107 | ||
108 | struct CYValue : | |
109 | CYData | |
110 | { | |
111 | void *value_; | |
112 | ||
113 | CYValue() { | |
114 | } | |
115 | ||
116 | CYValue(const void *value) : | |
117 | value_(const_cast<void *>(value)) | |
118 | { | |
119 | } | |
120 | ||
121 | CYValue(const CYValue &rhs) : | |
122 | value_(rhs.value_) | |
123 | { | |
124 | } | |
125 | ||
126 | virtual Type_privateData *GetType() const { | |
127 | return NULL; | |
128 | } | |
129 | }; | |
130 | ||
131 | struct CYOwned : | |
132 | CYValue | |
133 | { | |
134 | private: | |
bc60fb46 | 135 | JSGlobalContextRef context_; |
3c1c3635 JF |
136 | JSObjectRef owner_; |
137 | ||
138 | public: | |
139 | CYOwned(void *value, JSContextRef context, JSObjectRef owner) : | |
140 | CYValue(value), | |
bc60fb46 | 141 | context_(CYGetJSContext(context)), |
3c1c3635 JF |
142 | owner_(owner) |
143 | { | |
bc60fb46 | 144 | //XXX:JSGlobalContextRetain(context_); |
3c1c3635 JF |
145 | if (owner_ != NULL) |
146 | JSValueProtect(context_, owner_); | |
147 | } | |
148 | ||
149 | virtual ~CYOwned() { | |
150 | if (owner_ != NULL) | |
151 | JSValueUnprotect(context_, owner_); | |
bc60fb46 | 152 | //XXX:JSGlobalContextRelease(context_); |
3c1c3635 JF |
153 | } |
154 | ||
155 | JSObjectRef GetOwner() const { | |
156 | return owner_; | |
157 | } | |
158 | }; | |
159 | ||
37954781 JF |
160 | namespace cy { |
161 | struct Functor : | |
162 | CYValue | |
163 | { | |
077756a4 JF |
164 | private: |
165 | void set() { | |
166 | sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_); | |
167 | } | |
168 | ||
169 | public: | |
37954781 JF |
170 | sig::Signature signature_; |
171 | ffi_cif cif_; | |
172 | ||
67110a15 | 173 | Functor(const sig::Signature &signature, void (*value)()) : |
37954781 JF |
174 | CYValue(reinterpret_cast<void *>(value)) |
175 | { | |
077756a4 JF |
176 | sig::Copy(*pool_, signature_, signature); |
177 | set(); | |
178 | } | |
179 | ||
180 | Functor(const char *encoding, void (*value)()) : | |
181 | CYValue(reinterpret_cast<void *>(value)) | |
182 | { | |
183 | sig::Parse(*pool_, &signature_, encoding, &Structor_); | |
184 | set(); | |
37954781 JF |
185 | } |
186 | ||
01d0f64d | 187 | void (*GetValue() const)() { |
37954781 JF |
188 | return reinterpret_cast<void (*)()>(value_); |
189 | } | |
190 | ||
191 | static JSStaticFunction const * const StaticFunctions; | |
8493347d | 192 | static JSStaticValue const * const StaticValues; |
37954781 JF |
193 | }; } |
194 | ||
195 | struct Closure_privateData : | |
196 | cy::Functor | |
197 | { | |
bc60fb46 | 198 | JSGlobalContextRef context_; |
37954781 JF |
199 | JSObjectRef function_; |
200 | ||
67110a15 JF |
201 | Closure_privateData(JSContextRef context, JSObjectRef function, const sig::Signature &signature) : |
202 | cy::Functor(signature, NULL), | |
bc60fb46 | 203 | context_(CYGetJSContext(context)), |
37954781 JF |
204 | function_(function) |
205 | { | |
bc60fb46 | 206 | //XXX:JSGlobalContextRetain(context_); |
37954781 JF |
207 | JSValueProtect(context_, function_); |
208 | } | |
209 | ||
210 | virtual ~Closure_privateData() { | |
211 | JSValueUnprotect(context_, function_); | |
bc60fb46 | 212 | //XXX:JSGlobalContextRelease(context_); |
37954781 JF |
213 | } |
214 | }; | |
215 | ||
67110a15 | 216 | Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, void (*callback)(ffi_cif *, void *, void **, void *)); |
9ec7dd18 | 217 | void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef)); |
37954781 | 218 | |
3c1c3635 | 219 | #endif/*CYCRIPT_INTERNAL_HPP*/ |