From: Dmitry Volyntsev Date: Wed, 21 Jun 2023 23:17:42 +0000 (-0700) Subject: Added public API to throw standard exceptions. X-Git-Tag: 0.8.0~20 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=1110af1ec715926d9dec100f87bf62e71ae42f71;p=njs.git Added public API to throw standard exceptions. --- diff --git a/src/njs.h b/src/njs.h index d33d265c..accfc7cf 100644 --- a/src/njs.h +++ b/src/njs.h @@ -72,12 +72,23 @@ extern const njs_value_t njs_value_undefined; ((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_function(njs_vm_t *vm, const njs_str_t *name); 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 --git a/src/njs_error.c b/src/njs_error.c index 4cf2130e..e522e78f 100644 --- a/src/njs_error.c +++ b/src/njs_error.c @@ -28,7 +28,7 @@ static const njs_value_t njs_error_errors_string = njs_string("errors"); 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 *dst, njs_object_type_t type, 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 *dst, njs_object_type_t type, } 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_object_type_t type, const char *fmt, 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_type_t type, const char *fmt, ...) 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_value_t *dst, njs_object_type_t type, 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_t *value, njs_value_t *stack) 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_type_t type, const njs_value_t *name, 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_value_t *args, njs_uint_t nargs, } } - 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 --git a/src/njs_error.h b/src/njs_error.h index e2d6ed85..651b6229 100644 --- a/src/njs_error.h +++ b/src/njs_error.h @@ -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 --git a/src/njs_parser.c b/src/njs_parser.c index 5a26a84a..35c5bbea 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -9205,7 +9205,7 @@ njs_parser_error(njs_vm_t *vm, njs_object_type_t type, njs_str_t *file, 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 --git a/src/njs_promise.c b/src/njs_promise.c index 871f7fed..6dc3de18 100644 --- a/src/njs_promise.c +++ b/src/njs_promise.c @@ -1351,8 +1351,9 @@ njs_promise_perform_all(njs_vm_t *vm, njs_value_t *iterator, 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(njs_vm_t *vm, njs_value_t *args, 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 --git a/src/njs_vm.c b/src/njs_vm.c index dfbe2f13..b29cd403 100644 --- a/src/njs_vm.c +++ b/src/njs_vm.c @@ -733,12 +733,17 @@ njs_vm_throw(njs_vm_t *vm, const njs_value_t *value) 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 --git a/src/njs_vm.h b/src/njs_vm.h index 314f0eac..b1ecb7e2 100644 --- a/src/njs_vm.h +++ b/src/njs_vm.h @@ -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;