]> git.saurik.com Git - cycript.git/blame - JavaScript.hpp
Only link libcycript against libffi (not cycript).
[cycript.git] / JavaScript.hpp
CommitLineData
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
ef6f48be
JF
37#include <apr_pools.h>
38
39#include <String.hpp>
40
498c3570
JF
41extern JSStringRef Array_s;
42extern JSStringRef cy_s;
00b9328f
JF
43extern JSStringRef length_s;
44extern JSStringRef message_s;
45extern JSStringRef name_s;
46extern JSStringRef pop_s;
47extern JSStringRef prototype_s;
48extern JSStringRef push_s;
49extern JSStringRef splice_s;
50extern JSStringRef toCYON_s;
51extern JSStringRef toJSON_s;
4cb8aa43
JF
52extern JSStringRef toPointer_s;
53extern JSStringRef toString_s;
37954781 54
9cad30fa
JF
55void CYInitializeDynamic();
56JSGlobalContextRef CYGetJSContext();
57JSObjectRef CYGetGlobalObject(JSContextRef context);
58
59extern "C" void CYSetupContext(JSGlobalContextRef context);
0ced2e47 60const char *CYExecute(apr_pool_t *pool, CYUTF8String code);
9cad30fa
JF
61
62void CYSetArgs(int argc, const char *argv[]);
63
64bool CYCastBool(JSContextRef context, JSValueRef value);
65double CYCastDouble(JSContextRef context, JSValueRef value);
66
67CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value);
68const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value);
69
70JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index);
71JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name);
72
73void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value);
74void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone);
75void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone);
76
77JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name);
78
79JSValueRef CYCastJSValue(JSContextRef context, bool value);
80JSValueRef CYCastJSValue(JSContextRef context, double value);
81JSValueRef CYCastJSValue(JSContextRef context, int value);
82JSValueRef CYCastJSValue(JSContextRef context, unsigned int value);
83JSValueRef CYCastJSValue(JSContextRef context, long int value);
84JSValueRef CYCastJSValue(JSContextRef context, long unsigned int value);
85JSValueRef CYCastJSValue(JSContextRef context, long long int value);
86JSValueRef CYCastJSValue(JSContextRef context, long long unsigned int value);
87
88JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value);
89JSValueRef CYCastJSValue(JSContextRef context, const char *value);
90
91JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value);
92JSValueRef CYJSUndefined(JSContextRef context);
93JSValueRef CYJSNull(JSContextRef context);
94
95void *CYCastPointer_(JSContextRef context, JSValueRef value);
96
97template <typename Type_>
98_finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
99 return reinterpret_cast<Type_>(CYCastPointer_(context, value));
100}
101
102void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value);
103JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL);
104
105JSValueRef 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)());
106
107bool CYIsCallable(JSContextRef context, JSValueRef value);
108JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
109
110const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object);
111
112struct CYHooks {
113 void *(*ExecuteStart)(JSContextRef);
114 void (*ExecuteEnd)(JSContextRef, void *);
115
9cad30fa
JF
116 void (*CallFunction)(JSContextRef, ffi_cif *, void (*)(), uint8_t *, void **);
117
118 void (*Initialize)();
119 void (*SetupContext)(JSContextRef);
120
121 bool (*PoolFFI)(apr_pool_t *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef);
122 JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef);
123};
124
125extern struct CYHooks *hooks_;
126
9cad30fa
JF
127JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner);
128
129void CYFinalize(JSObjectRef object);
130
07db5f4d
JF
131size_t CYArrayLength(JSContextRef context, JSObjectRef array);
132JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index);
133void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value);
134
9cad30fa
JF
135const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value);
136
137JSStringRef CYCopyJSString(const char *value);
138JSStringRef CYCopyJSString(JSStringRef value);
139JSStringRef CYCopyJSString(CYUTF8String value);
140JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value);
141
142class CYJSString {
143 private:
144 JSStringRef string_;
145
146 void Clear_() {
147 if (string_ != NULL)
148 JSStringRelease(string_);
149 }
150
151 public:
152 CYJSString(const CYJSString &rhs) :
153 string_(CYCopyJSString(rhs.string_))
154 {
155 }
156
157 template <typename Arg0_>
158 CYJSString(Arg0_ arg0) :
159 string_(CYCopyJSString(arg0))
160 {
161 }
162
163 template <typename Arg0_, typename Arg1_>
164 CYJSString(Arg0_ arg0, Arg1_ arg1) :
165 string_(CYCopyJSString(arg0, arg1))
166 {
167 }
168
169 CYJSString &operator =(const CYJSString &rhs) {
170 Clear_();
171 string_ = CYCopyJSString(rhs.string_);
172 return *this;
173 }
174
175 ~CYJSString() {
176 Clear_();
177 }
178
179 void Clear() {
180 Clear_();
181 string_ = NULL;
182 }
183
184 operator JSStringRef() const {
185 return string_;
186 }
187};
188
37954781 189#endif/*CYCRIPT_JAVASCRIPT_HPP*/