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
funcdo_LOAD_CLASSDEREF(vm*Vm, iint32){
11651165
defervm.CheckException()
1166-
vm.NotImplemented("LOAD_CLASSDEREF", i)
1166+
name, _:=_var_name(vm, i)
1167+
1168+
// Lookup in locals
1169+
ifobj, 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+
ifres==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+
classC1:
6+
"Test 1"
7+
defmethod1(self, x):
8+
"method1"
9+
returnx+1
10+
defmethod2(self, m2):
11+
"method2"
12+
returnself.method1(m2)+m2
13+
14+
c=C1()
15+
assertc.method1(1) ==2
16+
assertc.method2(1) ==3
17+
18+
classC2:
19+
"Test 2"
20+
_VAR=1
21+
VAR=_VAR+1
22+
defmethod1(self, x):
23+
"method1"
24+
returnself.VAR+x
25+
defmethod2(self, m2):
26+
"method2"
27+
returnself.method1(m2)+m2
28+
29+
c=C2()
30+
assertc.method1(1) ==3
31+
assertc.method2(1) ==4
32+
33+
# CLASS_DEREF
34+
35+
# FIXME corner cases in CLASS_DEREF
36+
defclassderef(y):
37+
# FIXME should work on parameter of classderef - y - but doesn't
38+
x=y
39+
classDeRefTest:
40+
VAR=x
41+
defmethod1(self, x):
42+
"method1"
43+
returnself.VAR+x
44+
returnDeRefTest
45+
x=classderef(1)
46+
c=x()
47+
assertc.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
returnargs
151151
# FIXME this blows up: assert fn12() == ()
152152
# FIXME check kwargs passing
153+
154+
155+
#FIXME decorators

0 commit comments

Comments
(0)