changeset 121:74246210aa9d

njs_vm_function() interface to get a function by name.
author Igor Sysoev <igor@sysoev.ru>
date Thu, 14 Jul 2016 19:24:51 +0300
parents 07d2be75a7db
children a3e7ebdf9fa7
files njs/njs_variable.c njs/njscript.h
diffstat 2 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/njs/njs_variable.c	Wed Jul 13 13:56:12 2016 +0300
+++ b/njs/njs_variable.c	Thu Jul 14 19:24:51 2016 +0300
@@ -119,6 +119,30 @@
 }
 
 
+njs_function_t *
+njs_vm_function(njs_vm_t *vm, nxt_str_t *name)
+{
+    njs_value_t         *value;
+    njs_variable_t      *var;
+    nxt_lvlhsh_query_t  lhq;
+
+    lhq.key_hash = nxt_djb_hash(name->data, name->len);
+    lhq.key = *name;
+    lhq.proto = &njs_variables_hash_proto;
+
+    if (nxt_slow_path(nxt_lvlhsh_find(&vm->variables_hash, &lhq) != NXT_OK)) {
+        return NULL;
+    }
+
+    var = lhq.value;
+
+    value = (njs_value_t *) ((u_char *) vm->scopes[NJS_SCOPE_GLOBAL]
+                             + njs_offset(var->index));
+
+    return value->data.u.function;
+}
+
+
 static njs_variable_t *
 njs_variable_alloc(njs_vm_t *vm, njs_parser_t *parser, nxt_str_t *name)
 {
--- a/njs/njscript.h	Wed Jul 13 13:56:12 2016 +0300
+++ b/njs/njscript.h	Thu Jul 14 19:24:51 2016 +0300
@@ -91,6 +91,7 @@
     njs_opaque_value_t *args, nxt_uint_t nargs);
 NXT_EXPORT nxt_int_t njs_vm_run(njs_vm_t *vm);
 
+NXT_EXPORT njs_function_t *njs_vm_function(njs_vm_t *vm, nxt_str_t *name);
 NXT_EXPORT njs_ret_t njs_vm_return_string(njs_vm_t *vm, u_char *start,
     size_t size);
 NXT_EXPORT nxt_int_t njs_vm_retval(njs_vm_t *vm, nxt_str_t *retval);