comparison src/userfunc.c @ 26980:8796f1384750 v8.2.4019

patch 8.2.4019: Vim9: import mechanism is too complicated Commit: https://github.com/vim/vim/commit/d5f400c607182db6d4fbe2964471d796277f67e8 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Jan 6 21:10:28 2022 +0000 patch 8.2.4019: Vim9: import mechanism is too complicated Problem: Vim9: import mechanism is too complicated. Solution: Do not use the Javascript mechanism but a much simpler one.
author Bram Moolenaar <Bram@vim.org>
date Thu, 06 Jan 2022 22:15:04 +0100
parents ac75c145f0a9
children 4b8d836db103
comparison
equal deleted inserted replaced
26979:2fb4968983af 26980:8796f1384750
1606 p = name + 2; 1606 p = name + 2;
1607 len -= 2; 1607 len -= 2;
1608 } 1608 }
1609 import = find_imported(p, len, NULL); 1609 import = find_imported(p, len, NULL);
1610 1610
1611 // imported variable from another script 1611 // imported function from another script
1612 if (import != NULL) 1612 if (import != NULL)
1613 { 1613 {
1614 if (import->imp_funcname != NULL) 1614 name[len] = NUL;
1615 { 1615 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
1616 s = import->imp_funcname; 1616 name[len] = cc;
1617 *lenp = (int)STRLEN(s); 1617 *lenp = 0;
1618 return s; 1618 return (char_u *)""; // just in case
1619 }
1620 if (import->imp_flags & IMP_FLAGS_STAR)
1621 {
1622 name[len] = NUL;
1623 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
1624 name);
1625 name[len] = cc;
1626 *lenp = 0;
1627 return (char_u *)""; // just in case
1628 }
1629 else
1630 {
1631 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
1632 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1633 + import->imp_var_vals_idx;
1634 tv = sv->sv_tv;
1635 if (type != NULL)
1636 *type = sv->sv_type;
1637 did_type = TRUE;
1638 }
1639 } 1619 }
1640 } 1620 }
1641 1621
1642 if (tv != NULL) 1622 if (tv != NULL)
1643 { 1623 {
1671 1651
1672 if (s != NULL) 1652 if (s != NULL)
1673 { 1653 {
1674 if (!did_type && type != NULL && ht == get_script_local_ht()) 1654 if (!did_type && type != NULL && ht == get_script_local_ht())
1675 { 1655 {
1676 svar_T *sv = find_typval_in_script(tv); 1656 svar_T *sv = find_typval_in_script(tv, 0);
1677 1657
1678 if (sv != NULL) 1658 if (sv != NULL)
1679 *type = sv->sv_type; 1659 *type = sv->sv_type;
1680 } 1660 }
1681 return s; 1661 return s;
1903 * Find a function by name, return pointer to it in ufuncs. 1883 * Find a function by name, return pointer to it in ufuncs.
1904 * When "is_global" is true don't find script-local or imported functions. 1884 * When "is_global" is true don't find script-local or imported functions.
1905 * Return NULL for unknown function. 1885 * Return NULL for unknown function.
1906 */ 1886 */
1907 ufunc_T * 1887 ufunc_T *
1908 find_func_even_dead(char_u *name, int is_global, cctx_T *cctx) 1888 find_func_even_dead(char_u *name, int is_global, cctx_T *cctx UNUSED)
1909 { 1889 {
1910 hashitem_T *hi; 1890 hashitem_T *hi;
1911 ufunc_T *func; 1891 ufunc_T *func;
1912 imported_T *imported;
1913 1892
1914 if (!is_global) 1893 if (!is_global)
1915 { 1894 {
1916 char_u *after_script = NULL;
1917 long sid = 0;
1918 int find_script_local = in_vim9script() && eval_isnamec1(*name) 1895 int find_script_local = in_vim9script() && eval_isnamec1(*name)
1919 && (name[1] != ':' || *name == 's'); 1896 && (name[1] != ':' || *name == 's');
1920 1897
1921 if (find_script_local) 1898 if (find_script_local)
1922 { 1899 {
1923 // Find script-local function before global one. 1900 // Find script-local function before global one.
1924 func = find_func_with_sid(name[0] == 's' && name[1] == ':' 1901 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
1925 ? name + 2 : name, current_sctx.sc_sid); 1902 ? name + 2 : name, current_sctx.sc_sid);
1926 if (func != NULL) 1903 if (func != NULL)
1927 return func; 1904 return func;
1928 }
1929
1930 if (name[0] == K_SPECIAL
1931 && name[1] == KS_EXTRA
1932 && name[2] == KE_SNR)
1933 {
1934 // Caller changes s: to <SNR>99_name.
1935
1936 after_script = name + 3;
1937 sid = getdigits(&after_script);
1938 if (*after_script == '_')
1939 ++after_script;
1940 else
1941 after_script = NULL;
1942 }
1943 if (find_script_local || after_script != NULL)
1944 {
1945 // Find imported function before global one.
1946 if (after_script != NULL && sid != current_sctx.sc_sid)
1947 imported = find_imported_in_script(after_script, 0, sid);
1948 else
1949 imported = find_imported(after_script == NULL
1950 ? name : after_script, 0, cctx);
1951 if (imported != NULL && imported->imp_funcname != NULL)
1952 {
1953 hi = hash_find(&func_hashtab, imported->imp_funcname);
1954 if (!HASHITEM_EMPTY(hi))
1955 return HI2UF(hi);
1956 }
1957 } 1905 }
1958 } 1906 }
1959 1907
1960 hi = hash_find(&func_hashtab, 1908 hi = hash_find(&func_hashtab,
1961 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name); 1909 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
4255 emsg_funcname(e_invalid_argument_str, arg); 4203 emsg_funcname(e_invalid_argument_str, arg);
4256 4204
4257 // In Vim9 script a function cannot have the same name as a 4205 // In Vim9 script a function cannot have the same name as a
4258 // variable. 4206 // variable.
4259 if (vim9script && *arg == K_SPECIAL 4207 if (vim9script && *arg == K_SPECIAL
4260 && eval_variable(name_base, (int)STRLEN(name_base), NULL, NULL, 4208 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4261 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT 4209 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
4262 + EVAL_VAR_NO_FUNC) == OK) 4210 + EVAL_VAR_NO_FUNC) == OK)
4263 { 4211 {
4264 semsg(_(e_redefining_script_item_str), name_base); 4212 semsg(_(e_redefining_script_item_str), name_base);
4265 goto ret_free; 4213 goto ret_free;
4266 } 4214 }