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