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