]> git.saurik.com Git - cycript.git/blame - cycript.hpp
I don't understand what the hell is wrong with Apple's compiler, but it does not...
[cycript.git] / cycript.hpp
CommitLineData
e91fbe93 1/* Cycript - Inlining/Optimizing JavaScript Compiler
b4aa79af
JF
2 * Copyright (C) 2009 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/* }}} */
39
d35a3b07
JF
40#ifndef CYCRIPT_HPP
41#define CYCRIPT_HPP
057f943f 42
272a0dd4 43#ifdef __APPLE__
7c6c5b0a 44#include <JavaScriptCore/JavaScriptCore.h>
272a0dd4
JF
45#else
46#include <JavaScriptCore/JavaScript.h>
47#endif
057f943f 48
9185d5ef 49#include <apr_pools.h>
4afefdd9 50#include <ffi.h>
4afefdd9 51#include <sig/types.hpp>
520c130f 52#include <sstream>
057f943f 53
37954781
JF
54#include "String.hpp"
55
56#include <sqlite3.h>
57
994434e5
JF
58void CYInitialize();
59
967067aa
JF
60bool CYRecvAll_(int socket, uint8_t *data, size_t size);
61bool CYSendAll_(int socket, const uint8_t *data, size_t size);
62
856b8cd0 63void CYNumerify(std::ostringstream &str, double value);
520c130f 64void CYStringify(std::ostringstream &str, const char *data, size_t size);
b24eb750 65
1ef7d061
JF
66extern "C" void CYHandleClient(apr_pool_t *pool, int socket);
67
967067aa
JF
68template <typename Type_>
69bool CYRecvAll(int socket, Type_ *data, size_t size) {
70 return CYRecvAll_(socket, reinterpret_cast<uint8_t *>(data), size);
71}
72
73template <typename Type_>
74bool CYSendAll(int socket, const Type_ *data, size_t size) {
75 return CYSendAll_(socket, reinterpret_cast<const uint8_t *>(data), size);
76}
77
478d4ed0 78JSGlobalContextRef CYGetJSContext();
520c130f 79apr_pool_t *CYGetGlobalPool();
579ed526 80JSObjectRef CYGetGlobalObject(JSContextRef context);
498c3570 81
2385c806 82extern "C" void CYSetupContext(JSGlobalContextRef context);
967067aa 83const char *CYExecute(apr_pool_t *pool, const char *code);
4afefdd9
JF
84
85void CYSetArgs(int argc, const char *argv[]);
86
37954781
JF
87bool CYCastBool(JSContextRef context, JSValueRef value);
88double CYCastDouble(JSContextRef context, JSValueRef value);
aabea98c
JF
89
90CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value);
37954781
JF
91const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value);
92
93JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index);
94JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name);
2385c806 95
37954781
JF
96void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value);
97void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone);
2385c806 98void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone);
37954781 99
498c3570
JF
100JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name);
101
c074e774 102JSValueRef CYCastJSValue(JSContextRef context, bool value);
37954781
JF
103JSValueRef CYCastJSValue(JSContextRef context, double value);
104JSValueRef CYCastJSValue(JSContextRef context, int value);
105JSValueRef CYCastJSValue(JSContextRef context, unsigned int value);
106JSValueRef CYCastJSValue(JSContextRef context, long int value);
107JSValueRef CYCastJSValue(JSContextRef context, long unsigned int value);
108JSValueRef CYCastJSValue(JSContextRef context, long long int value);
109JSValueRef CYCastJSValue(JSContextRef context, long long unsigned int value);
110
111JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value);
112JSValueRef CYCastJSValue(JSContextRef context, const char *value);
113
114JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value);
115JSValueRef CYJSUndefined(JSContextRef context);
116JSValueRef CYJSNull(JSContextRef context);
117
118void *CYCastPointer_(JSContextRef context, JSValueRef value);
119
120template <typename Type_>
121_finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) {
122 return reinterpret_cast<Type_>(CYCastPointer_(context, value));
123}
124
125void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value);
126JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL);
127
128JSValueRef 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)());
129
130bool CYIsCallable(JSContextRef context, JSValueRef value);
b64ab4da 131JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
37954781
JF
132
133const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object);
134
135struct CYHooks {
d3760804
JF
136 void *(*ExecuteStart)(JSContextRef);
137 void (*ExecuteEnd)(JSContextRef, void *);
138
37954781 139 JSValueRef (*RuntimeProperty)(JSContextRef, CYUTF8String);
d3760804 140 void (*CallFunction)(JSContextRef, ffi_cif *, void (*)(), uint8_t *, void **);
3c9f7e22
JF
141
142 void (*Initialize)();
b5dd57dc 143 void (*SetupContext)(JSContextRef);
d3760804 144
37954781
JF
145 bool (*PoolFFI)(apr_pool_t *, JSContextRef, sig::Type *, ffi_type *, void *, JSValueRef);
146 JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef);
147};
148
149extern struct CYHooks *hooks_;
150
151char *sqlite3_column_pooled(apr_pool_t *pool, sqlite3_stmt *stmt, int n);
152
53ba31e9 153JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner);
37954781
JF
154
155void CYFinalize(JSObjectRef object);
156
d35a3b07 157#endif/*CYCRIPT_HPP*/