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>
diff --git a/src/vim9expr.c b/src/vim9expr.c
index 0e466bd..1b23b7f 100644
--- a/src/vim9expr.c
+++ b/src/vim9expr.c
@@ -252,6 +252,30 @@
}
/*
+ * Returns TRUE if the current function is inside the class "cl" or one of the
+ * parent classes.
+ */
+ static int
+inside_class_hierarchy(cctx_T *cctx_arg, class_T *cl)
+{
+ for (cctx_T *cctx = cctx_arg; cctx != NULL; cctx = cctx->ctx_outer)
+ {
+ if (cctx->ctx_ufunc != NULL && cctx->ctx_ufunc->uf_class != NULL)
+ {
+ class_T *clp = cctx->ctx_ufunc->uf_class;
+ while (clp != NULL)
+ {
+ if (clp == cl)
+ return TRUE;
+ clp = clp->class_extends;
+ }
+ }
+ }
+
+ return FALSE;
+}
+
+/*
* Compile ".member" coming after an object or class.
*/
static int
@@ -348,6 +372,12 @@
return FAIL;
}
+ if (ufunc->uf_private && !inside_class_hierarchy(cctx, cl))
+ {
+ semsg(_(e_cannot_access_private_method_str), name);
+ return FAIL;
+ }
+
// Compile the arguments and call the class function or object method.
// The object method will know that the object is on the stack, just
// before the arguments.