]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
d15b59f5 JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
d15b59f5 | 6 | /* |
c15969fd JF |
7 | * Cycript is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
d15b59f5 | 11 | * |
c15969fd JF |
12 | * Cycript is distributed in the hope that it will be useful, but |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
d15b59f5 | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
d15b59f5 JF |
20 | /* }}} */ |
21 | ||
37954781 JF |
22 | #ifndef CYCRIPT_JAVASCRIPT_HPP |
23 | #define CYCRIPT_JAVASCRIPT_HPP | |
24 | ||
9cad30fa JF |
25 | #include <JavaScriptCore/JSBase.h> |
26 | #include <JavaScriptCore/JSContextRef.h> | |
27 | #include <JavaScriptCore/JSStringRef.h> | |
28 | #include <JavaScriptCore/JSObjectRef.h> | |
29 | #include <JavaScriptCore/JSValueRef.h> | |
30 | ||
b128dfee DWT |
31 | #ifdef HAVE_FFI_FFI_H |
32 | #include <ffi/ffi.h> | |
33 | #else | |
9cad30fa | 34 | #include <ffi.h> |
b128dfee | 35 | #endif |
9cad30fa | 36 | |
b799113b JF |
37 | #include "Pooling.hpp" |
38 | #include "String.hpp" | |
ef6f48be | 39 | |
498c3570 JF |
40 | extern JSStringRef Array_s; |
41 | extern JSStringRef cy_s; | |
4dd55d2f | 42 | extern JSStringRef cyi_s; |
00b9328f JF |
43 | extern JSStringRef length_s; |
44 | extern JSStringRef message_s; | |
45 | extern JSStringRef name_s; | |
46 | extern JSStringRef pop_s; | |
47 | extern JSStringRef prototype_s; | |
48 | extern JSStringRef push_s; | |
49 | extern JSStringRef splice_s; | |
50 | extern JSStringRef toCYON_s; | |
51 | extern JSStringRef toJSON_s; | |
4cb8aa43 JF |
52 | extern JSStringRef toPointer_s; |
53 | extern JSStringRef toString_s; | |
37954781 | 54 | |
9cad30fa JF |
55 | void CYInitializeDynamic(); |
56 | JSGlobalContextRef CYGetJSContext(); | |
57 | JSObjectRef CYGetGlobalObject(JSContextRef context); | |
58 | ||
59 | extern "C" void CYSetupContext(JSGlobalContextRef context); | |
89d16b11 | 60 | const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code); |
9cad30fa JF |
61 | |
62 | void CYSetArgs(int argc, const char *argv[]); | |
63 | ||
64 | bool CYCastBool(JSContextRef context, JSValueRef value); | |
65 | double CYCastDouble(JSContextRef context, JSValueRef value); | |
66 | ||
b799113b JF |
67 | CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value); |
68 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSStringRef value); | |
9cad30fa | 69 | |
58321c0a | 70 | bool CYHasProperty(JSContextRef context, JSObjectRef object, JSStringRef name); |
9cad30fa JF |
71 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index); |
72 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name); | |
73 | ||
74 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value); | |
75 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone); | |
76 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone); | |
77 | ||
78 | JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name); | |
79 | ||
80 | JSValueRef CYCastJSValue(JSContextRef context, bool value); | |
81 | JSValueRef CYCastJSValue(JSContextRef context, double value); | |
82 | JSValueRef CYCastJSValue(JSContextRef context, int value); | |
83 | JSValueRef CYCastJSValue(JSContextRef context, unsigned int value); | |
84 | JSValueRef CYCastJSValue(JSContextRef context, long int value); | |
85 | JSValueRef CYCastJSValue(JSContextRef context, long unsigned int value); | |
86 | JSValueRef CYCastJSValue(JSContextRef context, long long int value); | |
87 | JSValueRef CYCastJSValue(JSContextRef context, long long unsigned int value); | |
88 | ||
89 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value); | |
90 | JSValueRef CYCastJSValue(JSContextRef context, const char *value); | |
91 | ||
92 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value); | |
93 | JSValueRef CYJSUndefined(JSContextRef context); | |
94 | JSValueRef CYJSNull(JSContextRef context); | |
95 | ||
96 | void *CYCastPointer_(JSContextRef context, JSValueRef value); | |
97 | ||
98 | template <typename Type_> | |
99 | _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
100 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
101 | } | |
102 | ||
b799113b | 103 | void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value); |
9cad30fa JF |
104 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL); |
105 | ||
9d512587 | 106 | JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, sig::Signature *signature, ffi_cif *cif, void (*function)()); |
9cad30fa JF |
107 | |
108 | bool CYIsCallable(JSContextRef context, JSValueRef value); | |
109 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]); | |
110 | ||
b799113b | 111 | const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object); |
9cad30fa JF |
112 | |
113 | struct CYHooks { | |
114 | void *(*ExecuteStart)(JSContextRef); | |
115 | void (*ExecuteEnd)(JSContextRef, void *); | |
116 | ||
9cad30fa JF |
117 | void (*CallFunction)(JSContextRef, ffi_cif *, void (*)(), uint8_t *, void **); |
118 | ||
119 | void (*Initialize)(); | |
120 | void (*SetupContext)(JSContextRef); | |
121 | ||
b799113b | 122 | bool (*PoolFFI)(CYPool *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef); |
9cad30fa JF |
123 | JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef); |
124 | }; | |
125 | ||
126 | extern struct CYHooks *hooks_; | |
127 | ||
9cad30fa JF |
128 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner); |
129 | ||
9a39f705 JF |
130 | JSObjectRef CYMakeType(JSContextRef context, const char *encoding); |
131 | JSObjectRef CYMakeType(JSContextRef context, sig::Type *type); | |
132 | JSObjectRef CYMakeType(JSContextRef context, sig::Signature *signature); | |
133 | ||
9cad30fa JF |
134 | void CYFinalize(JSObjectRef object); |
135 | ||
07db5f4d JF |
136 | size_t CYArrayLength(JSContextRef context, JSObjectRef array); |
137 | JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index); | |
138 | void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value); | |
139 | ||
b799113b | 140 | const char *CYPoolCString(CYPool &pool, JSContextRef context, JSValueRef value); |
9cad30fa JF |
141 | |
142 | JSStringRef CYCopyJSString(const char *value); | |
143 | JSStringRef CYCopyJSString(JSStringRef value); | |
144 | JSStringRef CYCopyJSString(CYUTF8String value); | |
145 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value); | |
146 | ||
51b6165e | 147 | void CYGarbageCollect(JSContextRef context); |
8fab8594 JF |
148 | void CYDestroyContext(); |
149 | ||
9cad30fa JF |
150 | class CYJSString { |
151 | private: | |
152 | JSStringRef string_; | |
153 | ||
154 | void Clear_() { | |
155 | if (string_ != NULL) | |
156 | JSStringRelease(string_); | |
157 | } | |
158 | ||
159 | public: | |
160 | CYJSString(const CYJSString &rhs) : | |
161 | string_(CYCopyJSString(rhs.string_)) | |
162 | { | |
163 | } | |
164 | ||
165 | template <typename Arg0_> | |
166 | CYJSString(Arg0_ arg0) : | |
167 | string_(CYCopyJSString(arg0)) | |
168 | { | |
169 | } | |
170 | ||
171 | template <typename Arg0_, typename Arg1_> | |
172 | CYJSString(Arg0_ arg0, Arg1_ arg1) : | |
173 | string_(CYCopyJSString(arg0, arg1)) | |
174 | { | |
175 | } | |
176 | ||
177 | CYJSString &operator =(const CYJSString &rhs) { | |
178 | Clear_(); | |
179 | string_ = CYCopyJSString(rhs.string_); | |
180 | return *this; | |
181 | } | |
182 | ||
183 | ~CYJSString() { | |
184 | Clear_(); | |
185 | } | |
186 | ||
187 | void Clear() { | |
188 | Clear_(); | |
189 | string_ = NULL; | |
190 | } | |
191 | ||
192 | operator JSStringRef() const { | |
193 | return string_; | |
194 | } | |
195 | }; | |
196 | ||
37954781 | 197 | #endif/*CYCRIPT_JAVASCRIPT_HPP*/ |