7
7
using namespace v8 ;
8
8
using namespace tns ;
9
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;
10
+ void JSONObjectHelper::RegisterFromFunction (Isolate *isolate, Local<Value>& jsonObject) {
11
+ if (!jsonObject->IsFunction ()) {
12
+ return ;
13
+ }
16
14
17
- m_serializeFunc = new Persistent<Function>(isolate, CreateSerializeFunc (isolate));
15
+ Isolate::Scope isolate_scope (isolate);
16
+ HandleScope handle_scope (isolate);
18
17
19
18
Local<Context> context = isolate->GetCurrentContext ();
19
+ Context::Scope context_scope (context);
20
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>();
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
+ }
28
26
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);
30
33
}
31
34
32
35
void JSONObjectHelper::ConvertCallbackStatic (const FunctionCallbackInfo<Value>& info) {
33
36
try {
34
37
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);
37
58
} catch (NativeScriptException& e) {
38
59
e.ReThrowToV8 ();
39
60
} catch (std::exception e) {
@@ -47,28 +68,7 @@ void JSONObjectHelper::ConvertCallbackStatic(const FunctionCallbackInfo<Value>&
47
68
}
48
69
}
49
70
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) {
72
72
std::string source =
73
73
" (() => function serialize(data) {"
74
74
" let store;"
@@ -102,7 +102,7 @@ Local<Function> JSONObjectHelper::CreateSerializeFunc(Isolate* isolate) {
102
102
" }"
103
103
" })()" ;
104
104
105
- Local<Context> context = isolate-> GetCurrentContext ();
105
+ Isolate* isolate = context-> GetIsolate ();
106
106
107
107
Local<Script> script = Script::Compile (context, ArgConverter::ConvertToV8String (isolate, source)).ToLocalChecked ();
108
108
0 commit comments