Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the built-in JSON.stringify for cross workers communication #1411

Merged
merged 1 commit into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
The application crashed because of an uncaught exception. You can look at "stackTrace" or "nativeException" for more detailed information about the exception.
```

- [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.

5.4.0
==

Expand Down
2 changes: 1 addition & 1 deletion test-app/app/src/main/assets/app/shared
Submodule shared updated 1 files
+50 −23 Workers/index.js
4 changes: 2 additions & 2 deletions test-app/runtime/src/main/cpp/CallbackHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ CallbackHandlers::WorkerObjectPostMessageCallback(const v8::FunctionCallbackInfo
ArgConverter::ConvertToV8String(isolate, "workerId"),
jsId);

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

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

JEnv env;
auto mId = env.GetStaticMethodID(RUNTIME_CLASS, "sendMessageFromWorkerToMain",
Expand Down
27 changes: 14 additions & 13 deletions test-app/runtime/src/main/cpp/V8GlobalHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,35 @@ Local<Function> GetSmartJSONStringifyFunction(Isolate* isolate) {
return smartStringifyPersistentFunction->Get(isolate);
}

Local<String> tns::JsonStringifyObject(Isolate* isolate, Handle<v8::Value> value) {
Local<String> tns::JsonStringifyObject(Isolate* isolate, Handle<v8::Value> value, bool handleCircularReferences) {
if (value.IsEmpty()) {
return String::Empty(isolate);
}

Local<Function> smartJSONStringifyFunction = GetSmartJSONStringifyFunction(isolate);
if (handleCircularReferences) {
Local<Function> smartJSONStringifyFunction = GetSmartJSONStringifyFunction(isolate);

if (!smartJSONStringifyFunction.IsEmpty()) {
if (value->IsObject()) {
v8::Local<v8::Value> resultValue;
v8::TryCatch tc(isolate);
if (!smartJSONStringifyFunction.IsEmpty()) {
if (value->IsObject()) {
v8::Local<v8::Value> resultValue;
v8::TryCatch tc(isolate);

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

if (success && !tc.HasCaught()) {
return resultValue->ToString(isolate);
if (success && !tc.HasCaught()) {
return resultValue->ToString(isolate);
}
}
}
}

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

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

return resultString;
Expand Down
2 changes: 1 addition & 1 deletion test-app/runtime/src/main/cpp/V8GlobalHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <map>

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

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

Expand Down