]> git.saurik.com Git - cycript.git/blame - Internal.hpp
Objective-C++ does not have a normal -Wall macro.
[cycript.git] / Internal.hpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c15969fd 2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
d15b59f5
JF
3*/
4
c15969fd 5/* GNU General Public License, Version 3 {{{ */
d15b59f5 6/*
c15969fd
JF
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.
d15b59f5 11 *
c15969fd
JF
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.
d15b59f5 16 *
c15969fd 17 * You should have received a copy of the GNU General Public License
b3378a02
JF
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
d15b59f5
JF
20/* }}} */
21
3c1c3635
JF
22#ifndef CYCRIPT_INTERNAL_HPP
23#define CYCRIPT_INTERNAL_HPP
24
b799113b
JF
25#include <sig/parse.hpp>
26#include <sig/ffi_type.hpp>
3c1c3635
JF
27
28#include <JavaScriptCore/JSBase.h>
bc60fb46 29#include <JavaScriptCore/JSContextRef.h>
37954781 30#include <JavaScriptCore/JSObjectRef.h>
3c1c3635
JF
31#include <JavaScriptCore/JSValueRef.h>
32
b799113b 33#include "Pooling.hpp"
3c1c3635 34
bc60fb46 35JSGlobalContextRef CYGetJSContext(JSContextRef context);
b799113b 36void Structor_(CYPool &pool, sig::Type *&type);
3c1c3635 37
8a23fb2e
JF
38JSObjectRef CYMakeType(JSContextRef context, const char *type);
39JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
40
3f9ae37c
JF
41extern JSClassRef Functor_;
42
3c1c3635
JF
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) {
b799113b
JF
52 type_ = new(*pool_) sig::Type;
53 sig::Copy(*pool_, *type_, *type);
3c1c3635
JF
54 }
55
b799113b
JF
56 Type_privateData(CYPool &pool, const char *type) :
57 CYData(pool),
3c1c3635
JF
58 ffi_(NULL)
59 {
807a88be 60 sig::Signature signature;
b799113b 61 sig::Parse(*pool_, &signature, type, &Structor_);
807a88be
JF
62 type_ = signature.elements[0].type;
63 }
3c1c3635 64
807a88be
JF
65 Type_privateData(const char *type) :
66 ffi_(NULL)
67 {
3c1c3635 68 sig::Signature signature;
b799113b 69 sig::Parse(*pool_, &signature, type, &Structor_);
3c1c3635
JF
70 type_ = signature.elements[0].type;
71 }
72
73 Type_privateData(sig::Type *type) :
74 ffi_(NULL)
75 {
76 if (type != NULL)
77 Set(type);
78 }
79
80 Type_privateData(sig::Type *type, ffi_type *ffi) {
b799113b
JF
81 ffi_ = new(*pool_) ffi_type;
82 sig::Copy(*pool_, *ffi_, *ffi);
3c1c3635
JF
83 Set(type);
84 }
85
86 ffi_type *GetFFI() {
87 if (ffi_ == NULL) {
b799113b 88 ffi_ = new(*pool_) ffi_type;
3c1c3635
JF
89
90 sig::Element element;
91 element.name = NULL;
92 element.type = type_;
93 element.offset = 0;
94
95 sig::Signature signature;
96 signature.elements = &element;
97 signature.count = 1;
98
99 ffi_cif cif;
b799113b 100 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
3c1c3635
JF
101 *ffi_ = *cif.rtype;
102 }
103
104 return ffi_;
105 }
106};
107
108struct CYValue :
109 CYData
110{
111 void *value_;
112
113 CYValue() {
114 }
115
116 CYValue(const void *value) :
117 value_(const_cast<void *>(value))
118 {
119 }
120
121 CYValue(const CYValue &rhs) :
122 value_(rhs.value_)
123 {
124 }
125
126 virtual Type_privateData *GetType() const {
127 return NULL;
128 }
129};
130
131struct CYOwned :
132 CYValue
133{
134 private:
bc60fb46 135 JSGlobalContextRef context_;
3c1c3635
JF
136 JSObjectRef owner_;
137
138 public:
139 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
140 CYValue(value),
bc60fb46 141 context_(CYGetJSContext(context)),
3c1c3635
JF
142 owner_(owner)
143 {
bc60fb46 144 //XXX:JSGlobalContextRetain(context_);
3c1c3635
JF
145 if (owner_ != NULL)
146 JSValueProtect(context_, owner_);
147 }
148
149 virtual ~CYOwned() {
150 if (owner_ != NULL)
151 JSValueUnprotect(context_, owner_);
bc60fb46 152 //XXX:JSGlobalContextRelease(context_);
3c1c3635
JF
153 }
154
155 JSObjectRef GetOwner() const {
156 return owner_;
157 }
158};
159
37954781
JF
160namespace cy {
161struct Functor :
162 CYValue
163{
164 sig::Signature signature_;
165 ffi_cif cif_;
166
167 Functor(const char *type, void (*value)()) :
168 CYValue(reinterpret_cast<void *>(value))
169 {
b799113b
JF
170 sig::Parse(*pool_, &signature_, type, &Structor_);
171 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
37954781
JF
172 }
173
01d0f64d 174 void (*GetValue() const)() {
37954781
JF
175 return reinterpret_cast<void (*)()>(value_);
176 }
177
178 static JSStaticFunction const * const StaticFunctions;
179}; }
180
181struct Closure_privateData :
182 cy::Functor
183{
bc60fb46 184 JSGlobalContextRef context_;
37954781
JF
185 JSObjectRef function_;
186
187 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
188 cy::Functor(type, NULL),
bc60fb46 189 context_(CYGetJSContext(context)),
37954781
JF
190 function_(function)
191 {
bc60fb46 192 //XXX:JSGlobalContextRetain(context_);
37954781
JF
193 JSValueProtect(context_, function_);
194 }
195
196 virtual ~Closure_privateData() {
197 JSValueUnprotect(context_, function_);
bc60fb46 198 //XXX:JSGlobalContextRelease(context_);
37954781
JF
199 }
200};
201
202Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
9ec7dd18 203void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
37954781 204
3c1c3635 205#endif/*CYCRIPT_INTERNAL_HPP*/