Skip to content

Commit 0c3fb71

Browse files
author
hartsantler
committed
fixed setting the instance type on assignment when calling a method and the method returns a known type.
1 parent 476c913 commit 0c3fb71

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

pythonscript/python_to_pythonjs.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ def visit_Assign(self, node):
475475
#writer.write( '''JS('%s( [%s, %s] )')''' %(setter, name, self.visit(node.value)) ) ## can not nest have nested JS() calls
476476
writer.write( '%s( [%s, %s] )' %(setter, name, self.visit(node.value)) )
477477
fallback = False
478-
478+
else:
479+
writer.write('#!!!! class is known: %s' %klass)
479480

480481
if fallback:
481482
code = 'set_attribute(%s, "%s", %s)' % (
@@ -492,19 +493,23 @@ def visit_Assign(self, node):
492493
self._instances[ target.id ] = node.value.func.id ## keep track of instances
493494
elif isinstance(node.value, Call) and isinstance(node.value.func, Name) and node.value.func.id in self._function_return_types:
494495
self._instances[ target.id ] = self._function_return_types[ node.value.func.id ]
495-
elif target.id in self._instances:
496-
self._instances.pop( target.id ) ## TODO is this correct?
497-
498-
if isinstance(node.value, Name): ## if this is a simple copy: "a = b" and "b" is known to be of some class
499-
name = self.visit(node.value)
500-
if name in self._instances: self._instances[ target.id ] = self._instances[ name ]
501-
writer.write('%s = %s' % (target.id, name))
496+
elif isinstance(node.value, Call) and isinstance(node.value.func, Attribute) and node.value.func.value.id in self._instances:
497+
typedef = self.get_typedef( node.value.func.value )
498+
method = node.value.func.attr
499+
if method in typedef.methods:
500+
func = typedef.get_pythonjs_function_name( method )
501+
if func in self._function_return_types:
502+
self._instances[ target.id ] = self._function_return_types[ func ]
503+
504+
elif isinstance(node.value, Name) and node_value in self._instances: ## if this is a simple copy: "a = b" and "b" is known to be of some class
505+
self._instances[ target.id ] = self._instances[ node_value ]
502506
elif isinstance(node.value, BinOp) and hasattr(node.value, 'operator_overloading') and node.value.operator_overloading in self._function_return_types:
503507
self._instances[ target.id ] = self._function_return_types[ node.value.operator_overloading ]
504-
writer.write('%s = %s' % (target.id, node_value))
505508

506-
else: ## blind assignment
507-
writer.write('%s = %s' % (target.id, node_value))
509+
elif target.id in self._instances:
510+
self._instances.pop( target.id )
511+
512+
writer.write('%s = %s' % (target.id, node_value))
508513

509514
else: # it's a Tuple
510515
id = self.identifier

tests/threejs_vector3_operator_overloading.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
show_vec(b)
4141

4242
c = b.clone()
43-
assert isinstance(c, Vector3)
43+
c.normalize()
44+
print( c.x )
45+
v1.normalize()
4446
c ^= v1 ## cross product
4547
show_vec(c)
4648

0 commit comments

Comments
 (0)