From 633267b37df426b728f220a1cf1e2504c9b2a382 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sun, 4 Apr 2021 16:43:55 +0200 Subject: [PATCH] ideas [skip-ci] --- py2wasm/python.py | 43 +++++++++++++++++++++++------------ py2wasm/wasm.py | 3 ++- tests/integration/test_fib.py | 6 ++++- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/py2wasm/python.py b/py2wasm/python.py index 6be9b1e..7566cbf 100644 --- a/py2wasm/python.py +++ b/py2wasm/python.py @@ -16,32 +16,47 @@ class Visitor(ast.NodeVisitor): # )) def visit_FunctionDef(self, node): + is_export = False + if node.decorator_list: # TODO: Support normal decorators assert 1 == len(node.decorator_list) + call = node.decorator_list[0] - assert 'external' == call.func.id - assert 1 == len(call.args) - assert isinstance(call.args[0].value, str) + if not isinstance(call, ast.Name): + assert isinstance(call, ast.Call) - import_ = Import( - call.args[0].value, - node.name, - node.name, - ) + assert 'external' == call.func.id + assert 1 == len(call.args) + assert isinstance(call.args[0].value, str) - import_.params = [ - arg.annotation.id - for arg in node.args.args - ] + import_ = Import( + call.args[0].value, + node.name, + node.name, + ) - self.module.imports.append(import_) - return + import_.params = [ + arg.annotation.id + for arg in node.args.args + ] + + self.module.imports.append(import_) + return + + assert call.id == 'export' + is_export = True func = Function( node.name, + is_export, ) + for arg in node.args.args: + func.params.append( + + ) + self._stack.append(func) self.generic_visit(node) self._stack.pop() diff --git a/py2wasm/wasm.py b/py2wasm/wasm.py index f0a3b32..ce6f81f 100644 --- a/py2wasm/wasm.py +++ b/py2wasm/wasm.py @@ -25,6 +25,7 @@ class Function: def __init__(self, name, exported=True): self.name = name self.exported = exported # TODO: Use __all__! + self.params = [] self.statements = [] def generate(self): @@ -39,7 +40,7 @@ class Module: self.functions = functions or [] def generate(self): - return '(module\n {}\n {})'.format( + return '(module\n {}\n {})\n'.format( '\n '.join(x.generate() for x in self.imports), '\n '.join(x.generate() for x in self.functions), ) diff --git a/tests/integration/test_fib.py b/tests/integration/test_fib.py index bcd4719..16fa464 100644 --- a/tests/integration/test_fib.py +++ b/tests/integration/test_fib.py @@ -75,8 +75,12 @@ def test_fib(): def logInt32(value: i32) -> None: ... +def helper(arg: i32) -> i32: + return arg + 1 + +@export def testEntry(): - logInt32(13 + 13 * 123) + logInt32(13 + 13 * helper(122)) """ result = Suite(code_py, 'test_fib').run_code()