]> git.saurik.com Git - cycript.git/blame - Internal.hpp
Got exceptions bridged, back and forth, with Java.
[cycript.git] / Internal.hpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
d15b59f5
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
d15b59f5 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b3378a02 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
d6848e73 33#include "JavaScript.hpp"
b799113b 34#include "Pooling.hpp"
d6848e73 35#include "Utility.hpp"
3c1c3635 36
bc60fb46 37JSGlobalContextRef CYGetJSContext(JSContextRef context);
0559abf8 38sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
8a23fb2e 39
3f9ae37c
JF
40extern JSClassRef Functor_;
41
dbf05bfd
JF
42template <typename Internal_>
43struct CYPrivate :
3c1c3635
JF
44 CYData
45{
46 static JSClassRef Class_;
47
dbf05bfd
JF
48 _finline JSValueRef GetPrototype(JSContextRef context) const {
49 return NULL;
50 }
51
52 template <typename... Args_>
53 _finline static JSClassRef GetClass(Args_ &&... args) {
54 return Class_;
55 }
56
57 template <typename... Args_>
58 static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
59 Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
60 JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal));
61 if (JSValueRef prototype = internal->GetPrototype(context))
62 CYSetPrototype(context, object, prototype);
63 return object;
64 }
65};
66
67struct Type_privateData :
68 CYPrivate<Type_privateData>
69{
3c1c3635
JF
70 ffi_type *ffi_;
71 sig::Type *type_;
72
807a88be
JF
73 Type_privateData(const char *type) :
74 ffi_(NULL)
75 {
3c1c3635 76 sig::Signature signature;
b799113b 77 sig::Parse(*pool_, &signature, type, &Structor_);
3c1c3635
JF
78 type_ = signature.elements[0].type;
79 }
80
0559abf8
JF
81 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
82 type_(type.Copy(*pool_))
baea6375 83 {
baea6375 84
0559abf8
JF
85 if (ffi == NULL)
86 ffi_ = NULL;
87 else {
88 ffi_ = new(*pool_) ffi_type;
89 sig::Copy(*pool_, *ffi_, *ffi);
90 }
3c1c3635
JF
91 }
92
93 ffi_type *GetFFI() {
94 if (ffi_ == NULL) {
3c1c3635
JF
95 sig::Element element;
96 element.name = NULL;
97 element.type = type_;
98 element.offset = 0;
99
100 sig::Signature signature;
101 signature.elements = &element;
102 signature.count = 1;
103
104 ffi_cif cif;
574d4720 105 sig::sig_ffi_cif(*pool_, false, signature, &cif);
446d46d2
JF
106
107 ffi_ = new(*pool_) ffi_type;
3c1c3635
JF
108 *ffi_ = *cif.rtype;
109 }
110
111 return ffi_;
112 }
113};
114
dbf05bfd 115template <typename Internal_, typename Value_>
3c1c3635 116struct CYValue :
dbf05bfd 117 CYPrivate<Internal_>
3c1c3635 118{
dbf05bfd 119 Value_ value_;
3c1c3635
JF
120
121 CYValue() {
122 }
123
dbf05bfd
JF
124 CYValue(const Value_ &value) :
125 value_(value)
3c1c3635
JF
126 {
127 }
128
129 CYValue(const CYValue &rhs) :
130 value_(rhs.value_)
131 {
132 }
3c1c3635
JF
133};
134
dbf05bfd
JF
135template <typename Internal_>
136JSClassRef CYPrivate<Internal_>::Class_;
d6848e73 137
d6848e73 138struct CYProtect {
3c1c3635 139 private:
bc60fb46 140 JSGlobalContextRef context_;
d6848e73 141 JSObjectRef object_;
3c1c3635
JF
142
143 public:
d6848e73 144 CYProtect(JSContextRef context, JSObjectRef object) :
bc60fb46 145 context_(CYGetJSContext(context)),
d6848e73 146 object_(object)
3c1c3635 147 {
bc60fb46 148 //XXX:JSGlobalContextRetain(context_);
d6848e73
JF
149 if (object_ != NULL)
150 JSValueProtect(context_, object_);
3c1c3635
JF
151 }
152
d6848e73
JF
153 ~CYProtect() {
154 if (object_ != NULL)
155 JSValueUnprotect(context_, object_);
bc60fb46 156 //XXX:JSGlobalContextRelease(context_);
3c1c3635
JF
157 }
158
5f7a1d38
JF
159 operator bool() const {
160 return object_ != NULL;
161 }
162
163 operator JSContextRef() const {
164 return context_;
165 }
166
d6848e73
JF
167 operator JSObjectRef() const {
168 return object_;
3c1c3635
JF
169 }
170};
171
37954781
JF
172namespace cy {
173struct Functor :
dbf05bfd 174 CYValue<Functor, void (*)()>
37954781 175{
077756a4
JF
176 private:
177 void set() {
574d4720 178 sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_);
077756a4
JF
179 }
180
181 public:
574d4720 182 bool variadic_;
37954781
JF
183 sig::Signature signature_;
184 ffi_cif cif_;
185
574d4720 186 Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
dbf05bfd 187 CYValue(value),
574d4720 188 variadic_(variadic)
37954781 189 {
077756a4
JF
190 sig::Copy(*pool_, signature_, signature);
191 set();
192 }
193
574d4720 194 Functor(void (*value)(), const char *encoding) :
dbf05bfd 195 CYValue(value),
574d4720 196 variadic_(false)
077756a4
JF
197 {
198 sig::Parse(*pool_, &signature_, encoding, &Structor_);
199 set();
37954781
JF
200 }
201
37954781 202 static JSStaticFunction const * const StaticFunctions;
8493347d 203 static JSStaticValue const * const StaticValues;
37954781
JF
204}; }
205
206struct Closure_privateData :
207 cy::Functor
208{
5f7a1d38 209 CYProtect function_;
24e7b1a6 210 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
37954781 211
24e7b1a6 212 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
574d4720 213 cy::Functor(NULL, false, signature),
5f7a1d38 214 function_(context, function),
24e7b1a6 215 adapter_(adapter)
37954781 216 {
37954781
JF
217 }
218};
219
24e7b1a6 220Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
9ec7dd18 221void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
37954781 222
3c1c3635 223#endif/*CYCRIPT_INTERNAL_HPP*/