Skip to content

Commit f2d5c24

Browse files
committed
patch 8.2.0312: Vim9: insufficient script tests
Problem: Vim9: insufficient script tests. Solution: Add more tests. Make "import * as Name" work.
1 parent 750802b commit f2d5c24

5 files changed

Lines changed: 159 additions & 72 deletions

File tree

src/proto/vim9script.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ void ex_vim9script(exarg_T *eap);
44
void ex_export(exarg_T *eap);
55
void free_imports(int sid);
66
void ex_import(exarg_T *eap);
7-
char_u *handle_import(char_u *arg_start, garray_T *gap, int sid);
7+
int find_exported(int sid, char_u **argp, int *name_len, ufunc_T **ufunc, type_T **type);
8+
char_u *handle_import(char_u *arg_start, garray_T *gap, int import_sid);
89
/* vim: set ft=c : */

src/testdir/test_vim9_script.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,28 @@ def Test_vim9script()
330330
unlet g:imported_added
331331
unlet g:imported_func
332332
unlet g:imported_name g:imported_name_appended
333+
delete('Ximport.vim')
334+
335+
let import_star_as_lines =<< trim END
336+
vim9script
337+
import * as Export from './Xexport.vim'
338+
def UseExport()
339+
g:imported = Export.exported
340+
enddef
341+
UseExport()
342+
END
343+
writefile(import_star_as_lines, 'Ximport.vim')
344+
source Ximport.vim
345+
assert_equal(9876, g:imported)
346+
347+
let import_star_lines =<< trim END
348+
vim9script
349+
import * from './Xexport.vim'
350+
g:imported = exported
351+
END
352+
writefile(import_star_lines, 'Ximport.vim')
353+
assert_fails('source Ximport.vim', 'E1045:')
354+
333355
delete('Ximport.vim')
334356
delete('Xexport.vim')
335357

@@ -352,6 +374,7 @@ def Test_vim9script_fails()
352374
CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
353375
CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
354376
CheckScriptFailure(['export let some = 123'], 'E1042:')
377+
CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1042:')
355378
CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:')
356379
CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
357380

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ static char *(features[]) =
738738

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
312,
741743
/**/
742744
311,
743745
/**/

src/vim9compile.c

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,11 @@ find_imported(char_u *name, size_t len, cctx_T *cctx)
15221522
* Generate an instruction to load script-local variable "name".
15231523
*/
15241524
static int
1525-
compile_load_scriptvar(cctx_T *cctx, char_u *name)
1525+
compile_load_scriptvar(
1526+
cctx_T *cctx,
1527+
char_u *name, // variable NUL terminated
1528+
char_u *start, // start of variable
1529+
char_u **end) // end of variable
15261530
{
15271531
scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
15281532
int idx = get_script_item_idx(current_sctx.sc_sid, name, FALSE);
@@ -1546,11 +1550,40 @@ compile_load_scriptvar(cctx_T *cctx, char_u *name)
15461550
import = find_imported(name, 0, cctx);
15471551
if (import != NULL)
15481552
{
1549-
// TODO: check this is a variable, not a function
1550-
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1551-
import->imp_sid,
1552-
import->imp_var_vals_idx,
1553-
import->imp_type);
1553+
if (import->imp_all)
1554+
{
1555+
char_u *p = skipwhite(*end);
1556+
int name_len;
1557+
ufunc_T *ufunc;
1558+
type_T *type;
1559+
1560+
// Used "import * as Name", need to lookup the member.
1561+
if (*p != '.')
1562+
{
1563+
semsg(_("E1060: expected dot after name: %s"), start);
1564+
return FAIL;
1565+
}
1566+
++p;
1567+
1568+
idx = find_exported(import->imp_sid, &p, &name_len, &ufunc, &type);
1569+
// TODO: what if it is a function?
1570+
if (idx < 0)
1571+
return FAIL;
1572+
*end = p;
1573+
1574+
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1575+
import->imp_sid,
1576+
idx,
1577+
type);
1578+
}
1579+
else
1580+
{
1581+
// TODO: check this is a variable, not a function
1582+
generate_VIM9SCRIPT(cctx, ISN_LOADSCRIPT,
1583+
import->imp_sid,
1584+
import->imp_var_vals_idx,
1585+
import->imp_type);
1586+
}
15541587
return OK;
15551588
}
15561589

@@ -1564,10 +1597,11 @@ compile_load_scriptvar(cctx_T *cctx, char_u *name)
15641597
* When "error" is FALSE do not give an error when not found.
15651598
*/
15661599
static int
1567-
compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error)
1600+
compile_load(char_u **arg, char_u *end_arg, cctx_T *cctx, int error)
15681601
{
15691602
type_T *type;
15701603
char_u *name;
1604+
char_u *end = end_arg;
15711605
int res = FAIL;
15721606

15731607
if (*(*arg + 1) == ':')
@@ -1589,7 +1623,7 @@ compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error)
15891623
}
15901624
else if (**arg == 's')
15911625
{
1592-
res = compile_load_scriptvar(cctx, name);
1626+
res = compile_load_scriptvar(cctx, name, NULL, NULL);
15931627
}
15941628
else
15951629
{
@@ -1645,7 +1679,7 @@ compile_load(char_u **arg, char_u *end, cctx_T *cctx, int error)
16451679
else if (SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
16461680
== SCRIPT_VERSION_VIM9)
16471681
// in Vim9 script "var" can be script-local.
1648-
res = compile_load_scriptvar(cctx, name);
1682+
res = compile_load_scriptvar(cctx, name, *arg, &end);
16491683
}
16501684
}
16511685
if (gen_load)
@@ -3412,7 +3446,7 @@ compile_assignment(char_u *arg, exarg_T *eap, cmdidx_T cmdidx, cctx_T *cctx)
34123446
generate_LOAD(cctx, ISN_LOADG, 0, name + 2, type);
34133447
break;
34143448
case dest_script:
3415-
compile_load_scriptvar(cctx, name + (name[1] == ':' ? 2 : 0));
3449+
compile_load_scriptvar(cctx, name + (name[1] == ':' ? 2 : 0), NULL, NULL);
34163450
break;
34173451
case dest_env:
34183452
// Include $ in the name here

src/vim9script.c

Lines changed: 88 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,88 @@ ex_import(exarg_T *eap)
150150
}
151151
}
152152

153+
/*
154+
* Find an exported item in "sid" matching the name at "*argp".
155+
* When it is a variable return the index.
156+
* When it is a user function return "*ufunc".
157+
* When not found returns -1 and "*ufunc" is NULL.
158+
*/
159+
int
160+
find_exported(
161+
int sid,
162+
char_u **argp,
163+
int *name_len,
164+
ufunc_T **ufunc,
165+
type_T **type)
166+
{
167+
char_u *name = *argp;
168+
char_u *arg = *argp;
169+
int cc;
170+
int idx = -1;
171+
svar_T *sv;
172+
scriptitem_T *script = SCRIPT_ITEM(sid);
173+
174+
// isolate one name
175+
while (eval_isnamec1(*arg))
176+
++arg;
177+
*name_len = (int)(arg - name);
178+
179+
// find name in "script"
180+
// TODO: also find script-local user function
181+
cc = *arg;
182+
*arg = NUL;
183+
idx = get_script_item_idx(sid, name, FALSE);
184+
if (idx >= 0)
185+
{
186+
sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
187+
if (!sv->sv_export)
188+
{
189+
semsg(_("E1049: Item not exported in script: %s"), name);
190+
*arg = cc;
191+
return -1;
192+
}
193+
*type = sv->sv_type;
194+
*ufunc = NULL;
195+
}
196+
else
197+
{
198+
char_u buffer[200];
199+
char_u *funcname;
200+
201+
// it could be a user function.
202+
if (STRLEN(name) < sizeof(buffer) - 10)
203+
funcname = buffer;
204+
else
205+
{
206+
funcname = alloc(STRLEN(name) + 10);
207+
if (funcname == NULL)
208+
{
209+
*arg = cc;
210+
return -1;
211+
}
212+
}
213+
funcname[0] = K_SPECIAL;
214+
funcname[1] = KS_EXTRA;
215+
funcname[2] = (int)KE_SNR;
216+
sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
217+
*ufunc = find_func(funcname, NULL);
218+
if (funcname != buffer)
219+
vim_free(funcname);
220+
221+
if (*ufunc == NULL)
222+
{
223+
semsg(_("E1048: Item not found in script: %s"), name);
224+
*arg = cc;
225+
return -1;
226+
}
227+
}
228+
*arg = cc;
229+
arg = skipwhite(arg);
230+
*argp = arg;
231+
232+
return idx;
233+
}
234+
153235
/*
154236
* Handle an ":import" command and add the resulting imported_T to "gap", when
155237
* not NULL, or script "import_sid" sn_imports.
@@ -289,77 +371,22 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid)
289371
}
290372
else
291373
{
292-
scriptitem_T *script = SCRIPT_ITEM(sid);
293-
294374
arg = arg_start;
295375
if (*arg == '{')
296376
arg = skipwhite(arg + 1);
297377
for (;;)
298378
{
299379
char_u *name = arg;
300380
int name_len;
301-
int cc;
302381
int idx;
303-
svar_T *sv;
304382
imported_T *imported;
305-
ufunc_T *ufunc;
383+
ufunc_T *ufunc = NULL;
384+
type_T *type;
306385

307-
// isolate one name
308-
while (eval_isnamec1(*arg))
309-
++arg;
310-
name_len = (int)(arg - name);
386+
idx = find_exported(sid, &arg, &name_len, &ufunc, &type);
311387

312-
// find name in "script"
313-
// TODO: also find script-local user function
314-
cc = *arg;
315-
*arg = NUL;
316-
idx = get_script_item_idx(sid, name, FALSE);
317-
if (idx >= 0)
318-
{
319-
sv = ((svar_T *)script->sn_var_vals.ga_data) + idx;
320-
if (!sv->sv_export)
321-
{
322-
semsg(_("E1049: Item not exported in script: %s"), name);
323-
*arg = cc;
324-
return NULL;
325-
}
326-
ufunc = NULL;
327-
}
328-
else
329-
{
330-
char_u buffer[200];
331-
char_u *funcname;
332-
333-
// it could be a user function.
334-
if (STRLEN(name) < sizeof(buffer) - 10)
335-
funcname = buffer;
336-
else
337-
{
338-
funcname = alloc(STRLEN(name) + 10);
339-
if (funcname == NULL)
340-
{
341-
*arg = cc;
342-
return NULL;
343-
}
344-
}
345-
funcname[0] = K_SPECIAL;
346-
funcname[1] = KS_EXTRA;
347-
funcname[2] = (int)KE_SNR;
348-
sprintf((char *)funcname + 3, "%ld_%s", (long)sid, name);
349-
ufunc = find_func(funcname, NULL);
350-
if (funcname != buffer)
351-
vim_free(funcname);
352-
353-
if (ufunc == NULL)
354-
{
355-
semsg(_("E1048: Item not found in script: %s"), name);
356-
*arg = cc;
357-
return NULL;
358-
}
359-
sv = NULL;
360-
}
361-
*arg = cc;
362-
arg = skipwhite(arg);
388+
if (idx < 0 && ufunc == NULL)
389+
return NULL;
363390

364391
imported = new_imported(gap != NULL ? gap
365392
: &SCRIPT_ITEM(import_sid)->sn_imports);
@@ -372,7 +399,7 @@ handle_import(char_u *arg_start, garray_T *gap, int import_sid)
372399
imported->imp_sid = sid;
373400
if (idx >= 0)
374401
{
375-
imported->imp_type = sv->sv_type;
402+
imported->imp_type = type;
376403
imported->imp_var_vals_idx = idx;
377404
}
378405
else

0 commit comments

Comments
 (0)