]> git.saurik.com Git - cycript.git/blame_incremental - Internal.hpp
Do not ever use NULL type_s, even for ? encoding.
[cycript.git] / Internal.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_INTERNAL_HPP
23#define CYCRIPT_INTERNAL_HPP
24
25#include <sig/parse.hpp>
26#include <sig/ffi_type.hpp>
27
28#include <JavaScriptCore/JSBase.h>
29#include <JavaScriptCore/JSContextRef.h>
30#include <JavaScriptCore/JSObjectRef.h>
31#include <JavaScriptCore/JSValueRef.h>
32
33#include "Pooling.hpp"
34
35JSGlobalContextRef CYGetJSContext(JSContextRef context);
36void Structor_(CYPool &pool, sig::Type *&type);
37
38JSObjectRef CYMakeType(JSContextRef context, const char *type);
39JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
40
41extern JSClassRef Functor_;
42
43struct Type_privateData :
44 CYData
45{
46 static JSClassRef Class_;
47
48 ffi_type *ffi_;
49 sig::Type *type_;
50
51 void Set(sig::Type *type) {
52 type_ = new(*pool_) sig::Type;
53 sig::Copy(*pool_, *type_, *type);
54 }
55
56 Type_privateData(CYPool &pool, const char *type) :
57 CYData(pool),
58 ffi_(NULL)
59 {
60 sig::Signature signature;
61 sig::Parse(*pool_, &signature, type, &Structor_);
62 type_ = signature.elements[0].type;
63 }
64
65 Type_privateData(const char *type) :
66 ffi_(NULL)
67 {
68 sig::Signature signature;
69 sig::Parse(*pool_, &signature, type, &Structor_);
70 type_ = signature.elements[0].type;
71 }
72
73 Type_privateData(sig::Type *type) :
74 ffi_(NULL)
75 {
76 // XXX: just in case I messed up migrating
77 _assert(type != NULL);
78 Set(type);
79 }
80
81 Type_privateData(sig::Type *type, ffi_type *ffi) {
82 ffi_ = new(*pool_) ffi_type;
83 sig::Copy(*pool_, *ffi_, *ffi);
84 Set(type);
85 }
86
87 ffi_type *GetFFI() {
88 if (ffi_ == NULL) {
89 sig::Element element;
90 element.name = NULL;
91 element.type = type_;
92 element.offset = 0;
93
94 sig::Signature signature;
95 signature.elements = &element;
96 signature.count = 1;
97
98 ffi_cif cif;
99 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
100
101 ffi_ = new(*pool_) ffi_type;
102 *ffi_ = *cif.rtype;
103 }
104
105 return ffi_;
106 }
107};
108
109struct CYValue :
110 CYData
111{
112 void *value_;
113
114 CYValue() {
115 }
116
117 CYValue(const void *value) :
118 value_(const_cast<void *>(value))
119 {
120 }
121
122 CYValue(const CYValue &rhs) :
123 value_(rhs.value_)
124 {
125 }
126
127 virtual Type_privateData *GetType() const {
128 return NULL;
129 }
130};
131
132struct CYOwned :
133 CYValue
134{
135 private:
136 JSGlobalContextRef context_;
137 JSObjectRef owner_;
138
139 public:
140 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
141 CYValue(value),
142 context_(CYGetJSContext(context)),
143 owner_(owner)
144 {
145 //XXX:JSGlobalContextRetain(context_);
146 if (owner_ != NULL)
147 JSValueProtect(context_, owner_);
148 }
149
150 virtual ~CYOwned() {
151 if (owner_ != NULL)
152 JSValueUnprotect(context_, owner_);
153 //XXX:JSGlobalContextRelease(context_);
154 }
155
156 JSObjectRef GetOwner() const {
157 return owner_;
158 }
159};
160
161namespace cy {
162struct Functor :
163 CYValue
164{
165 private:
166 void set() {
167 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
168 }
169
170 public:
171 sig::Signature signature_;
172 ffi_cif cif_;
173
174 Functor(const sig::Signature &signature, void (*value)()) :
175 CYValue(reinterpret_cast<void *>(value))
176 {
177 sig::Copy(*pool_, signature_, signature);
178 set();
179 }
180
181 Functor(const char *encoding, void (*value)()) :
182 CYValue(reinterpret_cast<void *>(value))
183 {
184 sig::Parse(*pool_, &signature_, encoding, &Structor_);
185 set();
186 }
187
188 void (*GetValue() const)() {
189 return reinterpret_cast<void (*)()>(value_);
190 }
191
192 static JSStaticFunction const * const StaticFunctions;
193}; }
194
195struct Closure_privateData :
196 cy::Functor
197{
198 JSGlobalContextRef context_;
199 JSObjectRef function_;
200
201 Closure_privateData(JSContextRef context, JSObjectRef function, const sig::Signature &signature) :
202 cy::Functor(signature, NULL),
203 context_(CYGetJSContext(context)),
204 function_(function)
205 {
206 //XXX:JSGlobalContextRetain(context_);
207 JSValueProtect(context_, function_);
208 }
209
210 virtual ~Closure_privateData() {
211 JSValueUnprotect(context_, function_);
212 //XXX:JSGlobalContextRelease(context_);
213 }
214};
215
216Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, void (*callback)(ffi_cif *, void *, void **, void *));
217void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
218
219#endif/*CYCRIPT_INTERNAL_HPP*/