1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 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 JSObjectRef CYMakeType(JSContextRef context, const char *type);
39 JSObjectRef CYMakeType(JSContextRef context, sig::Type *type);
41 struct Type_privateData :
44 static JSClassRef Class_;
49 void Set(sig::Type *type) {
50 type_ = new(pool_) sig::Type;
51 sig::Copy(pool_, *type_, *type);
54 Type_privateData(apr_pool_t *pool, const char *type) :
57 _assert(pool != NULL);
59 sig::Signature signature;
60 sig::Parse(pool_, &signature, type, &Structor_);
61 type_ = signature.elements[0].type;
64 Type_privateData(const char *type) :
67 sig::Signature signature;
68 sig::Parse(pool_, &signature, type, &Structor_);
69 type_ = signature.elements[0].type;
72 Type_privateData(sig::Type *type) :
79 Type_privateData(sig::Type *type, ffi_type *ffi) {
80 ffi_ = new(pool_) ffi_type;
81 sig::Copy(pool_, *ffi_, *ffi);
87 ffi_ = new(pool_) ffi_type;
94 sig::Signature signature;
95 signature.elements = &element;
99 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
115 CYValue(const void *value) :
116 value_(const_cast<void *>(value))
120 CYValue(const CYValue &rhs) :
125 virtual Type_privateData *GetType() const {
134 JSGlobalContextRef context_;
138 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
140 context_(CYGetJSContext(context)),
143 //XXX:JSGlobalContextRetain(context_);
145 JSValueProtect(context_, owner_);
150 JSValueUnprotect(context_, owner_);
151 //XXX:JSGlobalContextRelease(context_);
154 JSObjectRef GetOwner() const {
163 sig::Signature signature_;
166 Functor(const char *type, void (*value)()) :
167 CYValue(reinterpret_cast<void *>(value))
169 sig::Parse(pool_, &signature_, type, &Structor_);
170 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
173 void (*GetValue())() const {
174 return reinterpret_cast<void (*)()>(value_);
177 static JSStaticFunction const * const StaticFunctions;
180 struct Closure_privateData :
183 JSGlobalContextRef context_;
184 JSObjectRef function_;
186 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
187 cy::Functor(type, NULL),
188 context_(CYGetJSContext(context)),
191 //XXX:JSGlobalContextRetain(context_);
192 JSValueProtect(context_, function_);
195 virtual ~Closure_privateData() {
196 JSValueUnprotect(context_, function_);
197 //XXX:JSGlobalContextRelease(context_);
201 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
203 #endif/*CYCRIPT_INTERNAL_HPP*/