mirror of
https://github.com/vim/vim.git
synced 2025-09-04 21:33:48 -04:00
patch 9.0.1914: Vim9: few issues when accessing object members
Problem: Vim9: few issues when accessing object members Solution: When calling an object method, check for null object. Accessing a Dict object member doesn't work. closes: #13119 closes: #13123 closes: #13124 Signed-off-by: Christian Brabandt <cb@256bit.org> Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
parent
2ce070c27a
commit
1db1514365
@ -1582,7 +1582,6 @@ get_lval(
|
|||||||
lp->ll_tv->vval.v_object + 1)) + m_idx;
|
lp->ll_tv->vval.v_object + 1)) + m_idx;
|
||||||
else
|
else
|
||||||
lp->ll_tv = &cl->class_members_tv[m_idx];
|
lp->ll_tv = &cl->class_members_tv[m_idx];
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1881,6 +1881,13 @@ struct ufunc_S
|
|||||||
#define FC_NEW 0x8000 // constructor
|
#define FC_NEW 0x8000 // constructor
|
||||||
#define FC_ABSTRACT 0x10000 // abstract method
|
#define FC_ABSTRACT 0x10000 // abstract method
|
||||||
|
|
||||||
|
// Is "ufunc" an object method?
|
||||||
|
#define IS_OBJECT_METHOD(ufunc) ((ufunc->uf_flags & FC_OBJECT) == FC_OBJECT)
|
||||||
|
// Is "ufunc" a class new() constructor method?
|
||||||
|
#define IS_CONSTRUCTOR_METHOD(ufunc) ((ufunc->uf_flags & FC_NEW) == FC_NEW)
|
||||||
|
// Is "ufunc" an abstract class method?
|
||||||
|
#define IS_ABSTRACT_METHOD(ufunc) ((ufunc->uf_flags & FC_ABSTRACT) == FC_ABSTRACT)
|
||||||
|
|
||||||
#define MAX_FUNC_ARGS 20 // maximum number of function arguments
|
#define MAX_FUNC_ARGS 20 // maximum number of function arguments
|
||||||
#define VAR_SHORT_LEN 20 // short variable name length
|
#define VAR_SHORT_LEN 20 // short variable name length
|
||||||
#define FIXVAR_CNT 12 // number of fixed variables
|
#define FIXVAR_CNT 12 // number of fixed variables
|
||||||
|
@ -1507,6 +1507,7 @@ def Test_class_member()
|
|||||||
END
|
END
|
||||||
v9.CheckSourceFailure(lines, 'E1340: Argument already declared in the class: count')
|
v9.CheckSourceFailure(lines, 'E1340: Argument already declared in the class: count')
|
||||||
|
|
||||||
|
# Use a local variable in a method with the same name as a class variable
|
||||||
lines =<< trim END
|
lines =<< trim END
|
||||||
vim9script
|
vim9script
|
||||||
|
|
||||||
@ -5488,4 +5489,114 @@ def Test_nested_object_assignment()
|
|||||||
v9.CheckSourceFailure(lines, 'E46: Cannot change read-only variable "value"')
|
v9.CheckSourceFailure(lines, 'E46: Cannot change read-only variable "value"')
|
||||||
enddef
|
enddef
|
||||||
|
|
||||||
|
" Test for calling methods using a null object
|
||||||
|
def Test_null_object_method_call()
|
||||||
|
# Calling a object method using a null object in script context
|
||||||
|
var lines =<< trim END
|
||||||
|
vim9script
|
||||||
|
|
||||||
|
class C
|
||||||
|
def Foo()
|
||||||
|
assert_report('This method should not be executed')
|
||||||
|
enddef
|
||||||
|
endclass
|
||||||
|
|
||||||
|
var o: C
|
||||||
|
o.Foo()
|
||||||
|
END
|
||||||
|
v9.CheckSourceFailure(lines, 'E1360: Using a null object', 10)
|
||||||
|
|
||||||
|
# Calling a object method using a null object in def function context
|
||||||
|
lines =<< trim END
|
||||||
|
vim9script
|
||||||
|
|
||||||
|
class C
|
||||||
|
def Foo()
|
||||||
|
assert_report('This method should not be executed')
|
||||||
|
enddef
|
||||||
|
endclass
|
||||||
|
|
||||||
|
def T()
|
||||||
|
var o: C
|
||||||
|
o.Foo()
|
||||||
|
enddef
|
||||||
|
T()
|
||||||
|
END
|
||||||
|
v9.CheckSourceFailure(lines, 'E1360: Using a null object', 2)
|
||||||
|
|
||||||
|
# Calling a object method through another class method using a null object in
|
||||||
|
# script context
|
||||||
|
lines =<< trim END
|
||||||
|
vim9script
|
||||||
|
|
||||||
|
class C
|
||||||
|
def Foo()
|
||||||
|
assert_report('This method should not be executed')
|
||||||
|
enddef
|
||||||
|
|
||||||
|
static def Bar(o_any: any)
|
||||||
|
var o_typed: C = o_any
|
||||||
|
o_typed.Foo()
|
||||||
|
enddef
|
||||||
|
endclass
|
||||||
|
|
||||||
|
var o: C
|
||||||
|
C.Bar(o)
|
||||||
|
END
|
||||||
|
v9.CheckSourceFailure(lines, 'E1360: Using a null object', 2)
|
||||||
|
|
||||||
|
# Calling a object method through another class method using a null object in
|
||||||
|
# def function context
|
||||||
|
lines =<< trim END
|
||||||
|
vim9script
|
||||||
|
|
||||||
|
class C
|
||||||
|
def Foo()
|
||||||
|
assert_report('This method should not be executed')
|
||||||
|
enddef
|
||||||
|
|
||||||
|
static def Bar(o_any: any)
|
||||||
|
var o_typed: C = o_any
|
||||||
|
o_typed.Foo()
|
||||||
|
enddef
|
||||||
|
endclass
|
||||||
|
|
||||||
|
def T()
|
||||||
|
var o: C
|
||||||
|
C.Bar(o)
|
||||||
|
enddef
|
||||||
|
T()
|
||||||
|
END
|
||||||
|
v9.CheckSourceFailure(lines, 'E1360: Using a null object', 2)
|
||||||
|
enddef
|
||||||
|
|
||||||
|
" Test for using a dict as an object member
|
||||||
|
def Test_dict_object_member()
|
||||||
|
var lines =<< trim END
|
||||||
|
vim9script
|
||||||
|
|
||||||
|
class Context
|
||||||
|
public this.state: dict<number> = {}
|
||||||
|
def GetState(): dict<number>
|
||||||
|
return this.state
|
||||||
|
enddef
|
||||||
|
endclass
|
||||||
|
|
||||||
|
var ctx = Context.new()
|
||||||
|
ctx.state->extend({a: 1})
|
||||||
|
ctx.state['b'] = 2
|
||||||
|
assert_equal({a: 1, b: 2}, ctx.GetState())
|
||||||
|
|
||||||
|
def F()
|
||||||
|
ctx.state['c'] = 3
|
||||||
|
assert_equal({a: 1, b: 2, c: 3}, ctx.GetState())
|
||||||
|
enddef
|
||||||
|
F()
|
||||||
|
assert_equal(3, ctx.state.c)
|
||||||
|
ctx.state.c = 4
|
||||||
|
assert_equal(4, ctx.state.c)
|
||||||
|
END
|
||||||
|
v9.CheckSourceSuccess(lines)
|
||||||
|
enddef
|
||||||
|
|
||||||
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|
||||||
|
@ -699,6 +699,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
1914,
|
||||||
/**/
|
/**/
|
||||||
1913,
|
1913,
|
||||||
/**/
|
/**/
|
||||||
|
@ -534,7 +534,7 @@ validate_abstract_class_methods(
|
|||||||
for (int i = 0; i < extends_method_count; i++)
|
for (int i = 0; i < extends_method_count; i++)
|
||||||
{
|
{
|
||||||
ufunc_T *uf = extends_methods[i];
|
ufunc_T *uf = extends_methods[i];
|
||||||
if ((uf->uf_flags & FC_ABSTRACT) == 0)
|
if (!IS_ABSTRACT_METHOD(uf))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
int method_found = FALSE;
|
int method_found = FALSE;
|
||||||
|
@ -2617,7 +2617,7 @@ compile_return(char_u *arg, int check_return_type, int legacy, cctx_T *cctx)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cctx->ctx_ufunc->uf_flags & FC_NEW)
|
if (IS_CONSTRUCTOR_METHOD(cctx->ctx_ufunc))
|
||||||
{
|
{
|
||||||
// For a class new() constructor, return an object of the class.
|
// For a class new() constructor, return an object of the class.
|
||||||
generate_instr(cctx, ISN_RETURN_OBJECT);
|
generate_instr(cctx, ISN_RETURN_OBJECT);
|
||||||
|
@ -3286,7 +3286,7 @@ compile_def_function(
|
|||||||
|
|
||||||
// In the constructor allocate memory for the object and initialize the
|
// In the constructor allocate memory for the object and initialize the
|
||||||
// object members.
|
// object members.
|
||||||
if ((ufunc->uf_flags & FC_NEW) == FC_NEW)
|
if (IS_CONSTRUCTOR_METHOD(ufunc))
|
||||||
{
|
{
|
||||||
generate_CONSTRUCT(&cctx, ufunc->uf_class);
|
generate_CONSTRUCT(&cctx, ufunc->uf_class);
|
||||||
|
|
||||||
@ -3949,7 +3949,7 @@ nextline:
|
|||||||
if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
|
if (ufunc->uf_ret_type->tt_type == VAR_UNKNOWN)
|
||||||
ufunc->uf_ret_type = &t_void;
|
ufunc->uf_ret_type = &t_void;
|
||||||
else if (ufunc->uf_ret_type->tt_type != VAR_VOID
|
else if (ufunc->uf_ret_type->tt_type != VAR_VOID
|
||||||
&& (ufunc->uf_flags & FC_NEW) != FC_NEW)
|
&& !IS_CONSTRUCTOR_METHOD(ufunc))
|
||||||
{
|
{
|
||||||
emsg(_(e_missing_return_statement));
|
emsg(_(e_missing_return_statement));
|
||||||
goto erret;
|
goto erret;
|
||||||
@ -3957,7 +3957,7 @@ nextline:
|
|||||||
|
|
||||||
// Return void if there is no return at the end.
|
// Return void if there is no return at the end.
|
||||||
// For a constructor return the object.
|
// For a constructor return the object.
|
||||||
if ((ufunc->uf_flags & FC_NEW) == FC_NEW)
|
if (IS_CONSTRUCTOR_METHOD(ufunc))
|
||||||
{
|
{
|
||||||
generate_instr(&cctx, ISN_RETURN_OBJECT);
|
generate_instr(&cctx, ISN_RETURN_OBJECT);
|
||||||
ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
|
ufunc->uf_ret_type = &ufunc->uf_class->class_object_type;
|
||||||
|
@ -535,6 +535,15 @@ call_dfunc(
|
|||||||
// If this is an object method, the object is just before the arguments.
|
// If this is an object method, the object is just before the arguments.
|
||||||
typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
|
typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
|
||||||
|
|
||||||
|
if (obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL
|
||||||
|
&& !IS_CONSTRUCTOR_METHOD(ufunc))
|
||||||
|
{
|
||||||
|
// If this is not the constructor method, then a valid object is
|
||||||
|
// needed.
|
||||||
|
emsg(_(e_using_null_object));
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
// Check the argument types.
|
// Check the argument types.
|
||||||
if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
|
if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
@ -599,7 +608,7 @@ call_dfunc(
|
|||||||
|
|
||||||
// For an object method move the object from just before the arguments to
|
// For an object method move the object from just before the arguments to
|
||||||
// the first local variable.
|
// the first local variable.
|
||||||
if (ufunc->uf_flags & FC_OBJECT)
|
if (IS_OBJECT_METHOD(ufunc))
|
||||||
{
|
{
|
||||||
*STACK_TV_VAR(0) = *obj;
|
*STACK_TV_VAR(0) = *obj;
|
||||||
obj->v_type = VAR_UNKNOWN;
|
obj->v_type = VAR_UNKNOWN;
|
||||||
@ -1148,7 +1157,7 @@ func_return(ectx_T *ectx)
|
|||||||
// Clear the arguments. If this was an object method also clear the
|
// Clear the arguments. If this was an object method also clear the
|
||||||
// object, it is just before the arguments.
|
// object, it is just before the arguments.
|
||||||
int top = ectx->ec_frame_idx - argcount;
|
int top = ectx->ec_frame_idx - argcount;
|
||||||
if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
|
if (IS_OBJECT_METHOD(dfunc->df_ufunc))
|
||||||
--top;
|
--top;
|
||||||
for (idx = top; idx < ectx->ec_frame_idx; ++idx)
|
for (idx = top; idx < ectx->ec_frame_idx; ++idx)
|
||||||
clear_tv(STACK_TV(idx));
|
clear_tv(STACK_TV(idx));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user