mirror of
https://github.com/vim/vim.git
synced 2025-09-25 03:54:15 -04:00
patch 9.0.1140: cannot call an object method in a compiled function
Problem: Cannot call an object method in a compiled function. Solution: Compile the instructins to invoke an object method.
This commit is contained in:
@@ -1822,8 +1822,8 @@ struct ufunc_S
|
|||||||
// copy_lambda_to_global_func()
|
// copy_lambda_to_global_func()
|
||||||
#define FC_LAMBDA 0x2000 // one line "return {expr}"
|
#define FC_LAMBDA 0x2000 // one line "return {expr}"
|
||||||
|
|
||||||
#define FC_OBJECT 010000 // object method
|
#define FC_OBJECT 0x4000 // object method
|
||||||
#define FC_NEW 030000 // constructor (also an object method)
|
#define FC_NEW 0x8000 // constructor
|
||||||
|
|
||||||
#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
|
||||||
|
@@ -329,6 +329,7 @@ def Test_class_object_member_access()
|
|||||||
|
|
||||||
class MyCar
|
class MyCar
|
||||||
this.make: string
|
this.make: string
|
||||||
|
this.age = 5
|
||||||
|
|
||||||
def new(make_arg: string)
|
def new(make_arg: string)
|
||||||
this.make = make_arg
|
this.make = make_arg
|
||||||
@@ -337,6 +338,9 @@ def Test_class_object_member_access()
|
|||||||
def GetMake(): string
|
def GetMake(): string
|
||||||
return $"make = {this.make}"
|
return $"make = {this.make}"
|
||||||
enddef
|
enddef
|
||||||
|
def GetAge(): number
|
||||||
|
return this.age
|
||||||
|
enddef
|
||||||
endclass
|
endclass
|
||||||
|
|
||||||
var c = MyCar.new("abc")
|
var c = MyCar.new("abc")
|
||||||
@@ -347,6 +351,12 @@ def Test_class_object_member_access()
|
|||||||
|
|
||||||
var c2 = MyCar.new("123")
|
var c2 = MyCar.new("123")
|
||||||
assert_equal('make = 123', c2.GetMake())
|
assert_equal('make = 123', c2.GetMake())
|
||||||
|
|
||||||
|
def CheckCar()
|
||||||
|
assert_equal("make = def", c.GetMake())
|
||||||
|
assert_equal(5, c.GetAge())
|
||||||
|
enddef
|
||||||
|
CheckCar()
|
||||||
END
|
END
|
||||||
v9.CheckScriptSuccess(lines)
|
v9.CheckScriptSuccess(lines)
|
||||||
|
|
||||||
|
@@ -695,6 +695,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 */
|
||||||
|
/**/
|
||||||
|
1140,
|
||||||
/**/
|
/**/
|
||||||
1139,
|
1139,
|
||||||
/**/
|
/**/
|
||||||
|
@@ -45,7 +45,7 @@ lookup_local(char_u *name, size_t len, lvar_T *lvar, cctx_T *cctx)
|
|||||||
|
|
||||||
if (len == 4 && STRNCMP(name, "this", 4) == 0
|
if (len == 4 && STRNCMP(name, "this", 4) == 0
|
||||||
&& cctx->ctx_ufunc != NULL
|
&& cctx->ctx_ufunc != NULL
|
||||||
&& (cctx->ctx_ufunc->uf_flags & FC_OBJECT))
|
&& (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW)))
|
||||||
{
|
{
|
||||||
if (lvar != NULL)
|
if (lvar != NULL)
|
||||||
{
|
{
|
||||||
@@ -313,7 +313,7 @@ variable_exists(char_u *name, size_t len, cctx_T *cctx)
|
|||||||
|| arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
|
|| arg_exists(name, len, NULL, NULL, NULL, cctx) == OK
|
||||||
|| (len == 4
|
|| (len == 4
|
||||||
&& cctx->ctx_ufunc != NULL
|
&& cctx->ctx_ufunc != NULL
|
||||||
&& (cctx->ctx_ufunc->uf_flags & FC_OBJECT)
|
&& (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))
|
||||||
&& STRNCMP(name, "this", 4) == 0)))
|
&& STRNCMP(name, "this", 4) == 0)))
|
||||||
|| script_var_exists(name, len, cctx, NULL) == OK
|
|| script_var_exists(name, len, cctx, NULL) == OK
|
||||||
|| class_member_index(name, len, NULL, cctx) >= 0
|
|| class_member_index(name, len, NULL, cctx) >= 0
|
||||||
@@ -3018,7 +3018,7 @@ compile_def_function(
|
|||||||
goto erret;
|
goto erret;
|
||||||
|
|
||||||
// For an object method and constructor "this" is the first local variable.
|
// For an object method and constructor "this" is the first local variable.
|
||||||
if (ufunc->uf_flags & FC_OBJECT)
|
if (ufunc->uf_flags & (FC_OBJECT|FC_NEW))
|
||||||
{
|
{
|
||||||
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
|
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
|
||||||
+ ufunc->uf_dfunc_idx;
|
+ ufunc->uf_dfunc_idx;
|
||||||
|
@@ -532,6 +532,9 @@ call_dfunc(
|
|||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If this is an object method, the object is just before the arguments.
|
||||||
|
typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
|
||||||
|
|
||||||
// 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;
|
||||||
@@ -594,6 +597,14 @@ call_dfunc(
|
|||||||
}
|
}
|
||||||
ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
|
ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
|
||||||
|
|
||||||
|
// For an object method move the object from just before the arguments to
|
||||||
|
// the first local variable.
|
||||||
|
if (ufunc->uf_flags & FC_OBJECT)
|
||||||
|
{
|
||||||
|
*STACK_TV_VAR(0) = *obj;
|
||||||
|
obj->v_type = VAR_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
|
partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
|
||||||
if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
|
if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
|
||||||
{
|
{
|
||||||
@@ -1073,7 +1084,6 @@ func_return(ectx_T *ectx)
|
|||||||
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
|
dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
|
||||||
+ ectx->ec_dfunc_idx;
|
+ ectx->ec_dfunc_idx;
|
||||||
int argcount = ufunc_argcount(dfunc->df_ufunc);
|
int argcount = ufunc_argcount(dfunc->df_ufunc);
|
||||||
int top = ectx->ec_frame_idx - argcount;
|
|
||||||
estack_T *entry;
|
estack_T *entry;
|
||||||
int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
|
int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
|
||||||
+ STACK_FRAME_FUNC_OFF)->vval.v_number;
|
+ STACK_FRAME_FUNC_OFF)->vval.v_number;
|
||||||
@@ -1111,7 +1121,11 @@ func_return(ectx_T *ectx)
|
|||||||
if (handle_closure_in_use(ectx, TRUE) == FAIL)
|
if (handle_closure_in_use(ectx, TRUE) == FAIL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
|
|
||||||
// Clear the arguments.
|
// Clear the arguments. If this was an object method also clear the
|
||||||
|
// object, it is just before the arguments.
|
||||||
|
int top = ectx->ec_frame_idx - argcount;
|
||||||
|
if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
|
||||||
|
--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));
|
||||||
|
|
||||||
|
@@ -273,9 +273,12 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
|
|||||||
class_T *cl = (class_T *)type->tt_member;
|
class_T *cl = (class_T *)type->tt_member;
|
||||||
if (*name_end == '(')
|
if (*name_end == '(')
|
||||||
{
|
{
|
||||||
|
int function_count;
|
||||||
|
ufunc_T **functions;
|
||||||
|
|
||||||
if (type->tt_type == VAR_CLASS)
|
if (type->tt_type == VAR_CLASS)
|
||||||
{
|
{
|
||||||
garray_T *instr = &cctx->ctx_instr;
|
garray_T *instr = &cctx->ctx_instr;
|
||||||
if (instr->ga_len > 0)
|
if (instr->ga_len > 0)
|
||||||
{
|
{
|
||||||
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
|
isn_T *isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
|
||||||
@@ -288,34 +291,47 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < cl->class_class_function_count; ++i)
|
function_count = cl->class_class_function_count;
|
||||||
{
|
functions = cl->class_class_functions;
|
||||||
ufunc_T *fp = cl->class_class_functions[i];
|
|
||||||
// Use a separate pointer to avoid that ASAN complains about
|
|
||||||
// uf_name[] only being 4 characters.
|
|
||||||
char_u *ufname = (char_u *)fp->uf_name;
|
|
||||||
if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
|
|
||||||
{
|
|
||||||
*arg = skipwhite(name_end + 1);
|
|
||||||
int argcount = 0;
|
|
||||||
if (compile_arguments(arg, cctx, &argcount,
|
|
||||||
CA_NOT_SPECIAL) == FAIL)
|
|
||||||
return FAIL;
|
|
||||||
return generate_CALL(cctx, fp, argcount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
semsg(_(e_method_not_found_on_class_str_str),
|
|
||||||
cl->class_name, name);
|
|
||||||
return FAIL;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO: method call
|
// type->tt_type == VAR_OBJECT: method call
|
||||||
emsg("compile_class_object_index(): object call not handled yet");
|
function_count = cl->class_obj_method_count;
|
||||||
|
functions = cl->class_obj_methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ufunc_T *ufunc = NULL;
|
||||||
|
for (int i = 0; i < function_count; ++i)
|
||||||
|
{
|
||||||
|
ufunc_T *fp = functions[i];
|
||||||
|
// Use a separate pointer to avoid that ASAN complains about
|
||||||
|
// uf_name[] only being 4 characters.
|
||||||
|
char_u *ufname = (char_u *)fp->uf_name;
|
||||||
|
if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
|
||||||
|
{
|
||||||
|
ufunc = fp;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ufunc == NULL)
|
||||||
|
{
|
||||||
|
// TODO: different error for object method?
|
||||||
|
semsg(_(e_method_not_found_on_class_str_str), cl->class_name, 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.
|
||||||
|
*arg = skipwhite(name_end + 1);
|
||||||
|
int argcount = 0;
|
||||||
|
if (compile_arguments(arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL)
|
||||||
|
return FAIL;
|
||||||
|
return generate_CALL(cctx, ufunc, argcount);
|
||||||
}
|
}
|
||||||
else if (type->tt_type == VAR_OBJECT)
|
|
||||||
|
if (type->tt_type == VAR_OBJECT)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < cl->class_obj_member_count; ++i)
|
for (int i = 0; i < cl->class_obj_member_count; ++i)
|
||||||
{
|
{
|
||||||
|
@@ -1136,7 +1136,7 @@ check_reserved_name(char_u *name, cctx_T *cctx)
|
|||||||
&& !(STRCMP("this", name) == 0
|
&& !(STRCMP("this", name) == 0
|
||||||
&& cctx != NULL
|
&& cctx != NULL
|
||||||
&& cctx->ctx_ufunc != NULL
|
&& cctx->ctx_ufunc != NULL
|
||||||
&& (cctx->ctx_ufunc->uf_flags & FC_OBJECT)))
|
&& (cctx->ctx_ufunc->uf_flags & (FC_OBJECT|FC_NEW))))
|
||||||
{
|
{
|
||||||
semsg(_(e_cannot_use_reserved_name_str), name);
|
semsg(_(e_cannot_use_reserved_name_str), name);
|
||||||
return FAIL;
|
return FAIL;
|
||||||
|
Reference in New Issue
Block a user