Skip to content

Commit d1f1910

Browse files
authored
Merge pull request #484 from s-hadinger/fix_setmember_static
Fix calling 'setmember' with a function
2 parents 78bccb7 + 3f52d70 commit d1f1910

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/be_class.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,14 @@ bbool be_instance_setmember(bvm *vm, binstance *o, bstring *name, bvalue *src)
331331
v = obj->members[v.v.i];
332332
}
333333
if (var_basetype(&v) == BE_FUNCTION) {
334+
if (var_isfunction(src)) {
335+
var_clearstatic(src);
336+
}
334337
bvalue *top = vm->top;
335338
var_setval(top, &v);
336339
var_setinstance(top + 1, o); /* move instance to argv[0] */
337-
var_setstr(top + 2, name); /* move method name to argv[1] */
338-
var_setval(top + 3, src); /* move method name to argv[1] */
340+
var_setstr(top + 2, name); /* move key to argv[1] */
341+
var_setval(top + 3, src); /* move value to argv[2] */
339342
vm->top += 4; /* prevent collection results */
340343
be_dofunc(vm, top, 3); /* call method 'member' */
341344
vm->top -= 4;

tests/virtual_methods2.be

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,20 @@ ta.c = 30
3737
assert(ta.c == 30)
3838
assert(ta.virtual_c == 30)
3939
assert_attribute_error(def() ta.d = 0 end)
40+
41+
# bug: if a function is sent to 'setmember()', the internal BE_STATIC flag is added
42+
# which makes the function not callable and scrambles tostring()
43+
class A
44+
var _v
45+
def init()
46+
self._v = {}
47+
end
48+
def setmember(name, value)
49+
self._v[name] = value
50+
end
51+
end
52+
a = A()
53+
f = def(name, time_ms) return 42 end
54+
a.f = f
55+
56+
assert(a._v['f']() == 42)

0 commit comments

Comments
 (0)