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