|
| 1 | +#include "UnmanagedType.h" |
| 2 | +#include "NativeScriptException.h" |
| 3 | +#include "Caches.h" |
| 4 | +#include "Helpers.h" |
| 5 | +#include "Interop.h" |
| 6 | + |
| 7 | +using namespace v8; |
| 8 | + |
| 9 | +namespace tns { |
| 10 | + |
| 11 | +Local<Value> UnmanagedType::Create(Local<Context> context, UnmanagedTypeWrapper* wrapper) { |
| 12 | + Isolate* isolate = context->GetIsolate(); |
| 13 | + auto cache = Caches::Get(isolate); |
| 14 | + if (cache->UnmanagedTypeCtorFunc.get() == nullptr) { |
| 15 | + Local<FunctionTemplate> ctorFuncTemplate = FunctionTemplate::New(isolate, ConstructorCallback); |
| 16 | + ctorFuncTemplate->SetClassName(tns::ToV8String(isolate, "Unmanaged")); |
| 17 | + Local<ObjectTemplate> proto = ctorFuncTemplate->PrototypeTemplate(); |
| 18 | + |
| 19 | + Local<FunctionTemplate> takeUnretainedValueFuncTemplate = FunctionTemplate::New(isolate, UnmanagedType::TakeUnretainedValueCallback); |
| 20 | + Local<FunctionTemplate> takeRetainedValueFuncTemplate = FunctionTemplate::New(isolate, UnmanagedType::TakeRetainedValueCallback); |
| 21 | + proto->Set(tns::ToV8String(isolate, "takeUnretainedValue"), takeUnretainedValueFuncTemplate); |
| 22 | + proto->Set(tns::ToV8String(isolate, "takeRetainedValue"), takeRetainedValueFuncTemplate); |
| 23 | + |
| 24 | + Local<v8::Function> ctorFunc; |
| 25 | + bool success = ctorFuncTemplate->GetFunction(context).ToLocal(&ctorFunc); |
| 26 | + tns::Assert(success, isolate); |
| 27 | + |
| 28 | + cache->UnmanagedTypeCtorFunc = std::make_unique<Persistent<v8::Function>>(isolate, ctorFunc); |
| 29 | + } |
| 30 | + |
| 31 | + Local<External> ext = External::New(isolate, wrapper); |
| 32 | + |
| 33 | + Local<v8::Function> ctorFunc = cache->UnmanagedTypeCtorFunc->Get(isolate); |
| 34 | + Local<Value> result; |
| 35 | + Local<Value> args[] = { ext }; |
| 36 | + bool success = ctorFunc->NewInstance(context, 1, args).ToLocal(&result); |
| 37 | + tns::Assert(success, isolate); |
| 38 | + |
| 39 | + return result; |
| 40 | +} |
| 41 | + |
| 42 | +void UnmanagedType::ConstructorCallback(const FunctionCallbackInfo<Value>& info) { |
| 43 | + Local<External> ext = info[0].As<External>(); |
| 44 | + UnmanagedTypeWrapper* wrapper = static_cast<UnmanagedTypeWrapper*>(ext->Value()); |
| 45 | + tns::SetValue(info.GetIsolate(), info.This(), wrapper); |
| 46 | +} |
| 47 | + |
| 48 | +void UnmanagedType::TakeUnretainedValueCallback(const FunctionCallbackInfo<Value>& info) { |
| 49 | + try { |
| 50 | + info.GetReturnValue().Set(UnmanagedType::TakeValue(info, false)); |
| 51 | + } catch (NativeScriptException& ex) { |
| 52 | + ex.ReThrowToV8(info.GetIsolate()); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void UnmanagedType::TakeRetainedValueCallback(const FunctionCallbackInfo<Value>& info) { |
| 57 | + try { |
| 58 | + info.GetReturnValue().Set(UnmanagedType::TakeValue(info, true)); |
| 59 | + } catch (NativeScriptException& ex) { |
| 60 | + ex.ReThrowToV8(info.GetIsolate()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +Local<Value> UnmanagedType::TakeValue(const FunctionCallbackInfo<Value>& info, bool retained) { |
| 65 | + Isolate* isolate = info.GetIsolate(); |
| 66 | + Local<Context> context = isolate->GetCurrentContext(); |
| 67 | + |
| 68 | + BaseDataWrapper* baseWrapper = tns::GetValue(isolate, info.This()); |
| 69 | + UnmanagedTypeWrapper* wrapper = static_cast<UnmanagedTypeWrapper*>(baseWrapper); |
| 70 | + |
| 71 | + if (wrapper->ValueTaken()) { |
| 72 | + throw NativeScriptException("Unmanaged value has already been consumed."); |
| 73 | + } |
| 74 | + |
| 75 | + uint8_t* data = wrapper->Data(); |
| 76 | + const TypeEncoding* typeEncoding = wrapper->TypeEncoding(); |
| 77 | + |
| 78 | + BaseCall call((uint8_t*)&data); |
| 79 | + Local<Value> result = Interop::GetResult(context, typeEncoding, &call, false); |
| 80 | + |
| 81 | + if (retained) { |
| 82 | + id value = static_cast<id>((void*)data); |
| 83 | + [value release]; |
| 84 | + } |
| 85 | + |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +} |
0 commit comments