[njs] Moving njs_normalize_args() into njs_function_native_call().

Dmitry Volyntsev xeioex at nginx.com
Thu Jan 10 14:31:00 UTC 2019


details:   https://hg.nginx.org/njs/rev/0da53bc8472b
branches:  
changeset: 717:0da53bc8472b
user:      hongzhidao <hongzhidao at gmail.com>
date:      Tue Jan 08 04:04:02 2019 +0800
description:
Moving njs_normalize_args() into njs_function_native_call().

diffstat:

 njs/njs_function.c |  144 ++++++++++++++++++++++++++++++++++++++++++++++++-
 njs/njs_function.h |    3 +-
 njs/njs_vm.c       |  155 +---------------------------------------------------
 njs/njs_vm.h       |    2 -
 4 files changed, 150 insertions(+), 154 deletions(-)

diffs (388 lines):

diff -r 045ba10db769 -r 0da53bc8472b njs/njs_function.c
--- a/njs/njs_function.c	Mon Jan 07 22:14:17 2019 +0800
+++ b/njs/njs_function.c	Tue Jan 08 04:04:02 2019 +0800
@@ -8,6 +8,8 @@
 #include <string.h>
 
 
+static njs_ret_t njs_normalize_args(njs_vm_t *vm, njs_value_t *args,
+    uint8_t *args_types, nxt_uint_t nargs);
 static njs_ret_t njs_function_activate(njs_vm_t *vm, njs_function_t *function,
     njs_value_t *this, njs_value_t *args, nxt_uint_t nargs, njs_index_t retval);
 
@@ -533,13 +535,19 @@ njs_function_call(njs_vm_t *vm, njs_inde
 
 njs_ret_t
 njs_function_native_call(njs_vm_t *vm, njs_function_native_t native,
-    njs_value_t *args, nxt_uint_t nargs, njs_index_t retval)
+    njs_value_t *args, uint8_t *args_types, nxt_uint_t nargs,
+    njs_index_t retval)
 {
     njs_ret_t           ret;
     njs_value_t         *value;
     njs_function_t      *function;
     njs_native_frame_t  *frame;
 
+    ret = njs_normalize_args(vm, args, args_types, nargs);
+    if (ret != NJS_OK) {
+        return ret;
+    }
+
     ret = native(vm, args, nargs, retval);
 
     /*
@@ -587,6 +595,140 @@ njs_function_native_call(njs_vm_t *vm, n
 }
 
 
+static njs_ret_t
+njs_normalize_args(njs_vm_t *vm, njs_value_t *args, uint8_t *args_types,
+    nxt_uint_t nargs)
+{
+    nxt_uint_t  n;
+    njs_trap_t  trap;
+
+    n = nxt_min(nargs, NJS_ARGS_TYPES_MAX);
+
+    while (n != 0) {
+
+        switch (*args_types) {
+
+        case NJS_STRING_OBJECT_ARG:
+
+            if (njs_is_null_or_void(args)) {
+                goto type_error;
+            }
+
+            /* Fall through. */
+
+        case NJS_STRING_ARG:
+
+            if (njs_is_string(args)) {
+                break;
+            }
+
+            trap = NJS_TRAP_STRING_ARG;
+            goto trap;
+
+        case NJS_NUMBER_ARG:
+
+            if (njs_is_numeric(args)) {
+                break;
+            }
+
+            trap = NJS_TRAP_NUMBER_ARG;
+            goto trap;
+
+        case NJS_INTEGER_ARG:
+
+            if (njs_is_numeric(args)) {
+
+                /* Numbers are truncated to fit in 32-bit integers. */
+
+                if (isnan(args->data.u.number)) {
+                    args->data.u.number = 0;
+
+                } else if (args->data.u.number > 2147483647.0) {
+                    args->data.u.number = 2147483647.0;
+
+                } else if (args->data.u.number < -2147483648.0) {
+                    args->data.u.number = -2147483648.0;
+                }
+
+                break;
+            }
+
+            trap = NJS_TRAP_NUMBER_ARG;
+            goto trap;
+
+        case NJS_FUNCTION_ARG:
+
+            switch (args->type) {
+            case NJS_STRING:
+            case NJS_FUNCTION:
+                break;
+
+            default:
+                trap = NJS_TRAP_STRING_ARG;
+                goto trap;
+            }
+
+            break;
+
+        case NJS_REGEXP_ARG:
+
+            switch (args->type) {
+            case NJS_VOID:
+            case NJS_STRING:
+            case NJS_REGEXP:
+                break;
+
+            default:
+                trap = NJS_TRAP_STRING_ARG;
+                goto trap;
+            }
+
+            break;
+
+        case NJS_DATE_ARG:
+            if (!njs_is_date(args)) {
+                goto type_error;
+            }
+
+            break;
+
+        case NJS_OBJECT_ARG:
+
+            if (njs_is_null_or_void(args)) {
+                goto type_error;
+            }
+
+            break;
+
+        case NJS_SKIP_ARG:
+            break;
+
+        case 0:
+            return NJS_OK;
+        }
+
+        args++;
+        args_types++;
+        n--;
+    }
+
+    return NJS_OK;
+
+trap:
+
+    njs_vm_trap_value(vm, args);
+
+    return njs_trap(vm, trap);
+
+type_error:
+
+    njs_type_error(vm, "cannot convert %s to %s", njs_type_string(args->type),
+                   njs_arg_type_string(*args_types));
+
+    return NXT_ERROR;
+}
+
+
 njs_native_frame_t *
 njs_function_previous_frame(njs_native_frame_t *frame)
 {
diff -r 045ba10db769 -r 0da53bc8472b njs/njs_function.h
--- a/njs/njs_function.h	Mon Jan 07 22:14:17 2019 +0800
+++ b/njs/njs_function.h	Tue Jan 08 04:04:02 2019 +0800
@@ -172,7 +172,8 @@ njs_ret_t njs_function_frame(njs_vm_t *v
     nxt_bool_t ctor);
 njs_ret_t njs_function_call(njs_vm_t *vm, njs_index_t retval, size_t advance);
 njs_ret_t njs_function_native_call(njs_vm_t *vm, njs_function_native_t native,
-    njs_value_t *args, nxt_uint_t nargs, njs_index_t retval);
+    njs_value_t *args, uint8_t *args_types, nxt_uint_t nargs,
+    njs_index_t retval);
 njs_native_frame_t *njs_function_previous_frame(njs_native_frame_t *frame);
 void njs_function_frame_free(njs_vm_t *vm, njs_native_frame_t *frame);
 
diff -r 045ba10db769 -r 0da53bc8472b njs/njs_vm.c
--- a/njs/njs_vm.c	Mon Jan 07 22:14:17 2019 +0800
+++ b/njs/njs_vm.c	Tue Jan 08 04:04:02 2019 +0800
@@ -2042,11 +2042,6 @@ njs_vmcode_function_call(njs_vm_t *vm, n
     args = frame->arguments;
     nargs = frame->nargs;
 
-    ret = njs_normalize_args(vm, args, function->args_types, nargs);
-    if (ret != NJS_OK) {
-        return ret;
-    }
-
     if (function->continuation_size != 0) {
         cont = njs_vm_continuation(vm);
 
@@ -2060,7 +2055,8 @@ njs_vmcode_function_call(njs_vm_t *vm, n
         return 0;
     }
 
-    ret = njs_function_native_call(vm, function->u.native, args, nargs,
+    ret = njs_function_native_call(vm, function->u.native, args,
+                                   function->args_types, nargs,
                                    (njs_index_t) retval);
 
     switch (ret) {
@@ -2076,140 +2072,6 @@ njs_vmcode_function_call(njs_vm_t *vm, n
 }
 
 
-njs_ret_t
-njs_normalize_args(njs_vm_t *vm, njs_value_t *args, uint8_t *args_types,
-    nxt_uint_t nargs)
-{
-    nxt_uint_t  n;
-    njs_trap_t  trap;
-
-    n = nxt_min(nargs, NJS_ARGS_TYPES_MAX);
-
-    while (n != 0) {
-
-        switch (*args_types) {
-
-        case NJS_STRING_OBJECT_ARG:
-
-            if (njs_is_null_or_void(args)) {
-                goto type_error;
-            }
-
-            /* Fall through. */
-
-        case NJS_STRING_ARG:
-
-            if (njs_is_string(args)) {
-                break;
-            }
-
-            trap = NJS_TRAP_STRING_ARG;
-            goto trap;
-
-        case NJS_NUMBER_ARG:
-
-            if (njs_is_numeric(args)) {
-                break;
-            }
-
-            trap = NJS_TRAP_NUMBER_ARG;
-            goto trap;
-
-        case NJS_INTEGER_ARG:
-
-            if (njs_is_numeric(args)) {
-
-                /* Numbers are truncated to fit in 32-bit integers. */
-
-                if (isnan(args->data.u.number)) {
-                    args->data.u.number = 0;
-
-                } else if (args->data.u.number > 2147483647.0) {
-                    args->data.u.number = 2147483647.0;
-
-                } else if (args->data.u.number < -2147483648.0) {
-                    args->data.u.number = -2147483648.0;
-                }
-
-                break;
-            }
-
-            trap = NJS_TRAP_NUMBER_ARG;
-            goto trap;
-
-        case NJS_FUNCTION_ARG:
-
-            switch (args->type) {
-            case NJS_STRING:
-            case NJS_FUNCTION:
-                break;
-
-            default:
-                trap = NJS_TRAP_STRING_ARG;
-                goto trap;
-            }
-
-            break;
-
-        case NJS_REGEXP_ARG:
-
-            switch (args->type) {
-            case NJS_VOID:
-            case NJS_STRING:
-            case NJS_REGEXP:
-                break;
-
-            default:
-                trap = NJS_TRAP_STRING_ARG;
-                goto trap;
-            }
-
-            break;
-
-        case NJS_DATE_ARG:
-            if (!njs_is_date(args)) {
-                goto type_error;
-            }
-
-            break;
-
-        case NJS_OBJECT_ARG:
-
-            if (njs_is_null_or_void(args)) {
-                goto type_error;
-            }
-
-            break;
-
-        case NJS_SKIP_ARG:
-            break;
-
-        case 0:
-            return NJS_OK;
-        }
-
-        args++;
-        args_types++;
-        n--;
-    }
-
-    return NJS_OK;
-
-trap:
-
-    njs_vm_trap_value(vm, args);
-
-    return njs_trap(vm, trap);
-
-type_error:
-
-    njs_type_error(vm, "cannot convert %s to %s", njs_type_string(args->type),
-                   njs_arg_type_string(*args_types));
-
-    return NXT_ERROR;
-}
-
-
 const char *
 njs_type_string(njs_value_type_t type)
 {
@@ -2433,19 +2295,12 @@ njs_vmcode_continuation(njs_vm_t *vm, nj
     njs_native_frame_t  *frame;
     njs_continuation_t  *cont;
 
+    frame = vm->top_frame;
     cont = njs_vm_continuation(vm);
-    frame = vm->top_frame;
-
-    if (cont->args_types != NULL) {
-        ret = njs_normalize_args(vm, frame->arguments, cont->args_types,
-                                 frame->nargs);
-        if (ret != NJS_OK) {
-            return ret;
-        }
-    }
 
     ret = njs_function_native_call(vm, cont->function, frame->arguments,
-                                   frame->nargs, cont->retval);
+                                   cont->args_types, frame->nargs,
+                                   cont->retval);
 
     switch (ret) {
     case NXT_OK:
diff -r 045ba10db769 -r 0da53bc8472b njs/njs_vm.h
--- a/njs/njs_vm.h	Mon Jan 07 22:14:17 2019 +0800
+++ b/njs/njs_vm.h	Tue Jan 08 04:04:02 2019 +0800
@@ -1262,8 +1262,6 @@ njs_ret_t njs_vmcode_finally(njs_vm_t *v
 nxt_bool_t njs_values_strict_equal(const njs_value_t *val1,
     const njs_value_t *val2);
 
-njs_ret_t njs_normalize_args(njs_vm_t *vm, njs_value_t *args,
-    uint8_t *args_types, nxt_uint_t nargs);
 const char *njs_type_string(njs_value_type_t type);
 const char *njs_arg_type_string(uint8_t arg);
 


More information about the nginx-devel mailing list