]> git.saurik.com Git - cycript.git/blob - Internal.hpp
Add a new ?gc to run GC without running a script.
[cycript.git] / Internal.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. 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 void Structor_(CYPool &pool, sig::Type *&type);
37
38 JSObjectRef CYMakeType(JSContextRef context, const char *type);
39 JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
40
41 extern JSClassRef Functor_;
42
43 struct Type_privateData :
44 CYData
45 {
46 static JSClassRef Class_;
47
48 ffi_type *ffi_;
49 sig::Type *type_;
50
51 void Set(sig::Type *type) {
52 type_ = new(*pool_) sig::Type;
53 sig::Copy(*pool_, *type_, *type);
54 }
55
56 Type_privateData(CYPool &pool, const char *type) :
57 CYData(pool),
58 ffi_(NULL)
59 {
60 sig::Signature signature;
61 sig::Parse(*pool_, &signature, type, &Structor_);
62 type_ = signature.elements[0].type;
63 }
64
65 Type_privateData(const char *type) :
66 ffi_(NULL)
67 {
68 sig::Signature signature;
69 sig::Parse(*pool_, &signature, type, &Structor_);
70 type_ = signature.elements[0].type;
71 }
72
73 Type_privateData(sig::Type *type) :
74 ffi_(NULL)
75 {
76 if (type != NULL)
77 Set(type);
78 }
79
80 Type_privateData(sig::Type *type, ffi_type *ffi) {
81 ffi_ = new(*pool_) ffi_type;
82 sig::Copy(*pool_, *ffi_, *ffi);
83 Set(type);
84 }
85
86 ffi_type *GetFFI() {
87 if (ffi_ == NULL) {
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;
98 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
99
100 ffi_ = new(*pool_) ffi_type;
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:
135 JSGlobalContextRef context_;
136 JSObjectRef owner_;
137
138 public:
139 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
140 CYValue(value),
141 context_(CYGetJSContext(context)),
142 owner_(owner)
143 {
144 //XXX:JSGlobalContextRetain(context_);
145 if (owner_ != NULL)
146 JSValueProtect(context_, owner_);
147 }
148
149 virtual ~CYOwned() {
150 if (owner_ != NULL)
151 JSValueUnprotect(context_, owner_);
152 //XXX:JSGlobalContextRelease(context_);
153 }
154
155 JSObjectRef GetOwner() const {
156 return owner_;
157 }
158 };
159
160 namespace cy {
161 struct Functor :
162 CYValue
163 {
164 private:
165 void set() {
166 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
167 }
168
169 public:
170 sig::Signature signature_;
171 ffi_cif cif_;
172
173 Functor(const sig::Signature &signature, void (*value)()) :
174 CYValue(reinterpret_cast<void *>(value))
175 {
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();
185 }
186
187 void (*GetValue() const)() {
188 return reinterpret_cast<void (*)()>(value_);
189 }
190
191 static JSStaticFunction const * const StaticFunctions;
192 }; }
193
194 struct Closure_privateData :
195 cy::Functor
196 {
197 JSGlobalContextRef context_;
198 JSObjectRef function_;
199
200 Closure_privateData(JSContextRef context, JSObjectRef function, const sig::Signature &signature) :
201 cy::Functor(signature, NULL),
202 context_(CYGetJSContext(context)),
203 function_(function)
204 {
205 //XXX:JSGlobalContextRetain(context_);
206 JSValueProtect(context_, function_);
207 }
208
209 virtual ~Closure_privateData() {
210 JSValueUnprotect(context_, function_);
211 //XXX:JSGlobalContextRelease(context_);
212 }
213 };
214
215 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, void (*callback)(ffi_cif *, void *, void **, void *));
216 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
217
218 #endif/*CYCRIPT_INTERNAL_HPP*/