1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #ifndef CYCRIPT_INTERNAL_HPP
23 #define CYCRIPT_INTERNAL_HPP
25 #include "Pooling.hpp"
27 #include <JavaScriptCore/JSBase.h>
28 #include <JavaScriptCore/JSContextRef.h>
29 #include <JavaScriptCore/JSObjectRef.h>
30 #include <JavaScriptCore/JSValueRef.h>
32 #include <sig/parse.hpp>
33 #include <sig/ffi_type.hpp>
35 JSGlobalContextRef CYGetJSContext(JSContextRef context);
36 void Structor_(apr_pool_t *pool, sig::Type *&type);
38 struct Type_privateData :
41 static JSClassRef Class_;
46 void Set(sig::Type *type) {
47 type_ = new(pool_) sig::Type;
48 sig::Copy(pool_, *type_, *type);
51 Type_privateData(apr_pool_t *pool, const char *type) :
54 _assert(pool != NULL);
56 sig::Signature signature;
57 sig::Parse(pool_, &signature, type, &Structor_);
58 type_ = signature.elements[0].type;
61 Type_privateData(const char *type) :
64 sig::Signature signature;
65 sig::Parse(pool_, &signature, type, &Structor_);
66 type_ = signature.elements[0].type;
69 Type_privateData(sig::Type *type) :
76 Type_privateData(sig::Type *type, ffi_type *ffi) {
77 ffi_ = new(pool_) ffi_type;
78 sig::Copy(pool_, *ffi_, *ffi);
84 ffi_ = new(pool_) ffi_type;
91 sig::Signature signature;
92 signature.elements = &element;
96 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
112 CYValue(const void *value) :
113 value_(const_cast<void *>(value))
117 CYValue(const CYValue &rhs) :
122 virtual Type_privateData *GetType() const {
131 JSGlobalContextRef context_;
135 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
137 context_(CYGetJSContext(context)),
140 //XXX:JSGlobalContextRetain(context_);
142 JSValueProtect(context_, owner_);
147 JSValueUnprotect(context_, owner_);
148 //XXX:JSGlobalContextRelease(context_);
151 JSObjectRef GetOwner() const {
160 sig::Signature signature_;
163 Functor(const char *type, void (*value)()) :
164 CYValue(reinterpret_cast<void *>(value))
166 sig::Parse(pool_, &signature_, type, &Structor_);
167 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
170 void (*GetValue())() const {
171 return reinterpret_cast<void (*)()>(value_);
174 static JSStaticFunction const * const StaticFunctions;
177 struct Closure_privateData :
180 JSGlobalContextRef context_;
181 JSObjectRef function_;
183 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
184 cy::Functor(type, NULL),
185 context_(CYGetJSContext(context)),
188 //XXX:JSGlobalContextRetain(context_);
189 JSValueProtect(context_, function_);
192 virtual ~Closure_privateData() {
193 JSValueUnprotect(context_, function_);
194 //XXX:JSGlobalContextRelease(context_);
198 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
200 #endif/*CYCRIPT_INTERNAL_HPP*/