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);
88 ffi_ = new(*pool_) ffi_type;
95 sig::Signature signature;
96 signature.elements = &element;
100 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature, &cif);
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 {
164 sig::Signature signature_;
167 Functor(const char *type, void (*value)()) :
168 CYValue(reinterpret_cast<void *>(value))
170 sig::Parse(*pool_, &signature_, type, &Structor_);
171 sig::sig_ffi_cif(*pool_, &sig::ObjectiveC, &signature_, &cif_);
174 void (*GetValue() const)() {
175 return reinterpret_cast<void (*)()>(value_);
178 static JSStaticFunction const * const StaticFunctions;
181 struct Closure_privateData :
184 JSGlobalContextRef context_;
185 JSObjectRef function_;
187 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
188 cy::Functor(type, NULL),
189 context_(CYGetJSContext(context)),
192 //XXX:JSGlobalContextRetain(context_);
193 JSValueProtect(context_, function_);
196 virtual ~Closure_privateData() {
197 JSValueUnprotect(context_, function_);
198 //XXX:JSGlobalContextRelease(context_);
202 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
203 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
205 #endif/*CYCRIPT_INTERNAL_HPP*/