1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #ifndef CYCRIPT_INTERNAL_HPP
23 #define CYCRIPT_INTERNAL_HPP
25 #include <sig/parse.hpp>
26 #include <sig/ffi_type.hpp>
28 #include <JavaScriptCore/JSBase.h>
29 #include <JavaScriptCore/JSContextRef.h>
30 #include <JavaScriptCore/JSObjectRef.h>
31 #include <JavaScriptCore/JSValueRef.h>
33 #include "Pooling.hpp"
35 JSGlobalContextRef CYGetJSContext(JSContextRef context);
36 void Structor_(CYPool &pool, sig::Type *&type);
38 JSObjectRef CYMakeType(JSContextRef context, const char *type);
39 JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
41 extern JSClassRef Functor_;
43 struct Type_privateData :
46 static JSClassRef Class_;
51 void Set(sig::Type *type) {
52 type_ = new(*pool_) sig::Type;
53 sig::Copy(*pool_, *type_, *type);
56 Type_privateData(CYPool &pool, const char *type) :
60 sig::Signature signature;
61 sig::Parse(*pool_, &signature, type, &Structor_);
62 type_ = signature.elements[0].type;
65 Type_privateData(const char *type) :
68 sig::Signature signature;
69 sig::Parse(*pool_, &signature, type, &Structor_);
70 type_ = signature.elements[0].type;
73 Type_privateData(sig::Type *type) :
80 Type_privateData(sig::Type *type, ffi_type *ffi) {
81 ffi_ = new(*pool_) ffi_type;
82 sig::Copy(*pool_, *ffi_, *ffi);
93 sig::Signature signature;
94 signature.elements = &element;
98 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
100 ffi_ = new(*pool_) ffi_type;
116 CYValue(const void *value) :
117 value_(const_cast<void *>(value))
121 CYValue(const CYValue &rhs) :
126 virtual Type_privateData *GetType() const {
135 JSGlobalContextRef context_;
139 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
141 context_(CYGetJSContext(context)),
144 //XXX:JSGlobalContextRetain(context_);
146 JSValueProtect(context_, owner_);
151 JSValueUnprotect(context_, owner_);
152 //XXX:JSGlobalContextRelease(context_);
155 JSObjectRef GetOwner() const {
166 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
170 sig::Signature signature_;
173 Functor(const sig::Signature &signature, void (*value)()) :
174 CYValue(reinterpret_cast<void *>(value))
176 sig::Copy(*pool_, signature_, signature);
180 Functor(const char *encoding, void (*value)()) :
181 CYValue(reinterpret_cast<void *>(value))
183 sig::Parse(*pool_, &signature_, encoding, &Structor_);
187 void (*GetValue() const)() {
188 return reinterpret_cast<void (*)()>(value_);
191 static JSStaticFunction const * const StaticFunctions;
194 struct Closure_privateData :
197 JSGlobalContextRef context_;
198 JSObjectRef function_;
200 Closure_privateData(JSContextRef context, JSObjectRef function, const sig::Signature &signature) :
201 cy::Functor(signature, NULL),
202 context_(CYGetJSContext(context)),
205 //XXX:JSGlobalContextRetain(context_);
206 JSValueProtect(context_, function_);
209 virtual ~Closure_privateData() {
210 JSValueUnprotect(context_, function_);
211 //XXX:JSGlobalContextRelease(context_);
215 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, void (*callback)(ffi_cif *, void *, void **, void *));
216 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
218 #endif/*CYCRIPT_INTERNAL_HPP*/