]> git.saurik.com Git - cycript.git/blob - JavaScript.hpp
Hold JavaRef<jstring> throughout CYJavaUTF8String.
[cycript.git] / JavaScript.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_JAVASCRIPT_HPP
23 #define CYCRIPT_JAVASCRIPT_HPP
24
25 #include <set>
26 #include <string>
27
28 #include <JavaScriptCore/JSBase.h>
29 #include <JavaScriptCore/JSContextRef.h>
30 #include <JavaScriptCore/JSStringRef.h>
31 #include <JavaScriptCore/JSObjectRef.h>
32 #include <JavaScriptCore/JSValueRef.h>
33
34 #ifdef HAVE_FFI_FFI_H
35 #include <ffi/ffi.h>
36 #else
37 #include <ffi.h>
38 #endif
39
40 #include "Pooling.hpp"
41 #include "String.hpp"
42
43 extern JSStringRef Array_s;
44 extern JSStringRef constructor_s;
45 extern JSStringRef cy_s;
46 extern JSStringRef cyi_s;
47 extern JSStringRef cyt_s;
48 extern JSStringRef length_s;
49 extern JSStringRef message_s;
50 extern JSStringRef name_s;
51 extern JSStringRef pop_s;
52 extern JSStringRef prototype_s;
53 extern JSStringRef push_s;
54 extern JSStringRef splice_s;
55 extern JSStringRef toCYON_s;
56 extern JSStringRef toJSON_s;
57 extern JSStringRef toPointer_s;
58 extern JSStringRef toString_s;
59 extern JSStringRef weak_s;
60
61 void CYInitializeDynamic();
62 JSGlobalContextRef CYGetJSContext();
63 JSObjectRef CYGetGlobalObject(JSContextRef context);
64
65 extern "C" void CYSetupContext(JSGlobalContextRef context);
66 const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code);
67
68 #ifndef __ANDROID__
69 void CYCancel();
70 #endif
71
72 void CYSetArgs(const char *argv0, const char *script, int argc, const char *argv[]);
73
74 bool CYCastBool(JSContextRef context, JSValueRef value);
75 double CYCastDouble(JSContextRef context, JSValueRef value);
76
77 bool CYIsEqual(JSContextRef context, JSValueRef lhs, JSValueRef rhs);
78 bool CYIsStrictEqual(JSContextRef context, JSValueRef lhs, JSValueRef rhs);
79
80 CYUTF16String CYCastUTF16String(JSStringRef value);
81 CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value);
82 const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value);
83
84 bool CYHasProperty(JSContextRef context, JSObjectRef object, JSStringRef name);
85 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index);
86 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name);
87
88 void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value);
89 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone);
90 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone);
91
92 JSObjectRef CYGetPrototype(JSContextRef context, JSObjectRef object);
93 void CYSetPrototype(JSContextRef context, JSObjectRef object, JSValueRef prototype);
94
95 JSValueRef CYGetCachedValue(JSContextRef context, JSStringRef name);
96 JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name);
97
98 JSValueRef CYCastJSValue(JSContextRef context, bool value);
99 JSValueRef CYCastJSValue(JSContextRef context, double value);
100
101 JSValueRef CYCastJSValue(JSContextRef context, signed short int value);
102 JSValueRef CYCastJSValue(JSContextRef context, unsigned short int value);
103 JSValueRef CYCastJSValue(JSContextRef context, signed int value);
104 JSValueRef CYCastJSValue(JSContextRef context, unsigned int value);
105 JSValueRef CYCastJSValue(JSContextRef context, signed long int value);
106 JSValueRef CYCastJSValue(JSContextRef context, unsigned long int value);
107 JSValueRef CYCastJSValue(JSContextRef context, signed long long int value);
108 JSValueRef CYCastJSValue(JSContextRef context, unsigned long long int value);
109
110 JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value);
111 JSValueRef CYCastJSValue(JSContextRef context, const char *value);
112
113 JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value);
114 JSValueRef CYJSUndefined(JSContextRef context);
115 JSValueRef CYJSNull(JSContextRef context);
116
117 void *CYCastPointer_(JSContextRef context, JSValueRef value, bool *guess = NULL);
118
119 template <typename Type_>
120 _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value, bool *guess = NULL) {
121 return reinterpret_cast<Type_>(CYCastPointer_(context, value, guess));
122 }
123
124 void CYCallFunction(CYPool &pool, JSContextRef context, ffi_cif *cif, void (*function)(), void *value, void **values);
125 JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, bool variadic, const sig::Signature &signature, ffi_cif *cif, void (*function)());
126
127 bool CYIsCallable(JSContextRef context, JSValueRef value);
128 JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
129
130 const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object, std::set<void *> &objects);
131 std::set<void *> *CYCastObjects(JSContextRef context, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
132
133 struct CYHook {
134 void *(*ExecuteStart)(JSContextRef);
135 void (*ExecuteEnd)(JSContextRef, void *);
136
137 void (*CallFunction)(CYPool &, JSContextRef, ffi_cif *, void (*)(), void *, void **);
138
139 void (*Initialize)();
140 void (*SetupContext)(JSContextRef);
141
142 void *(*CastSymbol)(const char *);
143 };
144
145 struct CYRegisterHook {
146 CYRegisterHook(CYHook *hook);
147 };
148
149 JSObjectRef CYMakePointer(JSContextRef context, void *pointer, const sig::Type &type, ffi_type *ffi, JSObjectRef owner);
150
151 JSObjectRef CYMakeType(JSContextRef context, const sig::Type &type);
152
153 void CYFinalize(JSObjectRef object);
154
155 JSObjectRef CYObjectMakeArray(JSContextRef context, size_t length, const JSValueRef values[]);
156
157 size_t CYArrayLength(JSContextRef context, JSObjectRef array);
158 JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index);
159
160 void CYArrayPush(JSContextRef context, JSObjectRef array, size_t length, const JSValueRef arguments[]);
161 void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value);
162
163 bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t &index);
164
165 const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value);
166
167 JSStringRef CYCopyJSString(const char *value);
168 JSStringRef CYCopyJSString(JSStringRef value);
169 JSStringRef CYCopyJSString(CYUTF8String value);
170 JSStringRef CYCopyJSString(const std::string &value);
171 JSStringRef CYCopyJSString(CYUTF16String value);
172 JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value);
173
174 void CYGarbageCollect(JSContextRef context);
175 void CYDestroyContext();
176
177 class CYJSString {
178 private:
179 JSStringRef string_;
180
181 void Clear_() {
182 if (string_ != NULL)
183 JSStringRelease(string_);
184 }
185
186 public:
187 CYJSString() :
188 string_(NULL)
189 {
190 }
191
192 CYJSString(const CYJSString &rhs) :
193 string_(CYCopyJSString(rhs.string_))
194 {
195 }
196
197 template <typename Arg0_>
198 CYJSString(Arg0_ arg0) :
199 string_(CYCopyJSString(arg0))
200 {
201 }
202
203 template <typename Arg0_, typename Arg1_>
204 CYJSString(Arg0_ arg0, Arg1_ arg1) :
205 string_(CYCopyJSString(arg0, arg1))
206 {
207 }
208
209 CYJSString &operator =(const CYJSString &rhs) {
210 Clear_();
211 string_ = CYCopyJSString(rhs.string_);
212 return *this;
213 }
214
215 CYJSString &operator =(CYJSString &&rhs) {
216 std::swap(string_, rhs.string_);
217 return *this;
218 }
219
220 ~CYJSString() {
221 Clear_();
222 }
223
224 void Clear() {
225 Clear_();
226 string_ = NULL;
227 }
228
229 operator JSStringRef() const {
230 return string_;
231 }
232 };
233
234 template <size_t Size_>
235 class CYArrayBuilder {
236 private:
237 JSContextRef context_;
238 JSObjectRef &array_;
239 size_t size_;
240 JSValueRef values_[Size_];
241
242 void flush() {
243 if (array_ == NULL)
244 array_ = CYObjectMakeArray(context_, size_, values_);
245 else
246 CYArrayPush(context_, array_, size_, values_);
247 }
248
249 public:
250 CYArrayBuilder(JSContextRef context, JSObjectRef &array) :
251 context_(context),
252 array_(array),
253 size_(0)
254 {
255 }
256
257 ~CYArrayBuilder() {
258 flush();
259 }
260
261 void operator ()(JSValueRef value) {
262 if (size_ == Size_) {
263 flush();
264 size_ = 0;
265 }
266
267 values_[size_++] = value;
268 }
269 };
270
271 #ifdef __APPLE__
272 #define _weak __attribute__((__weak_import__));
273 #else
274 #define _weak
275 #endif
276
277 typedef struct OpaqueJSWeakObjectMap *JSWeakObjectMapRef;
278 typedef void (*JSWeakMapDestroyedCallback)(JSWeakObjectMapRef map, void *data);
279
280 extern "C" JSWeakObjectMapRef JSWeakObjectMapCreate(JSContextRef ctx, void *data, JSWeakMapDestroyedCallback destructor) _weak;
281 extern "C" void JSWeakObjectMapSet(JSContextRef ctx, JSWeakObjectMapRef map, void *key, JSObjectRef) _weak;
282 extern "C" JSObjectRef JSWeakObjectMapGet(JSContextRef ctx, JSWeakObjectMapRef map, void *key) _weak;
283 extern "C" bool JSWeakObjectMapClear(JSContextRef ctx, JSWeakObjectMapRef map, void *key, JSObjectRef object) _weak;
284 extern "C" void JSWeakObjectMapRemove(JSContextRef ctx, JSWeakObjectMapRef map, void* key) _weak;
285
286 typedef bool (*JSShouldTerminateCallback)(JSContextRef ctx, void *context);
287 extern "C" void JSContextGroupSetExecutionTimeLimit(JSContextGroupRef, double limit, JSShouldTerminateCallback, void *context) _weak;
288
289 #endif/*CYCRIPT_JAVASCRIPT_HPP*/