Skip to content

Commit bf0f523

Browse files
committed
vm: class definitions
1 parent 114c283 commit bf0f523

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

vm/eval.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,19 @@ func do_LOAD_DEREF(vm *Vm, i int32) {
11631163
// class bodies.
11641164
func do_LOAD_CLASSDEREF(vm *Vm, i int32) {
11651165
defer vm.CheckException()
1166-
vm.NotImplemented("LOAD_CLASSDEREF", i)
1166+
name, _ := _var_name(vm, i)
1167+
1168+
// Lookup in locals
1169+
if obj, ok := vm.frame.Locals[name]; ok {
1170+
vm.PUSH(obj)
1171+
}
1172+
// If that failed look at the cell
1173+
res := vm.frame.CellAndFreeVars[i].(*py.Cell).Get()
1174+
if res == nil {
1175+
unboundDeref(vm, i)
1176+
} else {
1177+
vm.PUSH(res)
1178+
}
11671179
}
11681180

11691181
// Stores TOS into the cell contained in slot i of the cell and free

vm/jumptable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,5 @@ func init() {
138138
jumpTable[SET_ADD] = do_SET_ADD
139139
jumpTable[MAP_ADD] = do_MAP_ADD
140140

141-
// jumpTable[LOAD_CLASSDEREF] = do_LOAD_CLASSDEREF
141+
jumpTable[LOAD_CLASSDEREF] = do_LOAD_CLASSDEREF
142142
}

vm/tests/class.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3.4
2+
3+
# Test class definitions
4+
5+
class C1:
6+
"Test 1"
7+
def method1(self, x):
8+
"method1"
9+
return x+1
10+
def method2(self, m2):
11+
"method2"
12+
return self.method1(m2)+m2
13+
14+
c = C1()
15+
assert c.method1(1) == 2
16+
assert c.method2(1) == 3
17+
18+
class C2:
19+
"Test 2"
20+
_VAR = 1
21+
VAR = _VAR + 1
22+
def method1(self, x):
23+
"method1"
24+
return self.VAR + x
25+
def method2(self, m2):
26+
"method2"
27+
return self.method1(m2)+m2
28+
29+
c = C2()
30+
assert c.method1(1) == 3
31+
assert c.method2(1) == 4
32+
33+
# CLASS_DEREF
34+
35+
# FIXME corner cases in CLASS_DEREF
36+
def classderef(y):
37+
# FIXME should work on parameter of classderef - y - but doesn't
38+
x = y
39+
class DeRefTest:
40+
VAR = x
41+
def method1(self, x):
42+
"method1"
43+
return self.VAR+x
44+
return DeRefTest
45+
x = classderef(1)
46+
c = x()
47+
assert c.method1(1) == 2
48+

vm/tests/functions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,6 @@ def fn12(*args,a=2,b=3,**kwargs) -> "RET":
150150
return args
151151
# FIXME this blows up: assert fn12() == ()
152152
# FIXME check kwargs passing
153+
154+
155+
#FIXME decorators

0 commit comments

Comments
 (0)