[njs] Improved exception creation.

Dmitry Volyntsev xeioex at nginx.com
Mon Feb 4 07:35:37 UTC 2019


details:   https://hg.nginx.org/njs/rev/83581029a022
branches:  
changeset: 754:83581029a022
user:      hongzhidao <hongzhidao at gmail.com>
date:      Mon Feb 04 05:26:42 2019 +0800
description:
Improved exception creation.

diffstat:

 njs/njs.c       |   2 +-
 njs/njs_error.c |  55 ++++++++++++++++++++++++++++++-------------------------
 njs/njs_error.h |   4 +++-
 njs/njs_vm.c    |   2 +-
 4 files changed, 35 insertions(+), 28 deletions(-)

diffs (138 lines):

diff -r d57755edf40b -r 83581029a022 njs/njs.c
--- a/njs/njs.c	Sat Feb 02 16:01:39 2019 +0800
+++ b/njs/njs.c	Mon Feb 04 05:26:42 2019 +0800
@@ -652,7 +652,7 @@ njs_vm_retval_set(njs_vm_t *vm, const nj
 nxt_noinline void
 njs_vm_memory_error(njs_vm_t *vm)
 {
-    njs_set_memory_error(vm, &vm->retval);
+    njs_memory_error_set(vm, &vm->retval);
 }
 
 
diff -r d57755edf40b -r 83581029a022 njs/njs_error.c
--- a/njs/njs_error.c	Sat Feb 02 16:01:39 2019 +0800
+++ b/njs/njs_error.c	Mon Feb 04 05:26:42 2019 +0800
@@ -15,38 +15,43 @@ static const njs_value_t  njs_error_name
 
 
 void
+njs_error_new(njs_vm_t *vm, njs_value_type_t type, u_char *start, size_t size)
+{
+    nxt_int_t     ret;
+    njs_value_t   string;
+    njs_object_t  *error;
+
+    ret = njs_string_new(vm, &string, start, size, size);
+    if (nxt_slow_path(ret != NXT_OK)) {
+        return;
+    }
+
+    error = njs_error_alloc(vm, type, NULL, &string);
+
+    if (nxt_fast_path(error != NULL)) {
+        vm->retval.data.u.object = error;
+        vm->retval.type = type;
+        vm->retval.data.truth = 1;
+    }
+}
+
+
+void
 njs_exception_error_create(njs_vm_t *vm, njs_value_type_t type,
     const char* fmt, ...)
 {
-    size_t        size;
-    va_list       args;
-    nxt_int_t     ret;
-    njs_value_t   string;
-    njs_object_t  *error;
-    u_char        buf[256], *p;
+    va_list  args;
+    u_char   buf[256], *p;
+
+    p = buf;
 
     if (fmt != NULL) {
         va_start(args, fmt);
         p = nxt_vsprintf(buf, buf + sizeof(buf), fmt, args);
         va_end(args);
-
-        size = p - buf;
-
-    } else {
-        size = 0;
     }
 
-    ret = njs_string_new(vm, &string, (const u_char *) buf, size, size);
-    if (nxt_slow_path(ret != NXT_OK)) {
-        return;
-    }
-
-    error = njs_error_alloc(vm, type, NULL, &string);
-    if (nxt_fast_path(error != NULL)) {
-        vm->retval.data.u.object = error;
-        vm->retval.type = type;
-        vm->retval.data.truth = 1;
-    }
+    njs_error_new(vm, type, buf, p - buf);
 }
 
 
@@ -467,7 +472,7 @@ const njs_object_init_t  njs_uri_error_c
 
 
 void
-njs_set_memory_error(njs_vm_t *vm, njs_value_t *value)
+njs_memory_error_set(njs_vm_t *vm, njs_value_t *value)
 {
     njs_object_t            *object;
     njs_object_prototype_t  *prototypes;
@@ -497,7 +502,7 @@ njs_set_memory_error(njs_vm_t *vm, njs_v
 void
 njs_memory_error(njs_vm_t *vm)
 {
-    njs_set_memory_error(vm, &vm->retval);
+    njs_memory_error_set(vm, &vm->retval);
 }
 
 
@@ -505,7 +510,7 @@ njs_ret_t
 njs_memory_error_constructor(njs_vm_t *vm, njs_value_t *args,
     nxt_uint_t nargs, njs_index_t unused)
 {
-    njs_set_memory_error(vm, &vm->retval);
+    njs_memory_error_set(vm, &vm->retval);
 
     return NXT_OK;
 }
diff -r d57755edf40b -r 83581029a022 njs/njs_error.h
--- a/njs/njs_error.h	Sat Feb 02 16:01:39 2019 +0800
+++ b/njs/njs_error.h	Mon Feb 04 05:26:42 2019 +0800
@@ -26,11 +26,13 @@
 #define njs_uri_error(vm, fmt, ...)                                           \
     njs_exception_error_create(vm, NJS_OBJECT_URI_ERROR, fmt, ##__VA_ARGS__)
 
+void njs_error_new(njs_vm_t *vm, njs_value_type_t type, u_char *start,
+    size_t size);
 void njs_exception_error_create(njs_vm_t *vm, njs_value_type_t type,
     const char* fmt, ...);
 
 void njs_memory_error(njs_vm_t *vm);
-void njs_set_memory_error(njs_vm_t *vm, njs_value_t *value);
+void njs_memory_error_set(njs_vm_t *vm, njs_value_t *value);
 
 njs_object_t *njs_error_alloc(njs_vm_t *vm, njs_value_type_t type,
     const njs_value_t *name, const njs_value_t *message);
diff -r d57755edf40b -r 83581029a022 njs/njs_vm.c
--- a/njs/njs_vm.c	Sat Feb 02 16:01:39 2019 +0800
+++ b/njs/njs_vm.c	Mon Feb 04 05:26:42 2019 +0800
@@ -3358,7 +3358,7 @@ njs_value_error_set(njs_vm_t *vm, njs_va
 
 memory_error:
 
-    njs_set_memory_error(vm, value);
+    njs_memory_error_set(vm, value);
 }
 
 


More information about the nginx-devel mailing list