summaryrefslogtreecommitdiffstats
path: root/src/evalfunc.c
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2023-08-23 21:08:11 +0200
committerChristian Brabandt <cb@256bit.org>2023-08-23 21:08:11 +0200
commitafe0466fb1695fa8b9782eea8a8e9f9540d4cb85 (patch)
treef71446a214e45f7c0cdcb186fe40d25d1a1da39d /src/evalfunc.c
parent1193951bebcff50d88403ce17dec5d3be14f131d (diff)
patch 9.0.1786: Vim9: need instanceof() functionv9.0.1786
Problem: Vim9: need instanceof() function Solution: Implement instanceof() builtin Implemented in the same form as Python's isinstance because it allows for checking multiple class types at the same time. closes: #12867 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: LemonBoy <thatlemon@gmail.com>
Diffstat (limited to 'src/evalfunc.c')
-rw-r--r--src/evalfunc.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c
index 3e020bcde0..bdfd6325fe 100644
--- a/src/evalfunc.c
+++ b/src/evalfunc.c
@@ -277,6 +277,15 @@ arg_number(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
}
/*
+ * Check "type" is an object.
+ */
+ static int
+arg_object(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
+{
+ return check_arg_type(&t_object, type, context);
+}
+
+/*
* Check "type" is a dict of 'any'.
*/
static int
@@ -745,6 +754,20 @@ arg_string_or_func(type_T *type, type_T *decl_type UNUSED, argcontext_T *context
}
/*
+ * Check "type" is a list of 'any' or a class.
+ */
+ static int
+arg_class_or_list(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
+{
+ if (type->tt_type == VAR_CLASS
+ || type->tt_type == VAR_LIST
+ || type_any_or_unknown(type))
+ return OK;
+ arg_type_mismatch(&t_class, type, context->arg_idx + 1);
+ return FAIL;
+}
+
+/*
* Check "type" is a list of 'any' or a blob or a string.
*/
static int
@@ -1125,6 +1148,7 @@ static argcheck_T arg1_len[] = {arg_len1};
static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr};
static argcheck_T arg14_maparg[] = {arg_string, arg_string, arg_bool, arg_bool};
static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string_mod, arg_filter_func};
+static argcheck_T arg2_instanceof[] = {arg_object, arg_class_or_list};
static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string_mod, arg_map_func};
static argcheck_T arg2_mapnew[] = {arg_list_or_dict_or_blob_or_string, NULL};
static argcheck_T arg25_matchadd[] = {arg_string, arg_string, arg_number, arg_number, arg_dict_any};
@@ -2124,6 +2148,8 @@ static funcentry_T global_functions[] =
ret_string, f_inputsecret},
{"insert", 2, 3, FEARG_1, arg23_insert,
ret_first_arg, f_insert},
+ {"instanceof", 2, 2, FEARG_1, arg2_instanceof,
+ ret_bool, f_instanceof},
{"interrupt", 0, 0, 0, NULL,
ret_void, f_interrupt},
{"invert", 1, 1, FEARG_1, arg1_number,