-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathReference.cpp
More file actions
479 lines (411 loc) · 16.5 KB
/
Copy pathReference.cpp
File metadata and controls
479 lines (411 loc) · 16.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "Reference.h"
#include "Caches.h"
#include "Constants.h"
#include "Helpers.h"
#include "Interop.h"
#include "ObjectManager.h"
#include "Pointer.h"
using namespace v8;
namespace tns {
void Reference::Register(Local<Context> context, Local<Object> interop) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<v8::Function> ctorFunc =
Reference::GetInteropReferenceCtorFunc(context);
bool success =
interop->Set(context, tns::ToV8String(isolate, "Reference"), ctorFunc)
.FromMaybe(false);
tns::Assert(success, isolate);
}
Local<Value> Reference::FromPointer(Local<Context> context, Local<Value> type,
void* handle) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<Value> pointer = Pointer::NewInstance(context, handle);
ObjectManager::Register(context, pointer);
Local<v8::Function> interopReferenceCtorFunc =
Reference::GetInteropReferenceCtorFunc(context);
Local<Value> args[2] = {type, pointer};
Local<Object> instance;
bool success = interopReferenceCtorFunc->NewInstance(context, 2, args)
.ToLocal(&instance);
tns::Assert(success, isolate);
ObjectManager::Register(context, instance);
return instance;
}
Local<v8::Function> Reference::GetInteropReferenceCtorFunc(
Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
auto cache = Caches::Get(isolate);
Persistent<v8::Function>* interopReferenceCtor =
cache->InteropReferenceCtorFunc.get();
if (interopReferenceCtor != nullptr) {
return interopReferenceCtor->Get(isolate);
}
Local<FunctionTemplate> ctorFuncTemplate =
FunctionTemplate::New(isolate, ReferenceConstructorCallback);
ctorFuncTemplate->SetClassName(tns::ToV8String(isolate, "Reference"));
ctorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(1);
ctorFuncTemplate->InstanceTemplate()->SetHandler(
IndexedPropertyHandlerConfiguration(IndexedPropertyGetCallback,
IndexedPropertySetCallback));
Local<ObjectTemplate> proto = ctorFuncTemplate->PrototypeTemplate();
proto->SetAccessorProperty(tns::ToV8String(isolate, "value"),
FunctionTemplate::New(isolate, GetValueCallback),
FunctionTemplate::New(isolate, SetValueCallback));
Local<v8::Function> ctorFunc;
if (!ctorFuncTemplate->GetFunction(context).ToLocal(&ctorFunc)) {
tns::Assert(false, isolate);
}
tns::SetValue(isolate, ctorFunc, new ReferenceTypeWrapper());
Local<Value> prototypeValue;
bool success = ctorFunc->Get(context, tns::ToV8String(isolate, "prototype"))
.ToLocal(&prototypeValue);
tns::Assert(success && prototypeValue->IsObject(), isolate);
Local<Object> prototype = prototypeValue.As<Object>();
Reference::RegisterToStringMethod(context, prototype);
cache->InteropReferenceCtorFunc =
std::make_unique<Persistent<v8::Function>>(isolate, ctorFunc);
cache->InteropReferenceCtorFunc->SetWrapperClassId(
Constants::ClassTypes::DataWrapper);
return ctorFunc;
}
void Reference::ReferenceConstructorCallback(
const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Persistent<Value>* val = nullptr;
BaseDataWrapper* typeWrapper = nullptr;
if (info.Length() == 1) {
if (!info[0]->IsNullOrUndefined()) {
val = new Persistent<Value>(isolate, info[0]);
}
} else if (info.Length() > 1) {
if (!info[0]->IsNullOrUndefined() && !info[1]->IsNullOrUndefined()) {
Local<Value> typeValue = info[0];
typeWrapper = tns::GetValue(isolate, typeValue);
tns::Assert(typeWrapper != nullptr, isolate);
val = new Persistent<Value>(isolate, info[1]);
}
}
if (val != nullptr) {
val->SetWeak();
}
ReferenceWrapper* wrapper = new ReferenceWrapper(typeWrapper, val);
Local<Object> thiz = info.This();
tns::SetValue(isolate, thiz, wrapper);
ObjectManager::Register(context, thiz);
}
v8::Intercepted Reference::IndexedPropertyGetCallback(
uint32_t index, const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Object> thiz = info.Holder();
Local<Context> context = isolate->GetCurrentContext();
DataPair pair = Reference::GetDataPair(thiz);
const TypeEncoding* typeEncoding = pair.typeEncoding_;
size_t size = pair.size_;
void* data = pair.data_;
if (data == nullptr && typeEncoding == nullptr &&
pair.typeWrapper_ == nullptr) {
// GetDataPair threw (released native object).
return v8::Intercepted::kYes;
}
void* ptr = (uint8_t*)data + index * size;
BaseCall call((uint8_t*)ptr);
Local<Value> result;
if (typeEncoding != nullptr) {
result = Interop::GetResult(context, typeEncoding, &call, false);
} else {
result = Interop::GetResultByType(context, pair.typeWrapper_, &call);
}
info.GetReturnValue().Set(result);
return v8::Intercepted::kYes;
}
v8::Intercepted Reference::IndexedPropertySetCallback(
uint32_t index, Local<Value> value,
const PropertyCallbackInfo<v8::Boolean>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> thiz = info.Holder();
DataPair pair = Reference::GetDataPair(thiz);
const TypeEncoding* typeEncoding = pair.typeEncoding_;
size_t size = pair.size_;
void* data = pair.data_;
if (data == nullptr && typeEncoding == nullptr &&
pair.typeWrapper_ == nullptr) {
// GetDataPair threw (released native object).
return v8::Intercepted::kYes;
}
void* ptr = (uint8_t*)data + index * size;
if (typeEncoding != nullptr) {
Interop::WriteValue(context, typeEncoding, ptr, value);
} else {
Interop::WriteTypeValue(context, pair.typeWrapper_, ptr, value);
}
// Not intercepted: the native write is done, but V8 must still perform
// the ordinary store, which is what the old void-returning callback did
// by falling through without setting a return value.
return v8::Intercepted::kNo;
}
void Reference::GetValueCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Value> result = Reference::GetReferredValue(context, info.This());
if (result.IsEmpty()) {
info.GetReturnValue().SetUndefined();
} else {
info.GetReturnValue().Set(result);
}
}
void Reference::SetValueCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Value> value = info[0];
BaseDataWrapper* baseWrapper =
tns::GetValueOrReport(isolate, info.This(), "Reference value set");
if (baseWrapper == nullptr) {
return;
}
tns::Assert(baseWrapper->Type() == WrapperType::Reference, isolate);
ReferenceWrapper* wrapper = static_cast<ReferenceWrapper*>(baseWrapper);
Persistent<Value>* poValue = new Persistent<Value>(isolate, value);
wrapper->SetData(nullptr);
wrapper->SetEncoding(nullptr);
wrapper->SetValue(poValue);
}
Local<Value> Reference::GetReferredValue(Local<Context> context,
Local<Value> value) {
Isolate* isolate = v8::Isolate::GetCurrent();
BaseDataWrapper* baseWrapper = tns::GetValue(isolate, value);
if (baseWrapper == nullptr || baseWrapper->Type() != WrapperType::Reference) {
return value;
}
ReferenceWrapper* wrapper = static_cast<ReferenceWrapper*>(baseWrapper);
if (wrapper->Data() != nullptr && wrapper->Encoding() != nullptr) {
const TypeEncoding* encoding = wrapper->Encoding();
uint8_t* data = (uint8_t*)wrapper->Data();
BaseCall call(data);
Local<Value> jsResult = Interop::GetResult(context, encoding, &call, true);
if (wrapper->Value() != nullptr) {
wrapper->Value()->Reset(isolate, jsResult);
} else {
wrapper->SetValue(new Persistent<Value>(isolate, jsResult));
}
wrapper->SetData(nullptr);
wrapper->SetEncoding(nullptr);
return jsResult;
}
if (wrapper->Value() == nullptr) {
return Local<Value>();
}
Local<Value> innerValue = wrapper->Value()->Get(isolate);
baseWrapper = tns::GetValue(isolate, innerValue);
if (baseWrapper != nullptr && baseWrapper->Type() == WrapperType::Reference) {
wrapper = static_cast<ReferenceWrapper*>(baseWrapper);
if (wrapper->Value() != nullptr) {
return Reference::GetReferredValue(context,
wrapper->Value()->Get(isolate));
}
}
BaseDataWrapper* typeWrapper = wrapper->TypeWrapper();
if (typeWrapper != nullptr &&
Reference::IsSupportedType(typeWrapper->Type()) &&
baseWrapper != nullptr && baseWrapper->Type() == WrapperType::Pointer) {
Reference::DataPair pair = Reference::GetDataPair(value.As<Object>());
if (pair.data_ != nullptr) {
BaseCall call((uint8_t*)pair.data_);
Local<Value> result;
if (pair.typeEncoding_ != nullptr) {
result = Interop::GetResult(context, pair.typeEncoding_, &call, false);
} else {
result = Interop::GetResultByType(context, typeWrapper, &call);
}
return result;
}
}
return innerValue;
}
void* Reference::GetWrappedPointer(Local<Context> context,
Local<Value> reference,
const TypeEncoding* typeEncoding) {
if (reference.IsEmpty() || reference->IsNullOrUndefined()) {
return nullptr;
}
Isolate* isolate = v8::Isolate::GetCurrent();
BaseDataWrapper* wrapper =
tns::GetValueOrReport(isolate, reference, "Reference pointer access");
if (wrapper == nullptr) {
return nullptr;
}
tns::Assert(wrapper->Type() == WrapperType::Reference, isolate);
ReferenceWrapper* refWrapper = static_cast<ReferenceWrapper*>(wrapper);
if (refWrapper->Data() != nullptr) {
return refWrapper->Data();
}
Local<Value> value = refWrapper->Value()->Get(isolate);
BaseDataWrapper* wrappedValue = tns::GetValue(isolate, value);
if (wrappedValue == nullptr) {
BaseDataWrapper* typeWrapper = refWrapper->TypeWrapper();
if (typeWrapper == nullptr) {
return nullptr;
}
if (typeWrapper->Type() == WrapperType::Primitive) {
PrimitiveDataWrapper* primitiveWrapper =
static_cast<PrimitiveDataWrapper*>(typeWrapper);
const TypeEncoding* enc = primitiveWrapper->TypeEncoding();
size_t size = primitiveWrapper->Size();
void* data = malloc(size);
Interop::WriteValue(context, enc, data, value);
refWrapper->SetData(data, true);
return data;
}
if (typeWrapper->Type() != WrapperType::StructType) {
return nullptr;
}
StructTypeWrapper* structTypeWrapper =
static_cast<StructTypeWrapper*>(refWrapper->TypeWrapper());
StructInfo structInfo = structTypeWrapper->StructInfo();
void* data = malloc(structInfo.FFIType()->size);
Interop::InitializeStruct(context, data, structInfo.Fields(), value);
refWrapper->SetData(data, true);
refWrapper->SetEncoding(typeEncoding);
return data;
}
if (wrappedValue->Type() == WrapperType::ObjCClass) {
ObjCClassWrapper* classWrapper =
static_cast<ObjCClassWrapper*>(wrappedValue);
void* handle = malloc(sizeof(Class*));
Class clazz = classWrapper->Klass();
*static_cast<Class*>(handle) = clazz;
refWrapper->SetData(handle, true);
return (Class**)handle;
}
if (wrappedValue->Type() == WrapperType::ObjCProtocol) {
ObjCProtocolWrapper* protoWrapper =
static_cast<ObjCProtocolWrapper*>(wrappedValue);
Protocol* proto = protoWrapper->Proto();
void* handle = malloc(sizeof(Protocol**));
*static_cast<Protocol**>(handle) = proto;
refWrapper->SetData(handle, true);
return (Protocol**)handle;
}
if (wrappedValue->Type() == WrapperType::ObjCObject) {
ObjCDataWrapper* dataWrapper = static_cast<ObjCDataWrapper*>(wrappedValue);
id target = dataWrapper->Data();
void* handle = malloc(sizeof(id));
*static_cast<id*>(handle) = target;
refWrapper->SetData(handle, true);
return (id)handle;
}
if (wrappedValue->Type() == WrapperType::Struct) {
StructWrapper* structWrapper = static_cast<StructWrapper*>(wrappedValue);
return structWrapper->Data();
}
if (wrappedValue->Type() == WrapperType::Pointer) {
PointerWrapper* pw = static_cast<PointerWrapper*>(wrappedValue);
void* data = pw->Data();
return data;
}
return nullptr;
}
void Reference::RegisterToStringMethod(Local<Context> context,
Local<Object> prototype) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<FunctionTemplate> funcTemplate = FunctionTemplate::New(
isolate, [](const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
BaseDataWrapper* wrapper =
tns::GetValueOrReport(isolate, info.This(), "Reference.toString");
if (wrapper == nullptr) {
if (tns::GetReleasedObjectPolicy() ==
tns::ReleasedObjectPolicy::kReport) {
info.GetReturnValue().Set(
tns::ToV8String(isolate, "<Reference: released>"));
}
return;
}
tns::Assert(wrapper->Type() == WrapperType::Reference, isolate);
ReferenceWrapper* refWrapper = static_cast<ReferenceWrapper*>(wrapper);
Persistent<Value>* value = refWrapper->Value();
char buffer[100];
if (value == nullptr) {
snprintf(buffer, 100, "<Reference: %p>", reinterpret_cast<void*>(0));
} else {
snprintf(buffer, 100, "<Reference: %p>", value);
}
Local<v8::String> result = tns::ToV8String(isolate, buffer);
info.GetReturnValue().Set(result);
});
Local<v8::Function> func;
tns::Assert(funcTemplate->GetFunction(context).ToLocal(&func), isolate);
bool success =
prototype->Set(context, tns::ToV8String(isolate, "toString"), func)
.FromMaybe(false);
tns::Assert(success, isolate);
}
Reference::DataPair Reference::GetDataPair(Local<Object> obj) {
Local<Context> context;
bool success =
obj->GetCreationContext(v8::Isolate::GetCurrent()).ToLocal(&context);
tns::Assert(success);
Isolate* isolate = v8::Isolate::GetCurrent();
BaseDataWrapper* wrapper =
tns::GetValueOrReport(isolate, obj, "Reference indexed access");
if (wrapper == nullptr) {
return DataPair(nullptr, nullptr, nullptr, 0);
}
tns::Assert(wrapper->Type() == WrapperType::Reference, isolate);
ReferenceWrapper* refWrapper = static_cast<ReferenceWrapper*>(wrapper);
BaseDataWrapper* typeWrapper = refWrapper->TypeWrapper();
size_t size = 0;
if (typeWrapper != nullptr) {
const TypeEncoding* typeEncoding = nullptr;
if (Reference::IsSupportedType(typeWrapper->Type())) {
switch (typeWrapper->Type()) {
case WrapperType::Primitive: {
PrimitiveDataWrapper* primitiveWrapper =
static_cast<PrimitiveDataWrapper*>(typeWrapper);
size = primitiveWrapper->Size();
typeEncoding = primitiveWrapper->TypeEncoding();
break;
}
case WrapperType::StructType: {
StructTypeWrapper* structTypeWrapper =
static_cast<StructTypeWrapper*>(refWrapper->TypeWrapper());
StructInfo structInfo = structTypeWrapper->StructInfo();
size = structInfo.FFIType()->size;
break;
}
default: {
break;
}
}
} else {
// TODO: Currently only PrimitiveDataWrappers and Structs are supported as
// type parameters Objective C class classes should also be handled
tns::Assert(false, isolate);
}
Local<Value> value = refWrapper->Value()->Get(isolate);
BaseDataWrapper* wrappedValue = tns::GetValue(isolate, value);
if (wrappedValue != nullptr &&
wrappedValue->Type() == WrapperType::Pointer) {
PointerWrapper* pw = static_cast<PointerWrapper*>(wrappedValue);
void* data = pw->Data();
DataPair pair(typeWrapper, typeEncoding, data, size);
return pair;
}
}
if (refWrapper->Encoding() != nullptr && refWrapper->Data() != nullptr) {
const TypeEncoding* typeEncoding = refWrapper->Encoding();
if (typeWrapper == nullptr) {
ffi_type* ffiType = FFICall::GetArgumentType(typeEncoding);
size = ffiType->size;
FFICall::DisposeFFIType(ffiType, typeEncoding);
}
DataPair pair(typeWrapper, typeEncoding, refWrapper->Data(), size);
return pair;
}
tns::Assert(false, isolate);
return DataPair(typeWrapper, nullptr, nullptr, size);
}
bool Reference::IsSupportedType(WrapperType type) {
return type == WrapperType::Primitive || type == WrapperType::StructType;
}
} // namespace tns