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 "JavaScript.hpp"
34 #include "Pooling.hpp"
35 #include "Utility.hpp"
37 JSGlobalContextRef CYGetJSContext(JSContextRef context);
38 sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
40 extern JSClassRef Functor_;
42 struct Type_privateData :
45 static JSClassRef Class_;
50 Type_privateData(const char *type) :
53 sig::Signature signature;
54 sig::Parse(*pool_, &signature, type, &Structor_);
55 type_ = signature.elements[0].type;
58 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
59 type_(type.Copy(*pool_))
65 ffi_ = new(*pool_) ffi_type;
66 sig::Copy(*pool_, *ffi_, *ffi);
77 sig::Signature signature;
78 signature.elements = &element;
82 sig::sig_ffi_cif(*pool_, false, signature, &cif);
84 ffi_ = new(*pool_) ffi_type;
100 CYValue(const void *value) :
101 value_(const_cast<void *>(value))
105 CYValue(const CYValue &rhs) :
111 template <typename Internal_, typename Value_>
115 static JSClassRef Class_;
117 using CYValue::CYValue;
119 _finline Value_ GetValue() const {
120 return reinterpret_cast<Value_>(value_);
123 _finline JSValueRef GetPrototype(JSContextRef context) const {
127 template <typename... Args_>
128 _finline static JSClassRef GetClass(Args_ &&... args) {
132 template <typename... Args_>
133 static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
134 Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
135 JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal));
136 if (JSValueRef prototype = internal->GetPrototype(context))
137 CYSetPrototype(context, object, prototype);
142 template <typename Internal_, typename Value_>
143 JSClassRef CYValue_<Internal_, Value_>::Class_;
147 JSGlobalContextRef context_;
151 CYProtect(JSContextRef context, JSObjectRef object) :
152 context_(CYGetJSContext(context)),
155 //XXX:JSGlobalContextRetain(context_);
157 JSValueProtect(context_, object_);
162 JSValueUnprotect(context_, object_);
163 //XXX:JSGlobalContextRelease(context_);
166 operator bool() const {
167 return object_ != NULL;
170 operator JSContextRef() const {
174 operator JSObjectRef() const {
185 sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_);
190 sig::Signature signature_;
193 Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
194 CYValue(reinterpret_cast<void *>(value)),
197 sig::Copy(*pool_, signature_, signature);
201 Functor(void (*value)(), const char *encoding) :
202 CYValue(reinterpret_cast<void *>(value)),
205 sig::Parse(*pool_, &signature_, encoding, &Structor_);
209 void (*GetValue() const)() {
210 return reinterpret_cast<void (*)()>(value_);
213 static JSStaticFunction const * const StaticFunctions;
214 static JSStaticValue const * const StaticValues;
217 struct Closure_privateData :
221 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
223 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
224 cy::Functor(NULL, false, signature),
225 function_(context, function),
231 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
232 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
234 #endif/*CYCRIPT_INTERNAL_HPP*/