Introduce jerry_port_track_promise_rejection (#4451)

This patch resolves #2931.

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2021-01-18 14:59:42 +01:00
committed by GitHub
parent 79a2392b79
commit c4676a21fe
8 changed files with 214 additions and 0 deletions
+55
View File
@@ -151,6 +151,32 @@ jerry_port_get_native_module (jerry_value_t name) /**< module specifier */
}
```
## Promise
```c
/**
* HostPromiseRejectionTracker operations
*/
typedef enum
{
JERRY_PROMISE_REJECTION_OPERATION_REJECT, /**< promise is rejected without any handlers */
JERRY_PROMISE_REJECTION_OPERATION_HANDLE, /**< handler is added to a rejected promise for the first time */
} jerry_promise_rejection_operation_t;
/**
* Track unhandled promise rejections.
*
* Note:
* This port function is called by jerry-core when JERRY_BUILTIN_PROMISE
* is enabled.
*
* @param promise rejected promise
* @param operation HostPromiseRejectionTracker operation
*/
void jerry_port_track_promise_rejection (const jerry_value_t promise,
const jerry_promise_rejection_operation_t operation);
```
## Date
```c
@@ -392,3 +418,32 @@ void jerry_port_sleep (uint32_t sleep_time)
} /* jerry_port_sleep */
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
```
## Promise
```c
#include "jerryscript-port.h"
/**
* Default implementation of jerry_port_track_promise_rejection.
* Prints the reason of the unhandled rejections.
*/
void
jerry_port_track_promise_rejection (const jerry_value_t promise, /**< rejected promise */
const jerry_promise_rejection_operation_t operation) /**< operation */
{
(void) operation; /* unused */
jerry_value_t reason = jerry_get_promise_result (promise);
jerry_value_t reason_to_string = jerry_value_to_string (reason);
jerry_size_t req_sz = jerry_get_utf8_string_size (reason_to_string);
jerry_char_t str_buf_p[req_sz + 1];
jerry_string_to_utf8_char_buffer (reason_to_string, str_buf_p, req_sz);
str_buf_p[req_sz] = '\0';
jerry_release_value (reason_to_string);
jerry_release_value (reason);
printf ("Uncaught (in promise) %s\n", str_buf_p);
} /* jerry_port_track_promise_rejection */
```