details: https://hg.nginx.org/njs/rev/09441c1c24ea branches: changeset: 2157:09441c1c24ea user: Dmitry Volyntsev <xei...@nginx.com> date: Wed Jun 21 16:17:42 2023 -0700 description: Added public API to throw standard exceptions.
diffstat: src/njs.h | 26 +++++++++++++++++++------- src/njs_error.c | 18 +++++++++--------- src/njs_error.h | 6 +++--- src/njs_parser.c | 2 +- src/njs_promise.c | 10 ++++++---- src/njs_vm.c | 9 +++++++-- src/njs_vm.h | 1 + 7 files changed, 46 insertions(+), 26 deletions(-) diffs (226 lines): diff -r c2bc1525be25 -r 09441c1c24ea src/njs.h --- a/src/njs.h Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs.h Wed Jun 21 16:17:42 2023 -0700 @@ -72,12 +72,23 @@ extern const njs_value_t njs_ ((n < nargs) ? njs_argument(args, n) \ : (njs_value_assign(lvalue, &njs_value_undefined), lvalue)) -#define njs_vm_log(vm, fmt, ...) njs_vm_logger(vm, NJS_LOG_LEVEL_INFO, fmt, \ - ##__VA_ARGS__) -#define njs_vm_warn(vm, fmt, ...) njs_vm_logger(vm, NJS_LOG_LEVEL_WARN, fmt, \ - ##__VA_ARGS__) -#define njs_vm_err(vm, fmt, ...) njs_vm_logger(vm, NJS_LOG_LEVEL_ERROR, fmt, \ - ##__VA_ARGS__) +#define njs_vm_log(vm, fmt, ...) \ + njs_vm_logger(vm, NJS_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) +#define njs_vm_warn(vm, fmt, ...) \ + njs_vm_logger(vm, NJS_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) +#define njs_vm_err(vm, fmt, ...) \ + njs_vm_logger(vm, NJS_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) + +#define njs_vm_error(vm, fmt, ...) \ + njs_vm_error2(vm, 0, fmt, ##__VA_ARGS__) +#define njs_vm_internal_error(vm, fmt, ...) \ + njs_vm_error2(vm, 2, fmt, ##__VA_ARGS__) +#define njs_vm_range_error(vm, fmt, ...) \ + njs_vm_error2(vm, 3, fmt, ##__VA_ARGS__) +#define njs_vm_syntax_error(vm, fmt, ...) \ + njs_vm_error2(vm, 5, fmt, ##__VA_ARGS__) +#define njs_vm_type_error(vm, fmt, ...) \ + njs_vm_error2(vm, 6, fmt, ##__VA_ARGS__) #define njs_deprecated(vm, text) \ do { \ @@ -419,7 +430,8 @@ NJS_EXPORT njs_function_t *njs_vm_functi NJS_EXPORT njs_bool_t njs_vm_constructor(njs_vm_t *vm); NJS_EXPORT void njs_vm_throw(njs_vm_t *vm, const njs_value_t *value); -NJS_EXPORT void njs_vm_error(njs_vm_t *vm, const char *fmt, ...); +NJS_EXPORT void njs_vm_error2(njs_vm_t *vm, unsigned type, const char *fmt, + ...); NJS_EXPORT void njs_vm_exception_get(njs_vm_t *vm, njs_value_t *retval); NJS_EXPORT njs_mp_t *njs_vm_memory_pool(njs_vm_t *vm); NJS_EXPORT njs_external_ptr_t njs_vm_external_ptr(njs_vm_t *vm); diff -r c2bc1525be25 -r 09441c1c24ea src/njs_error.c --- a/src/njs_error.c Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs_error.c Wed Jun 21 16:17:42 2023 -0700 @@ -28,7 +28,7 @@ static const njs_value_t njs_error_erro void -njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type, +njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_t *proto, u_char *start, size_t size) { ssize_t length; @@ -46,7 +46,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t return; } - error = njs_error_alloc(vm, type, NULL, &string, NULL); + error = njs_error_alloc(vm, proto, NULL, &string, NULL); if (njs_slow_path(error == NULL)) { return; } @@ -55,7 +55,7 @@ njs_error_new(njs_vm_t *vm, njs_value_t } void -njs_throw_error_va(njs_vm_t *vm, njs_object_type_t type, const char *fmt, +njs_throw_error_va(njs_vm_t *vm, njs_object_t *proto, const char *fmt, va_list args) { u_char buf[NJS_MAX_ERROR_STR], *p; @@ -66,7 +66,7 @@ njs_throw_error_va(njs_vm_t *vm, njs_obj p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args); } - njs_error_new(vm, &vm->exception, type, buf, p - buf); + njs_error_new(vm, &vm->exception, proto, buf, p - buf); } @@ -76,7 +76,7 @@ njs_throw_error(njs_vm_t *vm, njs_object va_list args; va_start(args, fmt); - njs_throw_error_va(vm, type, fmt, args); + njs_throw_error_va(vm, &vm->prototypes[type].object, fmt, args); va_end(args); } @@ -96,7 +96,7 @@ njs_error_fmt_new(njs_vm_t *vm, njs_valu va_end(args); } - njs_error_new(vm, dst, type, buf, p - buf); + njs_error_new(vm, dst, &vm->prototypes[type].object, buf, p - buf); } @@ -202,7 +202,7 @@ njs_error_stack(njs_vm_t *vm, njs_value_ njs_object_t * -njs_error_alloc(njs_vm_t *vm, njs_object_type_t type, const njs_value_t *name, +njs_error_alloc(njs_vm_t *vm, njs_object_t *proto, const njs_value_t *name, const njs_value_t *message, const njs_value_t *errors) { njs_int_t ret; @@ -223,7 +223,7 @@ njs_error_alloc(njs_vm_t *vm, njs_object error->fast_array = 0; error->error_data = 1; error->stack_attached = 0; - error->__proto__ = &vm->prototypes[type].object; + error->__proto__ = proto; error->slots = NULL; lhq.replace = 0; @@ -339,7 +339,7 @@ njs_error_constructor(njs_vm_t *vm, njs_ } } - error = njs_error_alloc(vm, type, NULL, + error = njs_error_alloc(vm, &vm->prototypes[type].object, NULL, njs_is_defined(value) ? value : NULL, njs_is_defined(&list) ? &list : NULL); if (njs_slow_path(error == NULL)) { diff -r c2bc1525be25 -r 09441c1c24ea src/njs_error.h --- a/src/njs_error.h Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs_error.h Wed Jun 21 16:17:42 2023 -0700 @@ -25,19 +25,19 @@ #define njs_uri_error(vm, fmt, ...) \ njs_throw_error(vm, NJS_OBJ_TYPE_URI_ERROR, fmt, ##__VA_ARGS__) -void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type, +void njs_error_new(njs_vm_t *vm, njs_value_t *dst, njs_object_t *proto, u_char *start, size_t size); void njs_noinline njs_error_fmt_new(njs_vm_t *vm, njs_value_t *dst, njs_object_type_t type, const char *fmt, ...); void njs_throw_error(njs_vm_t *vm, njs_object_type_t type, const char *fmt, ...); -void njs_throw_error_va(njs_vm_t *vm, njs_object_type_t type, const char *fmt, +void njs_throw_error_va(njs_vm_t *vm, njs_object_t *proto, const char *fmt, va_list args); void njs_memory_error(njs_vm_t *vm); void njs_memory_error_set(njs_vm_t *vm, njs_value_t *value); -njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_object_type_t type, +njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_object_t *proto, const njs_value_t *name, const njs_value_t *message, const njs_value_t *errors); njs_int_t njs_error_to_string(njs_vm_t *vm, njs_value_t *retval, diff -r c2bc1525be25 -r 09441c1c24ea src/njs_parser.c --- a/src/njs_parser.c Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs_parser.c Wed Jun 21 16:17:42 2023 -0700 @@ -9205,7 +9205,7 @@ njs_parser_error(njs_vm_t *vm, njs_objec p = njs_sprintf(p, end, " in %uD", line); } - njs_error_new(vm, &error, type, msg, p - msg); + njs_error_new(vm, &error, &vm->prototypes[type].object, msg, p - msg); njs_set_number(&value, line); njs_value_property_set(vm, &error, njs_value_arg(&line_number), &value); diff -r c2bc1525be25 -r 09441c1c24ea src/njs_promise.c --- a/src/njs_promise.c Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs_promise.c Wed Jun 21 16:17:42 2023 -0700 @@ -1351,8 +1351,9 @@ njs_promise_perform_all(njs_vm_t *vm, nj njs_set_array(&argument, pargs->args.data); if (handler == njs_promise_perform_any_handler) { - error = njs_error_alloc(vm, NJS_OBJ_TYPE_AGGREGATE_ERROR, - NULL, &string_any_rejected, &argument); + error = njs_error_alloc(vm, + &vm->prototypes[NJS_OBJ_TYPE_AGGREGATE_ERROR].object, + NULL, &string_any_rejected, &argument); if (njs_slow_path(error == NULL)) { return NJS_ERROR; } @@ -1728,8 +1729,9 @@ njs_promise_any_reject_element_functions if (--(*context->remaining_elements) == 0) { njs_mp_free(vm->mem_pool, context->remaining_elements); - error = njs_error_alloc(vm, NJS_OBJ_TYPE_AGGREGATE_ERROR, - NULL, &string_any_rejected, &arr_value); + error = njs_error_alloc(vm, + &vm->prototypes[NJS_OBJ_TYPE_AGGREGATE_ERROR].object, + NULL, &string_any_rejected, &arr_value); if (njs_slow_path(error == NULL)) { return NJS_ERROR; } diff -r c2bc1525be25 -r 09441c1c24ea src/njs_vm.c --- a/src/njs_vm.c Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs_vm.c Wed Jun 21 16:17:42 2023 -0700 @@ -733,12 +733,17 @@ njs_vm_throw(njs_vm_t *vm, const njs_val void -njs_vm_error(njs_vm_t *vm, const char *fmt, ...) +njs_vm_error2(njs_vm_t *vm, unsigned type, const char *fmt, ...) { va_list args; + if (type > (NJS_OBJ_TYPE_ERROR_MAX - NJS_OBJ_TYPE_ERROR)) { + return; + } + va_start(args, fmt); - njs_throw_error_va(vm, NJS_OBJ_TYPE_ERROR, fmt, args); + type += NJS_OBJ_TYPE_ERROR; + njs_throw_error_va(vm, &vm->prototypes[type].object, fmt, args); va_end(args); } diff -r c2bc1525be25 -r 09441c1c24ea src/njs_vm.h --- a/src/njs_vm.h Sun Jun 11 00:43:33 2023 +0200 +++ b/src/njs_vm.h Wed Jun 21 16:17:42 2023 -0700 @@ -76,6 +76,7 @@ typedef enum { NJS_OBJ_TYPE_URI_ERROR, NJS_OBJ_TYPE_MEMORY_ERROR, NJS_OBJ_TYPE_AGGREGATE_ERROR, +#define NJS_OBJ_TYPE_ERROR_MAX (NJS_OBJ_TYPE_AGGREGATE_ERROR) NJS_OBJ_TYPE_MAX, } njs_object_type_t; _______________________________________________ nginx-devel mailing list nginx-devel@nginx.org https://mailman.nginx.org/mailman/listinfo/nginx-devel