]>
Commit | Line | Data |
---|---|---|
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 |
58 | void CYInitialize(); |
59 | ||
967067aa JF |
60 | bool CYRecvAll_(int socket, uint8_t *data, size_t size); |
61 | bool CYSendAll_(int socket, const uint8_t *data, size_t size); | |
62 | ||
856b8cd0 | 63 | void CYNumerify(std::ostringstream &str, double value); |
520c130f | 64 | void CYStringify(std::ostringstream &str, const char *data, size_t size); |
b24eb750 | 65 | |
1ef7d061 JF |
66 | extern "C" void CYHandleClient(apr_pool_t *pool, int socket); |
67 | ||
967067aa JF |
68 | template <typename Type_> |
69 | bool CYRecvAll(int socket, Type_ *data, size_t size) { | |
70 | return CYRecvAll_(socket, reinterpret_cast<uint8_t *>(data), size); | |
71 | } | |
72 | ||
73 | template <typename Type_> | |
74 | bool CYSendAll(int socket, const Type_ *data, size_t size) { | |
75 | return CYSendAll_(socket, reinterpret_cast<const uint8_t *>(data), size); | |
76 | } | |
77 | ||
478d4ed0 | 78 | JSGlobalContextRef CYGetJSContext(); |
520c130f | 79 | apr_pool_t *CYGetGlobalPool(); |
579ed526 | 80 | JSObjectRef CYGetGlobalObject(JSContextRef context); |
498c3570 | 81 | |
2385c806 | 82 | extern "C" void CYSetupContext(JSGlobalContextRef context); |
967067aa | 83 | const char *CYExecute(apr_pool_t *pool, const char *code); |
4afefdd9 JF |
84 | |
85 | void CYSetArgs(int argc, const char *argv[]); | |
86 | ||
37954781 JF |
87 | bool CYCastBool(JSContextRef context, JSValueRef value); |
88 | double CYCastDouble(JSContextRef context, JSValueRef value); | |
aabea98c JF |
89 | |
90 | CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value); | |
37954781 JF |
91 | const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSStringRef value); |
92 | ||
93 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, size_t index); | |
94 | JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef name); | |
2385c806 | 95 | |
37954781 JF |
96 | void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value); |
97 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone); | |
2385c806 | 98 | void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef (*callback)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *), JSPropertyAttributes attributes = kJSPropertyAttributeNone); |
37954781 | 99 | |
498c3570 JF |
100 | JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name); |
101 | ||
c074e774 | 102 | JSValueRef CYCastJSValue(JSContextRef context, bool value); |
37954781 JF |
103 | JSValueRef CYCastJSValue(JSContextRef context, double value); |
104 | JSValueRef CYCastJSValue(JSContextRef context, int value); | |
105 | JSValueRef CYCastJSValue(JSContextRef context, unsigned int value); | |
106 | JSValueRef CYCastJSValue(JSContextRef context, long int value); | |
107 | JSValueRef CYCastJSValue(JSContextRef context, long unsigned int value); | |
108 | JSValueRef CYCastJSValue(JSContextRef context, long long int value); | |
109 | JSValueRef CYCastJSValue(JSContextRef context, long long unsigned int value); | |
110 | ||
111 | JSValueRef CYCastJSValue(JSContextRef context, JSStringRef value); | |
112 | JSValueRef CYCastJSValue(JSContextRef context, const char *value); | |
113 | ||
114 | JSObjectRef CYCastJSObject(JSContextRef context, JSValueRef value); | |
115 | JSValueRef CYJSUndefined(JSContextRef context); | |
116 | JSValueRef CYJSNull(JSContextRef context); | |
117 | ||
118 | void *CYCastPointer_(JSContextRef context, JSValueRef value); | |
119 | ||
120 | template <typename Type_> | |
121 | _finline Type_ CYCastPointer(JSContextRef context, JSValueRef value) { | |
122 | return reinterpret_cast<Type_>(CYCastPointer_(context, value)); | |
123 | } | |
124 | ||
125 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value); | |
126 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize = false, JSObjectRef owner = NULL); | |
127 | ||
128 | 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)()); | |
129 | ||
130 | bool CYIsCallable(JSContextRef context, JSValueRef value); | |
b64ab4da | 131 | JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]); |
37954781 JF |
132 | |
133 | const char *CYPoolCCYON(apr_pool_t *pool, JSContextRef context, JSObjectRef object); | |
134 | ||
135 | struct 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 | ||
149 | extern struct CYHooks *hooks_; | |
150 | ||
151 | char *sqlite3_column_pooled(apr_pool_t *pool, sqlite3_stmt *stmt, int n); | |
152 | ||
53ba31e9 | 153 | JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner); |
37954781 JF |
154 | |
155 | void CYFinalize(JSObjectRef object); | |
156 | ||
d35a3b07 | 157 | #endif/*CYCRIPT_HPP*/ |