comparison src/vim9expr.c @ 33025:1d18c7fe609f v9.0.1804

patch 9.0.1804: Vim9: no support for private object methods Commit: https://github.com/vim/vim/commit/cd7293bf6c358bb0e183582a2927fc03566d29f6 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sun Aug 27 19:18:23 2023 +0200 patch 9.0.1804: Vim9: no support for private object methods Problem: Vim9: no support for private object methods Solution: Add support for private object/class methods closes: #12920 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
author Christian Brabandt <cb@256bit.org>
date Sun, 27 Aug 2023 19:30:05 +0200
parents a8561f9b47aa
children 8362975375a4
comparison
equal deleted inserted replaced
33024:e5b8fc37e882 33025:1d18c7fe609f
250 } 250 }
251 return OK; 251 return OK;
252 } 252 }
253 253
254 /* 254 /*
255 * Returns TRUE if the current function is inside the class "cl" or one of the
256 * parent classes.
257 */
258 static int
259 inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
260 {
261 for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
262 {
263 if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
264 {
265 class_T *clp = cctx->ctx_ufunc->uf_class;
266 while (clp != NULL)
267 {
268 if (clp == cl)
269 return TRUE;
270 clp = clp->class_extends;
271 }
272 }
273 }
274
275 return FALSE;
276 }
277
278 /*
255 * Compile ".member" coming after an object or class. 279 * Compile ".member" coming after an object or class.
256 */ 280 */
257 static int 281 static int
258 compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type) 282 compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
259 { 283 {
343 } 367 }
344 if (ufunc == NULL) 368 if (ufunc == NULL)
345 { 369 {
346 // TODO: different error for object method? 370 // TODO: different error for object method?
347 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name); 371 semsg(_(e_method_not_found_on_class_str_str), cl->class_name, name);
372 return FAIL;
373 }
374
375 if (ufunc->uf_private && !inside_class_hierarchy(cctx, cl))
376 {
377 semsg(_(e_cannot_access_private_method_str), name);
348 return FAIL; 378 return FAIL;
349 } 379 }
350 380
351 // Compile the arguments and call the class function or object method. 381 // Compile the arguments and call the class function or object method.
352 // The object method will know that the object is on the stack, just 382 // The object method will know that the object is on the stack, just