1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CYCRIPT_JAVASCRIPT_HPP
23 #define CYCRIPT_JAVASCRIPT_HPP
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>
37 #include <apr_pools.h>
41 extern JSStringRef Array_s;
42 extern JSStringRef cy_s;
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;
52 extern JSStringRef toPointer_s;
53 extern JSStringRef toString_s;
55 void CYInitializeDynamic();
56 JSGlobalContextRef CYGetJSContext();
57 JSObjectRef CYGetGlobalObject(JSContextRef context);
59 extern "C" void CYSetupContext(JSGlobalContextRef context);
60 const char *CYExecute(apr_pool_t *pool, CYUTF8String code);
62 void CYSetArgs(int argc, const char *argv[]);
64 bool CYCastBool(JSContextRef context, JSValueRef value);
65 double CYCastDouble(JSContextRef context, JSValueRef value);
67 CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value);
68 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value);
70 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index);
71 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name);
73 void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value);
74 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone);
75 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone);
77 JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name);
79 JSValueRef CYCastJSValue(JSContextRef context, bool value);
80 JSValueRef CYCastJSValue(JSContextRef context, double value);
81 JSValueRef CYCastJSValue(JSContextRef context, int value);
82 JSValueRef CYCastJSValue(JSContextRef context, unsigned int value);
83 JSValueRef CYCastJSValue(JSContextRef context, long int value);
84 JSValueRef CYCastJSValue(JSContextRef context, long unsigned int value);
85 JSValueRef CYCastJSValue(JSContextRef context, long long int value);
86 JSValueRef CYCastJSValue(JSContextRef context, long long unsigned int value);
88 JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value);
89 JSValueRef CYCastJSValue(JSContextRef context, const char *value);
91 JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value);
92 JSValueRef CYJSUndefined(JSContextRef context);
93 JSValueRef CYJSNull(JSContextRef context);
95 void *CYCastPointer_(JSContextRef context, JSValueRef value);
97 template <typename Type_>
98 _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
99 return reinterpret_cast<Type_>(CYCastPointer_(context, value));
102 void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value);
103 JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL);
105 JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, void *setup[], size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)());
107 bool CYIsCallable(JSContextRef context, JSValueRef value);
108 JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
110 const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object);
113 void *(*ExecuteStart)(JSContextRef);
114 void (*ExecuteEnd)(JSContextRef, void *);
116 void (*CallFunction)(JSContextRef, ffi_cif *, void (*)(), uint8_t *, void **);
118 void (*Initialize)();
119 void (*SetupContext)(JSContextRef);
121 bool (*PoolFFI)(apr_pool_t *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef);
122 JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef);
125 extern struct CYHooks *hooks_;
127 JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner);
129 void CYFinalize(JSObjectRef object);
131 size_t CYArrayLength(JSContextRef context, JSObjectRef array);
132 JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index);
133 void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value);
135 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value);
137 JSStringRef CYCopyJSString(const char *value);
138 JSStringRef CYCopyJSString(JSStringRef value);
139 JSStringRef CYCopyJSString(CYUTF8String value);
140 JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value);
148 JSStringRelease(string_);
152 CYJSString(const CYJSString &rhs) :
153 string_(CYCopyJSString(rhs.string_))
157 template <typename Arg0_>
158 CYJSString(Arg0_ arg0) :
159 string_(CYCopyJSString(arg0))
163 template <typename Arg0_, typename Arg1_>
164 CYJSString(Arg0_ arg0, Arg1_ arg1) :
165 string_(CYCopyJSString(arg0, arg1))
169 CYJSString &operator =(const CYJSString &rhs) {
171 string_ = CYCopyJSString(rhs.string_);
184 operator JSStringRef() const {
189 #endif/*CYCRIPT_JAVASCRIPT_HPP*/