|
| 1 | +#include "NativeScriptException.h" |
| 2 | +#include "JSONObjectHelper.h" |
| 3 | +#include "ArgConverter.h" |
| 4 | +#include <sstream> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +using namespace v8; |
| 8 | +using namespace tns; |
| 9 | + |
| 10 | +JSONObjectHelper::JSONObjectHelper() |
| 11 | + : m_objectManager(nullptr), m_serializeFunc(nullptr) { |
| 12 | +} |
| 13 | + |
| 14 | +void JSONObjectHelper::CreateConvertFunctions(Isolate *isolate, const Local<Object> &global, ObjectManager* objectManager) { |
| 15 | + m_objectManager = objectManager; |
| 16 | + |
| 17 | + m_serializeFunc = new Persistent<Function>(isolate, CreateSerializeFunc(isolate)); |
| 18 | + |
| 19 | + Local<Context> context = isolate->GetCurrentContext(); |
| 20 | + |
| 21 | + Local<External> extData = External::New(isolate, this); |
| 22 | + Local<Function> fromFunc = FunctionTemplate::New(isolate, ConvertCallbackStatic, extData)->GetFunction(context).ToLocalChecked(); |
| 23 | + |
| 24 | + Local<Function> jsonObjectFunc = global->Get(context, ArgConverter::ConvertToV8String(isolate, "org")) |
| 25 | + .ToLocalChecked().As<Object>()->Get(context, ArgConverter::ConvertToV8String(isolate, "json")) |
| 26 | + .ToLocalChecked().As<Object>()->Get(context, ArgConverter::ConvertToV8String(isolate, "JSONObject")) |
| 27 | + .ToLocalChecked().As<Function>(); |
| 28 | + |
| 29 | + jsonObjectFunc->Set(context, ArgConverter::ConvertToV8String(isolate, "from"), fromFunc); |
| 30 | +} |
| 31 | + |
| 32 | +void JSONObjectHelper::ConvertCallbackStatic(const FunctionCallbackInfo<Value>& info) { |
| 33 | + try { |
| 34 | + Local<External> extData = info.Data().As<External>(); |
| 35 | + auto thiz = reinterpret_cast<JSONObjectHelper*>(extData->Value()); |
| 36 | + thiz->ConvertCallback(info); |
| 37 | + } catch (NativeScriptException& e) { |
| 38 | + e.ReThrowToV8(); |
| 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 | + } catch (...) { |
| 45 | + NativeScriptException nsEx(std::string("Error: c++ exception!")); |
| 46 | + nsEx.ReThrowToV8(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void JSONObjectHelper::ConvertCallback(const FunctionCallbackInfo<Value>& info) { |
| 51 | + if (info.Length() < 1) { |
| 52 | + NativeScriptException nsEx(std::string("The \"from\" function expects one parameter")); |
| 53 | + nsEx.ReThrowToV8(); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + Isolate* isolate = info.GetIsolate(); |
| 58 | + Local<Context> context = isolate->GetCurrentContext(); |
| 59 | + |
| 60 | + Local<Function> serializeFunc = m_serializeFunc->Get(isolate); |
| 61 | + Local<Value> args[] = { info[0] }; |
| 62 | + Local<Value> result; |
| 63 | + TryCatch tc(isolate); |
| 64 | + if (!serializeFunc->Call(context, Undefined(isolate), 1, args).ToLocal(&result)) { |
| 65 | + throw NativeScriptException(tc, "Error serializing to JSONObject"); |
| 66 | + } |
| 67 | + |
| 68 | + info.GetReturnValue().Set(result); |
| 69 | +} |
| 70 | + |
| 71 | +Local<Function> JSONObjectHelper::CreateSerializeFunc(Isolate* isolate) { |
| 72 | + std::string source = |
| 73 | + "(() => function serialize(data) {" |
| 74 | + " let store;" |
| 75 | + " switch (typeof data) {" |
| 76 | + " case \"string\":" |
| 77 | + " case \"boolean\":" |
| 78 | + " case \"number\": {" |
| 79 | + " return data;" |
| 80 | + " }" |
| 81 | + " case \"object\": {" |
| 82 | + " if (!data) {" |
| 83 | + " return null;" |
| 84 | + " }" |
| 85 | + "" |
| 86 | + " if (data instanceof Date) {" |
| 87 | + " return data.toJSON();" |
| 88 | + " }" |
| 89 | + "" |
| 90 | + " if (Array.isArray(data)) {" |
| 91 | + " store = new org.json.JSONArray();" |
| 92 | + " data.forEach((item) => store.put(serialize(item)));" |
| 93 | + " return store;" |
| 94 | + " }" |
| 95 | + "" |
| 96 | + " store = new org.json.JSONObject();" |
| 97 | + " Object.keys(data).forEach((key) => store.put(key, serialize(data[key])));" |
| 98 | + " return store;" |
| 99 | + " }" |
| 100 | + " default:" |
| 101 | + " return null;" |
| 102 | + " }" |
| 103 | + "})()"; |
| 104 | + |
| 105 | + Local<Context> context = isolate->GetCurrentContext(); |
| 106 | + |
| 107 | + Local<Script> script = Script::Compile(context, ArgConverter::ConvertToV8String(isolate, source)).ToLocalChecked(); |
| 108 | + |
| 109 | + Local<Value> result = script->Run(context).ToLocalChecked(); |
| 110 | + |
| 111 | + Local<Function> serializeFunc = result.As<Function>(); |
| 112 | + |
| 113 | + return serializeFunc; |
| 114 | +} |
0 commit comments