From edd2f203974acc9ddfbad9998c29bdc40ccc0719 Mon Sep 17 00:00:00 2001 From: Zoltan Herczeg Date: Thu, 11 Mar 2021 19:58:44 +0100 Subject: [PATCH] Remove jerry_port_track_promise_rejection (#4613) JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com --- docs/05.PORT-API.md | 55 ------------------- .../ecma/operations/ecma-promise-object.c | 22 -------- .../ecma/operations/ecma-promise-object.h | 4 +- jerry-core/include/jerryscript-port.h | 22 -------- jerry-main/main-utils.c | 44 +++++++++++++++ jerry-port/default/CMakeLists.txt | 1 - jerry-port/default/default-promise.c | 48 ---------------- targets/esp-idf/promise.c | 45 --------------- targets/nuttx-stm32f4/jerry_port.c | 29 ---------- 9 files changed, 46 insertions(+), 224 deletions(-) delete mode 100644 jerry-port/default/default-promise.c delete mode 100644 targets/esp-idf/promise.c diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index ab058eee7..1f96482ee 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -151,32 +151,6 @@ 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 @@ -418,32 +392,3 @@ 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 */ -``` diff --git a/jerry-core/ecma/operations/ecma-promise-object.c b/jerry-core/ecma/operations/ecma-promise-object.c index 67a404acc..defa6ea70 100644 --- a/jerry-core/ecma/operations/ecma-promise-object.c +++ b/jerry-core/ecma/operations/ecma-promise-object.c @@ -181,23 +181,6 @@ ecma_is_resolver_already_called (ecma_object_t *promise_obj_p) /**< promise */ return (ecma_promise_get_flags (promise_obj_p) & ECMA_PROMISE_ALREADY_RESOLVED) != 0; } /* ecma_is_resolver_already_called */ -/** - * HostPromiseRejectionTracker - * - * See also: ES11 25.6.1.9 - */ -static void -ecma_track_promise_rejection (ecma_object_t *obj_p, /**< rejected promise */ - jerry_promise_rejection_operation_t operation) /**< operation */ -{ - JERRY_ASSERT (ecma_is_promise (obj_p)); - - if (!(ecma_promise_get_flags (obj_p) & ECMA_PROMISE_HANDLED)) - { - jerry_port_track_promise_rejection (ecma_make_object_value (obj_p), operation); - } -} /* ecma_track_promise_rejection */ - /** * Reject a Promise with a reason. * @@ -252,7 +235,6 @@ ecma_reject_promise (ecma_value_t promise, /**< promise */ promise_p->reactions = ecma_new_collection (); ecma_collection_destroy (reactions); - ecma_track_promise_rejection (obj_p, JERRY_PROMISE_REJECTION_OPERATION_REJECT); } /* ecma_reject_promise */ /** @@ -969,7 +951,6 @@ ecma_promise_do_then (ecma_value_t promise, /**< the promise which call 'then' * { /* 9. */ ecma_value_t reason = ecma_promise_get_result (promise_obj_p); - ecma_track_promise_rejection (promise_obj_p, JERRY_PROMISE_REJECTION_OPERATION_HANDLE); ecma_enqueue_promise_reaction_job (ecma_make_object_value (result_capability_obj_p), on_rejected, reason); ecma_free_value (reason); @@ -990,9 +971,6 @@ ecma_promise_do_then (ecma_value_t promise, /**< the promise which call 'then' * #endif /* JERRY_PROMISE_CALLBACK */ } - /* ES11: 11. */ - promise_p->header.u.class_prop.extra_info |= ECMA_PROMISE_HANDLED; - /* 10. */ return ecma_copy_value (capability_p->header.u.class_prop.u.promise); } /* ecma_promise_do_then */ diff --git a/jerry-core/ecma/operations/ecma-promise-object.h b/jerry-core/ecma/operations/ecma-promise-object.h index 7bd14781e..194fccaf3 100644 --- a/jerry-core/ecma/operations/ecma-promise-object.h +++ b/jerry-core/ecma/operations/ecma-promise-object.h @@ -34,9 +34,9 @@ typedef enum ECMA_PROMISE_IS_PENDING = (1 << 0), /**< pending state */ ECMA_PROMISE_IS_FULFILLED = (1 << 1), /**< fulfilled state */ ECMA_PROMISE_ALREADY_RESOLVED = (1 << 2), /**< already resolved */ - ECMA_PROMISE_HANDLED = (1 << 3), /**< ES11: 25.6.6 [[PromiseIsHandled]] internal slot */ #if JERRY_PROMISE_CALLBACK - ECMA_PROMISE_UNHANDLED_REJECT = (1 << 4), /**< a Promise is rejected without a catch handler */ + ECMA_PROMISE_UNHANDLED_REJECT = (1 << 3), /**< a Promise is rejected without a catch handler, + * related to ES11: 25.6.6 [[PromiseIsHandled]] */ #endif /* JERRY_PROMISE_CALLBACK */ } ecma_promise_flags_t; diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index 1f22ef080..8ac4414aa 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -252,28 +252,6 @@ size_t jerry_port_normalize_path (const char *in_path_p, */ jerry_value_t jerry_port_get_native_module (jerry_value_t name); -/** - * 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); - /** * @} */ diff --git a/jerry-main/main-utils.c b/jerry-main/main-utils.c index b49ed4af9..cb2c21416 100644 --- a/jerry-main/main-utils.c +++ b/jerry-main/main-utils.c @@ -201,6 +201,48 @@ create_test262 (jerry_value_t global_obj) /**< global object */ return test262_object; } /* create_test262 */ +static void +promise_callback (jerry_promise_event_type_t event_type, /**< event type */ + const jerry_value_t object, /**< target object */ + const jerry_value_t value, /**< optional argument */ + void *user_p) /**< user pointer passed to the callback */ +{ + (void) value; /* unused */ + (void) user_p; /* unused */ + const jerry_size_t max_allowed_size = 5 * 1024 - 1; + + if (event_type != JERRY_PROMISE_EVENT_REJECT_WITHOUT_HANDLER) + { + return; + } + + jerry_value_t reason = jerry_get_promise_result (object); + jerry_value_t reason_to_string = jerry_value_to_string (reason); + + if (!jerry_value_is_error (reason_to_string)) + { + jerry_size_t buffer_size = jerry_get_utf8_string_size (reason_to_string); + + if (buffer_size > max_allowed_size) + { + buffer_size = max_allowed_size; + } + + JERRY_VLA (jerry_char_t, str_buf_p, buffer_size + 1); + jerry_string_to_utf8_char_buffer (reason_to_string, str_buf_p, buffer_size); + str_buf_p[buffer_size] = '\0'; + + jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught Promise rejection: %s\n", str_buf_p); + } + else + { + jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught Promise rejection (reason cannot be converted to string)\n"); + } + + jerry_release_value (reason_to_string); + jerry_release_value (reason); +} /* promise_callback */ + /** * Inits the engine and the debugger */ @@ -209,6 +251,8 @@ main_init_engine (main_args_t *arguments_p) /**< main arguments */ { jerry_init (arguments_p->init_flags); + jerry_promise_set_callback (JERRY_PROMISE_EVENT_FILTER_ERROR, promise_callback, NULL); + if (arguments_p->option_flags & OPT_FLAG_DEBUG_SERVER) { bool protocol = false; diff --git a/jerry-port/default/CMakeLists.txt b/jerry-port/default/CMakeLists.txt index f7038e2da..e383fda47 100644 --- a/jerry-port/default/CMakeLists.txt +++ b/jerry-port/default/CMakeLists.txt @@ -27,7 +27,6 @@ set(SOURCE_PORT_DEFAULT default-fatal.c default-io.c default-module.c - default-promise.c ) # Amalgamated JerryScript source/header build. diff --git a/jerry-port/default/default-promise.c b/jerry-port/default/default-promise.c deleted file mode 100644 index c156e475c..000000000 --- a/jerry-port/default/default-promise.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#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 */ - const jerry_size_t max_allowed_size = 5 * 1024 - 1; - - jerry_value_t reason = jerry_get_promise_result (promise); - jerry_value_t reason_to_string = jerry_value_to_string (reason); - jerry_release_value (reason); - - jerry_length_t end_pos = jerry_get_utf8_string_length (reason_to_string); - jerry_size_t string_size = jerry_get_utf8_string_size (reason_to_string); - jerry_size_t buffer_size = (string_size < max_allowed_size) ? string_size : max_allowed_size; - - JERRY_VLA (jerry_char_t, str_buf_p, buffer_size + 1); - jerry_size_t copied = jerry_substring_to_utf8_char_buffer (reason_to_string, - 0, - end_pos, - str_buf_p, - buffer_size); - str_buf_p[copied] = '\0'; - - jerry_release_value (reason_to_string); - - jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught (in promise) %s\n", str_buf_p); -} /* jerry_port_track_promise_rejection */ diff --git a/targets/esp-idf/promise.c b/targets/esp-idf/promise.c deleted file mode 100644 index 543c13439..000000000 --- a/targets/esp-idf/promise.c +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright JS Foundation and other contributors, http://js.foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "jerryscript-port.h" - -/** - * 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) -{ - (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); - - jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught (in promise) %s\n", str_buf_p); -} /* jerry_port_track_promise_rejection */ diff --git a/targets/nuttx-stm32f4/jerry_port.c b/targets/nuttx-stm32f4/jerry_port.c index 39af6e9c0..bdf21f7dd 100644 --- a/targets/nuttx-stm32f4/jerry_port.c +++ b/targets/nuttx-stm32f4/jerry_port.c @@ -227,32 +227,3 @@ jerry_port_get_current_context (void) { return current_context_p; } /* jerry_port_get_current_context */ - -/** - * 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) -{ - (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); - - jerry_port_log (JERRY_LOG_LEVEL_WARNING, "Uncaught (in promise) %s\n", str_buf_p); -} /* jerry_port_track_promise_rejection */