forked from rismay/cycript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Internal.hpp
205 lines (167 loc) · 5.04 KB
/
Internal.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Cycript - Optimizing JavaScript Compiler/Runtime
* Copyright (C) 2009-2013 Jay Freeman (saurik)
*/
/* GNU General Public License, Version 3 {{{ */
/*
* Cycript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Cycript is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cycript. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#ifndef CYCRIPT_INTERNAL_HPP
#define CYCRIPT_INTERNAL_HPP
#include <sig/parse.hpp>
#include <sig/ffi_type.hpp>
#include <JavaScriptCore/JSBase.h>
#include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSValueRef.h>
#include "Pooling.hpp"
JSGlobalContextRef CYGetJSContext(JSContextRef context);
void Structor_(CYPool &pool, sig::Type *&type);
JSObjectRef CYMakeType(JSContextRef context, const char *type);
JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
extern JSClassRef Functor_;
struct Type_privateData :
CYData
{
static JSClassRef Class_;
ffi_type *ffi_;
sig::Type *type_;
void Set(sig::Type *type) {
type_ = new(*pool_) sig::Type;
sig::Copy(*pool_, *type_, *type);
}
Type_privateData(CYPool &pool, const char *type) :
CYData(pool),
ffi_(NULL)
{
sig::Signature signature;
sig::Parse(*pool_, &signature, type, &Structor_);
type_ = signature.elements[0].type;
}
Type_privateData(const char *type) :
ffi_(NULL)
{
sig::Signature signature;
sig::Parse(*pool_, &signature, type, &Structor_);
type_ = signature.elements[0].type;
}
Type_privateData(sig::Type *type) :
ffi_(NULL)
{
if (type != NULL)
Set(type);
}
Type_privateData(sig::Type *type, ffi_type *ffi) {
ffi_ = new(*pool_) ffi_type;
sig::Copy(*pool_, *ffi_, *ffi);
Set(type);
}
ffi_type *GetFFI() {
if (ffi_ == NULL) {
ffi_ = new(*pool_) ffi_type;
sig::Element element;
element.name = NULL;
element.type = type_;
element.offset = 0;
sig::Signature signature;
signature.elements = &element;
signature.count = 1;
ffi_cif cif;
sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
*ffi_ = *cif.rtype;
}
return ffi_;
}
};
struct CYValue :
CYData
{
void *value_;
CYValue() {
}
CYValue(const void *value) :
value_(const_cast<void *>(value))
{
}
CYValue(const CYValue &rhs) :
value_(rhs.value_)
{
}
virtual Type_privateData *GetType() const {
return NULL;
}
};
struct CYOwned :
CYValue
{
private:
JSGlobalContextRef context_;
JSObjectRef owner_;
public:
CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
CYValue(value),
context_(CYGetJSContext(context)),
owner_(owner)
{
//XXX:JSGlobalContextRetain(context_);
if (owner_ != NULL)
JSValueProtect(context_, owner_);
}
virtual ~CYOwned() {
if (owner_ != NULL)
JSValueUnprotect(context_, owner_);
//XXX:JSGlobalContextRelease(context_);
}
JSObjectRef GetOwner() const {
return owner_;
}
};
namespace cy {
struct Functor :
CYValue
{
sig::Signature signature_;
ffi_cif cif_;
Functor(const char *type, void (*value)()) :
CYValue(reinterpret_cast<void *>(value))
{
sig::Parse(*pool_, &signature_, type, &Structor_);
sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
}
void (*GetValue() const)() {
return reinterpret_cast<void (*)()>(value_);
}
static JSStaticFunction const * const StaticFunctions;
}; }
struct Closure_privateData :
cy::Functor
{
JSGlobalContextRef context_;
JSObjectRef function_;
Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
cy::Functor(type, NULL),
context_(CYGetJSContext(context)),
function_(function)
{
//XXX:JSGlobalContextRetain(context_);
JSValueProtect(context_, function_);
}
virtual ~Closure_privateData() {
JSValueUnprotect(context_, function_);
//XXX:JSGlobalContextRelease(context_);
}
};
Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
#endif/*CYCRIPT_INTERNAL_HPP*/