To: vim_dev@googlegroups.com Subject: Patch 8.2.1399 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1399 Problem: Vim9: may find imported item in wrong script. Solution: When looking up script-local function use the embedded script ID. (issue #6644) Files: src/vim9compile.c, src/proto/vim9compile.pro, src/userfunc.c, src/testdir/test_vim9_script.vim *** ../vim-8.2.1398/src/vim9compile.c 2020-08-08 16:47:26.555257199 +0200 --- src/vim9compile.c 2020-08-08 21:58:31.927005210 +0200 *************** *** 2548,2559 **** imported_T * find_imported(char_u *name, size_t len, cctx_T *cctx) { - scriptitem_T *si; int idx; if (current_sctx.sc_sid <= 0) return NULL; - si = SCRIPT_ITEM(current_sctx.sc_sid); if (cctx != NULL) for (idx = 0; idx < cctx->ctx_imports.ga_len; ++idx) { --- 2548,2557 ---- *************** *** 2566,2571 **** --- 2564,2578 ---- return import; } + return find_imported_in_script(name, len, current_sctx.sc_sid); + } + + imported_T * + find_imported_in_script(char_u *name, size_t len, int sid) + { + scriptitem_T *si = SCRIPT_ITEM(sid); + int idx; + for (idx = 0; idx < si->sn_imports.ga_len; ++idx) { imported_T *import = ((imported_T *)si->sn_imports.ga_data) + idx; *** ../vim-8.2.1398/src/proto/vim9compile.pro 2020-08-05 10:53:15.087273357 +0200 --- src/proto/vim9compile.pro 2020-08-08 21:58:30.167011239 +0200 *************** *** 12,17 **** --- 12,18 ---- char *type_name(type_T *type, char **tofree); int get_script_item_idx(int sid, char_u *name, int check_writable); imported_T *find_imported(char_u *name, size_t len, cctx_T *cctx); + imported_T *find_imported_in_script(char_u *name, size_t len, int sid); int vim9_comment_start(char_u *p); char_u *peek_next_line_from_context(cctx_T *cctx); char_u *next_line_from_context(cctx_T *cctx, int skip_comment); *** ../vim-8.2.1398/src/userfunc.c 2020-08-08 14:26:27.640478505 +0200 --- src/userfunc.c 2020-08-08 22:13:33.392743141 +0200 *************** *** 791,796 **** --- 791,797 ---- { int vim9script = in_vim9script(); char_u *after_script = NULL; + long sid = 0; if (vim9script) { *************** *** 800,817 **** return func; } ! if (!vim9script ! && name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR) { - long sid; - // Caller changes s: to 99_name. after_script = name + 3; sid = getdigits(&after_script); ! if (sid == current_sctx.sc_sid && *after_script == '_') ++after_script; else after_script = NULL; --- 801,815 ---- return func; } ! if (name[0] == K_SPECIAL && name[1] == KS_EXTRA && name[2] == KE_SNR) { // Caller changes s: to 99_name. after_script = name + 3; sid = getdigits(&after_script); ! if (*after_script == '_') ++after_script; else after_script = NULL; *************** *** 819,826 **** if (vim9script || after_script != NULL) { // Find imported function before global one. ! imported = find_imported( ! after_script == NULL ? name : after_script, 0, cctx); if (imported != NULL && imported->imp_funcname != NULL) { hi = hash_find(&func_hashtab, imported->imp_funcname); --- 817,827 ---- if (vim9script || after_script != NULL) { // Find imported function before global one. ! if (after_script != NULL && sid != current_sctx.sc_sid) ! imported = find_imported_in_script(after_script, 0, sid); ! else ! imported = find_imported(after_script == NULL ! ? name : after_script, 0, cctx); if (imported != NULL && imported->imp_funcname != NULL) { hi = hash_find(&func_hashtab, imported->imp_funcname); *** ../vim-8.2.1398/src/testdir/test_vim9_script.vim 2020-08-08 15:45:58.233358630 +0200 --- src/testdir/test_vim9_script.vim 2020-08-08 22:04:46.089737442 +0200 *************** *** 1430,1435 **** --- 1430,1460 ---- &rtp = save_rtp enddef + def Test_use_import_in_mapping() + let lines =<< trim END + vim9script + export def Funcx() + g:result = 42 + enddef + END + writefile(lines, 'XsomeExport.vim') + lines =<< trim END + vim9script + import Funcx from './XsomeExport.vim' + nnoremap :call Funcx() + END + writefile(lines, 'Xmapscript.vim') + + source Xmapscript.vim + feedkeys("\", "xt") + assert_equal(42, g:result) + + unlet g:result + delete('XsomeExport.vim') + delete('Xmapscript.vim') + nunmap + enddef + def Test_vim9script_fails() CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:') CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:') *** ../vim-8.2.1398/src/version.c 2020-08-08 21:33:17.788742735 +0200 --- src/version.c 2020-08-08 21:52:49.336196610 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1399, /**/ -- hundred-and-one symptoms of being an internet addict: 157. You fum through a magazine, you first check to see if it has a web address. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///