]> git.saurik.com Git - cycript.git/blob - Internal.hpp
Avoid libapr's .la from breaking libiconv search.
[cycript.git] / Internal.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser 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 "Pooling.hpp"
26
27 #include <JavaScriptCore/JSBase.h>
28 #include <JavaScriptCore/JSContextRef.h>
29 #include <JavaScriptCore/JSObjectRef.h>
30 #include <JavaScriptCore/JSValueRef.h>
31
32 #include <sig/parse.hpp>
33 #include <sig/ffi_type.hpp>
34
35 JSGlobalContextRef CYGetJSContext(JSContextRef context);
36 void Structor_(apr_pool_t *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(apr_pool_t *pool, const char *type) :
57 ffi_(NULL)
58 {
59 _assert(pool != NULL);
60 pool_ = pool;
61 sig::Signature signature;
62 sig::Parse(pool_, &signature, type, &Structor_);
63 type_ = signature.elements[0].type;
64 }
65
66 Type_privateData(const char *type) :
67 ffi_(NULL)
68 {
69 sig::Signature signature;
70 sig::Parse(pool_, &signature, type, &Structor_);
71 type_ = signature.elements[0].type;
72 }
73
74 Type_privateData(sig::Type *type) :
75 ffi_(NULL)
76 {
77 if (type != NULL)
78 Set(type);
79 }
80
81 Type_privateData(sig::Type *type, ffi_type *ffi) {
82 ffi_ = new(pool_) ffi_type;
83 sig::Copy(pool_, *ffi_, *ffi);
84 Set(type);
85 }
86
87 ffi_type *GetFFI() {
88 if (ffi_ == NULL) {
89 ffi_ = new(pool_) ffi_type;
90
91 sig::Element element;
92 element.name = NULL;
93 element.type = type_;
94 element.offset = 0;
95
96 sig::Signature signature;
97 signature.elements = &element;
98 signature.count = 1;
99
100 ffi_cif cif;
101 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
102 *ffi_ = *cif.rtype;
103 }
104
105 return ffi_;
106 }
107 };
108
109 struct CYValue :
110 CYData
111 {
112 void *value_;
113
114 CYValue() {
115 }
116
117 CYValue(const void *value) :
118 value_(const_cast<void *>(value))
119 {
120 }
121
122 CYValue(const CYValue &rhs) :
123 value_(rhs.value_)
124 {
125 }
126
127 virtual Type_privateData *GetType() const {
128 return NULL;
129 }
130 };
131
132 struct CYOwned :
133 CYValue
134 {
135 private:
136 JSGlobalContextRef context_;
137 JSObjectRef owner_;
138
139 public:
140 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
141 CYValue(value),
142 context_(CYGetJSContext(context)),
143 owner_(owner)
144 {
145 //XXX:JSGlobalContextRetain(context_);
146 if (owner_ != NULL)
147 JSValueProtect(context_, owner_);
148 }
149
150 virtual ~CYOwned() {
151 if (owner_ != NULL)
152 JSValueUnprotect(context_, owner_);
153 //XXX:JSGlobalContextRelease(context_);
154 }
155
156 JSObjectRef GetOwner() const {
157 return owner_;
158 }
159 };
160
161 namespace cy {
162 struct Functor :
163 CYValue
164 {
165 sig::Signature signature_;
166 ffi_cif cif_;
167
168 Functor(const char *type, void (*value)()) :
169 CYValue(reinterpret_cast<void *>(value))
170 {
171 sig::Parse(pool_, &signature_, type, &Structor_);
172 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
173 }
174
175 void (*GetValue() const)() {
176 return reinterpret_cast<void (*)()>(value_);
177 }
178
179 static JSStaticFunction const * const StaticFunctions;
180 }; }
181
182 struct Closure_privateData :
183 cy::Functor
184 {
185 JSGlobalContextRef context_;
186 JSObjectRef function_;
187
188 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
189 cy::Functor(type, NULL),
190 context_(CYGetJSContext(context)),
191 function_(function)
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 char *type, void (*callback)(ffi_cif *, void *, void **, void *));
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*/