1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
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.
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.
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/>.
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 extern JSStringRef Array_s;
38 extern JSStringRef cy_s;
39 extern JSStringRef length_s;
40 extern JSStringRef message_s;
41 extern JSStringRef name_s;
42 extern JSStringRef pop_s;
43 extern JSStringRef prototype_s;
44 extern JSStringRef push_s;
45 extern JSStringRef splice_s;
46 extern JSStringRef toCYON_s;
47 extern JSStringRef toJSON_s;
48 extern JSStringRef toPointer_s;
49 extern JSStringRef toString_s;
51 void CYInitializeDynamic();
52 JSGlobalContextRef CYGetJSContext();
53 JSObjectRef CYGetGlobalObject(JSContextRef context);
55 extern "C" void CYSetupContext(JSGlobalContextRef context);
56 const char *CYExecute(apr_pool_t *pool, CYUTF8String code);
58 void CYSetArgs(int argc, const char *argv[]);
60 bool CYCastBool(JSContextRef context, JSValueRef value);
61 double CYCastDouble(JSContextRef context, JSValueRef value);
63 CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value);
64 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value);
66 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index);
67 JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name);
69 void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value);
70 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone);
71 void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone);
73 JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name);
75 JSValueRef CYCastJSValue(JSContextRef context, bool value);
76 JSValueRef CYCastJSValue(JSContextRef context, double value);
77 JSValueRef CYCastJSValue(JSContextRef context, int value);
78 JSValueRef CYCastJSValue(JSContextRef context, unsigned int value);
79 JSValueRef CYCastJSValue(JSContextRef context, long int value);
80 JSValueRef CYCastJSValue(JSContextRef context, long unsigned int value);
81 JSValueRef CYCastJSValue(JSContextRef context, long long int value);
82 JSValueRef CYCastJSValue(JSContextRef context, long long unsigned int value);
84 JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value);
85 JSValueRef CYCastJSValue(JSContextRef context, const char *value);
87 JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value);
88 JSValueRef CYJSUndefined(JSContextRef context);
89 JSValueRef CYJSNull(JSContextRef context);
91 void *CYCastPointer_(JSContextRef context, JSValueRef value);
93 template <typename Type_>
94 _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
95 return reinterpret_cast<Type_>(CYCastPointer_(context, value));
98 void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value);
99 JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL);
101 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)());
103 bool CYIsCallable(JSContextRef context, JSValueRef value);
104 JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
106 const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object);
109 void *(*ExecuteStart)(JSContextRef);
110 void (*ExecuteEnd)(JSContextRef, void *);
112 void (*CallFunction)(JSContextRef, ffi_cif *, void (*)(), uint8_t *, void **);
114 void (*Initialize)();
115 void (*SetupContext)(JSContextRef);
117 bool (*PoolFFI)(apr_pool_t *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef);
118 JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef);
121 extern struct CYHooks *hooks_;
123 JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner);
125 void CYFinalize(JSObjectRef object);
127 size_t CYArrayLength(JSContextRef context, JSObjectRef array);
128 JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index);
129 void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value);
131 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value);
133 JSStringRef CYCopyJSString(const char *value);
134 JSStringRef CYCopyJSString(JSStringRef value);
135 JSStringRef CYCopyJSString(CYUTF8String value);
136 JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value);
144 JSStringRelease(string_);
148 CYJSString(const CYJSString &rhs) :
149 string_(CYCopyJSString(rhs.string_))
153 template <typename Arg0_>
154 CYJSString(Arg0_ arg0) :
155 string_(CYCopyJSString(arg0))
159 template <typename Arg0_, typename Arg1_>
160 CYJSString(Arg0_ arg0, Arg1_ arg1) :
161 string_(CYCopyJSString(arg0, arg1))
165 CYJSString &operator =(const CYJSString &rhs) {
167 string_ = CYCopyJSString(rhs.string_);
180 operator JSStringRef() const {
185 #endif/*CYCRIPT_JAVASCRIPT_HPP*/