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 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(apr_pool_t *pool, const char *type) :
59 _assert(pool != NULL);
61 sig::Signature signature;
62 sig::Parse(pool_, &signature, type, &Structor_);
63 type_ = signature.elements[0].type;
66 Type_privateData(const char *type) :
69 sig::Signature signature;
70 sig::Parse(pool_, &signature, type, &Structor_);
71 type_ = signature.elements[0].type;
74 Type_privateData(sig::Type *type) :
81 Type_privateData(sig::Type *type, ffi_type *ffi) {
82 ffi_ = new(pool_) ffi_type;
83 sig::Copy(pool_, *ffi_, *ffi);
89 ffi_ = new(pool_) ffi_type;
96 sig::Signature signature;
97 signature.elements = &element;
101 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
117 CYValue(const void *value) :
118 value_(const_cast<void *>(value))
122 CYValue(const CYValue &rhs) :
127 virtual Type_privateData *GetType() const {
136 JSGlobalContextRef context_;
140 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
142 context_(CYGetJSContext(context)),
145 //XXX:JSGlobalContextRetain(context_);
147 JSValueProtect(context_, owner_);
152 JSValueUnprotect(context_, owner_);
153 //XXX:JSGlobalContextRelease(context_);
156 JSObjectRef GetOwner() const {
165 sig::Signature signature_;
168 Functor(const char *type, void (*value)()) :
169 CYValue(reinterpret_cast<void *>(value))
171 sig::Parse(pool_, &signature_, type, &Structor_);
172 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_);
175 void (*GetValue())() const {
176 return reinterpret_cast<void (*)()>(value_);
179 static JSStaticFunction const * const StaticFunctions;
182 struct Closure_privateData :
185 JSGlobalContextRef context_;
186 JSObjectRef function_;
188 Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) :
189 cy::Functor(type, NULL),
190 context_(CYGetJSContext(context)),
193 //XXX:JSGlobalContextRetain(context_);
194 JSValueProtect(context_, function_);
197 virtual ~Closure_privateData() {
198 JSValueUnprotect(context_, function_);
199 //XXX:JSGlobalContextRelease(context_);
203 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const char *type, void (*callback)(ffi_cif *, void *, void **, void *));
204 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
206 #endif/*CYCRIPT_INTERNAL_HPP*/