Skip to content
This repository was archived by the owner on Apr 27, 2022. It is now read-only.

Commit f9a84f6

Browse files
committed
Add more builtin function return types.
Add a test for using void value.
1 parent 07f66a4 commit f9a84f6

File tree

4 files changed

+113
-102
lines changed

4 files changed

+113
-102
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ full code is below):
4646
| Lua | 0.078817 |
4747
| Vim new | 0.073595 |
4848

49-
That looks very hopeful! It's just one example, but it shows how much
49+
That looks very promising! It's just one example, but it shows how much
5050
we can gain, and also that Vim script can be faster than builtin
5151
interfaces.
5252

5353
In practice the script would not do something useless as counting but change
54-
the text. For example, re-indent all the lines. Example:
54+
the text. For example, re-indent all the lines:
5555

5656
``` vim
5757
let totallen = 0

src/evalfunc.c

Lines changed: 99 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -306,133 +306,133 @@ static funcentry_T global_functions[] =
306306
{
307307
#ifdef FEAT_FLOAT
308308
{"abs", 1, 1, FEARG_1, &t_any, f_abs},
309-
{"acos", 1, 1, FEARG_1, &t_any, f_acos}, // WJMc
309+
{"acos", 1, 1, FEARG_1, &t_float, f_acos}, // WJMc
310310
#endif
311311
{"add", 2, 2, FEARG_1, &t_any, f_add},
312-
{"and", 2, 2, FEARG_1, &t_any, f_and},
313-
{"append", 2, 2, FEARG_LAST, &t_any, f_append},
314-
{"appendbufline", 3, 3, FEARG_LAST, &t_any, f_appendbufline},
315-
{"argc", 0, 1, 0, &t_any, f_argc},
316-
{"argidx", 0, 0, 0, &t_any, f_argidx},
317-
{"arglistid", 0, 2, 0, &t_any, f_arglistid},
312+
{"and", 2, 2, FEARG_1, &t_number, f_and},
313+
{"append", 2, 2, FEARG_LAST, &t_number, f_append},
314+
{"appendbufline", 3, 3, FEARG_LAST, &t_number, f_appendbufline},
315+
{"argc", 0, 1, 0, &t_number, f_argc},
316+
{"argidx", 0, 0, 0, &t_number, f_argidx},
317+
{"arglistid", 0, 2, 0, &t_number, f_arglistid},
318318
{"argv", 0, 2, 0, &t_any, f_argv},
319319
#ifdef FEAT_FLOAT
320-
{"asin", 1, 1, FEARG_1, &t_any, f_asin}, // WJMc
321-
#endif
322-
{"assert_beeps", 1, 2, FEARG_1, &t_any, f_assert_beeps},
323-
{"assert_equal", 2, 3, FEARG_2, &t_any, f_assert_equal},
324-
{"assert_equalfile", 2, 2, FEARG_1, &t_any, f_assert_equalfile},
325-
{"assert_exception", 1, 2, 0, &t_any, f_assert_exception},
326-
{"assert_fails", 1, 3, FEARG_1, &t_any, f_assert_fails},
327-
{"assert_false", 1, 2, FEARG_1, &t_any, f_assert_false},
328-
{"assert_inrange", 3, 4, FEARG_3, &t_any, f_assert_inrange},
329-
{"assert_match", 2, 3, FEARG_2, &t_any, f_assert_match},
330-
{"assert_notequal", 2, 3, FEARG_2, &t_any, f_assert_notequal},
331-
{"assert_notmatch", 2, 3, FEARG_2, &t_any, f_assert_notmatch},
332-
{"assert_report", 1, 1, FEARG_1, &t_any, f_assert_report},
333-
{"assert_true", 1, 2, FEARG_1, &t_any, f_assert_true},
320+
{"asin", 1, 1, FEARG_1, &t_float, f_asin}, // WJMc
321+
#endif
322+
{"assert_beeps", 1, 2, FEARG_1, &t_number, f_assert_beeps},
323+
{"assert_equal", 2, 3, FEARG_2, &t_number, f_assert_equal},
324+
{"assert_equalfile", 2, 2, FEARG_1, &t_number, f_assert_equalfile},
325+
{"assert_exception", 1, 2, 0, &t_number, f_assert_exception},
326+
{"assert_fails", 1, 3, FEARG_1, &t_number, f_assert_fails},
327+
{"assert_false", 1, 2, FEARG_1, &t_number, f_assert_false},
328+
{"assert_inrange", 3, 4, FEARG_3, &t_number, f_assert_inrange},
329+
{"assert_match", 2, 3, FEARG_2, &t_number, f_assert_match},
330+
{"assert_notequal", 2, 3, FEARG_2, &t_number, f_assert_notequal},
331+
{"assert_notmatch", 2, 3, FEARG_2, &t_number, f_assert_notmatch},
332+
{"assert_report", 1, 1, FEARG_1, &t_number, f_assert_report},
333+
{"assert_true", 1, 2, FEARG_1, &t_number, f_assert_true},
334334
#ifdef FEAT_FLOAT
335-
{"atan", 1, 1, FEARG_1, &t_any, f_atan},
336-
{"atan2", 2, 2, FEARG_1, &t_any, f_atan2},
335+
{"atan", 1, 1, FEARG_1, &t_float, f_atan},
336+
{"atan2", 2, 2, FEARG_1, &t_float, f_atan2},
337337
#endif
338338
#ifdef FEAT_BEVAL
339-
{"balloon_gettext", 0, 0, 0, &t_any, f_balloon_gettext},
340-
{"balloon_show", 1, 1, FEARG_1, &t_any, f_balloon_show},
339+
{"balloon_gettext", 0, 0, 0, &t_string, f_balloon_gettext},
340+
{"balloon_show", 1, 1, FEARG_1, &t_void, f_balloon_show},
341341
# if defined(FEAT_BEVAL_TERM)
342-
{"balloon_split", 1, 1, FEARG_1, &t_any, f_balloon_split},
342+
{"balloon_split", 1, 1, FEARG_1, &t_list_string, f_balloon_split},
343343
# endif
344344
#endif
345-
{"browse", 4, 4, 0, &t_any, f_browse},
346-
{"browsedir", 2, 2, 0, &t_any, f_browsedir},
347-
{"bufadd", 1, 1, FEARG_1, &t_any, f_bufadd},
348-
{"bufexists", 1, 1, FEARG_1, &t_any, f_bufexists},
349-
{"buffer_exists", 1, 1, FEARG_1, &t_any, f_bufexists}, // obsolete
350-
{"buffer_name", 0, 1, FEARG_1, &t_any, f_bufname}, // obsolete
351-
{"buffer_number", 0, 1, FEARG_1, &t_any, f_bufnr}, // obsolete
352-
{"buflisted", 1, 1, FEARG_1, &t_any, f_buflisted},
353-
{"bufload", 1, 1, FEARG_1, &t_any, f_bufload},
354-
{"bufloaded", 1, 1, FEARG_1, &t_any, f_bufloaded},
355-
{"bufname", 0, 1, FEARG_1, &t_any, f_bufname},
356-
{"bufnr", 0, 2, FEARG_1, &t_any, f_bufnr},
357-
{"bufwinid", 1, 1, FEARG_1, &t_any, f_bufwinid},
358-
{"bufwinnr", 1, 1, FEARG_1, &t_any, f_bufwinnr},
359-
{"byte2line", 1, 1, FEARG_1, &t_any, f_byte2line},
360-
{"byteidx", 2, 2, FEARG_1, &t_any, f_byteidx},
361-
{"byteidxcomp", 2, 2, FEARG_1, &t_any, f_byteidxcomp},
345+
{"browse", 4, 4, 0, &t_string, f_browse},
346+
{"browsedir", 2, 2, 0, &t_string, f_browsedir},
347+
{"bufadd", 1, 1, FEARG_1, &t_number, f_bufadd},
348+
{"bufexists", 1, 1, FEARG_1, &t_number, f_bufexists},
349+
{"buffer_exists", 1, 1, FEARG_1, &t_number, f_bufexists}, // obsolete
350+
{"buffer_name", 0, 1, FEARG_1, &t_string, f_bufname}, // obsolete
351+
{"buffer_number", 0, 1, FEARG_1, &t_number, f_bufnr}, // obsolete
352+
{"buflisted", 1, 1, FEARG_1, &t_number, f_buflisted},
353+
{"bufload", 1, 1, FEARG_1, &t_void, f_bufload},
354+
{"bufloaded", 1, 1, FEARG_1, &t_number, f_bufloaded},
355+
{"bufname", 0, 1, FEARG_1, &t_string, f_bufname},
356+
{"bufnr", 0, 2, FEARG_1, &t_number, f_bufnr},
357+
{"bufwinid", 1, 1, FEARG_1, &t_number, f_bufwinid},
358+
{"bufwinnr", 1, 1, FEARG_1, &t_number, f_bufwinnr},
359+
{"byte2line", 1, 1, FEARG_1, &t_number, f_byte2line},
360+
{"byteidx", 2, 2, FEARG_1, &t_number, f_byteidx},
361+
{"byteidxcomp", 2, 2, FEARG_1, &t_number, f_byteidxcomp},
362362
{"call", 2, 3, FEARG_1, &t_any, f_call},
363363
#ifdef FEAT_FLOAT
364-
{"ceil", 1, 1, FEARG_1, &t_any, f_ceil},
364+
{"ceil", 1, 1, FEARG_1, &t_float, f_ceil},
365365
#endif
366366
#ifdef FEAT_JOB_CHANNEL
367-
{"ch_canread", 1, 1, FEARG_1, &t_any, f_ch_canread},
368-
{"ch_close", 1, 1, FEARG_1, &t_any, f_ch_close},
369-
{"ch_close_in", 1, 1, FEARG_1, &t_any, f_ch_close_in},
367+
{"ch_canread", 1, 1, FEARG_1, &t_number, f_ch_canread},
368+
{"ch_close", 1, 1, FEARG_1, &t_void, f_ch_close},
369+
{"ch_close_in", 1, 1, FEARG_1, &t_void, f_ch_close_in},
370370
{"ch_evalexpr", 2, 3, FEARG_1, &t_any, f_ch_evalexpr},
371371
{"ch_evalraw", 2, 3, FEARG_1, &t_any, f_ch_evalraw},
372-
{"ch_getbufnr", 2, 2, FEARG_1, &t_any, f_ch_getbufnr},
373-
{"ch_getjob", 1, 1, FEARG_1, &t_any, f_ch_getjob},
374-
{"ch_info", 1, 1, FEARG_1, &t_any, f_ch_info},
375-
{"ch_log", 1, 2, FEARG_1, &t_any, f_ch_log},
376-
{"ch_logfile", 1, 2, FEARG_1, &t_any, f_ch_logfile},
377-
{"ch_open", 1, 2, FEARG_1, &t_any, f_ch_open},
378-
{"ch_read", 1, 2, FEARG_1, &t_any, f_ch_read},
379-
{"ch_readblob", 1, 2, FEARG_1, &t_any, f_ch_readblob},
380-
{"ch_readraw", 1, 2, FEARG_1, &t_any, f_ch_readraw},
381-
{"ch_sendexpr", 2, 3, FEARG_1, &t_any, f_ch_sendexpr},
382-
{"ch_sendraw", 2, 3, FEARG_1, &t_any, f_ch_sendraw},
383-
{"ch_setoptions", 2, 2, FEARG_1, &t_any, f_ch_setoptions},
384-
{"ch_status", 1, 2, FEARG_1, &t_any, f_ch_status},
385-
#endif
386-
{"changenr", 0, 0, 0, &t_any, f_changenr},
387-
{"char2nr", 1, 2, FEARG_1, &t_any, f_char2nr},
388-
{"chdir", 1, 1, FEARG_1, &t_any, f_chdir},
389-
{"cindent", 1, 1, FEARG_1, &t_any, f_cindent},
390-
{"clearmatches", 0, 1, FEARG_1, &t_any, f_clearmatches},
391-
{"col", 1, 1, FEARG_1, &t_any, f_col},
392-
{"complete", 2, 2, FEARG_2, &t_any, f_complete},
393-
{"complete_add", 1, 1, FEARG_1, &t_any, f_complete_add},
394-
{"complete_check", 0, 0, 0, &t_any, f_complete_check},
395-
{"complete_info", 0, 1, FEARG_1, &t_any, f_complete_info},
396-
{"confirm", 1, 4, FEARG_1, &t_any, f_confirm},
372+
{"ch_getbufnr", 2, 2, FEARG_1, &t_number, f_ch_getbufnr},
373+
{"ch_getjob", 1, 1, FEARG_1, &t_job, f_ch_getjob},
374+
{"ch_info", 1, 1, FEARG_1, &t_dict_any, f_ch_info},
375+
{"ch_log", 1, 2, FEARG_1, &t_void, f_ch_log},
376+
{"ch_logfile", 1, 2, FEARG_1, &t_void, f_ch_logfile},
377+
{"ch_open", 1, 2, FEARG_1, &t_channel, f_ch_open},
378+
{"ch_read", 1, 2, FEARG_1, &t_string, f_ch_read},
379+
{"ch_readblob", 1, 2, FEARG_1, &t_blob, f_ch_readblob},
380+
{"ch_readraw", 1, 2, FEARG_1, &t_string, f_ch_readraw},
381+
{"ch_sendexpr", 2, 3, FEARG_1, &t_void, f_ch_sendexpr},
382+
{"ch_sendraw", 2, 3, FEARG_1, &t_void, f_ch_sendraw},
383+
{"ch_setoptions", 2, 2, FEARG_1, &t_void, f_ch_setoptions},
384+
{"ch_status", 1, 2, FEARG_1, &t_string, f_ch_status},
385+
#endif
386+
{"changenr", 0, 0, 0, &t_number, f_changenr},
387+
{"char2nr", 1, 2, FEARG_1, &t_number, f_char2nr},
388+
{"chdir", 1, 1, FEARG_1, &t_string, f_chdir},
389+
{"cindent", 1, 1, FEARG_1, &t_number, f_cindent},
390+
{"clearmatches", 0, 1, FEARG_1, &t_void, f_clearmatches},
391+
{"col", 1, 1, FEARG_1, &t_number, f_col},
392+
{"complete", 2, 2, FEARG_2, &t_void, f_complete},
393+
{"complete_add", 1, 1, FEARG_1, &t_number, f_complete_add},
394+
{"complete_check", 0, 0, 0, &t_number, f_complete_check},
395+
{"complete_info", 0, 1, FEARG_1, &t_dict_any, f_complete_info},
396+
{"confirm", 1, 4, FEARG_1, &t_number, f_confirm},
397397
{"copy", 1, 1, FEARG_1, &t_any, f_copy},
398398
#ifdef FEAT_FLOAT
399-
{"cos", 1, 1, FEARG_1, &t_any, f_cos},
400-
{"cosh", 1, 1, FEARG_1, &t_any, f_cosh},
399+
{"cos", 1, 1, FEARG_1, &t_float, f_cos},
400+
{"cosh", 1, 1, FEARG_1, &t_float, f_cosh},
401401
#endif
402-
{"count", 2, 4, FEARG_1, &t_any, f_count},
403-
{"cscope_connection",0,3, 0, &t_any, f_cscope_connection},
404-
{"cursor", 1, 3, FEARG_1, &t_any, f_cursor},
402+
{"count", 2, 4, FEARG_1, &t_number, f_count},
403+
{"cscope_connection",0,3, 0, &t_number, f_cscope_connection},
404+
{"cursor", 1, 3, FEARG_1, &t_number, f_cursor},
405405
#ifdef MSWIN
406-
{"debugbreak", 1, 1, FEARG_1, &t_any, f_debugbreak},
406+
{"debugbreak", 1, 1, FEARG_1, &t_number, f_debugbreak},
407407
#endif
408408
{"deepcopy", 1, 2, FEARG_1, &t_any, f_deepcopy},
409-
{"delete", 1, 2, FEARG_1, &t_any, f_delete},
410-
{"deletebufline", 2, 3, FEARG_1, &t_any, f_deletebufline},
411-
{"did_filetype", 0, 0, 0, &t_any, f_did_filetype},
412-
{"diff_filler", 1, 1, FEARG_1, &t_any, f_diff_filler},
413-
{"diff_hlID", 2, 2, FEARG_1, &t_any, f_diff_hlID},
414-
{"empty", 1, 1, FEARG_1, &t_any, f_empty},
415-
{"environ", 0, 0, 0, &t_any, f_environ},
416-
{"escape", 2, 2, FEARG_1, &t_any, f_escape},
409+
{"delete", 1, 2, FEARG_1, &t_number, f_delete},
410+
{"deletebufline", 2, 3, FEARG_1, &t_number, f_deletebufline},
411+
{"did_filetype", 0, 0, 0, &t_number, f_did_filetype},
412+
{"diff_filler", 1, 1, FEARG_1, &t_number, f_diff_filler},
413+
{"diff_hlID", 2, 2, FEARG_1, &t_number, f_diff_hlID},
414+
{"empty", 1, 1, FEARG_1, &t_number, f_empty},
415+
{"environ", 0, 0, 0, &t_dict_string, f_environ},
416+
{"escape", 2, 2, FEARG_1, &t_string, f_escape},
417417
{"eval", 1, 1, FEARG_1, &t_any, f_eval},
418-
{"eventhandler", 0, 0, 0, &t_any, f_eventhandler},
419-
{"executable", 1, 1, FEARG_1, &t_any, f_executable},
420-
{"execute", 1, 2, FEARG_1, &t_any, f_execute},
421-
{"exepath", 1, 1, FEARG_1, &t_any, f_exepath},
422-
{"exists", 1, 1, FEARG_1, &t_any, f_exists},
418+
{"eventhandler", 0, 0, 0, &t_number, f_eventhandler},
419+
{"executable", 1, 1, FEARG_1, &t_number, f_executable},
420+
{"execute", 1, 2, FEARG_1, &t_string, f_execute},
421+
{"exepath", 1, 1, FEARG_1, &t_string, f_exepath},
422+
{"exists", 1, 1, FEARG_1, &t_number, f_exists},
423423
#ifdef FEAT_FLOAT
424-
{"exp", 1, 1, FEARG_1, &t_any, f_exp},
424+
{"exp", 1, 1, FEARG_1, &t_float, f_exp},
425425
#endif
426426
{"expand", 1, 3, FEARG_1, &t_any, f_expand},
427-
{"expandcmd", 1, 1, FEARG_1, &t_any, f_expandcmd},
427+
{"expandcmd", 1, 1, FEARG_1, &t_string, f_expandcmd},
428428
{"extend", 2, 3, FEARG_1, &t_any, f_extend},
429-
{"feedkeys", 1, 2, FEARG_1, &t_any, f_feedkeys},
430-
{"file_readable", 1, 1, FEARG_1, &t_any, f_filereadable}, // obsolete
431-
{"filereadable", 1, 1, FEARG_1, &t_any, f_filereadable},
432-
{"filewritable", 1, 1, FEARG_1, &t_any, f_filewritable},
429+
{"feedkeys", 1, 2, FEARG_1, &t_void, f_feedkeys},
430+
{"file_readable", 1, 1, FEARG_1, &t_number, f_filereadable}, // obsolete
431+
{"filereadable", 1, 1, FEARG_1, &t_number, f_filereadable},
432+
{"filewritable", 1, 1, FEARG_1, &t_number, f_filewritable},
433433
{"filter", 2, 2, FEARG_1, &t_any, f_filter},
434-
{"finddir", 1, 3, FEARG_1, &t_any, f_finddir},
435-
{"findfile", 1, 3, FEARG_1, &t_any, f_findfile},
434+
{"finddir", 1, 3, FEARG_1, &t_string, f_finddir},
435+
{"findfile", 1, 3, FEARG_1, &t_string, f_findfile},
436436
#ifdef FEAT_FLOAT
437437
{"float2nr", 1, 1, FEARG_1, &t_any, f_float2nr},
438438
{"floor", 1, 1, FEARG_1, &t_any, f_floor},

src/testdir/test_vim9_script.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func Test_assignment_failure()
4343

4444
call CheckDefFailure(['let var: dict<string> = #{key: 123}'], 'expected dict<string> but got dict<number>')
4545
call CheckDefFailure(['let var: dict<number> = #{key: "xx"}'], 'expected dict<number> but got dict<string>')
46+
47+
call CheckDefFailure(['let var = feedkeys("0")'], 'E1031:')
48+
call CheckDefFailure(['let var: number = feedkeys("0")'], 'expected number but got void')
4649
endfunc
4750

4851
func Test_const()

src/vim9compile.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2836,7 +2836,15 @@ compile_assignment(char_u *arg, cmdidx_T cmdidx, cctx_T *cctx)
28362836

28372837
lvar = ((lvar_T *)cctx->ctx_locals.ga_data) + idx;
28382838
if (!has_type)
2839-
lvar->lv_type = stacktype;
2839+
{
2840+
if (stacktype->tt_type == VAR_VOID)
2841+
{
2842+
emsg(_("E1031: Cannot use void value"));
2843+
goto theend;
2844+
}
2845+
else
2846+
lvar->lv_type = stacktype;
2847+
}
28402848
else
28412849
if (check_type(lvar->lv_type, stacktype, TRUE) == FAIL)
28422850
goto theend;

0 commit comments

Comments
 (0)