|
| 1 | +#include "ArrayBufferHelper.h" |
| 2 | +#include "JEnv.h" |
| 3 | +#include "V8GlobalHelpers.h" |
| 4 | +#include "NativeScriptException.h" |
| 5 | +#include <sstream> |
| 6 | + |
| 7 | + |
| 8 | +using namespace v8; |
| 9 | +using namespace tns; |
| 10 | + |
| 11 | +ArrayBufferHelper::ArrayBufferHelper() |
| 12 | + : m_objectManager(nullptr), m_ByteBufferClass(nullptr), m_isDirectMethodID(nullptr) |
| 13 | +{ |
| 14 | +} |
| 15 | + |
| 16 | +void ArrayBufferHelper::CreateConvertFunctions(Isolate *isolate, const Local<Object>& global, ObjectManager *objectManager) |
| 17 | +{ |
| 18 | + m_objectManager = objectManager; |
| 19 | + |
| 20 | + auto extData = External::New(isolate, this); |
| 21 | + auto fromFunc = FunctionTemplate::New(isolate, CreateFromCallbackStatic, extData)->GetFunction(); |
| 22 | + auto ctx = isolate->GetCurrentContext(); |
| 23 | + auto arrBufferCtorFunc = global->Get(ConvertToV8String("ArrayBuffer")).As<Function>(); |
| 24 | + arrBufferCtorFunc->Set(ctx, ConvertToV8String("from"), fromFunc); |
| 25 | +} |
| 26 | + |
| 27 | +void ArrayBufferHelper::CreateFromCallbackStatic(const FunctionCallbackInfo<Value>& info) |
| 28 | +{ |
| 29 | + try |
| 30 | + { |
| 31 | + auto extData = info.Data().As<External>(); |
| 32 | + auto thiz = reinterpret_cast<ArrayBufferHelper*>(extData->Value()); |
| 33 | + thiz->CreateFromCallbackImpl(info); |
| 34 | + } |
| 35 | + catch (NativeScriptException& e) |
| 36 | + { |
| 37 | + e.ReThrowToV8(); |
| 38 | + } |
| 39 | + catch (std::exception e) { |
| 40 | + std::stringstream ss; |
| 41 | + ss << "Error: c++ exception: " << e.what() << std::endl; |
| 42 | + NativeScriptException nsEx(ss.str()); |
| 43 | + nsEx.ReThrowToV8(); |
| 44 | + } |
| 45 | + catch (...) { |
| 46 | + NativeScriptException nsEx(std::string("Error: c++ exception!")); |
| 47 | + nsEx.ReThrowToV8(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +void ArrayBufferHelper::CreateFromCallbackImpl(const FunctionCallbackInfo<Value>& info) |
| 52 | +{ |
| 53 | + auto isolate = info.GetIsolate(); |
| 54 | + auto len = info.Length(); |
| 55 | + |
| 56 | + if (len != 1) |
| 57 | + { |
| 58 | + throw NativeScriptException("Wrong number of arguments (1 expected)"); |
| 59 | + } |
| 60 | + |
| 61 | + auto arg = info[0]; |
| 62 | + |
| 63 | + if (!arg->IsObject()) |
| 64 | + { |
| 65 | + throw NativeScriptException("Wrong type of argument (object expected)"); |
| 66 | + } |
| 67 | + |
| 68 | + auto argObj = arg.As<Object>(); |
| 69 | + |
| 70 | + auto obj = m_objectManager->GetJavaObjectByJsObject(argObj); |
| 71 | + |
| 72 | + if (obj.IsNull()) |
| 73 | + { |
| 74 | + throw NativeScriptException("Wrong type of argument (object expected)"); |
| 75 | + } |
| 76 | + |
| 77 | + JEnv env; |
| 78 | + |
| 79 | + if (m_ByteBufferClass == nullptr) |
| 80 | + { |
| 81 | + m_ByteBufferClass = env.FindClass("java/nio/ByteBuffer"); |
| 82 | + assert(m_ByteBufferClass != nullptr); |
| 83 | + } |
| 84 | + |
| 85 | + auto isByteBuffer = env.IsInstanceOf(obj, m_ByteBufferClass); |
| 86 | + |
| 87 | + if (!isByteBuffer) |
| 88 | + { |
| 89 | + throw NativeScriptException("Wrong type of argument (ByteBuffer expected)"); |
| 90 | + } |
| 91 | + |
| 92 | + if (m_isDirectMethodID == nullptr) |
| 93 | + { |
| 94 | + m_isDirectMethodID = env.GetMethodID(m_ByteBufferClass, "isDirect", "()Z"); |
| 95 | + assert(m_isDirectMethodID != nullptr); |
| 96 | + } |
| 97 | + |
| 98 | + auto ret = env.CallBooleanMethod(obj, m_isDirectMethodID); |
| 99 | + |
| 100 | + auto isDirectBuffer = ret == JNI_TRUE; |
| 101 | + |
| 102 | + if (!isDirectBuffer) |
| 103 | + { |
| 104 | + throw NativeScriptException("Direct ByteBuffer expected)"); |
| 105 | + } |
| 106 | + |
| 107 | + auto data = env.GetDirectBufferAddress(obj); |
| 108 | + auto size = env.GetDirectBufferCapacity(obj); |
| 109 | + |
| 110 | + auto arrayBuffer = ArrayBuffer::New(isolate, data, size); |
| 111 | + auto ctx = isolate->GetCurrentContext(); |
| 112 | + arrayBuffer->Set(ctx, ConvertToV8String("nativeObject"), argObj); |
| 113 | + |
| 114 | + info.GetReturnValue().Set(arrayBuffer); |
| 115 | +} |
0 commit comments