comparison src/vim9expr.c @ 34472:5c1a025192ed v9.1.0148

patch 9.1.0148: Vim9: can't call internal methods with objects Commit: https://github.com/vim/vim/commit/d3eae7bc116297f70220f21ded436ed0a88066d8 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Mar 3 16:26:58 2024 +0100 patch 9.1.0148: Vim9: can't call internal methods with objects Problem: Vim9: can't call internal methods with objects Solution: Add support for empty(), len() and string() function calls for objects (Yegappan Lakshmanan) closes: #14129 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sun, 03 Mar 2024 16:45:06 +0100
parents 1629cc65d78d
children 3a3f13bac124
comparison
equal deleted inserted replaced
34471:7f6302969e3d 34472:5c1a025192ed
1012 emsg(_(e_missing_closing_paren)); 1012 emsg(_(e_missing_closing_paren));
1013 return FAIL; 1013 return FAIL;
1014 } 1014 }
1015 1015
1016 /* 1016 /*
1017 * Compile a builtin method call of an object (e.g. string(), len(), empty(),
1018 * etc.) if the class implements it.
1019 */
1020 static int
1021 compile_builtin_method_call(cctx_T *cctx, class_builtin_T builtin_method)
1022 {
1023 type_T *type = get_decl_type_on_stack(cctx, 0);
1024 int res = FAIL;
1025
1026 // If the built in function is invoked on an object and the class
1027 // implements the corresponding built in method, then invoke the object
1028 // method.
1029 if (type->tt_type == VAR_OBJECT)
1030 {
1031 int method_idx;
1032 ufunc_T *uf = class_get_builtin_method(type->tt_class, builtin_method,
1033 &method_idx);
1034 if (uf != NULL)
1035 res = generate_CALL(cctx, uf, type->tt_class, method_idx, 0);
1036 }
1037
1038 return res;
1039 }
1040
1041
1042 /*
1017 * Compile a function call: name(arg1, arg2) 1043 * Compile a function call: name(arg1, arg2)
1018 * "arg" points to "name", "arg + varlen" to the "(". 1044 * "arg" points to "name", "arg + varlen" to the "(".
1019 * "argcount_init" is 1 for "value->method()" 1045 * "argcount_init" is 1 for "value->method()"
1020 * Instructions: 1046 * Instructions:
1021 * EVAL arg1 1047 * EVAL arg1
1165 || (STRCMP(name, "mkdir") == 0 && argcount > 1)) 1191 || (STRCMP(name, "mkdir") == 0 && argcount > 1))
1166 { 1192 {
1167 // May have the "D" or "R" flag, reserve a variable for a 1193 // May have the "D" or "R" flag, reserve a variable for a
1168 // deferred function call. 1194 // deferred function call.
1169 if (get_defer_var_idx(cctx) == 0) 1195 if (get_defer_var_idx(cctx) == 0)
1196 idx = -1;
1197 }
1198
1199 class_builtin_T builtin_method = CLASS_BUILTIN_INVALID;
1200 if (STRCMP(name, "string") == 0)
1201 builtin_method = CLASS_BUILTIN_STRING;
1202 else if (STRCMP(name, "empty") == 0)
1203 builtin_method = CLASS_BUILTIN_EMPTY;
1204 else if (STRCMP(name, "len") == 0)
1205 builtin_method = CLASS_BUILTIN_LEN;
1206 if (builtin_method != CLASS_BUILTIN_INVALID)
1207 {
1208 res = compile_builtin_method_call(cctx, builtin_method);
1209 if (res == OK)
1170 idx = -1; 1210 idx = -1;
1171 } 1211 }
1172 1212
1173 if (idx >= 0) 1213 if (idx >= 0)
1174 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1); 1214 res = generate_BCALL(cctx, idx, argcount, argcount_init == 1);