((char_u **)gap->ga_data)[gap->ga_len - 1];
((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
ga_clear_strings(gap);
+ ga_clear(freegap);
}
else
{
dictitem_T *di = find_var_in_ht(ht, 0, lp->ll_name, TRUE);
if (di == NULL)
- // variable is not found
+ // script is autoloaded. So variable will be found later
goto success;
*dip = di;
v9.CheckScriptFailure(lines, 'E1333: Cannot access protected variable "_Id" in class "Test2"', 5)
enddef
+" Test for using lambda block in classes
+def Test_lambda_block_in_class()
+ # This used to crash Vim
+ var lines =<< trim END
+ vim9script
+ class IdClass1
+ const Id: func(number): number = (num: number): number => {
+ # Return a ID
+ return num * 10
+ }
+ endclass
+ var id = IdClass1.new()
+ assert_equal(20, id.Id(2))
+ END
+ v9.CheckScriptSuccess(lines)
+
+ # This used to crash Vim
+ lines =<< trim END
+ vim9script
+ class IdClass2
+ static const Id: func(number): number = (num: number): number => {
+ # Return a ID
+ return num * 2
+ }
+ endclass
+ assert_equal(16, IdClass2.Id(8))
+ END
+ v9.CheckScriptSuccess(lines)
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
v9.CheckSourceSuccess(lines)
enddef
+" Test for using lambda block in enums
+def Test_lambda_block_in_enum()
+ # This used to crash Vim
+ var lines =<< trim END
+ vim9script
+ enum IdEnum1
+ ID1
+ const Id: func(number): number = (num: number): number => {
+ # Return a ID
+ return num / 2
+ }
+ endenum
+ assert_equal(5, IdEnum1.ID1.Id(10))
+ END
+ v9.CheckScriptSuccess(lines)
+
+ # This used to crash Vim
+ lines =<< trim END
+ vim9script
+ enum IdEnum2
+ ID1
+ static const Id: func(number): number = (num: number): number => {
+ # Return a ID
+ return num + 2
+ }
+ endenum
+ assert_equal(12, IdEnum2.Id(10))
+ END
+ v9.CheckScriptSuccess(lines)
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
&rtp = save_rtp
enddef
+" Test for autoloading an imported dict func
+def Test_autoload_import_dict_func()
+ mkdir('Xdir/autoload', 'pR')
+ var lines =<< trim END
+ vim9script
+ export var al_exported_nr: number = 33
+ def Al_AddNum(n: number)
+ al_exported_nr += n
+ enddef
+ export var al_exportedDict: dict<func> = {Fn: Al_AddNum}
+ END
+ writefile(lines, 'Xdir/autoload/Xdictfunc.vim')
+
+ var save_rtp = &rtp
+ exe 'set rtp^=' .. getcwd() .. '/Xdir'
+ lines =<< trim END
+ import './Xdir/autoload/Xdictfunc.vim'
+ call Xdictfunc#al_exportedDict.Fn(5)
+ call assert_equal(38, Xdictfunc#al_exported_nr)
+ call call(Xdictfunc#al_exportedDict.Fn, [3])
+ call assert_equal(41, Xdictfunc#al_exported_nr)
+ END
+ v9.CheckScriptSuccess(lines)
+ &rtp = save_rtp
+enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
v9.CheckSourceSuccess(lines)
enddef
+" Test for evaluating a lambda block from a string
+def Test_eval_lambda_block()
+ var lines =<< trim END
+ vim9script
+ var Fn = eval("(x: number): number => {\nreturn x * 2\n}")
+ assert_equal(6, Fn(3))
+ END
+ v9.CheckSourceSuccess(lines)
+enddef
+
" Keep this last, it messes up highlighting.
def Test_substitute_cmd()
new
char_u *name;
int lnum_save = -1;
linenr_T sourcing_lnum_top = SOURCING_LNUM;
+ char_u *line_arg = NULL;
*arg = skipwhite(*arg + 1);
if (**arg == '|' || !ends_excmd2(start, *arg))
return FAIL;
}
+ // When there is a line break use what follows for the lambda body.
+ // Makes lambda body initializers work for object and enum member
+ // variables.
+ if (**arg == '\n')
+ line_arg = *arg + 1;
+
CLEAR_FIELD(eap);
eap.cmdidx = CMD_block;
eap.forceit = FALSE;
}
ga_init2(&newlines, sizeof(char_u *), 10);
- if (get_function_body(&eap, &newlines, NULL,
+ if (get_function_body(&eap, &newlines, line_arg,
&evalarg->eval_tofree_ga) == FAIL)
goto erret;
for (idx = 0; idx < newlines.ga_len; ++idx)
{
- char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
+ char_u *p = ((char_u **)newlines.ga_data)[idx];
+ if (p == NULL)
+ // comment line in the lambda body
+ continue;
+
+ p = skipwhite(p);
if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
goto erret;
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 263,
/**/
262,
/**/