]> granicus.if.org Git - vim/commitdiff
patch 9.0.1147: cannot access a class member in a compiled function v9.0.1147
authorBram Moolenaar <Bram@vim.org>
Wed, 4 Jan 2023 18:54:09 +0000 (18:54 +0000)
committerBram Moolenaar <Bram@vim.org>
Wed, 4 Jan 2023 18:54:09 +0000 (18:54 +0000)
Problem:    Cannot access a class member in a compiled function.
Solution:   Implement looking up a class member.

src/testdir/test_vim9_class.vim
src/version.c
src/vim9expr.c

index 60730c4d5d3ca6d62030a8e991831d7c9a1b2365..a55671bcb65e90f1d7b6b7f6427b316c7023e3d1 100644 (file)
@@ -439,6 +439,11 @@ def Test_class_member()
       TextPos.AddToCounter(3)
       assert_equal(3, TextPos.counter)
       assert_fails('echo TextPos.noSuchMember', 'E1338:')
+      
+      def GetCounter(): number
+        return TextPos.counter
+      enddef
+      assert_equal(3, GetCounter())
 
       assert_fails('TextPos.noSuchMember = 2', 'E1337:')
       assert_fails('TextPos.counter = 5', 'E1335:')
index 44d8db9b00fd9c28e624b49ce75dc1a2386b18a4..39f7a0b0c72eef1e311f307795a1979f482a1c5e 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1147,
 /**/
     1146,
 /**/
index daf014f84bf3825aae3b253f51d39575a9e1d512..3acb97bef6c491cebd747dc28a3d9c1a3293bc67 100644 (file)
@@ -263,6 +263,22 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
        return FAIL;
     }
 
+    if (type->tt_type == VAR_CLASS)
+    {
+       garray_T *instr = &cctx->ctx_instr;
+       if (instr->ga_len > 0)
+       {
+           isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
+           if (isn->isn_type == ISN_LOADSCRIPT)
+           {
+               // The class was recognized as a script item.  We only need
+               // to know what class it is, drop the instruction.
+               --instr->ga_len;
+               vim_free(isn->isn_arg.script.scriptref);
+           }
+       }
+    }
+
     ++*arg;
     char_u *name = *arg;
     char_u *name_end = find_name_end(name, NULL, NULL, FNE_CHECK_START);
@@ -278,19 +294,6 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
 
        if (type->tt_type == VAR_CLASS)
        {
-           garray_T *instr = &cctx->ctx_instr;
-           if (instr->ga_len > 0)
-           {
-               isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
-               if (isn->isn_type == ISN_LOADSCRIPT)
-               {
-                   // The class was recognized as a script item.  We only need
-                   // to know what class it is, drop the instruction.
-                   --instr->ga_len;
-                   vim_free(isn->isn_arg.script.scriptref);
-               }
-           }
-
            function_count = cl->class_class_function_count;
            functions = cl->class_class_functions;
        }
@@ -344,10 +347,8 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
                    return FAIL;
                }
 
-               generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
-
                *arg = name_end;
-               return OK;
+               return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
            }
        }
 
@@ -355,8 +356,20 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
     }
     else
     {
-       // TODO: class member
-       emsg("compile_class_object_index(): class member not handled yet");
+       // load class member
+       int idx;
+       for (idx = 0; idx < cl->class_class_member_count; ++idx)
+       {
+           ocmember_T *m = &cl->class_class_members[idx];
+           if (STRNCMP(name, m->ocm_name, len) == 0 && m->ocm_name[len] == NUL)
+               break;
+       }
+       if (idx < cl->class_class_member_count)
+       {
+           *arg = name_end;
+           return generate_CLASSMEMBER(cctx, TRUE, cl, idx);
+       }
+       semsg(_(e_class_member_not_found_str), name);
     }
 
     return FAIL;