Skip to content

Commit 29ac5df

Browse files
committed
patch 9.0.1209: getting interface member does not always work
Problem: Getting interface member does not always work. Solution: Convert the index on the interface to the index on the object. (closes #11825)
1 parent a41e221 commit 29ac5df

11 files changed

Lines changed: 162 additions & 20 deletions

File tree

src/proto/vim9class.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* vim9class.c */
2+
int object_index_from_itf_index(class_T *itf, int idx, class_T *cl);
23
void ex_class(exarg_T *eap);
34
type_T *class_member_type(class_T *cl, char_u *name, char_u *name_end, int *member_idx);
45
void ex_enum(exarg_T *eap);

src/proto/vim9instr.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ isn_T *generate_instr_type(cctx_T *cctx, isntype_T isn_type, type_T *type);
55
isn_T *generate_instr_debug(cctx_T *cctx);
66
int generate_CONSTRUCT(cctx_T *cctx, class_T *cl);
77
int generate_GET_OBJ_MEMBER(cctx_T *cctx, int idx, type_T *type);
8+
int generate_GET_ITF_MEMBER(cctx_T *cctx, class_T *itf, int itf_idx, type_T *type);
89
int generate_STORE_THIS(cctx_T *cctx, int idx);
910
int may_generate_2STRING(int offset, int tolerant, cctx_T *cctx);
1011
int generate_add_instr(cctx_T *cctx, vartype_T vartype, type_T *type1, type_T *type2, exprtype_T expr_type);

src/structs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,14 @@ typedef struct {
14831483
char_u *ocm_init; // allocated
14841484
} ocmember_T;
14851485

1486+
// used for the lookup table of a class member index
1487+
typedef struct itf2class_S itf2class_T;
1488+
struct itf2class_S {
1489+
itf2class_T *i2c_next;
1490+
class_T *i2c_class;
1491+
// array with ints follows
1492+
};
1493+
14861494
#define CLASS_INTERFACE 1
14871495

14881496
// "class_T": used for v_class of typval of VAR_CLASS
@@ -1501,6 +1509,7 @@ struct class_S
15011509
int class_interface_count;
15021510
char_u **class_interfaces; // allocated array of names
15031511
class_T **class_interfaces_cl; // interfaces (counts as reference)
1512+
itf2class_T *class_itf2class; // member index lookup tables
15041513

15051514
// class members: "static varname"
15061515
int class_class_member_count;

src/testdir/test_vim9_class.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,32 @@ def Test_class_implements_interface()
870870
endclass
871871
END
872872
v9.CheckScriptFailure(lines, 'E1349: Function "Methods" of interface "Some" not implemented')
873+
874+
# Check different order of members in class and interface works.
875+
lines =<< trim END
876+
vim9script
877+
878+
interface Result
879+
this.label: string
880+
this.errpos: number
881+
endinterface
882+
883+
# order of members is opposite of interface
884+
class Failure implements Result
885+
this.errpos: number = 42
886+
this.label: string = 'label'
887+
endclass
888+
889+
def Test()
890+
var result: Result = Failure.new()
891+
892+
assert_equal('label', result.label)
893+
assert_equal(42, result.errpos)
894+
enddef
895+
896+
Test()
897+
END
898+
v9.CheckScriptSuccess(lines)
873899
enddef
874900

875901
def Test_class_used_as_type()

src/version.c

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

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1209,
698700
/**/
699701
1208,
700702
/**/

src/vim9.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ typedef enum {
3434
ISN_INSTR, // instructions compiled from expression
3535
ISN_CONSTRUCT, // construct an object, using contstruct_T
3636
ISN_GET_OBJ_MEMBER, // object member, index is isn_arg.number
37+
ISN_GET_ITF_MEMBER, // interface member, index is isn_arg.classmember
3738
ISN_STORE_THIS, // store value in "this" object member, index is
3839
// isn_arg.number
39-
ISN_LOAD_CLASSMEMBER, // load class member, using classmember_T
40-
ISN_STORE_CLASSMEMBER, // store in class member, using classmember_T
40+
ISN_LOAD_CLASSMEMBER, // load class member, using isn_arg.classmember
41+
ISN_STORE_CLASSMEMBER, // store in class member, using isn_arg.classmember
4142

4243
// get and set variables
4344
ISN_LOAD, // push local variable isn_arg.number
@@ -480,7 +481,7 @@ typedef struct {
480481
class_T *construct_class; // class the object is created from
481482
} construct_T;
482483

483-
// arguments to ISN_STORE_CLASSMEMBER and ISN_LOAD_CLASSMEMBER
484+
// arguments to ISN_STORE_CLASSMEMBER, ISN_LOAD_CLASSMEMBER, ISN_GET_ITF_MEMBER
484485
typedef struct {
485486
class_T *cm_class;
486487
int cm_idx;

src/vim9class.c

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,32 @@ add_members_to_class(
196196
return OK;
197197
}
198198

199+
/*
200+
* Convert a member index "idx" of interface "itf" to the member index of class
201+
* "cl" implementing that interface.
202+
*/
203+
int
204+
object_index_from_itf_index(class_T *itf, int idx, class_T *cl)
205+
{
206+
if (idx > itf->class_obj_member_count)
207+
{
208+
siemsg("index %d out of range for interface %s", idx, itf->class_name);
209+
return 0;
210+
}
211+
itf2class_T *i2c;
212+
for (i2c = itf->class_itf2class; i2c != NULL; i2c = i2c->i2c_next)
213+
if (i2c->i2c_class == cl)
214+
break;
215+
if (i2c == NULL)
216+
{
217+
siemsg("class %s not found on interface %s",
218+
cl->class_name, itf->class_name);
219+
return 0;
220+
}
221+
int *table = (int *)(i2c + 1);
222+
return table[idx];
223+
}
224+
199225
/*
200226
* Handle ":class" and ":abstract class" up to ":endclass".
201227
* Handle ":interface" up to ":endinterface".
@@ -765,22 +791,6 @@ ex_class(exarg_T *eap)
765791

766792
cl->class_extends = extends_cl;
767793

768-
if (ga_impl.ga_len > 0)
769-
{
770-
// Move the "implements" names into the class.
771-
cl->class_interface_count = ga_impl.ga_len;
772-
cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
773-
if (cl->class_interfaces == NULL)
774-
goto cleanup;
775-
for (int i = 0; i < ga_impl.ga_len; ++i)
776-
cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
777-
VIM_CLEAR(ga_impl.ga_data);
778-
ga_impl.ga_len = 0;
779-
780-
cl->class_interfaces_cl = intf_classes;
781-
intf_classes = NULL;
782-
}
783-
784794
// Add class and object members to "cl".
785795
if (add_members_to_class(&classmembers,
786796
extends_cl == NULL ? NULL
@@ -798,6 +808,48 @@ ex_class(exarg_T *eap)
798808
&cl->class_obj_member_count) == FAIL)
799809
goto cleanup;
800810

811+
if (ga_impl.ga_len > 0)
812+
{
813+
// Move the "implements" names into the class.
814+
cl->class_interface_count = ga_impl.ga_len;
815+
cl->class_interfaces = ALLOC_MULT(char_u *, ga_impl.ga_len);
816+
if (cl->class_interfaces == NULL)
817+
goto cleanup;
818+
for (int i = 0; i < ga_impl.ga_len; ++i)
819+
cl->class_interfaces[i] = ((char_u **)ga_impl.ga_data)[i];
820+
VIM_CLEAR(ga_impl.ga_data);
821+
ga_impl.ga_len = 0;
822+
823+
// For each interface add a lookuptable for the member index on the
824+
// interface to the member index in this class.
825+
for (int i = 0; i < cl->class_interface_count; ++i)
826+
{
827+
class_T *ifcl = intf_classes[i];
828+
itf2class_T *if2cl = alloc_clear(sizeof(itf2class_T)
829+
+ ifcl->class_obj_member_count * sizeof(int));
830+
if (if2cl == NULL)
831+
goto cleanup;
832+
if2cl->i2c_next = ifcl->class_itf2class;
833+
ifcl->class_itf2class = if2cl;
834+
if2cl->i2c_class = cl;
835+
836+
for (int if_i = 0; if_i < ifcl->class_obj_member_count; ++if_i)
837+
for (int cl_i = 0; cl_i < cl->class_obj_member_count; ++cl_i)
838+
{
839+
if (STRCMP(ifcl->class_obj_members[if_i].ocm_name,
840+
cl->class_obj_members[cl_i].ocm_name) == 0)
841+
{
842+
int *table = (int *)(if2cl + 1);
843+
table[if_i] = cl_i;
844+
break;
845+
}
846+
}
847+
}
848+
849+
cl->class_interfaces_cl = intf_classes;
850+
intf_classes = NULL;
851+
}
852+
801853
if (is_class && cl->class_class_member_count > 0)
802854
{
803855
// Allocate a typval for each class member and initialize it.
@@ -1411,6 +1463,13 @@ class_unref(class_T *cl)
14111463
vim_free(cl->class_interfaces);
14121464
vim_free(cl->class_interfaces_cl);
14131465

1466+
itf2class_T *next;
1467+
for (itf2class_T *i2c = cl->class_itf2class; i2c != NULL; i2c = next)
1468+
{
1469+
next = i2c->i2c_next;
1470+
vim_free(i2c);
1471+
}
1472+
14141473
for (int i = 0; i < cl->class_class_member_count; ++i)
14151474
{
14161475
ocmember_T *m = &cl->class_class_members[i];

src/vim9compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,6 +2056,8 @@ compile_load_lhs_with_index(lhs_T *lhs, char_u *var_start, cctx_T *cctx)
20562056

20572057
if (generate_LOAD(cctx, ISN_LOAD, 0, NULL, lhs->lhs_type) == FAIL)
20582058
return FAIL;
2059+
if (cl->class_flags & CLASS_INTERFACE)
2060+
return generate_GET_ITF_MEMBER(cctx, cl, lhs->lhs_member_idx, type);
20592061
return generate_GET_OBJ_MEMBER(cctx, lhs->lhs_member_idx, type);
20602062
}
20612063

src/vim9execute.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5184,6 +5184,7 @@ exec_instructions(ectx_T *ectx)
51845184
break;
51855185

51865186
case ISN_GET_OBJ_MEMBER:
5187+
case ISN_GET_ITF_MEMBER:
51875188
{
51885189
tv = STACK_TV_BOT(-1);
51895190
if (tv->v_type != VAR_OBJECT)
@@ -5200,8 +5201,20 @@ exec_instructions(ectx_T *ectx)
52005201
clear_type_list(&type_list);
52015202
goto on_error;
52025203
}
5203-
int idx = iptr->isn_arg.number;
5204+
52045205
object_T *obj = tv->vval.v_object;
5206+
int idx;
5207+
if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
5208+
idx = iptr->isn_arg.number;
5209+
else
5210+
{
5211+
idx = iptr->isn_arg.classmember.cm_idx;
5212+
// convert the interface index to the object index
5213+
idx = object_index_from_itf_index(
5214+
iptr->isn_arg.classmember.cm_class,
5215+
idx, obj->obj_class);
5216+
}
5217+
52055218
// the members are located right after the object struct
52065219
typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
52075220
copy_tv(mtv, tv);
@@ -6912,6 +6925,11 @@ list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
69126925
iptr->isn_arg.string); break;
69136926
case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
69146927
(int)iptr->isn_arg.number); break;
6928+
case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
6929+
pfx, current,
6930+
(int)iptr->isn_arg.classmember.cm_idx,
6931+
iptr->isn_arg.classmember.cm_class->class_name);
6932+
break;
69156933
case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
69166934
(int)iptr->isn_arg.number); break;
69176935
case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;

src/vim9expr.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
364364
}
365365

366366
*arg = name_end;
367+
if (cl->class_flags & CLASS_INTERFACE)
368+
return generate_GET_ITF_MEMBER(cctx, cl, i, m->ocm_type);
367369
return generate_GET_OBJ_MEMBER(cctx, i, m->ocm_type);
368370
}
369371
}

0 commit comments

Comments
 (0)