1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. 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 sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
38 extern JSClassRef Functor_;
40 struct Type_privateData :
43 static JSClassRef Class_;
48 Type_privateData(const char *type) :
51 sig::Signature signature;
52 sig::Parse(*pool_, &signature, type, &Structor_);
53 type_ = signature.elements[0].type;
56 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
57 type_(type.Copy(*pool_))
63 ffi_ = new(*pool_) ffi_type;
64 sig::Copy(*pool_, *ffi_, *ffi);
75 sig::Signature signature;
76 signature.elements = &element;
80 sig::sig_ffi_cif(*pool_, &signature, &cif);
82 ffi_ = new(*pool_) ffi_type;
98 CYValue(const void *value) :
99 value_(const_cast<void *>(value))
103 CYValue(const CYValue &rhs) :
108 virtual Type_privateData *GetType() const {
117 JSGlobalContextRef context_;
121 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
123 context_(CYGetJSContext(context)),
126 //XXX:JSGlobalContextRetain(context_);
128 JSValueProtect(context_, owner_);
133 JSValueUnprotect(context_, owner_);
134 //XXX:JSGlobalContextRelease(context_);
137 JSObjectRef GetOwner() const {
148 sig::sig_ffi_cif(*pool_, &signature_, &cif_);
152 sig::Signature signature_;
155 Functor(const sig::Signature &signature, void (*value)()) :
156 CYValue(reinterpret_cast<void *>(value))
158 sig::Copy(*pool_, signature_, signature);
162 Functor(const char *encoding, void (*value)()) :
163 CYValue(reinterpret_cast<void *>(value))
165 sig::Parse(*pool_, &signature_, encoding, &Structor_);
169 void (*GetValue() const)() {
170 return reinterpret_cast<void (*)()>(value_);
173 static JSStaticFunction const * const StaticFunctions;
174 static JSStaticValue const * const StaticValues;
177 struct Closure_privateData :
180 JSGlobalContextRef context_;
181 JSObjectRef function_;
182 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
184 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
185 cy::Functor(signature, NULL),
186 context_(CYGetJSContext(context)),
190 //XXX:JSGlobalContextRetain(context_);
191 JSValueProtect(context_, function_);
194 virtual ~Closure_privateData() {
195 JSValueUnprotect(context_, function_);
196 //XXX:JSGlobalContextRelease(context_);
200 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
201 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
203 #endif/*CYCRIPT_INTERNAL_HPP*/