77using namespace v8 ;
88using namespace tns ;
99
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;
10+ void JSONObjectHelper::RegisterFromFunction (Isolate *isolate, Local<Value>& jsonObject) {
11+ if (!jsonObject->IsFunction ()) {
12+ return ;
13+ }
1614
17- m_serializeFunc = new Persistent<Function>(isolate, CreateSerializeFunc (isolate));
15+ Isolate::Scope isolate_scope (isolate);
16+ HandleScope handle_scope (isolate);
1817
1918 Local<Context> context = isolate->GetCurrentContext ();
19+ Context::Scope context_scope (context);
2020
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>();
21+ Local<Function> jsonObjectFunc = jsonObject.As <Function>();
22+ auto fromKey = ArgConverter::ConvertToV8String (isolate, " from" );
23+ if (jsonObjectFunc->Has (context, fromKey).FromMaybe (false )) {
24+ return ;
25+ }
2826
29- jsonObjectFunc->Set (context, ArgConverter::ConvertToV8String (isolate, " from" ), fromFunc);
27+ Persistent<Function>* serializeFunc = new Persistent<Function>(isolate, CreateSerializeFunc (context));
28+ Local<External> extData = External::New (isolate, serializeFunc);
29+ Local<Function> fromFunc;
30+ bool ok = FunctionTemplate::New (isolate, ConvertCallbackStatic, extData)->GetFunction (context).ToLocal (&fromFunc);
31+ assert (ok);
32+ jsonObjectFunc->Set (context, fromKey, fromFunc);
3033}
3134
3235void JSONObjectHelper::ConvertCallbackStatic (const FunctionCallbackInfo<Value>& info) {
3336 try {
3437 Local<External> extData = info.Data ().As <External>();
35- auto thiz = reinterpret_cast <JSONObjectHelper*>(extData->Value ());
36- thiz->ConvertCallback (info);
38+ auto poSerializeFunc = reinterpret_cast <Persistent<Function>*>(extData->Value ());
39+ Isolate* isolate = info.GetIsolate ();
40+ Local<Function> serializeFunc = poSerializeFunc->Get (isolate);
41+
42+ if (info.Length () < 1 ) {
43+ NativeScriptException nsEx (std::string (" The \" from\" function expects one parameter" ));
44+ nsEx.ReThrowToV8 ();
45+ return ;
46+ }
47+
48+ Local<Context> context = isolate->GetCurrentContext ();
49+
50+ Local<Value> args[] = { info[0 ] };
51+ Local<Value> result;
52+ TryCatch tc (isolate);
53+ if (!serializeFunc->Call (context, Undefined (isolate), 1 , args).ToLocal (&result)) {
54+ throw NativeScriptException (tc, " Error serializing to JSONObject" );
55+ }
56+
57+ info.GetReturnValue ().Set (result);
3758 } catch (NativeScriptException& e) {
3859 e.ReThrowToV8 ();
3960 } catch (std::exception e) {
@@ -47,28 +68,7 @@ void JSONObjectHelper::ConvertCallbackStatic(const FunctionCallbackInfo<Value>&
4768 }
4869}
4970
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) {
71+ Local<Function> JSONObjectHelper::CreateSerializeFunc (Local<Context> context) {
7272 std::string source =
7373 " (() => function serialize(data) {"
7474 " let store;"
@@ -102,7 +102,7 @@ Local<Function> JSONObjectHelper::CreateSerializeFunc(Isolate* isolate) {
102102 " }"
103103 " })()" ;
104104
105- Local<Context> context = isolate-> GetCurrentContext ();
105+ Isolate* isolate = context-> GetIsolate ();
106106
107107 Local<Script> script = Script::Compile (context, ArgConverter::ConvertToV8String (isolate, source)).ToLocalChecked ();
108108
0 commit comments