mirror of
https://github.com/vim/vim.git
synced 2025-09-23 03:43:49 -04:00
patch 8.2.0312: Vim9: insufficient script tests
Problem: Vim9: insufficient script tests. Solution: Add more tests. Make "import * as Name" work.
This commit is contained in:
149
src/vim9script.c
149
src/vim9script.c
@@ -150,6 +150,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.
|
||||
@@ -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
|
||||
|
Reference in New Issue
Block a user