From a00c1ea95421b64c8ee5603c402889db88e00cf6 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 8 Nov 2019 16:29:24 +0300 Subject: [PATCH] Passing to native function additional magic argument. This allows to make more generic function handlers. --- src/njs_function.c | 17 +++++++++++------ src/njs_value.h | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/njs_function.c b/src/njs_function.c index e72d8cdb..d6fcccf8 100644 --- a/src/njs_function.c +++ b/src/njs_function.c @@ -591,8 +591,7 @@ njs_function_native_call(njs_vm_t *vm) function = native->function; ret = function->u.native(vm, native->arguments, native->nargs, - frame->retval); - + function->magic); if (njs_slow_path(ret == NJS_ERROR)) { return ret; } @@ -919,9 +918,10 @@ njs_function_instance_length(njs_vm_t *vm, njs_object_prop_t *prop, static njs_int_t njs_function_prototype_call(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, - njs_index_t retval) + njs_index_t unused) { njs_int_t ret; + njs_frame_t *frame; njs_function_t *function; const njs_value_t *this; @@ -939,6 +939,8 @@ njs_function_prototype_call(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, nargs = 0; } + frame = (njs_frame_t *) vm->top_frame; + function = njs_function(&args[0]); /* Skip the "call" method frame. */ @@ -949,7 +951,7 @@ njs_function_prototype_call(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, return ret; } - ret = njs_function_frame_invoke(vm, retval); + ret = njs_function_frame_invoke(vm, frame->retval); if (njs_slow_path(ret != NJS_OK)) { return ret; } @@ -960,10 +962,11 @@ njs_function_prototype_call(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, static njs_int_t njs_function_prototype_apply(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, - njs_index_t retval) + njs_index_t unused) { uint32_t i, length; njs_int_t ret; + njs_frame_t *frame; njs_value_t name, *this, *arr_like; njs_array_t *arr; njs_function_t *func; @@ -1021,12 +1024,14 @@ activate: /* Skip the "apply" method frame. */ vm->top_frame->skip = 1; + frame = (njs_frame_t *) vm->top_frame; + ret = njs_function_frame(vm, func, this, args, length, 0); if (njs_slow_path(ret != NJS_OK)) { return ret; } - ret = njs_function_frame_invoke(vm, retval); + ret = njs_function_frame_invoke(vm, frame->retval); if (njs_slow_path(ret != NJS_OK)) { return ret; } diff --git a/src/njs_value.h b/src/njs_value.h index 75a1ec2e..cf79c12f 100644 --- a/src/njs_value.h +++ b/src/njs_value.h @@ -234,14 +234,14 @@ struct njs_function_s { njs_object_t object; uint8_t args_offset; - uint8_t args_count; - /* Function is a closure. */ + uint8_t args_count:5; uint8_t closure:1; - uint8_t native:1; uint8_t ctor:1; + uint8_t magic; + union { njs_function_lambda_t *lambda; njs_function_native_t native; @@ -391,12 +391,13 @@ typedef struct { } -#define njs_native_function(_function, _args_count) { \ +#define _njs_native_function(_function, _args_count, _magic) { \ .data = { \ .type = NJS_FUNCTION, \ .truth = 1, \ .u.function = & (njs_function_t) { \ .native = 1, \ + .magic = _magic, \ .args_count = _args_count, \ .args_offset = 1, \ .u.native = _function, \ @@ -408,6 +409,14 @@ typedef struct { } +#define njs_native_function(_function, _args_count) \ + _njs_native_function(_function, _args_count, 0) + + +#define njs_native_function2(_function, _args_count, _magic) \ + _njs_native_function(_function, _args_count, _magic) + + #define njs_prop_handler(_handler) { \ .data = { \ .type = NJS_INVALID, \ -- 2.47.3