Repository navigation
Expand file tree
/
Copy pathInterop.h
More file actions
299 lines (279 loc) · 14 KB
/
Copy pathInterop.h
File metadata and controls
299 lines (279 loc) · 14 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef Interop_h
#define Interop_h
#include <CoreFoundation/CFBase.h>
#include <objc/message.h>
#include "Common.h"
#include "DataWrapper.h"
#include "FFICall.h"
#include "Metadata.h"
#include "libffi.h"
namespace tns {
typedef void (*FFIMethodCallback)(ffi_cif* cif, void* retValue,
void** argValues, void* userData);
// The receiver of a call that returns an object, as far as releasing an owned
// result that already has a wrapper is concerned.
enum class OwnedReturnReceiver : uint8_t {
// A class method or a C function.
None,
// An instance method that leaves its receiver to the caller.
Kept,
// An initializer or NS_REPLACES_RECEIVER method sent to a receiver that
// existed before the call.
Consumed,
// An initializer or NS_REPLACES_RECEIVER method sent to a receiver the
// runtime allocated for this call.
AllocatedForCall,
};
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_;
bool receiverAllocatedForCall_ = false;
};
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,
bool receiverAllocatedForCall = false)
: 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()) {
receiverAllocatedForCall_ = receiverAllocatedForCall;
}
};
class Interop {
public:
// The natives behind ns:interop and ns:interop/ios (runtime/js/ns-interop.js,
// ns-interop-ios.js), built when the module is first resolved.
static v8::MaybeLocal<v8::Object> NsInteropBinding(
v8::Local<v8::Context> context);
static v8::MaybeLocal<v8::Object> NsInteropIosBinding(
v8::Local<v8::Context> context);
// ns:runtime members; `target` is that module's binding.
static void RegisterExternalSizeFunctions(v8::Local<v8::Context> context,
v8::Local<v8::Object> target);
static void RegisterEscapeExceptionFunction(v8::Local<v8::Context> context,
v8::Local<v8::Object> target);
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);
// Whether a method answers in place of its receiver, consuming the caller's
// reference to it: the init family, and NS_REPLACES_RECEIVER methods, which
// the metadata does not record.
static bool ConsumesReceiver(SEL selector, bool isInitializer);
// Whether a method takes over the caller's reference to its argument at
// `index`, counted from the first after the selector: NS_RELEASES_ARGUMENT
// parameters, which the metadata does not record.
static bool ConsumesArgument(SEL selector, int index);
// The index ConsumesArgument names for `selector` when that parameter is an
// object, else -1. `typeEncoding` is the signature's return type, followed
// by its `paramsCount` parameter types.
static int ConsumedObjectArgumentIndex(SEL selector,
const TypeEncoding* typeEncoding,
int paramsCount);
static bool IsObjectEncoding(const TypeEncoding* typeEncoding);
// Whether a method outside the copy, mutableCopy and new families answers
// an object its caller owns (NS_RETURNS_RETAINED), for implementations the
// metadata does not describe.
static bool ReturnsRetained(SEL selector);
// Whether the +1 of an owned object or block result is the caller's to
// drop once the runtime holds what it keeps: an Instances entry and a
// native block's wrapper hold references of their own, and a primitive or
// the JS function a block was made for keeps none.
static bool ReleasesCachedOwnedReturn(bool ownsReturnedObject,
OwnedReturnReceiver receiver);
static v8::Local<v8::Value> GetResultByType(
v8::Local<v8::Context> context, BaseDataWrapper* typeWrapper,
BaseCall* call,
v8::Local<v8::Object> parentStruct = v8::Local<v8::Object>());
// A non-empty `parentStruct` makes a struct-typed result a view into the
// parent's buffer (`call` points at the field) instead of a copy. The
// default `ownedReturnReceiver` never releases an owned result found already
// wrapped.
static v8::Local<v8::Value> GetResult(
v8::Local<v8::Context> context, const TypeEncoding* typeEncoding,
BaseCall* call, bool marshalToPrimitive,
v8::Local<v8::Object> parentStruct = v8::Local<v8::Object>(),
bool isStructMember = false, bool ownsReturnedObject = false,
bool returnsUnmanaged = false, bool isInitializer = false,
OwnedReturnReceiver ownedReturnReceiver = OwnedReturnReceiver::Consumed);
static void SetStructPropertyValue(v8::Local<v8::Context> context,
StructWrapper* wrapper,
const StructField& field,
v8::Local<v8::Value> value);
static void InitializeStruct(v8::Local<v8::Context> context, void* destBuffer,
const 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);
// The JS function `block` calls when CreateBlock built it for this isolate,
// held strongly until the block is disposed; empty for any other block and
// once disposal has started. `block` must be alive, and the caller must
// hold the isolate's Locker.
static v8::Local<v8::Value> JSBlockCallback(v8::Isolate* isolate,
const void* block);
// Whether CreateBlock built `block` (for any isolate). `block` must be alive.
static bool IsJSBlock(const void* block);
static v8::Local<v8::Value> GetPrimitiveReturnType(
v8::Local<v8::Context> context, BinaryTypeEncodingType type,
BaseCall* call);
private:
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,
const std::vector<StructField>& fields,
v8::Local<v8::Value> inititalizer,
ptrdiff_t& position);
static bool GetPrimitiveTypes(v8::Local<v8::Context> context,
v8::Local<v8::Object>* cTypes,
v8::Local<v8::Object>* appleTypes);
static v8::Local<v8::Object> CreateInteropType(v8::Local<v8::Context> context,
const 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,
const StructInfo& structInfo,
v8::Local<v8::Object> 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;
// The wrapper caching this block on the JS function it was built from. It
// is owned here rather than through that function: the cache slot lives in
// a V8 heap that can be torn down (a worker isolate) while the block is
// still referenced by native code.
BlockWrapper* blockWrapper;
static JSBlockDescriptor kJSBlockDescriptor;
} JSBlock;
// Takes a reference to a cached JSBlock only while it is live. Block_copy
// would also revive a block whose last release already started its dispose.
static bool TryRetainJSBlock(JSBlock* block);
};
} // namespace tns
#endif /* Interop_h */