Skip to content

Commit cbcc5ba

Browse files
64-bitmanchrisbra
authored andcommitted
patch 9.1.2012: Vim9: cannot initialize class member with protected var
Problem: Vim9: cannot initialize class member with protected var Solution: Allow this to work if this happens within the same class (Foxe Chen) closes: #18949 Signed-off-by: Foxe Chen <chen.foxe@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 5e577c7 commit cbcc5ba

7 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/eval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
131131
evalarg->eval_getline = eap->ea_getline;
132132
evalarg->eval_cookie = eap->cookie;
133133
}
134+
evalarg->eval_class = eap->class;
134135
}
135136

136137
/*

src/ex_cmds.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,8 @@ struct exarg
19731973
void *cookie; // argument for getline()
19741974
#ifdef FEAT_EVAL
19751975
cstack_T *cstack; // condition stack for ":if" etc.
1976+
class_T *class; // Name of class being defined. Used by :class
1977+
// and :enum commands.
19761978
#endif
19771979
};
19781980

src/globals.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ EXTERN listitem_T range_list_item;
20252025
EXTERN evalarg_T EVALARG_EVALUATE
20262026
# ifdef DO_INIT
20272027
= {EVAL_EVALUATE, 0, NULL, NULL, NULL, NULL, GA_EMPTY, GA_EMPTY, NULL,
2028-
{0, 0, (int)sizeof(char_u *), 20, NULL}, 0, NULL}
2028+
{0, 0, (int)sizeof(char_u *), 20, NULL}, 0, NULL, NULL}
20292029
# endif
20302030
;
20312031
#endif

src/structs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,6 +2282,9 @@ typedef struct {
22822282

22832283
// pointer to the lines concatenated for a lambda.
22842284
char_u *eval_tofree_lambda;
2285+
2286+
// pointer to name of class being constructed
2287+
class_T *eval_class;
22852288
} evalarg_T;
22862289

22872290
// Flag for expression evaluation.

src/testdir/test_vim9_class.vim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12407,10 +12407,14 @@ def Test_protected_new_method()
1240712407
vim9script
1240812408
class A
1240912409
static var _instance: A
12410+
static var handler: func = A._Internal
1241012411
var str: string
1241112412
def _new(str: string)
1241212413
this.str = str
1241312414
enddef
12415+
static def _Internal(): string
12416+
return "test"
12417+
enddef
1241412418
static def GetInstance(str: string): A
1241512419
if _instance == null
1241612420
_instance = A._new(str)
@@ -12422,6 +12426,24 @@ def Test_protected_new_method()
1242212426
var b: A = A.GetInstance('bar')
1242312427
assert_equal('foo', a.str)
1242412428
assert_equal('foo', b.str)
12429+
assert_equal('test', A.handler())
12430+
END
12431+
v9.CheckSourceSuccess(lines)
12432+
12433+
lines =<< trim END
12434+
vim9script
12435+
enum Test
12436+
A,
12437+
B,
12438+
C
12439+
12440+
static var handler: func = Test._Internal
12441+
12442+
static def _Internal(): string
12443+
return "test"
12444+
enddef
12445+
endenum
12446+
assert_equal('test', Test.handler())
1242512447
END
1242612448
v9.CheckSourceSuccess(lines)
1242712449
enddef

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
2012,
737739
/**/
738740
2011,
739741
/**/

src/vim9class.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,8 @@ ex_class(exarg_T *eap)
21222122
cl->class_object_type.tt_type = VAR_OBJECT;
21232123
cl->class_object_type.tt_class = cl;
21242124

2125+
eap->class = cl;
2126+
21252127
// Add the class to the script-local variables.
21262128
// TODO: handle other context, e.g. in a function
21272129
// TODO: does uf_hash need to be cleared?
@@ -3243,7 +3245,8 @@ class_object_index(
32433245
if (fp != NULL)
32443246
{
32453247
// Protected methods are not accessible outside the class
3246-
if (*name == '_')
3248+
if (fp->uf_defclass != evalarg->eval_class
3249+
&& *name == '_')
32473250
{
32483251
semsg(_(e_cannot_access_protected_method_str), fp->uf_name);
32493252
goto done;

0 commit comments

Comments
 (0)