comparison src/userfunc.c @ 27068:6a4fc2e6e6eb v8.2.4063

patch 8.2.4063: Vim9: exported function in autoload script not found Commit: https://github.com/vim/vim/commit/b8822442d716df0230c79531132e530e95cc17e3 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jan 11 15:24:05 2022 +0000 patch 8.2.4063: Vim9: exported function in autoload script not found Problem: Vim9: exported function in autoload script not found. (Yegappan Lakshmanan) Solution: Use the autoload prefix to search for the function.
author Bram Moolenaar <Bram@vim.org>
date Tue, 11 Jan 2022 16:30:03 +0100
parents 58cfcd3ed15b
children ceff6a546748
comparison
equal deleted inserted replaced
27067:89bc175b25a5 27068:6a4fc2e6e6eb
1869 * Find a function "name" in script "sid". 1869 * Find a function "name" in script "sid".
1870 */ 1870 */
1871 static ufunc_T * 1871 static ufunc_T *
1872 find_func_with_sid(char_u *name, int sid) 1872 find_func_with_sid(char_u *name, int sid)
1873 { 1873 {
1874 hashitem_T *hi; 1874 hashitem_T *hi;
1875 char_u buffer[200]; 1875 char_u buffer[200];
1876 1876
1877 if (!SCRIPT_ID_VALID(sid))
1878 return NULL; // not in a script
1879
1880 // A script-local function is stored as "<SNR>99_name".
1877 buffer[0] = K_SPECIAL; 1881 buffer[0] = K_SPECIAL;
1878 buffer[1] = KS_EXTRA; 1882 buffer[1] = KS_EXTRA;
1879 buffer[2] = (int)KE_SNR; 1883 buffer[2] = (int)KE_SNR;
1880 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s", 1884 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1881 (long)sid, name); 1885 (long)sid, name);
1882 hi = hash_find(&func_hashtab, buffer); 1886 hi = hash_find(&func_hashtab, buffer);
1883 if (!HASHITEM_EMPTY(hi)) 1887 if (!HASHITEM_EMPTY(hi))
1884 return HI2UF(hi); 1888 return HI2UF(hi);
1889 return NULL;
1890 }
1891
1892 /*
1893 * Find a function "name" in script "sid" prefixing the autoload prefix.
1894 */
1895 static ufunc_T *
1896 find_func_with_prefix(char_u *name, int sid)
1897 {
1898 hashitem_T *hi;
1899 char_u buffer[200];
1900 scriptitem_T *si;
1901
1902 if (vim_strchr(name, AUTOLOAD_CHAR) != 0)
1903 return NULL; // already has the prefix
1904 if (!SCRIPT_ID_VALID(sid))
1905 return NULL; // not in a script
1906 si = SCRIPT_ITEM(sid);
1907 if (si->sn_autoload_prefix != NULL)
1908 {
1909 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1910 char_u *auto_name;
1911
1912 // An exported function in an autoload script is stored as
1913 // "dir#path#name".
1914 if (len < sizeof(buffer))
1915 auto_name = buffer;
1916 else
1917 auto_name = alloc(len);
1918 if (auto_name != NULL)
1919 {
1920 vim_snprintf((char *)auto_name, len, "%s%s",
1921 si->sn_autoload_prefix, name);
1922 hi = hash_find(&func_hashtab, auto_name);
1923 if (auto_name != buffer)
1924 vim_free(auto_name);
1925 if (!HASHITEM_EMPTY(hi))
1926 return HI2UF(hi);
1927 }
1928 }
1885 1929
1886 return NULL; 1930 return NULL;
1887 } 1931 }
1888 1932
1889 /* 1933 /*
1915 hi = hash_find(&func_hashtab, 1959 hi = hash_find(&func_hashtab,
1916 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name); 1960 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
1917 if (!HASHITEM_EMPTY(hi)) 1961 if (!HASHITEM_EMPTY(hi))
1918 return HI2UF(hi); 1962 return HI2UF(hi);
1919 1963
1920 return NULL; 1964 // Find autoload function if this is an autoload script.
1965 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
1966 ? name + 2 : name, current_sctx.sc_sid);
1921 } 1967 }
1922 1968
1923 /* 1969 /*
1924 * Find a function by name, return pointer to it in ufuncs. 1970 * Find a function by name, return pointer to it in ufuncs.
1925 * "cctx" is passed in a :def function to find imported functions. 1971 * "cctx" is passed in a :def function to find imported functions.