]> git.saurik.com Git - cycript.git/blob - Internal.hpp
Insanely massive refactoring, bringing both exceptions and languages into near parity.
[cycript.git] / Internal.hpp
1 #ifndef CYCRIPT_INTERNAL_HPP
2 #define CYCRIPT_INTERNAL_HPP
3
4 #include "Pooling.hpp"
5
6 #include <JavaScriptCore/JSBase.h>
7 #include <JavaScriptCore/JSValueRef.h>
8
9 #include <sig/parse.hpp>
10 #include <sig/ffi_type.hpp>
11
12 void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type);
13
14 struct Type_privateData :
15 CYData
16 {
17 static JSClassRef Class_;
18
19 ffi_type *ffi_;
20 sig::Type *type_;
21
22 void Set(sig::Type *type) {
23 type_ = new(pool_) sig::Type;
24 sig::Copy(pool_, *type_, *type);
25 }
26
27 Type_privateData(apr_pool_t *pool, const char *type) :
28 ffi_(NULL)
29 {
30 if (pool != NULL)
31 pool_ = pool;
32
33 sig::Signature signature;
34 sig::Parse(pool_, &signature, type, &Structor_);
35 type_ = signature.elements[0].type;
36 }
37
38 Type_privateData(sig::Type *type) :
39 ffi_(NULL)
40 {
41 if (type != NULL)
42 Set(type);
43 }
44
45 Type_privateData(sig::Type *type, ffi_type *ffi) {
46 ffi_ = new(pool_) ffi_type;
47 sig::Copy(pool_, *ffi_, *ffi);
48 Set(type);
49 }
50
51 ffi_type *GetFFI() {
52 if (ffi_ == NULL) {
53 ffi_ = new(pool_) ffi_type;
54
55 sig::Element element;
56 element.name = NULL;
57 element.type = type_;
58 element.offset = 0;
59
60 sig::Signature signature;
61 signature.elements = &element;
62 signature.count = 1;
63
64 ffi_cif cif;
65 sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature, &cif);
66 *ffi_ = *cif.rtype;
67 }
68
69 return ffi_;
70 }
71 };
72
73 struct CYValue :
74 CYData
75 {
76 void *value_;
77
78 CYValue() {
79 }
80
81 CYValue(const void *value) :
82 value_(const_cast<void *>(value))
83 {
84 }
85
86 CYValue(const CYValue &rhs) :
87 value_(rhs.value_)
88 {
89 }
90
91 virtual Type_privateData *GetType() const {
92 return NULL;
93 }
94 };
95
96 struct CYOwned :
97 CYValue
98 {
99 private:
100 JSContextRef context_;
101 JSObjectRef owner_;
102
103 public:
104 CYOwned(void *value, JSContextRef context, JSObjectRef owner) :
105 CYValue(value),
106 context_(context),
107 owner_(owner)
108 {
109 if (owner_ != NULL)
110 JSValueProtect(context_, owner_);
111 }
112
113 virtual ~CYOwned() {
114 if (owner_ != NULL)
115 JSValueUnprotect(context_, owner_);
116 }
117
118 JSObjectRef GetOwner() const {
119 return owner_;
120 }
121 };
122
123 #endif/*CYCRIPT_INTERNAL_HPP*/