[njs] Introduced njs_vm_bind_handler().

Dmitry Volyntsev xeioex at nginx.com
Fri Jun 30 03:39:25 UTC 2023


details:   https://hg.nginx.org/njs/rev/7aa15ae3d8be
branches:  
changeset: 2168:7aa15ae3d8be
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Jun 29 18:58:45 2023 -0700
description:
Introduced njs_vm_bind_handler().

diffstat:

 src/njs.h    |   3 +++
 src/njs_vm.c |  49 +++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 44 insertions(+), 8 deletions(-)

diffs (86 lines):

diff -r bc69e9407e66 -r 7aa15ae3d8be src/njs.h
--- a/src/njs.h	Wed Jun 28 22:15:59 2023 -0700
+++ b/src/njs.h	Thu Jun 29 18:58:45 2023 -0700
@@ -424,6 +424,9 @@ NJS_EXPORT void njs_disassembler(njs_vm_
 
 NJS_EXPORT njs_int_t njs_vm_bind(njs_vm_t *vm, const njs_str_t *var_name,
     const njs_value_t *value, njs_bool_t shared);
+njs_int_t njs_vm_bind_handler(njs_vm_t *vm, const njs_str_t *var_name,
+    njs_prop_handler_t handler, uint16_t magic16, uint32_t magic32,
+    njs_bool_t shared);
 NJS_EXPORT njs_int_t njs_vm_value(njs_vm_t *vm, const njs_str_t *path,
     njs_value_t *retval);
 NJS_EXPORT njs_function_t *njs_vm_function(njs_vm_t *vm, const njs_str_t *name);
diff -r bc69e9407e66 -r 7aa15ae3d8be src/njs_vm.c
--- a/src/njs_vm.c	Wed Jun 28 22:15:59 2023 -0700
+++ b/src/njs_vm.c	Thu Jun 29 18:58:45 2023 -0700
@@ -792,21 +792,15 @@ njs_vm_value(njs_vm_t *vm, const njs_str
 }
 
 
-njs_int_t
-njs_vm_bind(njs_vm_t *vm, const njs_str_t *var_name, const njs_value_t *value,
+static njs_int_t
+njs_vm_bind2(njs_vm_t *vm, const njs_str_t *var_name, njs_object_prop_t *prop,
     njs_bool_t shared)
 {
     njs_int_t           ret;
     njs_object_t        *global;
     njs_lvlhsh_t        *hash;
-    njs_object_prop_t   *prop;
     njs_lvlhsh_query_t  lhq;
 
-    prop = njs_object_prop_alloc(vm, &njs_value_undefined, value, 1);
-    if (njs_slow_path(prop == NULL)) {
-        return NJS_ERROR;
-    }
-
     ret = njs_string_new(vm, &prop->name, var_name->start, var_name->length, 0);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
@@ -832,6 +826,45 @@ njs_vm_bind(njs_vm_t *vm, const njs_str_
 }
 
 
+njs_int_t
+njs_vm_bind(njs_vm_t *vm, const njs_str_t *var_name, const njs_value_t *value,
+    njs_bool_t shared)
+{
+    njs_object_prop_t   *prop;
+
+    prop = njs_object_prop_alloc(vm, &njs_value_undefined, value, 1);
+    if (njs_slow_path(prop == NULL)) {
+        return NJS_ERROR;
+    }
+
+    return njs_vm_bind2(vm, var_name, prop, shared);
+}
+
+
+njs_int_t
+njs_vm_bind_handler(njs_vm_t *vm, const njs_str_t *var_name,
+    njs_prop_handler_t handler, uint16_t magic16, uint32_t magic32,
+    njs_bool_t shared)
+{
+    njs_object_prop_t  *prop;
+
+    prop = njs_object_prop_alloc(vm, &njs_string_empty,
+                                 &njs_value_invalid, 1);
+    if (njs_slow_path(prop == NULL)) {
+        return NJS_ERROR;
+    }
+
+    prop->type = NJS_PROPERTY_HANDLER;
+    prop->u.value.type = NJS_INVALID;
+    prop->u.value.data.truth = 1;
+    njs_prop_magic16(prop) = magic16;
+    njs_prop_magic32(prop) = magic32;
+    njs_prop_handler(prop) = handler;
+
+    return njs_vm_bind2(vm, var_name, prop, shared);
+}
+
+
 void
 njs_value_string_get(njs_value_t *value, njs_str_t *dst)
 {


More information about the nginx-devel mailing list