]> git.kaiwu.me - njs.git/commitdiff
Moving njs_normalize_args() into njs_function_native_call().
authorhongzhidao <hongzhidao@gmail.com>
Mon, 7 Jan 2019 20:04:02 +0000 (04:04 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Mon, 7 Jan 2019 20:04:02 +0000 (04:04 +0800)
njs/njs_function.c
njs/njs_function.h
njs/njs_vm.c
njs/njs_vm.h

index 41032d67344aadb7b7c1648f9f6f852a1d2c2328..9372227aa5b1627a0ffff5ddaa6def7c9927a1cb 100644 (file)
@@ -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_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_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, njs_function_native_t native,
 }
 
 
+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)
 {
index 77bc0489e2ddf8fe320f827c6043ab7031e254f5..ec869726e74fe0e9947fe409c13233cdeff64439 100644 (file)
@@ -172,7 +172,8 @@ njs_ret_t njs_function_frame(njs_vm_t *vm, njs_function_t *function,
     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);
 
index 9c9b6a72100355ae2f3a5c7d09286e2e57427964..be9f1ff3304ae41593c7a4f72166911d26f983d2 100644 (file)
@@ -2042,11 +2042,6 @@ njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval)
     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, njs_value_t *invld, njs_value_t *retval)
         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, njs_value_t *invld, njs_value_t *retval)
 }
 
 
-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, njs_value_t *invld1, njs_value_t *invld2)
     njs_native_frame_t  *frame;
     njs_continuation_t  *cont;
 
-    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;
-        }
-    }
+    cont = njs_vm_continuation(vm);
 
     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:
index 50d2e9a3b960234113fbc68b92357fa7f3648c29..0e0fb5d59d7f3ae54d55d6e8ab4f969a59fa654f 100644 (file)
@@ -1262,8 +1262,6 @@ njs_ret_t njs_vmcode_finally(njs_vm_t *vm, njs_value_t *invld,
 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);