v9.CheckScriptSuccess(lines)
enddef
+" Test for using a interface method using a child object
+def Test_interface_method_from_child()
+ var lines =<< trim END
+ vim9script
+
+ interface A
+ def Foo(): string
+ endinterface
+
+ class B implements A
+ def Foo(): string
+ return 'foo'
+ enddef
+ endclass
+
+ class C extends B
+ def Bar(): string
+ return 'bar'
+ enddef
+ endclass
+
+ def T1(a: A)
+ assert_equal('foo', a.Foo())
+ enddef
+
+ def T2(b: B)
+ assert_equal('foo', b.Foo())
+ enddef
+
+ var c = C.new()
+ T1(c)
+ T2(c)
+ END
+ v9.CheckScriptSuccess(lines)
+enddef
+
+" Test for using an interface method using a child object when it is overridden
+" by the child class.
+" FIXME: This test fails.
+" def Test_interface_overridden_method_from_child()
+" var lines =<< trim END
+" vim9script
+"
+" interface A
+" def Foo(): string
+" endinterface
+"
+" class B implements A
+" def Foo(): string
+" return 'b-foo'
+" enddef
+" endclass
+"
+" class C extends B
+" def Bar(): string
+" return 'bar'
+" enddef
+" def Foo(): string
+" return 'c-foo'
+" enddef
+" endclass
+"
+" def T1(a: A)
+" assert_equal('c-foo', a.Foo())
+" enddef
+"
+" def T2(b: B)
+" assert_equal('c-foo', b.Foo())
+" enddef
+"
+" var c = C.new()
+" T1(c)
+" T2(c)
+" END
+" v9.CheckScriptSuccess(lines)
+" enddef
+
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
if (cl == itf)
return idx;
- itf2class_T *i2c = NULL;
- int searching = TRUE;
+ itf2class_T *i2c = NULL;
+ int searching = TRUE;
+ int method_offset = 0;
+
for (class_T *super = cl; super != NULL && searching;
super = super->class_extends)
+ {
for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
+ {
if (i2c->i2c_class == super && i2c->i2c_is_method == is_method)
{
searching = FALSE;
break;
}
+ }
+ if (searching && is_method)
+ // The parent class methods are stored after the current class
+ // methods.
+ method_offset += is_static
+ ? super->class_class_function_count_child
+ : super->class_obj_method_count_child;
+ }
if (i2c == NULL)
{
siemsg("class %s not found on interface %s",
{
// A table follows the i2c for the class
int *table = (int *)(i2c + 1);
- return table[idx];
+ // "method_offset" is 0, if method is in the current class. If method
+ // is in a parent class, then it is non-zero.
+ return table[idx] + method_offset;
}
}