To: vim_dev@googlegroups.com Subject: Patch 9.0.1185 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.1185 Problem: Using class from imported script not tested. Solution: Add tests. Implement what is missing. Files: src/vim9type.c, src/evalvars.c, src/proto/evalvars.pro, src/vim9class.c, src/testdir/test_vim9_class.vim *** ../vim-9.0.1184/src/vim9type.c 2023-01-12 15:01:28.833314453 +0000 --- src/vim9type.c 2023-01-12 16:12:48.754979318 +0000 *************** *** 982,988 **** if (optional && *p == '?') ++p; ! while (ASCII_ISALNUM(*p) || *p == '_') ++p; // Skip over ""; this is permissive about white space. --- 982,990 ---- if (optional && *p == '?') ++p; ! ! // Also skip over "." for imported classes: "import.ClassName". ! while (ASCII_ISALNUM(*p) || *p == '_' || *p == '.') ++p; // Skip over ""; this is permissive about white space. *************** *** 1091,1097 **** char_u *p = *arg; size_t len; ! // skip over the first word while (ASCII_ISALNUM(*p) || *p == '_') ++p; len = p - *arg; --- 1093,1099 ---- char_u *p = *arg; size_t len; ! // Skip over the first word. while (ASCII_ISALNUM(*p) || *p == '_') ++p; len = p - *arg; *************** *** 1293,1302 **** break; } ! // It can be a class or interface name. typval_T tv; tv.v_type = VAR_UNKNOWN; ! if (eval_variable(*arg, (int)len, 0, &tv, NULL, EVAL_VAR_IMPORT) == OK) { if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL) { --- 1295,1304 ---- break; } ! // It can be a class or interface name, possibly imported. typval_T tv; tv.v_type = VAR_UNKNOWN; ! if (eval_variable_import(*arg, &tv) == OK) { if (tv.v_type == VAR_CLASS && tv.vval.v_class != NULL) { *** ../vim-9.0.1184/src/evalvars.c 2023-01-06 18:42:16.430674097 +0000 --- src/evalvars.c 2023-01-12 16:50:30.537657495 +0000 *************** *** 3105,3110 **** --- 3105,3135 ---- } /* + * Get the value of internal variable "name", also handling "import.name". + * Return OK or FAIL. If OK is returned "rettv" must be cleared. + */ + int + eval_variable_import( + char_u *name, + typval_T *rettv) + { + char_u *s = name; + while (ASCII_ISALNUM(*s) || *s == '_') + ++s; + int len = (int)(s - name); + + if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL) + return FAIL; + if (rettv->v_type == VAR_ANY && *s == '.') + { + int sid = rettv->vval.v_number; + return eval_variable(s + 1, 0, sid, rettv, NULL, 0); + } + return OK; + } + + + /* * Check if variable "name[len]" is a local variable or an argument. * If so, "*eval_lavars_used" is set to TRUE. */ *** ../vim-9.0.1184/src/proto/evalvars.pro 2022-09-11 15:14:00.551020049 +0100 --- src/proto/evalvars.pro 2023-01-12 16:12:54.786981365 +0000 *************** *** 60,65 **** --- 60,66 ---- char_u *v_throwpoint(char_u *oldval); char_u *set_cmdarg(exarg_T *eap, char_u *oldarg); int eval_variable(char_u *name, int len, scid_T sid, typval_T *rettv, dictitem_T **dip, int flags); + int eval_variable_import(char_u *name, typval_T *rettv); void check_vars(char_u *name, int len); dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload); dictitem_T *find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload); *** ../vim-9.0.1184/src/vim9class.c 2023-01-12 15:01:28.833314453 +0000 --- src/vim9class.c 2023-01-12 16:38:15.217892971 +0000 *************** *** 244,252 **** } char_u *name_start = arg; // TODO: // generics: - // handle "is_export" if it is set // Name for "extends BaseClass" char_u *extends = NULL; --- 244,256 ---- } char_u *name_start = arg; + // "export class" gets used when creating the class, don't use "is_export" + // for the items inside the class. + int class_export = is_export; + is_export = FALSE; + // TODO: // generics: // Name for "extends BaseClass" char_u *extends = NULL; *************** *** 558,564 **** { typval_T tv; tv.v_type = VAR_UNKNOWN; ! if (eval_variable(extends, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL) { semsg(_(e_class_name_not_found_str), extends); success = FALSE; --- 562,568 ---- { typval_T tv; tv.v_type = VAR_UNKNOWN; ! if (eval_variable_import(extends, &tv) == FAIL) { semsg(_(e_class_name_not_found_str), extends); success = FALSE; *************** *** 594,600 **** char_u *impl = ((char_u **)ga_impl.ga_data)[i]; typval_T tv; tv.v_type = VAR_UNKNOWN; ! if (eval_variable(impl, 0, 0, &tv, NULL, EVAL_VAR_IMPORT) == FAIL) { semsg(_(e_interface_name_not_found_str), impl); success = FALSE; --- 598,604 ---- char_u *impl = ((char_u **)ga_impl.ga_data)[i]; typval_T tv; tv.v_type = VAR_UNKNOWN; ! if (eval_variable_import(impl, &tv) == FAIL) { semsg(_(e_interface_name_not_found_str), impl); success = FALSE; *************** *** 930,935 **** --- 934,940 ---- typval_T tv; tv.v_type = VAR_CLASS; tv.vval.v_class = cl; + is_export = class_export; set_var_const(cl->class_name, current_sctx.sc_sid, NULL, &tv, FALSE, ASSIGN_DECL, 0); return; *** ../vim-9.0.1184/src/testdir/test_vim9_class.vim 2023-01-12 15:01:28.833314453 +0000 --- src/testdir/test_vim9_class.vim 2023-01-12 15:36:11.079099286 +0000 *************** *** 974,978 **** --- 974,1000 ---- v9.CheckScriptSuccess(lines) enddef + def Test_class_import() + var lines =<< trim END + vim9script + export class Animal + this.kind: string + this.name: string + endclass + END + writefile(lines, 'Xanimal.vim', 'D') + + lines =<< trim END + vim9script + import './Xanimal.vim' as animal + + var a: animal.Animal + a = animal.Animal.new('fish', 'Eric') + assert_equal('fish', a.kind) + assert_equal('Eric', a.name) + END + v9.CheckScriptSuccess(lines) + enddef + " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker *** ../vim-9.0.1184/src/version.c 2023-01-12 15:01:28.833314453 +0000 --- src/version.c 2023-01-12 15:30:18.675137407 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 1185, /**/ -- Q: What's a light-year? A: One-third less calories than a regular year. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///