diff --git a/test-app/runtime/src/main/cpp/JSONObjectHelper.cpp b/test-app/runtime/src/main/cpp/JSONObjectHelper.cpp index 9ca78d5a2..a415e0c0a 100644 --- a/test-app/runtime/src/main/cpp/JSONObjectHelper.cpp +++ b/test-app/runtime/src/main/cpp/JSONObjectHelper.cpp @@ -7,33 +7,54 @@ using namespace v8; using namespace tns; -JSONObjectHelper::JSONObjectHelper() - : m_objectManager(nullptr), m_serializeFunc(nullptr) { -} - -void JSONObjectHelper::CreateConvertFunctions(Isolate *isolate, const Local &global, ObjectManager* objectManager) { - m_objectManager = objectManager; +void JSONObjectHelper::RegisterFromFunction(Isolate *isolate, Local& jsonObject) { + if (!jsonObject->IsFunction()) { + return; + } - m_serializeFunc = new Persistent(isolate, CreateSerializeFunc(isolate)); + Isolate::Scope isolate_scope(isolate); + HandleScope handle_scope(isolate); Local context = isolate->GetCurrentContext(); + Context::Scope context_scope(context); - Local extData = External::New(isolate, this); - Local fromFunc = FunctionTemplate::New(isolate, ConvertCallbackStatic, extData)->GetFunction(context).ToLocalChecked(); - - Local jsonObjectFunc = global->Get(context, ArgConverter::ConvertToV8String(isolate, "org")) - .ToLocalChecked().As()->Get(context, ArgConverter::ConvertToV8String(isolate, "json")) - .ToLocalChecked().As()->Get(context, ArgConverter::ConvertToV8String(isolate, "JSONObject")) - .ToLocalChecked().As(); + Local jsonObjectFunc = jsonObject.As(); + auto fromKey = ArgConverter::ConvertToV8String(isolate, "from"); + if (jsonObjectFunc->Has(context, fromKey).FromMaybe(false)) { + return; + } - jsonObjectFunc->Set(context, ArgConverter::ConvertToV8String(isolate, "from"), fromFunc); + Persistent* serializeFunc = new Persistent(isolate, CreateSerializeFunc(context)); + Local extData = External::New(isolate, serializeFunc); + Local fromFunc; + bool ok = FunctionTemplate::New(isolate, ConvertCallbackStatic, extData)->GetFunction(context).ToLocal(&fromFunc); + assert(ok); + jsonObjectFunc->Set(context, fromKey, fromFunc); } void JSONObjectHelper::ConvertCallbackStatic(const FunctionCallbackInfo& info) { try { Local extData = info.Data().As(); - auto thiz = reinterpret_cast(extData->Value()); - thiz->ConvertCallback(info); + auto poSerializeFunc = reinterpret_cast*>(extData->Value()); + Isolate* isolate = info.GetIsolate(); + Local serializeFunc = poSerializeFunc->Get(isolate); + + if (info.Length() < 1) { + NativeScriptException nsEx(std::string("The \"from\" function expects one parameter")); + nsEx.ReThrowToV8(); + return; + } + + Local context = isolate->GetCurrentContext(); + + Local args[] = { info[0] }; + Local result; + TryCatch tc(isolate); + if (!serializeFunc->Call(context, Undefined(isolate), 1, args).ToLocal(&result)) { + throw NativeScriptException(tc, "Error serializing to JSONObject"); + } + + info.GetReturnValue().Set(result); } catch (NativeScriptException& e) { e.ReThrowToV8(); } catch (std::exception e) { @@ -47,28 +68,7 @@ void JSONObjectHelper::ConvertCallbackStatic(const FunctionCallbackInfo& } } -void JSONObjectHelper::ConvertCallback(const FunctionCallbackInfo& info) { - if (info.Length() < 1) { - NativeScriptException nsEx(std::string("The \"from\" function expects one parameter")); - nsEx.ReThrowToV8(); - return; - } - - Isolate* isolate = info.GetIsolate(); - Local context = isolate->GetCurrentContext(); - - Local serializeFunc = m_serializeFunc->Get(isolate); - Local args[] = { info[0] }; - Local result; - TryCatch tc(isolate); - if (!serializeFunc->Call(context, Undefined(isolate), 1, args).ToLocal(&result)) { - throw NativeScriptException(tc, "Error serializing to JSONObject"); - } - - info.GetReturnValue().Set(result); -} - -Local JSONObjectHelper::CreateSerializeFunc(Isolate* isolate) { +Local JSONObjectHelper::CreateSerializeFunc(Local context) { std::string source = "(() => function serialize(data) {" " let store;" @@ -102,7 +102,7 @@ Local JSONObjectHelper::CreateSerializeFunc(Isolate* isolate) { " }" "})()"; - Local context = isolate->GetCurrentContext(); + Isolate* isolate = context->GetIsolate(); Local