comparison src/evalfunc.c @ 32972:e4851934751a

patch 9.0.1786: Vim9: need instanceof() function Commit: https://github.com/vim/vim/commit/afe0466fb1695fa8b9782eea8a8e9f9540d4cb85 Author: LemonBoy <thatlemon@gmail.com> Date: Wed Aug 23 21:08:11 2023 +0200 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 <cb@256bit.org> Co-authored-by: LemonBoy <thatlemon@gmail.com>
author Christian Brabandt <cb@256bit.org>
date Wed, 23 Aug 2023 21:15:08 +0200
parents c517845bd10e
children 04c75e67ca30
comparison
equal deleted inserted replaced
32971:ccfca4f03a2b 32972:e4851934751a
275 { 275 {
276 return check_arg_type(&t_number, type, context); 276 return check_arg_type(&t_number, type, context);
277 } 277 }
278 278
279 /* 279 /*
280 * Check "type" is an object.
281 */
282 static int
283 arg_object(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
284 {
285 return check_arg_type(&t_object, type, context);
286 }
287
288 /*
280 * Check "type" is a dict of 'any'. 289 * Check "type" is a dict of 'any'.
281 */ 290 */
282 static int 291 static int
283 arg_dict_any(type_T *type, type_T *decl_type UNUSED, argcontext_T *context) 292 arg_dict_any(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
284 { 293 {
739 || type->tt_type == VAR_BOOL 748 || type->tt_type == VAR_BOOL
740 || type->tt_type == VAR_NUMBER 749 || type->tt_type == VAR_NUMBER
741 || type_any_or_unknown(type)) 750 || type_any_or_unknown(type))
742 return OK; 751 return OK;
743 arg_type_mismatch(&t_func_any, type, context->arg_idx + 1); 752 arg_type_mismatch(&t_func_any, type, context->arg_idx + 1);
753 return FAIL;
754 }
755
756 /*
757 * Check "type" is a list of 'any' or a class.
758 */
759 static int
760 arg_class_or_list(type_T *type, type_T *decl_type UNUSED, argcontext_T *context)
761 {
762 if (type->tt_type == VAR_CLASS
763 || type->tt_type == VAR_LIST
764 || type_any_or_unknown(type))
765 return OK;
766 arg_type_mismatch(&t_class, type, context->arg_idx + 1);
744 return FAIL; 767 return FAIL;
745 } 768 }
746 769
747 /* 770 /*
748 * Check "type" is a list of 'any' or a blob or a string. 771 * Check "type" is a list of 'any' or a blob or a string.
1123 static argcheck_T arg23_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number}; 1146 static argcheck_T arg23_insert[] = {arg_list_or_blob, arg_item_of_prev, arg_number};
1124 static argcheck_T arg1_len[] = {arg_len1}; 1147 static argcheck_T arg1_len[] = {arg_len1};
1125 static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr}; 1148 static argcheck_T arg3_libcall[] = {arg_string, arg_string, arg_string_or_nr};
1126 static argcheck_T arg14_maparg[] = {arg_string, arg_string, arg_bool, arg_bool}; 1149 static argcheck_T arg14_maparg[] = {arg_string, arg_string, arg_bool, arg_bool};
1127 static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string_mod, arg_filter_func}; 1150 static argcheck_T arg2_filter[] = {arg_list_or_dict_or_blob_or_string_mod, arg_filter_func};
1151 static argcheck_T arg2_instanceof[] = {arg_object, arg_class_or_list};
1128 static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string_mod, arg_map_func}; 1152 static argcheck_T arg2_map[] = {arg_list_or_dict_or_blob_or_string_mod, arg_map_func};
1129 static argcheck_T arg2_mapnew[] = {arg_list_or_dict_or_blob_or_string, NULL}; 1153 static argcheck_T arg2_mapnew[] = {arg_list_or_dict_or_blob_or_string, NULL};
1130 static argcheck_T arg25_matchadd[] = {arg_string, arg_string, arg_number, arg_number, arg_dict_any}; 1154 static argcheck_T arg25_matchadd[] = {arg_string, arg_string, arg_number, arg_number, arg_dict_any};
1131 static argcheck_T arg25_matchaddpos[] = {arg_string, arg_list_any, arg_number, arg_number, arg_dict_any}; 1155 static argcheck_T arg25_matchaddpos[] = {arg_string, arg_list_any, arg_number, arg_number, arg_dict_any};
1132 static argcheck_T arg119_printf[] = {arg_string_or_nr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; 1156 static argcheck_T arg119_printf[] = {arg_string_or_nr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
2122 ret_number_bool, f_inputsave}, 2146 ret_number_bool, f_inputsave},
2123 {"inputsecret", 1, 2, FEARG_1, arg2_string, 2147 {"inputsecret", 1, 2, FEARG_1, arg2_string,
2124 ret_string, f_inputsecret}, 2148 ret_string, f_inputsecret},
2125 {"insert", 2, 3, FEARG_1, arg23_insert, 2149 {"insert", 2, 3, FEARG_1, arg23_insert,
2126 ret_first_arg, f_insert}, 2150 ret_first_arg, f_insert},
2151 {"instanceof", 2, 2, FEARG_1, arg2_instanceof,
2152 ret_bool, f_instanceof},
2127 {"interrupt", 0, 0, 0, NULL, 2153 {"interrupt", 0, 0, 0, NULL,
2128 ret_void, f_interrupt}, 2154 ret_void, f_interrupt},
2129 {"invert", 1, 1, FEARG_1, arg1_number, 2155 {"invert", 1, 1, FEARG_1, arg1_number,
2130 ret_number, f_invert}, 2156 ret_number, f_invert},
2131 {"isabsolutepath", 1, 1, FEARG_1, arg1_string, 2157 {"isabsolutepath", 1, 1, FEARG_1, arg1_string,