diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-02-23 21:25:54 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-02-23 21:25:54 +0100 |
commit | f2d5c240a56853c0bbbc7979e9bff095de6c73ec (patch) | |
tree | 1244a029c4c623cd9e48743af1e7d442f9562402 /src/vim9script.c | |
parent | 750802b55c6edda4d3bc78c41ad0a25a3450a557 (diff) | |
download | vim-git-f2d5c240a56853c0bbbc7979e9bff095de6c73ec.tar.gz |
patch 8.2.0312: Vim9: insufficient script testsv8.2.0312
Problem: Vim9: insufficient script tests.
Solution: Add more tests. Make "import * as Name" work.
Diffstat (limited to 'src/vim9script.c')
-rw-r--r-- | src/vim9script.c | 149 |
1 files changed, 88 insertions, 61 deletions
diff --git a/src/vim9script.c b/src/vim9script.c index b73b592a2..97b714985 100644 --- a/src/vim9script.c +++ b/src/vim9script.c @@ -151,6 +151,88 @@ ex_import(exarg_T *eap) } /* + * Find an exported item in "sid" matching the name at "*argp". + * When it is a variable return the index. + * When it is a user function return "*ufunc". + * When not found returns -1 and "*ufunc" is NULL. + */ + int +find_exported( + int sid, + char_u **argp, + int *name_len, + ufunc_T **ufunc, + type_T **type) +{ + char_u *name = *argp; + char_u *arg = *argp; + int cc; + int idx = -1; + svar_T *sv; + scriptitem_T *script = SCRIPT_ITEM(sid); + + // isolate one name + while (eval_isnamec1(*arg)) + ++arg; + *name_len = (int)(arg - name); + + // find name in "script" + // TODO: also find script-local user function + cc = *arg; + *arg = NUL; + idx = get_script_item_idx(sid, name, FALSE); + if (idx >= 0) + { + sv = ((svar_T *)script->sn_var_vals.ga_data) + idx; + if (!sv->sv_export) + { + semsg(_("E1049: Item not exported in script: %s"), name); + *arg = cc; + return -1; + } + *type = sv->sv_type; + *ufunc = NULL; + } + else + { + char_u buffer[200]; + char_u *funcname; + + // it could be a user function. + if (STRLEN(name) < sizeof(buffer) - 10) + funcname = buffer; + else + { + funcname = alloc(STRLEN(name) + 10); + if (funcname == NULL) + { + *arg = cc; + return -1; + } + } + funcname[0] = K_SPECIAL; + funcname[1] = KS_EXTRA; + funcname[2] = (int)KE_SNR; + sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name); + *ufunc = find_func(funcname, NULL); + if (funcname != buffer) + vim_free(funcname); + + if (*ufunc == NULL) + { + semsg(_("E1048: Item not found in script: %s"), name); + *arg = cc; + return -1; + } + } + *arg = cc; + arg = skipwhite(arg); + *argp = arg; + + return idx; +} + +/* * Handle an ":import" command and add the resulting imported_T to "gap", when * not NULL, or script "import_sid" sn_imports. * Returns a pointer to after the command or NULL in case of failure @@ -289,8 +371,6 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid) } else { - scriptitem_T *script = SCRIPT_ITEM(sid); - arg = arg_start; if (*arg == '{') arg = skipwhite(arg + 1); @@ -298,68 +378,15 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid) { char_u *name = arg; int name_len; - int cc; int idx; - svar_T *sv; imported_T *imported; - ufunc_T *ufunc; + ufunc_T *ufunc = NULL; + type_T *type; - // isolate one name - while (eval_isnamec1(*arg)) - ++arg; - name_len = (int)(arg - name); + idx = find_exported(sid, &arg, &name_len, &ufunc, &type); - // find name in "script" - // TODO: also find script-local user function - cc = *arg; - *arg = NUL; - idx = get_script_item_idx(sid, name, FALSE); - if (idx >= 0) - { - sv = ((svar_T *)script->sn_var_vals.ga_data) + idx; - if (!sv->sv_export) - { - semsg(_("E1049: Item not exported in script: %s"), name); - *arg = cc; - return NULL; - } - ufunc = NULL; - } - else - { - char_u buffer[200]; - char_u *funcname; - - // it could be a user function. - if (STRLEN(name) < sizeof(buffer) - 10) - funcname = buffer; - else - { - funcname = alloc(STRLEN(name) + 10); - if (funcname == NULL) - { - *arg = cc; - return NULL; - } - } - funcname[0] = K_SPECIAL; - funcname[1] = KS_EXTRA; - funcname[2] = (int)KE_SNR; - sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name); - ufunc = find_func(funcname, NULL); - if (funcname != buffer) - vim_free(funcname); - - if (ufunc == NULL) - { - semsg(_("E1048: Item not found in script: %s"), name); - *arg = cc; - return NULL; - } - sv = NULL; - } - *arg = cc; - arg = skipwhite(arg); + if (idx < 0 && ufunc == NULL) + return NULL; imported = new_imported(gap != NULL ? gap : &SCRIPT_ITEM(import_sid)->sn_imports); @@ -372,7 +399,7 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid) imported->imp_sid = sid; if (idx >= 0) { - imported->imp_type = sv->sv_type; + imported->imp_type = type; imported->imp_var_vals_idx = idx; } else |