]> git.kaiwu.me - njs.git/commitdiff
External methods now have njs_function_t structure.
authorIgor Sysoev <igor@sysoev.ru>
Mon, 25 Jan 2016 12:14:00 +0000 (15:14 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Mon, 25 Jan 2016 12:14:00 +0000 (15:14 +0300)
njs/njs_extern.c
njs/njs_extern.h
njs/njs_function.c
njs/njs_function.h
njs/njs_vm.c

index a2ac7846c7e2542baed1471a9892bc55047dec86..a52b0a76583e583f89e4cfccadba18c0b1f0a2c2 100644 (file)
@@ -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;
 
index b2a77a3ae62abdff2783a1debce106f9a3cf4014..ea39a574ec16afe432bc8e0d4f97d9d29d5b0a96 100644 (file)
@@ -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;
index 9a31f6491378ef30ec2a348590a6f8b0412beb71..59912702516bc3fb9c6871b15e032e6c9f3b9742 100644 (file)
@@ -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);
index 1575bddd718367078ed21eb21ea43992e73af71a..7fa31ad5baa9bc6541e5607d27540343f11ea4a1 100644 (file)
@@ -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);
index e5d278face39ff08b9759385143ce1668f0f1231..3db49495c1a529720f4319da090e2893c6e158a8 100644 (file)
@@ -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, &param);
+    ret = vm->frame->u.function->u.native(vm, &param);
     /*
      * A native method can return:
      *   NXT_OK on method success;