Skip to content

Commit 1bc0b9d

Browse files
author
hartsantler
committed
Go backend: fixed subclasses without __init__.
1 parent 4f39bd5 commit 1bc0b9d

2 files changed

Lines changed: 69 additions & 16 deletions

File tree

pythonjs/pythonjs_to_go.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, requirejs=False, insert_runtime=False):
2424
def visit_ClassDef(self, node):
2525
self._class_stack.append( node )
2626
node._parents = set()
27+
node._struct_def = dict()
2728
out = []
2829
sdef = dict()
2930
props = set()
@@ -61,7 +62,13 @@ def visit_ClassDef(self, node):
6162
props.add( kw.arg )
6263
sdef[ kw.arg ] = kw.value.id
6364

65+
node._struct_def.update( sdef )
6466
out.append( 'type %s struct {' %node.name)
67+
if base_classes:
68+
for bnode in base_classes:
69+
## Go only needs the name of the parent struct and all its items are inserted automatically ##
70+
out.append('%s' %bnode.name)
71+
6572
for name in sdef:
6673
out.append('%s %s' %(name, sdef[name]))
6774
out.append('}')
@@ -76,7 +83,25 @@ def visit_ClassDef(self, node):
7683
if b.name == '__init__':
7784
init = b
7885

79-
if init:
86+
87+
parent_init = None
88+
if base_classes:
89+
for bnode in base_classes:
90+
for b in bnode.body:
91+
if isinstance(b, ast.FunctionDef):
92+
if b.name in method_names:
93+
continue
94+
if b.name == '__init__':
95+
parent_init = {'class':bnode, 'init':b}
96+
#continue
97+
out.append( self.visit(b) )
98+
99+
if init or parent_init:
100+
if parent_init:
101+
classname = parent_init['class'].name
102+
init = parent_init['init']
103+
else:
104+
classname = node.name
80105

81106
out.append( 'func __new__%s( %s ) *%s {' %(node.name, init._args_signature, node.name))
82107
out.append( ' ob := %s{}' %node.name )
@@ -88,21 +113,6 @@ def visit_ClassDef(self, node):
88113
out.append( 'func __new__%s() *%s { return &%s{} }' %(node.name, node.name, node.name))
89114

90115

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-
106116
self._class_stack.pop()
107117
return '\n'.join(out)
108118

regtests/go/subclass.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
simple class
3+
'''
4+
class A:
5+
{
6+
x:int,
7+
y:int,
8+
z:int,
9+
}
10+
def __init__(self, x:int, y:int, z:int=1):
11+
self.x = x
12+
self.y = y
13+
self.z = z
14+
15+
def mymethod(self, m:int) -> int:
16+
return self.x * m
17+
18+
class B(A):
19+
{
20+
w:string
21+
}
22+
23+
def method2(self, v:string) ->string:
24+
self.w = v
25+
return self.w
26+
27+
def call_method( cb:func(int)(int), mx:int ) ->int:
28+
return cb(mx)
29+
30+
def main():
31+
a = A( 100, 200, z=9999 )
32+
print( a.x )
33+
print( a.y )
34+
print( a.z )
35+
36+
b = a.mymethod(3)
37+
print( b )
38+
39+
c = call_method( a.mymethod, 4 )
40+
print( c )
41+
42+
x = B(1,2,z=3)
43+
print( x.method2('hello world') )

0 commit comments

Comments
 (0)