From afe0466fb1695fa8b9782eea8a8e9f9540d4cb85 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 23 Aug 2023 21:08:11 +0200 Subject: patch 9.0.1786: Vim9: need instanceof() function 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 Co-authored-by: LemonBoy --- src/evalfunc.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/evalfunc.c') diff --git a/src/evalfunc.c b/src/evalfunc.c index 3e020bcde0..bdfd6325fe 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -276,6 +276,15 @@ arg_number(type_T *type, type_T *decl_type UNUSED, argcontext_T *context) return check_arg_type(&t_number, type, 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'. */ @@ -744,6 +753,20 @@ arg_string_or_func(type_T *type, type_T *decl_type UNUSED, argcontext_T *context return FAIL; } +/* + * 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. */ @@ -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, -- cgit v1.2.3