I wrote:
> > Hm, I haven't looked closely at the vim9 implementation, but here Vim only
> > seems to handle `this` correctly. Which means, if you use:
> > ```
> > var that = yobj.xobj
> > ```
> > it starts to work :)
> >
> > I think, instead of hard-coding the 5 we need this patch here:
> > ```patch
> > diff --git a/src/vim9compile.c b/src/vim9compile.c
> > index 64526aa9d..0f795f799 100644
> > --- a/src/vim9compile.c
> > +++ b/src/vim9compile.c
> > @@ -2065,10 +2065,16 @@ compile_load_lhs_with_index(lhs_T *lhs, char_u
> > *var_start, cctx_T *cctx)
> > {
> > if (lhs->lhs_type->tt_type == VAR_OBJECT)
> > {
> > - // "this.value": load "this" object and get the value at index
> > + char_u *dot = vim_strchr(var_start, '.');
> > +
> > + if (dot == NULL)
> > + return FAIL;
> > +
> > + // "object.value" or "this.value":
> > + // load "this" object and get the value at index
> > // for an object or class member get the type of the member
> > class_T *cl = lhs->lhs_type->tt_class;
> > - type_T *type = class_member_type(cl, var_start + 5,
> > + type_T *type = class_member_type(cl, dot + 1,
> > lhs->lhs_end,
> > &lhs->lhs_member_idx);
> > if (lhs->lhs_member_idx < 0)
> > return FAIL;
> > ```
>
> Thanks for the patch. This fixes the example given, but when I add a
> test for it then it fails. I'll have to do some debugging to find out
> why. I added the test in testdir/test_vim9_class.vim:
>
> --- a/src/testdir/test_vim9_class.vim 2023-04-18 19:07:21.202891893 +0100
> +++ b/src/testdir/test_vim9_class.vim 2023-04-24 13:52:50.350343550 +0100
> @@ -401,6 +401,13 @@
> var f = Foo.new(3)
> f.Add(17)
> assert_equal(20, f.x)
> +
> + def AddToFoo(obj: Foo)
> + obj.x += 3
> + enddef
> +
> + AddToFoo(f)
> + assert_equal(23, f.x)
> END
> v9.CheckScriptSuccess(lines)
> enddef
Turns out a LOAD instruction is generated that assumes "this". Thus
when the object is an argument it will load a random variable value.
I found a fix for that, will send out a patch.
--
Mushrooms always grow in damp places and so they look like umbrellas.
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/vim_dev/20230424161434.8987E1C074F%40moolenaar.net.