@@ -38,7 +38,7 @@ def __init__(self, requirejs=True, insert_runtime=True, webworker=False, functio
3838 self ._exports = set ()
3939 self ._inline_lambda = False
4040
41- self .special_decorators = set (['__typedef__' , '__glsl__' , '__pyfunction__' ])
41+ self .special_decorators = set (['__typedef__' , '__glsl__' , '__pyfunction__' , 'expression' ])
4242 self ._glsl = False
4343 self ._has_glsl = False
4444 self ._typed_vars = dict ()
@@ -242,8 +242,15 @@ def _visit_function(self, node):
242242 gpu_vectorize = False
243243 gpu_method = False
244244 args_typedefs = {}
245+ func_expr = False
246+
245247 for decor in node .decorator_list :
246- if isinstance (decor , ast .Name ) and decor .id == '__pyfunction__' :
248+ if isinstance (decor , ast .Call ) and isinstance (decor .func , ast .Name ) and decor .func .id == 'expression' :
249+ assert len (decor .args )== 1
250+ func_expr = True
251+ node .name = self .visit (decor .args [0 ])
252+
253+ elif isinstance (decor , ast .Name ) and decor .id == '__pyfunction__' :
247254 is_pyfunc = True
248255 elif isinstance (decor , ast .Name ) and decor .id == '__glsl__' :
249256 glsl = True
@@ -473,10 +480,13 @@ def _visit_function(self, node):
473480 elif len (self ._function_stack ) == 1 :
474481 ## this style will not make function global to the eval context in NodeJS ##
475482 #buffer = self.indent() + 'function %s(%s) {\n' % (node.name, ', '.join(args))
476- ## this is required for eval to be able to work in NodeJS, note there is no var keyword.
477483
478- if self ._func_expressions :
479- buffer = self .indent () + '%s = function(%s) {\n ' % (node .name , ', ' .join (args ))
484+ ## note if there is no var keyword and this function is at the global level,
485+ ## then it should be callable from eval in NodeJS - this is not correct.
486+ ## infact, var should always be used with function expressions.
487+
488+ if self ._func_expressions or func_expr :
489+ buffer = self .indent () + 'var %s = function(%s) {\n ' % (node .name , ', ' .join (args ))
480490 else :
481491 buffer = self .indent () + 'function %s(%s) {\n ' % (node .name , ', ' .join (args ))
482492
@@ -485,7 +495,7 @@ def _visit_function(self, node):
485495
486496 else :
487497
488- if self ._func_expressions :
498+ if self ._func_expressions or func_expr :
489499 buffer = self .indent () + 'var %s = function(%s) {\n ' % (node .name , ', ' .join (args ))
490500 else :
491501 buffer = self .indent () + 'function %s(%s) {\n ' % (node .name , ', ' .join (args ))
0 commit comments