Skip to content

Commit 90d2937

Browse files
author
hartsantler
committed
gpu class: fixed method subroutines.
1 parent 026fe9d commit 90d2937

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -830,14 +830,16 @@ def _visit_js_classdef(self, node):
830830
else:
831831
class_decorators.append( decorator )
832832

833+
method_names = [] ## write back in order (required by GLSL)
833834
methods = {}
834835
class_vars = []
835836

836837
for item in node.body:
837838
if isinstance(item, FunctionDef):
839+
method_names.append(item.name)
838840
methods[ item.name ] = item
839841
if self.is_gpu_method( item ):
840-
item.args.args[0].id = name ## change self to the class name
842+
item.args.args[0].id = name ## change self to the class name, pythonjs.py changes it to 'ClassName self'
841843
else:
842844
item.args.args = item.args.args[1:] ## remove self
843845
finfo = inspect_function( item )
@@ -901,9 +903,9 @@ def _visit_js_classdef(self, node):
901903
writer.write('%s.__uid__ = "" + _PythonJS_UID' %name)
902904
writer.write('_PythonJS_UID += 1')
903905

904-
keys = methods.keys()
905-
keys.sort()
906-
for mname in keys:
906+
#keys = methods.keys()
907+
#keys.sort()
908+
for mname in method_names:
907909
method = methods[mname]
908910
gpu_method = False
909911
for dec in method.decorator_list:
@@ -913,8 +915,10 @@ def _visit_js_classdef(self, node):
913915

914916
if gpu_method:
915917
method.name = '%s_%s' %(name, method.name)
918+
self._in_gpu_method = name ## name of class
916919
line = self.visit(method)
917920
if line: writer.write( line )
921+
self._in_gpu_method = None
918922

919923
else:
920924

@@ -2072,13 +2076,18 @@ def visit_Call(self, node):
20722076
raise SyntaxError( self.format_error(node) )
20732077

20742078
elif self._with_ll or name == 'inline' or self._with_glsl:
2079+
F = self.visit(node.func)
20752080
args = [self.visit(arg) for arg in node.args]
2081+
if hasattr(self, '_in_gpu_method') and self._in_gpu_method and isinstance(node.func, ast.Attribute):
2082+
F = '%s_%s' %(self._in_gpu_method, node.func.attr)
2083+
args.insert(0, 'self')
2084+
20762085
if node.keywords:
20772086
args.extend( [self.visit(x.value) for x in node.keywords] )
2078-
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
2087+
return '%s(%s)' %( F, ','.join(args) )
20792088

20802089
else:
2081-
return '%s(%s)' %( self.visit(node.func), ','.join(args) )
2090+
return '%s(%s)' %( F, ','.join(args) )
20822091

20832092
elif self._with_js or self._with_dart:
20842093
args = list( map(self.visit, node.args) )

regtests/webclgl/gpu_class.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ class MyObject:
55
## below `self` is not `this` in javascript
66
## `self` is a GLSL struct of MyObject
77
@gpu.method
8-
float def mymethod(self, x,y):
8+
float def subroutine(self, x,y):
99
float x
1010
float y
11-
return self.subroutine(x,y) * self.attr1
11+
return x + y * self.attr2
12+
## subroutines must be defined ahead of where they are used
1213

1314
@gpu.method
14-
float def subroutine(self, x,y):
15+
float def mymethod(self, x,y):
1516
float x
1617
float y
17-
return x + y
18+
return self.subroutine(x,y) * self.attr1
19+
1820

1921
## here `self` is javascript's `this`
2022
def __init__(self, a, b):
@@ -25,11 +27,8 @@ def __init__(self, a, b):
2527

2628

2729
class myclass:
28-
def new_struct(self, a, b):
29-
return MyObject( a, b )
30-
3130
def run(self, w):
32-
self.array = [ self.new_struct( x, 1.1 ) for x in range(w) ]
31+
self.array = [ MyObject( x, 1.1 ) for x in range(w) ]
3332

3433
@returns( array=64 )
3534
@gpu.main

0 commit comments

Comments
 (0)