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