forked from NativeScript/ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterop.h
More file actions
205 lines (188 loc) · 8.87 KB
/
Interop.h
File metadata and controls
205 lines (188 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef Interop_h
#define Interop_h
#include <CoreFoundation/CFBase.h>
#include <objc/message.h>
#include "libffi.h"
#include "Common.h"
#include "Metadata.h"
#include "DataWrapper.h"
#include "FFICall.h"
namespace tns {
typedef void (*FFIMethodCallback)(ffi_cif* cif, void* retValue, void** argValues, void* userData);
struct MethodCall {
MethodCall(v8::Local<v8::Context> context,
bool isPrimitiveFunction,
void* functionPointer,
const TypeEncoding* typeEncoding,
V8Args& args,
id target,
Class clazz,
SEL selector,
bool callSuper,
MetaType metaType,
bool provideErrorOutParameter,
bool ownsReturnedObject,
bool returnsUnmanaged,
bool isInitializer)
: context_(context),
isPrimitiveFunction_(isPrimitiveFunction),
functionPointer_(functionPointer),
typeEncoding_(typeEncoding),
args_(args),
target_(target),
clazz_(clazz),
selector_(selector),
callSuper_(callSuper),
metaType_(metaType),
provideErrorOutParameter_(provideErrorOutParameter),
ownsReturnedObject_(ownsReturnedObject),
returnsUnmanaged_(returnsUnmanaged),
isInitializer_(isInitializer) {
}
v8::Local<v8::Context> context_;
bool isPrimitiveFunction_;
void* functionPointer_;
const TypeEncoding* typeEncoding_;
V8Args& args_;
id target_;
Class clazz_;
SEL selector_;
bool callSuper_;
MetaType metaType_;
bool provideErrorOutParameter_;
bool ownsReturnedObject_;
bool returnsUnmanaged_;
bool isInitializer_;
};
struct CMethodCall: MethodCall {
CMethodCall(
v8::Local<v8::Context> context,
void* functionPointer,
const TypeEncoding* typeEncoding,
V8Args& args,
bool ownsReturnedObject,
bool returnsUnmanaged)
: MethodCall(
context,
true,
functionPointer,
typeEncoding,
args,
nil,
nil,
nil,
false,
MetaType::Undefined,
false,
ownsReturnedObject,
returnsUnmanaged,
false) {
}
};
struct ObjCMethodCall: public MethodCall {
ObjCMethodCall(
v8::Local<v8::Context> context,
const MethodMeta* meta,
id target,
Class clazz,
V8Args& args,
bool callSuper)
: MethodCall(
context,
false,
callSuper ? (void*)objc_msgSendSuper : (void*)objc_msgSend,
meta->encodings()->first(),
args,
target,
clazz,
meta->selector(),
callSuper,
meta->type(),
meta->hasErrorOutParameter() && args.Length() < meta->encodings()->count - 1,
meta->ownsReturnedCocoaObject(),
false,
meta->isInitializer()) {
}
};
class Interop {
public:
static void RegisterInteropTypes(v8::Local<v8::Context> context);
static IMP CreateMethod(const uint8_t initialParamIndex, const uint8_t argsCount, const TypeEncoding* typeEncoding, FFIMethodCallback callback, void* userData);
static id CallInitializer(v8::Local<v8::Context> context, const MethodMeta* methodMeta, id target, Class clazz, V8Args& args);
static v8::Local<v8::Value> CallFunction(ObjCMethodCall& methodCall);
static v8::Local<v8::Value> CallFunction(CMethodCall& methodCall);
static v8::Local<v8::Value> GetResultByType(v8::Local<v8::Context> context, BaseDataWrapper* typeWrapper, BaseCall* call, std::shared_ptr<v8::Persistent<v8::Value>> parentStruct = nullptr);
static v8::Local<v8::Value> GetResult(v8::Local<v8::Context> context, const TypeEncoding* typeEncoding, BaseCall* call, bool marshalToPrimitive, std::shared_ptr<v8::Persistent<v8::Value>> parentStruct = nullptr, bool isStructMember = false, bool ownsReturnedObject = false, bool returnsUnmanaged = false, bool isInitializer = false);
static void SetStructPropertyValue(v8::Local<v8::Context> context, StructWrapper* wrapper, StructField field, v8::Local<v8::Value> value);
static void InitializeStruct(v8::Local<v8::Context> context, void* destBuffer, std::vector<StructField> fields, v8::Local<v8::Value> inititalizer);
static void WriteTypeValue(v8::Local<v8::Context> context, BaseDataWrapper* typeWrapper, void* dest, v8::Local<v8::Value> arg);
static void WriteValue(v8::Local<v8::Context> context, const TypeEncoding* typeEncoding, void* dest, v8::Local<v8::Value> arg);
static id ToObject(v8::Local<v8::Context> context, v8::Local<v8::Value> arg);
static v8::Local<v8::Value> GetPrimitiveReturnType(v8::Local<v8::Context> context, BinaryTypeEncodingType type, BaseCall* call);
private:
static void ExecuteWriteValueDebugValidationsIfInDebug(v8::Local<v8::Context> context, const TypeEncoding* typeEncoding, void* dest, v8::Local<v8::Value> arg);
static std::pair<IMP, ffi_closure*> CreateMethodInternal(const uint8_t initialParamIndex, const uint8_t argsCount, const TypeEncoding* typeEncoding, FFIMethodCallback callback, void* userData);
static CFTypeRef CreateBlock(const uint8_t initialParamIndex, const uint8_t argsCount, const TypeEncoding* typeEncoding, FFIMethodCallback callback, void* userData);
template <typename T>
static void SetStructValue(v8::Local<v8::Value> value, void* destBuffer, ptrdiff_t position);
static void InitializeStruct(v8::Local<v8::Context> context, void* destBuffer, std::vector<StructField> fields, v8::Local<v8::Value> inititalizer, ptrdiff_t& position);
static void RegisterInteropType(v8::Local<v8::Context> context, v8::Local<v8::Object> types, std::string name, PrimitiveDataWrapper* wrapper, bool autoDelete = true);
static void RegisterBufferFromDataFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterStringFromCString(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterHandleOfFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterAllocFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterFreeFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterAdoptFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void RegisterSizeOfFunction(v8::Local<v8::Context> context, v8::Local<v8::Object> interop);
static void SetFFIParams(v8::Local<v8::Context> context, const TypeEncoding* typeEncoding, FFICall* call, const int argsCount, const int initialParameterIndex, V8Args& args);
static bool isRefTypeEqual(const TypeEncoding* typeEncoding,const char* clazz);
static v8::Local<v8::Array> ToArray(v8::Local<v8::Object> object);
static v8::Local<v8::Value> StructToValue(v8::Local<v8::Context> context, void* result, StructInfo structInfo, std::shared_ptr<v8::Persistent<v8::Value>> parentStruct);
static const TypeEncoding* CreateEncoding(BinaryTypeEncodingType type);
static v8::Local<v8::Value> HandleOf(v8::Local<v8::Context> context, v8::Local<v8::Value> value);
static v8::Local<v8::Value> CallFunctionInternal(MethodCall& methodCall);
static bool IsNumbericType(BinaryTypeEncodingType type);
static v8::Local<v8::Object> GetInteropType(v8::Local<v8::Context> context, BinaryTypeEncodingType type);
static std::vector<std::string> GetAdditionalProtocols(const TypeEncoding* typeEncoding);
static SEL GetSwizzledMethodSelector(SEL selector);
template <typename T>
static inline void SetValue(void* dest, T value) {
if (std::is_same<T, SEL>::value) {
memcpy(dest, &value, sizeof(SEL*));
} else {
*static_cast<T*>(dest) = value;
}
}
template <typename T>
static void SetNumericValue(void* dest, double value) {
if (value < std::numeric_limits<T>::lowest()) {
Interop::SetValue(dest, std::numeric_limits<T>::lowest());
} else if (value > std::numeric_limits<T>::max()) {
Interop::SetValue(dest, std::numeric_limits<T>::max());
} else {
Interop::SetValue(dest, (T)value);
}
}
typedef struct JSBlock {
typedef struct {
uintptr_t reserved;
uintptr_t size;
void (*copy)(struct JSBlock*, const struct JSBlock*);
void (*dispose)(struct JSBlock*);
} JSBlockDescriptor;
enum {
BLOCK_NEEDS_FREE = (1 << 24), // runtime
BLOCK_HAS_COPY_DISPOSE = (1 << 25), // compiler
};
void* isa;
volatile int32_t flags; // contains ref count
int32_t reserved;
const void* invoke;
JSBlockDescriptor* descriptor;
void* userData;
ffi_closure* ffiClosure;
static JSBlockDescriptor kJSBlockDescriptor;
} JSBlock;
};
}
#endif /* Interop_h */