Skip to content

Commit 125bd84

Browse files
committed
switch global symbol table when calling functions in different global ctx
1 parent 8ed4040 commit 125bd84

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

custom_components/pyscript/eval.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,16 @@ async def call(self, ast_ctx, *args, **kwargs):
626626
sym_table[name] = value
627627
else:
628628
sym_table[name] = EvalLocalVar(name)
629-
ast_ctx.sym_table_stack.append(ast_ctx.sym_table)
629+
if ast_ctx.global_ctx != self.global_ctx:
630+
#
631+
# switch to the global symbol table in the global context
632+
# where the function was defined
633+
#
634+
prev_sym_table = [ast_ctx.global_sym_table, ast_ctx.sym_table, ast_ctx.sym_table_stack]
635+
ast_ctx.global_sym_table = self.global_ctx.get_global_sym_table()
636+
ast_ctx.sym_table_stack = [ast_ctx.global_sym_table]
637+
else:
638+
ast_ctx.sym_table_stack.append(ast_ctx.sym_table)
630639
ast_ctx.sym_table = sym_table
631640
code_str, code_list = ast_ctx.code_str, ast_ctx.code_list
632641
ast_ctx.code_str, ast_ctx.code_list = self.code_str, self.code_list
@@ -644,9 +653,12 @@ async def call(self, ast_ctx, *args, **kwargs):
644653
val = None
645654
if ast_ctx.get_exception_obj():
646655
break
647-
ast_ctx.sym_table = ast_ctx.sym_table_stack.pop()
648656
ast_ctx.curr_func = prev_func
649657
ast_ctx.code_str, ast_ctx.code_list = code_str, code_list
658+
if ast_ctx.global_ctx != self.global_ctx:
659+
ast_ctx.global_sym_table, ast_ctx.sym_table, ast_ctx.sym_table_stack = prev_sym_table
660+
else:
661+
ast_ctx.sym_table = ast_ctx.sym_table_stack.pop()
650662
return val
651663

652664

0 commit comments

Comments
 (0)