comparison src/userfunc.c @ 27464:a14c4d3e3260 v8.2.4260

patch 8.2.4260: Vim9: can still use a global function without g: Commit: https://github.com/vim/vim/commit/848faddb870f3ba4d84fcacd1cccb5cdbbfd9c41 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jan 30 15:28:30 2022 +0000 patch 8.2.4260: Vim9: can still use a global function without g: Problem: Vim9: can still use a global function without g: at the script level. Solution: Also check for g: at the script level. (issue #9637)
author Bram Moolenaar <Bram@vim.org>
date Sun, 30 Jan 2022 16:30:04 +0100
parents 4c16acb2525f
children 55613f0d59bc
comparison
equal deleted inserted replaced
27463:c50f682d1183 27464:a14c4d3e3260
1900 { 1900 {
1901 hashitem_T *hi; 1901 hashitem_T *hi;
1902 char_u buffer[200]; 1902 char_u buffer[200];
1903 scriptitem_T *si; 1903 scriptitem_T *si;
1904 1904
1905 if (vim_strchr(name, AUTOLOAD_CHAR) != 0) 1905 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
1906 return NULL; // already has the prefix 1906 return NULL; // already has the prefix
1907 if (!SCRIPT_ID_VALID(sid)) 1907 if (!SCRIPT_ID_VALID(sid))
1908 return NULL; // not in a script 1908 return NULL; // not in a script
1909 si = SCRIPT_ITEM(sid); 1909 si = SCRIPT_ITEM(sid);
1910 if (si->sn_autoload_prefix != NULL) 1910 if (si->sn_autoload_prefix != NULL)
2000 */ 2000 */
2001 int 2001 int
2002 func_is_global(ufunc_T *ufunc) 2002 func_is_global(ufunc_T *ufunc)
2003 { 2003 {
2004 return ufunc->uf_name[0] != K_SPECIAL; 2004 return ufunc->uf_name[0] != K_SPECIAL;
2005 }
2006
2007 /*
2008 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2009 */
2010 int
2011 func_requires_g_prefix(ufunc_T *ufunc)
2012 {
2013 return ufunc->uf_name[0] != K_SPECIAL
2014 && (ufunc->uf_flags & FC_LAMBDA) == 0
2015 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2005 } 2016 }
2006 2017
2007 /* 2018 /*
2008 * Copy the function name of "fp" to buffer "buf". 2019 * Copy the function name of "fp" to buffer "buf".
2009 * "buf" must be able to hold the function name plus three bytes. 2020 * "buf" must be able to hold the function name plus three bytes.
3440 { 3451 {
3441 /* 3452 /*
3442 * User defined function. 3453 * User defined function.
3443 */ 3454 */
3444 if (fp == NULL) 3455 if (fp == NULL)
3456 {
3445 fp = find_func(rfname, is_global); 3457 fp = find_func(rfname, is_global);
3458 if (fp != NULL && !is_global && in_vim9script()
3459 && func_requires_g_prefix(fp))
3460 // In Vim9 script g: is required to find a global
3461 // non-autoload function.
3462 fp = NULL;
3463 }
3446 3464
3447 // Trigger FuncUndefined event, may load the function. 3465 // Trigger FuncUndefined event, may load the function.
3448 if (fp == NULL 3466 if (fp == NULL
3449 && apply_autocmds(EVENT_FUNCUNDEFINED, 3467 && apply_autocmds(EVENT_FUNCUNDEFINED,
3450 rfname, rfname, TRUE, NULL) 3468 rfname, rfname, TRUE, NULL)
3670 char_u *end; 3688 char_u *end;
3671 int lead; 3689 int lead;
3672 char_u sid_buf[20]; 3690 char_u sid_buf[20];
3673 int len; 3691 int len;
3674 int extra = 0; 3692 int extra = 0;
3693 int prefix_g = FALSE;
3675 lval_T lv; 3694 lval_T lv;
3676 int vim9script; 3695 int vim9script;
3677 3696
3678 if (fdp != NULL) 3697 if (fdp != NULL)
3679 CLEAR_POINTER(fdp); 3698 CLEAR_POINTER(fdp);
3835 else 3854 else
3836 { 3855 {
3837 // skip over "s:" and "g:" 3856 // skip over "s:" and "g:"
3838 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':')) 3857 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
3839 { 3858 {
3840 if (is_global != NULL && lv.ll_name[0] == 'g') 3859 if (lv.ll_name[0] == 'g')
3841 *is_global = TRUE; 3860 {
3861 if (is_global != NULL)
3862 {
3863 *is_global = TRUE;
3864 }
3865 else
3866 {
3867 // dropping "g:" without setting "is_global" won't work in
3868 // Vim9script, put it back later
3869 prefix_g = TRUE;
3870 extra = 2;
3871 }
3872 }
3842 lv.ll_name += 2; 3873 lv.ll_name += 2;
3843 } 3874 }
3844 len = (int)(end - lv.ll_name); 3875 len = (int)(end - lv.ll_name);
3845 } 3876 }
3846 if (len <= 0) 3877 if (len <= 0)
3916 name[0] = K_SPECIAL; 3947 name[0] = K_SPECIAL;
3917 name[1] = KS_EXTRA; 3948 name[1] = KS_EXTRA;
3918 name[2] = (int)KE_SNR; 3949 name[2] = (int)KE_SNR;
3919 if (vim9script || lead > 3) // If it's "<SID>" 3950 if (vim9script || lead > 3) // If it's "<SID>"
3920 STRCPY(name + 3, sid_buf); 3951 STRCPY(name + 3, sid_buf);
3952 }
3953 else if (prefix_g)
3954 {
3955 name[0] = 'g';
3956 name[1] = ':';
3921 } 3957 }
3922 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len); 3958 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3923 name[lead + extra + len] = NUL; 3959 name[lead + extra + len] = NUL;
3924 } 3960 }
3925 *pp = end; 3961 *pp = end;