]> git.saurik.com Git - cycript.git/blob - Internal.hpp
8b77fd5284edb29f961d09d5218cd55eb5f6dd27
[cycript.git] / Internal.hpp
1 #ifndef CYCRIPT_INTERNAL_HPP
2 #define CYCRIPT_INTERNAL_HPP
3
4 #include "Pooling.hpp"
5
6 #include <JavaScriptCore/JSBase.h>
7 #include <JavaScriptCore/JSObjectRef.h>
8 #include <JavaScriptCore/JSValueRef.h>
9
10 #include <sig/parse.hpp>
11 #include <sig/ffi_type.hpp>
12
13 void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type);
14
15 struct Type_privateData :
16 CYData
17 {
18 static JSClassRef Class_;
19
20 ffi_type *ffi_;
21 sig::Type *type_;
22
23 void Set(sig::Type *type) {
24 type_ = new(pool_) sig::Type;
25 sig::Copy(pool_, *type_, *type);
26 }
27
28 Type_privateData(apr_pool_t *pool, const char *type) :
29 ffi_(NULL)
30 {
31 if (pool != NULL)
32 pool_ = pool;
33
34 sig::Signature signature;
35 sig::Parse(pool_, &signature, type, &Structor_);
36 type_ = signature.elements[0].type;
37 }
38
39 Type_privateData(sig::Type *type) :
40 ffi_(NULL)
41 {
42 if (type != NULL)
43 Set(type);
44 }
45
46 Type_privateData(sig::Type *type, ffi_type *ffi) {
47 ffi_ = new(pool_) ffi_type;
48 sig::Copy(pool_, *ffi_, *ffi);
49 Set(type);
50 }
51
52 ffi_type *GetFFI() {
53 if (ffi_ == NULL) {
54 ffi_ = new(pool_) ffi_type;
55
56 sig::Element element;
57 element.name = NULL;
58 element.type = type_;
59 element.offset = 0;
60
61 sig::Signature signature;
62 signature.elements = &element;
63 signature.count = 1;
64
65 ffi_cif cif;
66 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
67 *ffi_ = *cif.rtype;
68 }
69
70 return ffi_;
71 }
72 };
73
74 struct CYValue :
75 CYData
76 {
77 void *value_;
78
79 CYValue() {
80 }
81
82 CYValue(const void *value) :
83 value_(const_cast<void *>(value))
84 {
85 }
86
87 CYValue(const CYValue &rhs) :
88 value_(rhs.value_)
89 {
90 }
91
92 virtual Type_privateData *GetType() const {
93 return NULL;
94 }
95 };
96
97 struct CYOwned :
98 CYValue
99 {
100 private:
101 JSContextRef context_;
102 JSObjectRef owner_;
103
104 public:
105 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
106 CYValue(value),
107 context_(context),
108 owner_(owner)
109 {
110 if (owner_ != NULL)
111 JSValueProtect(context_, owner_);
112 }
113
114 virtual ~CYOwned() {
115 if (owner_ != NULL)
116 JSValueUnprotect(context_, owner_);
117 }
118
119 JSObjectRef GetOwner() const {
120 return owner_;
121 }
122 };
123
124 namespace cy {
125 struct Functor :
126 CYValue
127 {
128 sig::Signature signature_;
129 ffi_cif cif_;
130
131 Functor(const char *type, void (*value)()) :
132 CYValue(reinterpret_cast<void *>(value))
133 {
134 sig::Parse(pool_, &signature_, type, &Structor_);
135 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
136 }
137
138 void (*GetValue())() const {
139 return reinterpret_cast<void (*)()>(value_);
140 }
141
142 static JSStaticFunction const * const StaticFunctions;
143 }; }
144
145 struct Closure_privateData :
146 cy::Functor
147 {
148 JSContextRef context_;
149 JSObjectRef function_;
150
151 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
152 cy::Functor(type, NULL),
153 context_(context),
154 function_(function)
155 {
156 JSValueProtect(context_, function_);
157 }
158
159 virtual ~Closure_privateData() {
160 JSValueUnprotect(context_, function_);
161 }
162 };
163
164 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
165
166 #endif/*CYCRIPT_INTERNAL_HPP*/