From cc15a8424c2f464d3a234430d9703265cc349e36 Mon Sep 17 00:00:00 2001 From: Patrick Soquet Date: Tue, 8 Mar 2022 12:52:06 +0100 Subject: [PATCH] XS: report unhandled rejection reason --- xs/sources/xsPromise.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/xs/sources/xsPromise.c b/xs/sources/xsPromise.c index f75183cd44..75a5357d9a 100644 --- a/xs/sources/xsPromise.c +++ b/xs/sources/xsPromise.c @@ -196,12 +196,14 @@ void fxNewPromiseCapabilityCallback(txMachine* the) void fxAddUnhandledRejection(txMachine* the, txSlot* promise) { + txSlot* reason = mxPromiseResult(promise); txSlot* list = &mxUnhandledPromises; txSlot** address = &list->value.reference->next; txSlot* slot; while ((slot = *address)) { if (slot->value.weakRef.target == promise) break; + slot = slot->next; address = &slot->next; } if (!slot) { @@ -211,6 +213,9 @@ void fxAddUnhandledRejection(txMachine* the, txSlot* promise) slot = *address = fxNewSlot(the); slot->kind = XS_WEAK_REF_KIND; slot->value.weakRef.target = promise; + slot = slot->next = fxNewSlot(the); + slot->kind = reason->kind; + slot->value = reason->value; } } @@ -218,19 +223,30 @@ void fxCheckUnhandledRejections(txMachine* the, txBoolean atExit) { txSlot* list = &mxUnhandledPromises; if (atExit) { - if (list->value.reference->next) + txSlot* slot = list->value.reference->next; + while (slot) { + slot = slot->next; + mxException.value = slot->value; + mxException.kind = slot->kind; fxAbort(the, XS_UNHANDLED_REJECTION_EXIT); + slot = slot->next; + } } else { txSlot** address = &list->value.reference->next; txSlot* slot; while ((slot = *address)) { if (slot->value.weakRef.target == C_NULL) { + slot = slot->next; *address = slot->next; + mxException.value = slot->value; + mxException.kind = slot->kind; fxAbort(the, XS_UNHANDLED_REJECTION_EXIT); } - else + else { + slot = slot->next; address = &slot->next; + } } } } @@ -1105,9 +1121,11 @@ void fxQueueJob(txMachine* the, txInteger count, txSlot* promise) txSlot** address = &list->value.reference->next; while ((slot = *address)) { if (slot->value.weakRef.target == promise) { + slot = slot->next; *address = slot->next; break; } + slot = slot->next; address = &slot->next; } #ifdef mxPromisePrint