[njs] Added njs_date_alloc().

Dmitry Volyntsev xeioex at nginx.com
Tue Nov 2 12:41:00 UTC 2021


details:   https://hg.nginx.org/njs/rev/4ddfb2f2227f
branches:  
changeset: 1738:4ddfb2f2227f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Nov 02 12:38:42 2021 +0000
description:
Added njs_date_alloc().

diffstat:

 src/njs_date.c |  42 ++++++++++++++++++++++++++++--------------
 src/njs_date.h |   1 +
 2 files changed, 29 insertions(+), 14 deletions(-)

diffs (74 lines):

diff -r 0c5745fc200d -r 4ddfb2f2227f src/njs_date.c
--- a/src/njs_date.c	Tue Nov 02 12:38:02 2021 +0000
+++ b/src/njs_date.c	Tue Nov 02 12:38:42 2021 +0000
@@ -346,6 +346,33 @@ njs_date_args(njs_vm_t *vm, njs_value_t 
 }
 
 
+njs_date_t *
+njs_date_alloc(njs_vm_t *vm, double time)
+{
+    njs_date_t  *date;
+
+    date = njs_mp_alloc(vm->mem_pool, sizeof(njs_date_t));
+    if (njs_slow_path(date == NULL)) {
+        njs_memory_error(vm);
+        return NULL;
+    }
+
+    njs_lvlhsh_init(&date->object.hash);
+    njs_lvlhsh_init(&date->object.shared_hash);
+    date->object.type = NJS_DATE;
+    date->object.shared = 0;
+    date->object.extensible = 1;
+    date->object.error_data = 0;
+    date->object.fast_array = 0;
+    date->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_DATE].object;
+    date->object.slots = NULL;
+
+    date->time = time;
+
+    return date;
+}
+
+
 static njs_int_t
 njs_date_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
@@ -393,24 +420,11 @@ njs_date_constructor(njs_vm_t *vm, njs_v
         time = njs_make_date(tm, 1);
     }
 
-    date = njs_mp_alloc(vm->mem_pool, sizeof(njs_date_t));
+    date = njs_date_alloc(vm, time);
     if (njs_slow_path(date == NULL)) {
-        njs_memory_error(vm);
         return NJS_ERROR;
     }
 
-    njs_lvlhsh_init(&date->object.hash);
-    njs_lvlhsh_init(&date->object.shared_hash);
-    date->object.type = NJS_DATE;
-    date->object.shared = 0;
-    date->object.extensible = 1;
-    date->object.error_data = 0;
-    date->object.fast_array = 0;
-    date->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_DATE].object;
-    date->object.slots = NULL;
-
-    date->time = time;
-
     njs_set_date(&vm->retval, date);
 
     return NJS_OK;
diff -r 0c5745fc200d -r 4ddfb2f2227f src/njs_date.h
--- a/src/njs_date.h	Tue Nov 02 12:38:02 2021 +0000
+++ b/src/njs_date.h	Tue Nov 02 12:38:42 2021 +0000
@@ -8,6 +8,7 @@
 #define _NJS_DATE_H_INCLUDED_
 
 
+njs_date_t *njs_date_alloc(njs_vm_t *vm, double time);
 njs_int_t njs_date_to_string(njs_vm_t *vm, njs_value_t *retval,
     const njs_value_t *date);
 


More information about the nginx-devel mailing list