Skip to content

Commit 1b405c7

Browse files
committed
Parse JSON in JavaScript
1 parent 912d671 commit 1b405c7

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

module.cc

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,13 @@ void CaptureStackTraces(const FunctionCallbackInfo<Value> &args) {
467467
capture_from_isolate, result.poll_state.c_str(),
468468
NewStringType::kNormal);
469469
if (!stateStr.IsEmpty()) {
470-
v8::MaybeLocal<v8::Value> maybeStateVal =
471-
v8::JSON::Parse(current_context, stateStr.ToLocalChecked());
472-
v8::Local<v8::Value> stateVal;
473-
if (maybeStateVal.ToLocal(&stateVal)) {
474-
threadObj
475-
->Set(current_context,
476-
String::NewFromUtf8(capture_from_isolate, "pollState",
477-
NewStringType::kInternalized)
478-
.ToLocalChecked(),
479-
stateVal)
480-
.Check();
481-
}
470+
threadObj
471+
->Set(current_context,
472+
String::NewFromUtf8(capture_from_isolate, "pollState",
473+
NewStringType::kInternalized)
474+
.ToLocalChecked(),
475+
stateStr.ToLocalChecked())
476+
.Check();
482477
}
483478
}
484479

@@ -487,18 +482,13 @@ void CaptureStackTraces(const FunctionCallbackInfo<Value> &args) {
487482
capture_from_isolate, result.stack_trace.async_state.c_str(),
488483
NewStringType::kNormal);
489484
if (!stateStr.IsEmpty()) {
490-
v8::MaybeLocal<v8::Value> maybeStateVal =
491-
v8::JSON::Parse(current_context, stateStr.ToLocalChecked());
492-
v8::Local<v8::Value> stateVal;
493-
if (maybeStateVal.ToLocal(&stateVal)) {
494-
threadObj
495-
->Set(current_context,
496-
String::NewFromUtf8(capture_from_isolate, "asyncState",
497-
NewStringType::kInternalized)
498-
.ToLocalChecked(),
499-
stateVal)
500-
.Check();
501-
}
485+
threadObj
486+
->Set(current_context,
487+
String::NewFromUtf8(capture_from_isolate, "asyncState",
488+
NewStringType::kInternalized)
489+
.ToLocalChecked(),
490+
stateStr.ToLocalChecked())
491+
.Check();
502492
}
503493
}
504494

src/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface Native {
4949
registerThread(threadName: string): void;
5050
registerThread(storage: AsyncStorageArgs, threadName: string): void;
5151
threadPoll(enableLastSeen?: boolean, pollState?: object): void;
52-
captureStackTrace<A = unknown, P = unknown>(): Record<string, Thread<A, P>>;
52+
captureStackTrace(): Record<string, Thread<string, string>>;
5353
getThreadsLastSeen(): Record<string, number>;
5454
}
5555

@@ -230,7 +230,20 @@ export function threadPoll(enableLastSeen: boolean = true, pollState?: object):
230230
* Captures stack traces for all registered threads.
231231
*/
232232
export function captureStackTrace<A = unknown, P = unknown>(): Record<string, Thread<A, P>> {
233-
return native.captureStackTrace<A, P>();
233+
const result = native.captureStackTrace();
234+
235+
// Parse the asyncState and pollState from JSON strings back into objects
236+
const transformedResult: Record<string, Thread<A, P>> = {};
237+
for (const [key, value] of Object.entries(result)) {
238+
const thread: Thread<A, P> = {
239+
frames: value.frames,
240+
...(value.asyncState && { asyncState: JSON.parse(value.asyncState) }),
241+
...(value.pollState && { pollState: JSON.parse(value.pollState) }),
242+
};
243+
transformedResult[key] = thread;
244+
}
245+
246+
return transformedResult;
234247
}
235248

236249
/**

0 commit comments

Comments
 (0)