]> git.saurik.com Git - cycript.git/blob - Internal.hpp
Use pkg-config to find Ruby 2.1 (*sigh*) on Linux.
[cycript.git] / Internal.hpp
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 "Pooling.hpp"
34
35 JSGlobalContextRef CYGetJSContext(JSContextRef context);
36 sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
37
38 extern JSClassRef Functor_;
39
40 struct Type_privateData :
41 CYData
42 {
43 static JSClassRef Class_;
44
45 ffi_type *ffi_;
46 sig::Type *type_;
47
48 Type_privateData(const char *type) :
49 ffi_(NULL)
50 {
51 sig::Signature signature;
52 sig::Parse(*pool_, &signature, type, &Structor_);
53 type_ = signature.elements[0].type;
54 }
55
56 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
57 type_(type.Copy(*pool_))
58 {
59
60 if (ffi == NULL)
61 ffi_ = NULL;
62 else {
63 ffi_ = new(*pool_) ffi_type;
64 sig::Copy(*pool_, *ffi_, *ffi);
65 }
66 }
67
68 ffi_type *GetFFI() {
69 if (ffi_ == NULL) {
70 sig::Element element;
71 element.name = NULL;
72 element.type = type_;
73 element.offset = 0;
74
75 sig::Signature signature;
76 signature.elements = &element;
77 signature.count = 1;
78
79 ffi_cif cif;
80 sig::sig_ffi_cif(*pool_, false, signature, &cif);
81
82 ffi_ = new(*pool_) ffi_type;
83 *ffi_ = *cif.rtype;
84 }
85
86 return ffi_;
87 }
88 };
89
90 struct CYValue :
91 CYData
92 {
93 void *value_;
94
95 CYValue() {
96 }
97
98 CYValue(const void *value) :
99 value_(const_cast<void *>(value))
100 {
101 }
102
103 CYValue(const CYValue &rhs) :
104 value_(rhs.value_)
105 {
106 }
107
108 virtual Type_privateData *GetType() const {
109 return NULL;
110 }
111 };
112
113 struct CYOwned :
114 CYValue
115 {
116 private:
117 JSGlobalContextRef context_;
118 JSObjectRef owner_;
119
120 public:
121 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
122 CYValue(value),
123 context_(CYGetJSContext(context)),
124 owner_(owner)
125 {
126 //XXX:JSGlobalContextRetain(context_);
127 if (owner_ != NULL)
128 JSValueProtect(context_, owner_);
129 }
130
131 virtual ~CYOwned() {
132 if (owner_ != NULL)
133 JSValueUnprotect(context_, owner_);
134 //XXX:JSGlobalContextRelease(context_);
135 }
136
137 JSObjectRef GetOwner() const {
138 return owner_;
139 }
140 };
141
142 namespace cy {
143 struct Functor :
144 CYValue
145 {
146 private:
147 void set() {
148 sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_);
149 }
150
151 public:
152 bool variadic_;
153 sig::Signature signature_;
154 ffi_cif cif_;
155
156 Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
157 CYValue(reinterpret_cast<void *>(value)),
158 variadic_(variadic)
159 {
160 sig::Copy(*pool_, signature_, signature);
161 set();
162 }
163
164 Functor(void (*value)(), const char *encoding) :
165 CYValue(reinterpret_cast<void *>(value)),
166 variadic_(false)
167 {
168 sig::Parse(*pool_, &signature_, encoding, &Structor_);
169 set();
170 }
171
172 void (*GetValue() const)() {
173 return reinterpret_cast<void (*)()>(value_);
174 }
175
176 static JSStaticFunction const * const StaticFunctions;
177 static JSStaticValue const * const StaticValues;
178 }; }
179
180 struct Closure_privateData :
181 cy::Functor
182 {
183 JSGlobalContextRef context_;
184 JSObjectRef function_;
185 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
186
187 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
188 cy::Functor(NULL, false, signature),
189 context_(CYGetJSContext(context)),
190 function_(function),
191 adapter_(adapter)
192 {
193 //XXX:JSGlobalContextRetain(context_);
194 JSValueProtect(context_, function_);
195 }
196
197 virtual ~Closure_privateData() {
198 JSValueUnprotect(context_, function_);
199 //XXX:JSGlobalContextRelease(context_);
200 }
201 };
202
203 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
204 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
205
206 #endif/*CYCRIPT_INTERNAL_HPP*/