Skip to content

Commit 027a3a8

Browse files
author
hartsantler
committed
Go backend: subclasses, methods can over-ride the parents, but not extend.
1 parent de044f4 commit 027a3a8

3 files changed

Lines changed: 76 additions & 6 deletions

File tree

pythonjs/pythonjs_to_go.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ def visit_ClassDef(self, node):
3232

3333
self._classes[ node.name ] = node
3434
self._class_props[ node.name ] = props
35+
36+
37+
for base in node.bases:
38+
n = self.visit(base)
39+
if n == 'object':
40+
continue
41+
node._parents.add( n )
42+
43+
bases.add( n )
44+
if n in self._class_props:
45+
props.update( self._class_props[n] )
46+
base_classes.add( self._classes[n] )
47+
#else: ## special case - subclassing a builtin like `list`
48+
# continue
49+
50+
for p in self._classes[ n ]._parents:
51+
bases.add( p )
52+
props.update( self._class_props[p] )
53+
base_classes.add( self._classes[p] )
54+
55+
3556
for decor in node.decorator_list: ## class decorators
3657
if isinstance(decor, ast.Call):
3758
assert decor.func.id=='__struct__'
@@ -47,10 +68,11 @@ def visit_ClassDef(self, node):
4768

4869

4970
init = None
50-
71+
method_names = set()
5172
for b in node.body:
52-
out.append( self.visit(b) )
5373
assert isinstance(b, ast.FunctionDef)
74+
method_names.add( b.name )
75+
out.append( self.visit(b) )
5476
if b.name == '__init__':
5577
init = b
5678

@@ -66,6 +88,21 @@ def visit_ClassDef(self, node):
6688
out.append( 'func __new__%s() *%s { return &%s{} }' %(node.name, node.name, node.name))
6789

6890

91+
if base_classes:
92+
for bnode in base_classes:
93+
for b in bnode.body:
94+
if isinstance(b, ast.FunctionDef):
95+
if b.name == '__init__': continue
96+
if b.name in method_names: continue
97+
out.append( self.visit(b) )
98+
#args = [self.visit(a) for a in b.args.args][1:]
99+
#args = ','.join(args)
100+
#if args:
101+
# out.append(self.indent()+ '%s(%s) { return %s.__%s(this,%s); }'%(b.name, args, bnode.name, b.name, args) )
102+
#else:
103+
# out.append(self.indent()+ '%s() { return %s.__%s(this); }'%(b.name, bnode.name, b.name) )
104+
105+
69106
self._class_stack.pop()
70107
return '\n'.join(out)
71108

@@ -443,6 +480,7 @@ def visit_While(self, node):
443480

444481
def _inline_code_helper(self, s):
445482
return s
483+
#return 'js.Global.Call("eval", "%s")' %s ## TODO inline JS()
446484

447485

448486

regtests/class/mi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
multiple inheritance
33
'''
44
class A:
5-
def foo(self):
5+
def foo(self) -> int:
66
return 1
77

88
class B:
9-
def bar(self):
9+
def bar(self) -> int:
1010
return 2
1111

1212
class C( A, B ):
13-
def call_foo_bar(self):
13+
def call_foo_bar(self) -> int:
1414
a = self.foo()
1515
a += self.bar()
1616
return a
1717

1818
## extend foo ##
19-
def foo(self):
19+
def foo(self) -> int:
2020
a = A.foo(self)
2121
a += 100
2222
return a

regtests/class/mi_override.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
multiple inheritance
3+
'''
4+
class A:
5+
def foo(self) -> int:
6+
return 1
7+
8+
class B:
9+
def bar(self) -> int:
10+
return 2
11+
12+
class C( A, B ):
13+
def call_foo_bar(self) -> int:
14+
a = self.foo()
15+
a += self.bar()
16+
return a
17+
18+
## override foo ##
19+
def foo(self) -> int:
20+
return 100
21+
22+
def main():
23+
a = A()
24+
TestError( a.foo()==1 )
25+
b = B()
26+
TestError( b.bar()==2 )
27+
28+
c = C()
29+
TestError( c.foo()==100 )
30+
TestError( c.bar()==2 )
31+
32+
TestError( c.call_foo_bar()==102 )

0 commit comments

Comments
 (0)