File tree 4 files changed +17
-18
lines changed
test-app/runtime/src/main/cpp
4 files changed +17
-18
lines changed Original file line number Diff line number Diff line change @@ -58,13 +58,6 @@ void CallbackHandlers::Init(Isolate* isolate) {
58
58
59
59
assert (INIT_WORKER_METHOD_ID != nullptr );
60
60
61
- Local<Object> json = isolate->GetCurrentContext ()->Global ()->Get (String::NewFromUtf8 (isolate, " JSON" ))->ToObject ();
62
- Local<Function> stringify = json->Get (String::NewFromUtf8 (isolate, " stringify" )).As <Function>();
63
-
64
- auto persistentStringify = new Persistent<Function>(isolate, stringify);
65
-
66
- isolateToJsonStringify.insert ({isolate, persistentStringify});
67
-
68
61
MetadataNode::Init (isolate);
69
62
70
63
MethodCache::Init ();
@@ -1381,8 +1374,6 @@ void CallbackHandlers::TerminateWorkerThread(Isolate* isolate) {
1381
1374
int CallbackHandlers::nextWorkerId = 0 ;
1382
1375
std::map<int , Persistent<Object>*> CallbackHandlers::id2WorkerMap;
1383
1376
1384
- std::map<Isolate*, Persistent<Function>*> CallbackHandlers::isolateToJsonStringify;
1385
-
1386
1377
short CallbackHandlers::MAX_JAVA_STRING_ARRAY_LENGTH = 100 ;
1387
1378
jclass CallbackHandlers::RUNTIME_CLASS = nullptr ;
1388
1379
jclass CallbackHandlers::JAVA_LANG_STRING = nullptr ;
Original file line number Diff line number Diff line change @@ -26,8 +26,6 @@ class CallbackHandlers {
26
26
*/
27
27
static std::map<int , v8::Persistent<v8::Object>*> id2WorkerMap;
28
28
29
- static std::map<v8::Isolate*, v8::Persistent<v8::Function>*> isolateToJsonStringify;
30
-
31
29
static int nextWorkerId;
32
30
33
31
static void Init (v8::Isolate* isolate);
Original file line number Diff line number Diff line change @@ -19,17 +19,22 @@ string tns::ConvertToString(const Local<v8::String>& s) {
19
19
}
20
20
21
21
Local<String> tns::JsonStringifyObject (Isolate* isolate, Handle <v8::Value> value) {
22
+ v8::HandleScope scope (isolate);
23
+
22
24
if (value.IsEmpty ()) {
23
25
return String::Empty (isolate);
24
26
}
25
27
26
- auto stringifyFunction = CallbackHandlers::isolateToJsonStringify. find (isolate)-> second ;
27
- auto func = Local<Function>:: New (isolate, *stringifyFunction) ;
28
- Local<Value> args[] = { value } ;
28
+ v8::Local<v8::String> resultString ;
29
+ v8::TryCatch tc ;
30
+ auto success = v8::JSON::Stringify (isolate-> GetCurrentContext (), value-> ToObject (isolate)). ToLocal (&resultString) ;
29
31
30
- auto result = func->Call (Undefined (isolate), 1 , args);
32
+ if (!success && tc.HasCaught ()) {
33
+ auto message = tc.Message ()->Get ();
34
+ resultString = v8::String::Concat (ArgConverter::ConvertToV8String (isolate, " Couldn't convert object to a JSON string: " ), message);
35
+ }
31
36
32
- return result-> ToString (isolate) ;
37
+ return resultString ;
33
38
}
34
39
35
40
jstring tns::ConvertToJavaString (const Local<Value>& value) {
Original file line number Diff line number Diff line change @@ -188,9 +188,14 @@ void Console::dirCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
188
188
if (propIsFunction) {
189
189
ss << " ()" ;
190
190
} else if (propertyValue->IsObject ()) {
191
- ss << " : " << ArgConverter::ConvertToString (JsonStringifyObject (isolate, propertyValue));
191
+ std::string jsonStringifiedObject = ArgConverter::ConvertToString (JsonStringifyObject (isolate, propertyValue));
192
+ // if object prints out as the error string for circular references, replace with #CR instead for brevity
193
+ if (jsonStringifiedObject.find (" circular structure" ) != std::string::npos) {
194
+ jsonStringifiedObject = " #CR" ;
195
+ }
196
+ ss << " : " << jsonStringifiedObject;
192
197
} else {
193
- ss << " : " << ArgConverter::ConvertToString (propertyValue->ToDetailString (isolate));
198
+ ss << " : \" " << ArgConverter::ConvertToString (propertyValue->ToDetailString (isolate)) << " \" " ;
194
199
}
195
200
196
201
ss << std::endl;
You can’t perform that action at this time.
0 commit comments