Skip to content

Commit ef2491b

Browse files
authored
Merge pull request #1411 from NativeScript/darind/workers-json-stringify
Use the built-in JSON.stringify for cross workers communication
2 parents 6e534ba + 8fcbaf5 commit ef2491b

File tree

5 files changed

+20
-17
lines changed

5 files changed

+20
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
The application crashed because of an uncaught exception. You can look at "stackTrace" or "nativeException" for more detailed information about the exception.
2626
```
2727
28+
- [The built-in `JSON.stringify` method is used for cross workers communication](https://github.com/NativeScript/android-runtime/issues/1408). Circular object references are no longer supported and attempting to send such object will throw an exception.
29+
2830
5.4.0
2931
==
3032

test-app/runtime/src/main/cpp/CallbackHandlers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ CallbackHandlers::WorkerObjectPostMessageCallback(const v8::FunctionCallbackInfo
992992
ArgConverter::ConvertToV8String(isolate, "workerId"),
993993
jsId);
994994

995-
Local<String> msg = tns::JsonStringifyObject(isolate, args[0])->ToString(isolate);
995+
Local<String> msg = tns::JsonStringifyObject(isolate, args[0], false);
996996
auto context = isolate->GetCurrentContext();
997997
// get worker's ID that is associated on the other side - in Java
998998
auto id = jsId->Int32Value(context).ToChecked();
@@ -1090,7 +1090,7 @@ CallbackHandlers::WorkerGlobalPostMessageCallback(const v8::FunctionCallbackInfo
10901090
return;
10911091
}
10921092

1093-
Local<String> msg = tns::JsonStringifyObject(isolate, args[0])->ToString(isolate);
1093+
Local<String> msg = tns::JsonStringifyObject(isolate, args[0], false);
10941094

10951095
JEnv env;
10961096
auto mId = env.GetStaticMethodID(RUNTIME_CLASS, "sendMessageFromWorkerToMain",

test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,35 @@ Local<Function> GetSmartJSONStringifyFunction(Isolate* isolate) {
6666
return smartStringifyPersistentFunction->Get(isolate);
6767
}
6868

69-
Local<String> tns::JsonStringifyObject(Isolate* isolate, Handle<v8::Value> value) {
69+
Local<String> tns::JsonStringifyObject(Isolate* isolate, Handle<v8::Value> value, bool handleCircularReferences) {
7070
if (value.IsEmpty()) {
7171
return String::Empty(isolate);
7272
}
7373

74-
Local<Function> smartJSONStringifyFunction = GetSmartJSONStringifyFunction(isolate);
74+
if (handleCircularReferences) {
75+
Local<Function> smartJSONStringifyFunction = GetSmartJSONStringifyFunction(isolate);
7576

76-
if (!smartJSONStringifyFunction.IsEmpty()) {
77-
if (value->IsObject()) {
78-
v8::Local<v8::Value> resultValue;
79-
v8::TryCatch tc(isolate);
77+
if (!smartJSONStringifyFunction.IsEmpty()) {
78+
if (value->IsObject()) {
79+
v8::Local<v8::Value> resultValue;
80+
v8::TryCatch tc(isolate);
8081

81-
Local<Value> args[] = { value->ToObject(isolate) };
82-
auto success = smartJSONStringifyFunction->Call(isolate->GetCurrentContext(), Undefined(isolate), 1, args).ToLocal(&resultValue);
82+
Local<Value> args[] = { value->ToObject(isolate) };
83+
auto success = smartJSONStringifyFunction->Call(isolate->GetCurrentContext(), Undefined(isolate), 1, args).ToLocal(&resultValue);
8384

84-
if (success && !tc.HasCaught()) {
85-
return resultValue->ToString(isolate);
85+
if (success && !tc.HasCaught()) {
86+
return resultValue->ToString(isolate);
87+
}
8688
}
8789
}
8890
}
8991

9092
v8::Local<v8::String> resultString;
9193
v8::TryCatch tc(isolate);
92-
auto success = v8::JSON::Stringify(isolate->GetCurrentContext(), value->ToObject(isolate), ArgConverter::ConvertToV8String(isolate, "2")).ToLocal(&resultString);
94+
auto success = v8::JSON::Stringify(isolate->GetCurrentContext(), value->ToObject(isolate)).ToLocal(&resultString);
9395

9496
if (!success && tc.HasCaught()) {
95-
auto message = tc.Message()->Get();
96-
resultString = v8::String::Concat(isolate, ArgConverter::ConvertToV8String(isolate, "Couldn't convert object to a JSON string: "), message);
97+
throw NativeScriptException(tc);
9798
}
9899

99100
return resultString;

test-app/runtime/src/main/cpp/V8GlobalHelpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <map>
99

1010
namespace tns {
11-
v8::Local<v8::String> JsonStringifyObject(v8::Isolate* isolate, v8::Handle<v8::Value> value);
11+
v8::Local<v8::String> JsonStringifyObject(v8::Isolate* isolate, v8::Handle<v8::Value> value, bool handleCircularReferences = true);
1212

1313
bool V8GetPrivateValue(v8::Isolate* isolate, const v8::Local<v8::Object>& obj, const v8::Local<v8::String>& propName, v8::Local<v8::Value>& out);
1414

0 commit comments

Comments
 (0)