]> git.saurik.com Git - cycript.git/blob - Internal.hpp
Do not allow JavaMethod calls on non-Java objects.
[cycript.git] / Internal.hpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 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 "JavaScript.hpp"
34 #include "Pooling.hpp"
35 #include "Utility.hpp"
36
37 JSGlobalContextRef CYGetJSContext(JSContextRef context);
38 sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
39
40 extern JSClassRef Functor_;
41
42 template <typename Internal_>
43 struct CYPrivate :
44 CYData
45 {
46 static JSClassRef Class_;
47
48 _finline JSValueRef GetPrototype(JSContextRef context) const {
49 return NULL;
50 }
51
52 template <typename... Args_>
53 _finline static JSClassRef GetClass(Args_ &&... args) {
54 return Class_;
55 }
56
57 template <typename... Args_>
58 static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
59 Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
60 JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal));
61 if (JSValueRef prototype = internal->GetPrototype(context))
62 CYSetPrototype(context, object, prototype);
63 return object;
64 }
65
66 static Internal_ *Get(JSContextRef context, JSObjectRef object) {
67 _assert(JSValueIsObjectOfClass(context, object, Class_));
68 return static_cast<Internal_ *>(JSObjectGetPrivate(object));
69 }
70 };
71
72 struct Type_privateData :
73 CYPrivate<Type_privateData>
74 {
75 ffi_type *ffi_;
76 sig::Type *type_;
77
78 Type_privateData(const char *type) :
79 ffi_(NULL)
80 {
81 sig::Signature signature;
82 sig::Parse(*pool_, &signature, type, &Structor_);
83 type_ = signature.elements[0].type;
84 }
85
86 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
87 type_(type.Copy(*pool_))
88 {
89
90 if (ffi == NULL)
91 ffi_ = NULL;
92 else {
93 ffi_ = new(*pool_) ffi_type;
94 sig::Copy(*pool_, *ffi_, *ffi);
95 }
96 }
97
98 ffi_type *GetFFI() {
99 if (ffi_ == NULL) {
100 sig::Element element;
101 element.name = NULL;
102 element.type = type_;
103 element.offset = 0;
104
105 sig::Signature signature;
106 signature.elements = &element;
107 signature.count = 1;
108
109 ffi_cif cif;
110 sig::sig_ffi_cif(*pool_, false, signature, &cif);
111
112 ffi_ = new(*pool_) ffi_type;
113 *ffi_ = *cif.rtype;
114 }
115
116 return ffi_;
117 }
118 };
119
120 template <typename Internal_, typename Value_>
121 struct CYValue :
122 CYPrivate<Internal_>
123 {
124 Value_ value_;
125
126 CYValue() {
127 }
128
129 CYValue(const Value_ &value) :
130 value_(value)
131 {
132 }
133
134 CYValue(const CYValue &rhs) :
135 value_(rhs.value_)
136 {
137 }
138 };
139
140 template <typename Internal_>
141 JSClassRef CYPrivate<Internal_>::Class_;
142
143 struct CYProtect {
144 private:
145 JSGlobalContextRef context_;
146 JSObjectRef object_;
147
148 public:
149 CYProtect(JSContextRef context, JSObjectRef object) :
150 context_(CYGetJSContext(context)),
151 object_(object)
152 {
153 //XXX:JSGlobalContextRetain(context_);
154 if (object_ != NULL)
155 JSValueProtect(context_, object_);
156 }
157
158 ~CYProtect() {
159 if (object_ != NULL)
160 JSValueUnprotect(context_, object_);
161 //XXX:JSGlobalContextRelease(context_);
162 }
163
164 operator bool() const {
165 return object_ != NULL;
166 }
167
168 operator JSContextRef() const {
169 return context_;
170 }
171
172 operator JSObjectRef() const {
173 return object_;
174 }
175 };
176
177 namespace cy {
178 struct Functor :
179 CYValue<Functor, void (*)()>
180 {
181 private:
182 void set() {
183 sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_);
184 }
185
186 public:
187 bool variadic_;
188 sig::Signature signature_;
189 ffi_cif cif_;
190
191 Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
192 CYValue(value),
193 variadic_(variadic)
194 {
195 sig::Copy(*pool_, signature_, signature);
196 set();
197 }
198
199 Functor(void (*value)(), const char *encoding) :
200 CYValue(value),
201 variadic_(false)
202 {
203 sig::Parse(*pool_, &signature_, encoding, &Structor_);
204 set();
205 }
206
207 static JSStaticFunction const * const StaticFunctions;
208 static JSStaticValue const * const StaticValues;
209 }; }
210
211 struct Closure_privateData :
212 cy::Functor
213 {
214 CYProtect function_;
215 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
216
217 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
218 cy::Functor(NULL, false, signature),
219 function_(context, function),
220 adapter_(adapter)
221 {
222 }
223 };
224
225 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
226 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
227
228 #endif/*CYCRIPT_INTERNAL_HPP*/