From 3cb477d1408a2eb4e7267db9ce69115f53d24562 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Mon, 25 Jan 2016 15:14:00 +0300 Subject: [PATCH] External methods now have njs_function_t structure. --- njs/njs_extern.c | 12 +++++++++++- njs/njs_extern.h | 2 +- njs/njs_function.c | 11 +++++------ njs/njs_function.h | 8 ++------ njs/njs_vm.c | 13 +++++-------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/njs/njs_extern.c b/njs/njs_extern.c index a2ac7846..a52b0a76 100644 --- a/njs/njs_extern.c +++ b/njs/njs_extern.c @@ -76,6 +76,17 @@ njs_add_external(nxt_lvlhsh_t *hash, nxt_mem_cache_pool_t *mcp, uintptr_t object ext->value.data.truth = 1; ext->value.data.u.external = ext; + if (external->method != NULL) { + ext->function = nxt_mem_cache_zalloc(mcp, sizeof(njs_function_t)); + if (nxt_slow_path(ext->function == NULL)) { + return NXT_ERROR; + } + + ext->function->native = 1; + ext->function->args_offset = 1; + ext->function->u.native = external->method; + } + nxt_lvlhsh_init(&ext->hash); ext->type = external->type; ext->get = external->get; @@ -83,7 +94,6 @@ njs_add_external(nxt_lvlhsh_t *hash, nxt_mem_cache_pool_t *mcp, uintptr_t object ext->find = external->find; ext->foreach = external->foreach; ext->next = external->next; - ext->method = external->method; ext->object = object; ext->data = external->data; diff --git a/njs/njs_extern.h b/njs/njs_extern.h index b2a77a3a..ea39a574 100644 --- a/njs/njs_extern.h +++ b/njs/njs_extern.h @@ -24,7 +24,7 @@ struct njs_extern_s { njs_extern_foreach_t foreach; njs_extern_next_t next; - njs_extern_method_t method; + njs_function_t *function; uintptr_t object; uintptr_t data; diff --git a/njs/njs_function.c b/njs/njs_function.c index 9a31f649..59912702 100644 --- a/njs/njs_function.c +++ b/njs/njs_function.c @@ -42,14 +42,14 @@ njs_function_alloc(njs_vm_t *vm) njs_value_t * -njs_function_native_frame(njs_vm_t *vm, njs_native_t native, size_t local_size, +njs_function_native_frame(njs_vm_t *vm, njs_function_t *function, njs_vmcode_t *code) { size_t size; njs_value_t *this; njs_native_frame_t *frame; - size = NJS_NATIVE_FRAME_SIZE + local_size + size = NJS_NATIVE_FRAME_SIZE + function->local_state_size + code->nargs * sizeof(njs_value_t); frame = njs_function_frame_alloc(vm, size); @@ -57,11 +57,11 @@ njs_function_native_frame(njs_vm_t *vm, njs_native_t native, size_t local_size, return NULL; } - frame->u.native = native; - frame->native = 1; + frame->u.function = function; frame->ctor = code->ctor; - this = (njs_value_t *) ((u_char *) njs_native_data(frame) + local_size); + this = (njs_value_t *) ((u_char *) njs_native_data(frame) + + function->local_state_size); frame->arguments = this + 1; vm->scopes[NJS_SCOPE_CALLEE_ARGUMENTS] = frame->arguments; @@ -169,7 +169,6 @@ njs_function_frame(njs_vm_t *vm, njs_function_t *function, njs_param_t *param, } native_frame->u.function = function; - native_frame->native = 0; native_frame->ctor = ctor; args = (njs_value_t *) ((u_char *) native_frame + NJS_FRAME_SIZE); diff --git a/njs/njs_function.h b/njs/njs_function.h index 1575bddd..7fa31ad5 100644 --- a/njs/njs_function.h +++ b/njs/njs_function.h @@ -65,7 +65,6 @@ struct njs_native_frame_s { union { njs_function_t *function; u_char *return_address; - njs_native_t native; u_char *restart; } u; @@ -76,9 +75,6 @@ struct njs_native_frame_s { uint32_t free_size; - /* Script or native function or method. */ - uint8_t native; /* 1 bit */ - /* Function is called as constructor with "new" keyword. */ uint8_t ctor; /* 1 bit */ @@ -123,8 +119,8 @@ njs_native_frame_t *njs_function_frame_alloc(njs_vm_t *vm, size_t size); njs_ret_t njs_function_constructor(njs_vm_t *vm, njs_param_t *param); njs_ret_t njs_function_apply(njs_vm_t *vm, njs_value_t *name, njs_param_t *param); -njs_value_t *njs_function_native_frame(njs_vm_t *vm, njs_native_t native, - size_t local_size, njs_vmcode_t *code); +njs_value_t *njs_function_native_frame(njs_vm_t *vm, njs_function_t *function, + njs_vmcode_t *code); njs_ret_t njs_function_frame(njs_vm_t *vm, njs_function_t *function, njs_param_t *param, nxt_bool_t ctor); njs_ret_t njs_function_call(njs_vm_t *vm, njs_index_t retval); diff --git a/njs/njs_vm.c b/njs/njs_vm.c index e5d278fa..3db49495 100644 --- a/njs/njs_vm.c +++ b/njs/njs_vm.c @@ -2088,8 +2088,7 @@ njs_vmcode_function_frame(njs_vm_t *vm, njs_value_t *invld, njs_value_t *name) function = value->data.u.function; if (function->native) { - this = njs_function_native_frame(vm, function->u.native, 0, - &func->code); + this = njs_function_native_frame(vm, function, &func->code); if (nxt_fast_path(this != NULL)) { *this = njs_value_void; @@ -2174,9 +2173,7 @@ njs_vmcode_method_frame(njs_vm_t *vm, njs_value_t *name, njs_value_t *object) return ret; } - this = njs_function_native_frame(vm, function->u.native, - function->local_state_size, - &method->code); + this = njs_function_native_frame(vm, function, &method->code); if (nxt_slow_path(this == NULL)) { return NXT_ERROR; } @@ -2199,7 +2196,7 @@ njs_vmcode_method_frame(njs_vm_t *vm, njs_value_t *name, njs_value_t *object) ext = pq.lhq.value; if (ext->type == NJS_EXTERN_METHOD) { - this = njs_function_native_frame(vm, ext->method, 0, + this = njs_function_native_frame(vm, ext->function, &method->code); if (nxt_slow_path(this == NULL)) { @@ -2237,7 +2234,7 @@ njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval) /* Update code pointer here to store it as return address in call frame. */ vm->current += sizeof(njs_vmcode_function_call_t); - if (!vm->frame->native) { + if (!vm->frame->u.function->native) { (void) njs_function_call(vm, (njs_index_t) retval); return 0; } @@ -2248,7 +2245,7 @@ njs_vmcode_function_call(njs_vm_t *vm, njs_value_t *invld, njs_value_t *retval) param.args = args; param.this = args - 1; - ret = vm->frame->u.native(vm, ¶m); + ret = vm->frame->u.function->u.native(vm, ¶m); /* * A native method can return: * NXT_OK on method success; -- 2.47.3