1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 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 struct CYPropertyName;
39 JSGlobalContextRef CYGetJSContext(JSContextRef context);
40 sig::Type *Structor_(CYPool &pool, sig::Aggregate *aggregate);
45 // XXX: without this, CYData is zero-initialized?!
51 _finline JSValueRef GetPrototype(JSContextRef context) const {
56 template <typename Internal_, typename Base_ = CYRoot>
60 static JSClassRef Class_;
62 template <typename... Args_>
63 _finline static JSClassRef GetClass(Args_ &&... args) {
67 template <typename... Args_>
68 static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
69 Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
70 JSObjectRef object(JSObjectMake(context, Internal_::GetClass(cy::Forward<Args_>(args)...), internal));
71 if (JSValueRef prototype = internal->GetPrototype(context))
72 CYSetPrototype(context, object, prototype);
76 static Internal_ *Get(JSContextRef context, JSObjectRef object) {
77 _assert(JSValueIsObjectOfClass(context, object, Class_));
78 return static_cast<Internal_ *>(JSObjectGetPrivate(object));
82 template <typename Internal_, typename Base_>
83 JSClassRef CYPrivateOld<Internal_, Base_>::Class_;
85 template <typename Internal_>
87 static JSClassRef Class_;
89 template <typename... Args_>
90 static JSObjectRef Make(JSContextRef context, Args_ &&... args) {
91 Internal_ *internal(new Internal_(cy::Forward<Args_>(args)...));
92 JSObjectRef object(JSObjectMake(context, Class_, internal));
93 if (JSValueRef prototype = internal->GetPrototype(context))
94 CYSetPrototype(context, object, prototype);
98 template <typename Arg_>
99 static JSObjectRef Cache(JSContextRef context, Arg_ *arg) {
100 JSObjectRef global(CYGetGlobalObject(context));
101 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
104 sprintf(label, "%s%p", Internal_::Cache_, arg);
105 CYJSString name(label);
107 JSValueRef value(CYGetProperty(context, cy, name));
108 if (!JSValueIsUndefined(context, value))
109 return CYCastJSObject(context, value);
111 JSObjectRef object(Make(context, arg));
112 CYSetProperty(context, cy, name, object);
116 static Internal_ *Get(JSContextRef context, JSObjectRef object) {
117 _assert(JSValueIsObjectOfClass(context, object, Class_));
118 return static_cast<Internal_ *>(JSObjectGetPrivate(object));
122 template <typename Internal_>
123 JSClassRef CYPrivate<Internal_>::Class_;
125 struct Type_privateData :
131 Type_privateData(const char *type) :
134 sig::Signature signature;
135 sig::Parse(*pool_, &signature, type, &Structor_);
136 type_ = signature.elements[0].type;
139 Type_privateData(const sig::Type &type, ffi_type *ffi = NULL) :
140 type_(type.Copy(*pool_))
146 ffi_ = new(*pool_) ffi_type;
147 sig::Copy(*pool_, *ffi_, *ffi);
153 sig::Element element;
155 element.type = type_;
158 sig::Signature signature;
159 signature.elements = &element;
163 sig::sig_ffi_cif(*pool_, false, signature, &cif);
165 ffi_ = new(*pool_) ffi_type;
175 JSGlobalContextRef context_;
179 CYProtect(JSContextRef context, JSObjectRef object) :
180 context_(CYGetJSContext(context)),
183 //XXX:JSGlobalContextRetain(context_);
185 JSValueProtect(context_, object_);
190 JSValueUnprotect(context_, object_);
191 //XXX:JSGlobalContextRelease(context_);
194 operator bool() const {
195 return object_ != NULL;
198 operator JSContextRef() const {
202 operator JSObjectRef() const {
213 CYBuffer(JSContextRef context) :
214 owner_(CYPrivate<CYRoot>::Make(context)),
215 pool_(CYPrivate<CYRoot>::Get(context, owner_)->pool_)
217 auto internal(CYPrivate<CYRoot>::Get(context, owner_));
218 internal->pool_->malloc<int>(10);
221 operator JSObjectRef() const {
225 operator CYPool *() const {
229 CYPool *operator ->() const {
239 static JSClassRef Class_;
243 sig::sig_ffi_cif(*pool_, variadic_ ? signature_.count : 0, signature_, &cif_);
249 sig::Signature signature_;
252 Functor(void (*value)(), bool variadic, const sig::Signature &signature) :
256 sig::Copy(*pool_, signature_, signature);
260 Functor(void (*value)(), const char *encoding) :
264 sig::Parse(*pool_, &signature_, encoding, &Structor_);
268 virtual CYPropertyName *GetName(CYPool &pool) const;
271 struct Closure_privateData :
275 JSValueRef (*adapter_)(JSContextRef, size_t, JSValueRef[], JSObjectRef);
277 Closure_privateData(JSContextRef context, JSObjectRef function, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef), const sig::Signature &signature) :
278 cy::Functor(NULL, false, signature),
279 function_(context, function),
285 Closure_privateData *CYMakeFunctor_(JSContextRef context, JSObjectRef function, const sig::Signature &signature, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
286 void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg, JSValueRef (*adapter)(JSContextRef, size_t, JSValueRef[], JSObjectRef));
288 #endif/*CYCRIPT_INTERNAL_HPP*/