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