1
0
forked from aniani/vim

patch 8.2.3188: Vim9: argument types are not checked at compile time

Problem:    Vim9: argument types are not checked at compile time.
Solution:   Add several more type checks, also at runtime. (Yegappan
            Lakshmanan, closes #8587)
This commit is contained in:
Yegappan Lakshmanan
2021-07-20 17:51:51 +02:00
committed by Bram Moolenaar
parent 9bb0dad0d8
commit 83494b4ac6
34 changed files with 1417 additions and 299 deletions

View File

@@ -384,6 +384,16 @@ check_for_nonempty_string_arg(typval_T *args, int idx)
return OK;
}
/*
* Check for an optional string argument at 'idx'
*/
int
check_for_opt_string_arg(typval_T *args, int idx)
{
return (args[idx].v_type == VAR_UNKNOWN
|| check_for_string_arg(args, idx) != FAIL);
}
/*
* Give an error and return FAIL unless "args[idx]" is a number.
*/
@@ -401,6 +411,16 @@ check_for_number_arg(typval_T *args, int idx)
return OK;
}
/*
* Check for an optional number argument at 'idx'
*/
int
check_for_opt_number_arg(typval_T *args, int idx)
{
return (args[idx].v_type == VAR_UNKNOWN
|| check_for_number_arg(args, idx) != FAIL);
}
/*
* Give an error and return FAIL unless "args[idx]" is a bool.
*/
@@ -421,6 +441,16 @@ check_for_bool_arg(typval_T *args, int idx)
return OK;
}
/*
* Check for an optional bool argument at 'idx'
*/
int
check_for_opt_bool_arg(typval_T *args, int idx)
{
return (args[idx].v_type == VAR_UNKNOWN
|| check_for_bool_arg(args, idx) != FAIL);
}
/*
* Give an error and return FAIL unless "args[idx]" is a list.
*/
@@ -438,6 +468,16 @@ check_for_list_arg(typval_T *args, int idx)
return OK;
}
/*
* Check for an optional list argument at 'idx'
*/
int
check_for_opt_list_arg(typval_T *args, int idx)
{
return (args[idx].v_type == VAR_UNKNOWN
|| check_for_list_arg(args, idx) != FAIL);
}
/*
* Give an error and return FAIL unless "args[idx]" is a dict.
*/
@@ -455,6 +495,169 @@ check_for_dict_arg(typval_T *args, int idx)
return OK;
}
/*
* Check for an optional dict argument at 'idx'
*/
int
check_for_opt_dict_arg(typval_T *args, int idx)
{
return (args[idx].v_type == VAR_UNKNOWN
|| check_for_dict_arg(args, idx) != FAIL);
}
/*
* Give an error and return FAIL unless "args[idx]" is a blob.
*/
int
check_for_blob_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_BLOB)
{
if (idx >= 0)
semsg(_(e_blob_required_for_argument_nr), idx + 1);
else
emsg(_(e_blobreq));
return FAIL;
}
return OK;
}
/*
* Give an error and return FAIL unless "args[idx]" is a channel or a job.
*/
int
check_for_chan_or_job_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_CHANNEL && args[idx].v_type != VAR_JOB)
{
if (idx >= 0)
semsg(_(e_chan_or_job_required_for_argument_nr), idx + 1);
else
emsg(_(e_chan_or_job_req));
return FAIL;
}
return OK;
}
/*
* Give an error and return FAIL unless "args[idx]" is a job.
*/
int
check_for_job_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_JOB)
{
if (idx >= 0)
semsg(_(e_job_required_for_argument_nr), idx + 1);
else
emsg(_(e_jobreq));
return FAIL;
}
return OK;
}
/*
* Give an error and return FAIL unless "args[idx]" is a string or
* a number.
*/
int
check_for_string_or_number_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
{
if (idx >= 0)
semsg(_(e_string_required_for_argument_nr), idx + 1);
else
emsg(_(e_stringreq));
return FAIL;
}
return OK;
}
/*
* Give an error and return FAIL unless "args[idx]" is a string or
* a number (buffer)
*/
int
check_for_buffer_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
{
if (idx >= 0)
semsg(_(e_string_required_for_argument_nr), idx + 1);
else
emsg(_(e_stringreq));
return FAIL;
}
return OK;
}
/*
* Give an error and return FAIL unless "args[idx]" is a string or
* a number (line)
*/
int
check_for_lnum_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_NUMBER)
{
if (idx >= 0)
semsg(_(e_string_required_for_argument_nr), idx + 1);
else
emsg(_(e_stringreq));
return FAIL;
}
return OK;
}
/*
* Give an error and return FAIL unless "args[idx]" is a string or
* a number (line)
*/
int
check_for_opt_lnum_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_UNKNOWN
&& args[idx].v_type != VAR_STRING
&& args[idx].v_type != VAR_NUMBER)
{
if (idx >= 0)
semsg(_(e_string_required_for_argument_nr), idx + 1);
else
emsg(_(e_stringreq));
return FAIL;
}
return OK;
}
/*
* Check for an optional string or number argument at 'idx'
*/
int
check_for_opt_string_or_number_arg(typval_T *args, int idx)
{
return (args[idx].v_type == VAR_UNKNOWN
|| check_for_string_or_number_arg(args, idx) != FAIL);
}
/*
* Give an error and return FAIL unless "args[idx]" is a string or
* a blob.
*/
int
check_for_string_or_blob_arg(typval_T *args, int idx)
{
if (args[idx].v_type != VAR_STRING && args[idx].v_type != VAR_BLOB)
{
if (idx >= 0)
semsg(_(e_string_required_for_argument_nr), idx + 1);
else
emsg(_(e_stringreq));
return FAIL;
}
return OK;
}
/*
* Get the string value of a variable.
* If it is a Number variable, the number is converted into a string.