Skip to content

Commit 9599fec

Browse files
author
hartsantler
committed
fixed custom decorators, added NAME variable to generated functions because jsfunc.name is a readonly variable.
1 parent 90e2b5a commit 9599fec

3 files changed

Lines changed: 12 additions & 3 deletions

File tree

bindings/blockly.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def setup(self, name, title=None, color=None):
6969
self.external_function = None
7070
self.on_click_callback = None
7171

72+
def callback(self, jsfunc): ## decorator
73+
self.set_external_function( jsfunc.NAME )
74+
return jsfunc
75+
7276
def set_on_click_callback(self, callback):
7377
self.on_click_callback = callback
7478

pythonjs/python_to_pythonjs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,10 @@ def visit_FunctionDef(self, node):
10471047
self._function_return_types[ node.name ] = self._return_type
10481048

10491049
writer.pull()
1050+
## note, in javascript function.name is a non-standard readonly attribute,
1051+
## the compiler creates anonymous functions with name set to an empty string.
1052+
writer.write('%s.NAME = "%s"' %(node.name,node.name))
1053+
10501054
if self._with_js and with_js_decorators:
10511055
## these with-js functions are assigned to a some objects prototype,
10521056
## here we assume that they depend on the special "this" variable,
@@ -1063,7 +1067,7 @@ def visit_FunctionDef(self, node):
10631067
# apply decorators
10641068
for decorator in decorators:
10651069
assert not self._with_js
1066-
writer.write('%s = %s(create_array(%s))' % (node.name, self.visit(decorator), node.name))
1070+
writer.write('%s = get_attribute(%s,"__call__")( [%s] )' % (node.name, self.visit(decorator), node.name))
10671071

10681072
def visit_Continue(self, node):
10691073
if self._with_js:

tests/blockly_custom_blocks.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@
135135
from blockly import *
136136

137137
myblock = StatementBlock('myblock', title='My Block', stack_input=True)
138-
myblock.set_external_function('testextern')
138+
#myblock.set_external_function('testextern')
139139

140-
def testextern():
140+
@myblock.callback
141+
def my_extern_func():
141142
print 'hello extern func'
142143

143144
def show_code():

0 commit comments

Comments
 (0)