Import service names

This commit is contained in:
Johan B.W. de Vries 2023-04-11 10:25:30 +02:00
parent cea236494f
commit 0aa8207987
3 changed files with 29 additions and 11 deletions

View File

@ -599,7 +599,7 @@ def import_(inp: ourlang.Function) -> wasm.Import:
assert inp.imported
return wasm.Import(
'imports',
inp.imported,
inp.name,
inp.name,
[

View File

@ -292,7 +292,7 @@ class Function:
name: str
lineno: int
exported: bool
imported: bool
imported: Optional[str]
statements: List[Statement]
returns_type3: Type3
posonlyargs: List[FunctionParam]
@ -301,7 +301,7 @@ class Function:
self.name = name
self.lineno = lineno
self.exported = False
self.imported = False
self.imported = None
self.statements = []
self.returns_type3 = type3types.none # FIXME: This could be a placeholder
self.posonlyargs = []

View File

@ -133,16 +133,34 @@ class OurVisitor:
# Do stmts at the end so we have the return value
for decorator in node.decorator_list:
if not isinstance(decorator, ast.Name):
_raise_static_error(decorator, 'Function decorators must be string')
if not isinstance(decorator.ctx, ast.Load):
_raise_static_error(decorator, 'Must be load context')
_not_implemented(decorator.id in ('exported', 'imported'), 'Custom decorators')
if isinstance(decorator, ast.Call):
if not isinstance(decorator.func, ast.Name):
_raise_static_error(decorator, 'Function decorators must be string')
if not isinstance(decorator.func.ctx, ast.Load):
_raise_static_error(decorator, 'Must be load context')
_not_implemented(decorator.func.id == 'imported', 'Custom decorators')
if decorator.id == 'exported':
function.exported = True
if 1 != len(decorator.args):
_raise_static_error(decorator, 'One argument expected')
if not isinstance(decorator.args[0], ast.Constant):
_raise_static_error(decorator, 'Service name must be a constant')
if not isinstance(decorator.args[0].value, str):
_raise_static_error(decorator, 'Service name must be a stirng')
if 0 != len(decorator.keywords): # TODO: Allow for namespace keyword
_raise_static_error(decorator, 'No keyword arguments expected')
function.imported = decorator.args[0].value
else:
function.imported = True
if not isinstance(decorator, ast.Name):
_raise_static_error(decorator, 'Function decorators must be string')
if not isinstance(decorator.ctx, ast.Load):
_raise_static_error(decorator, 'Must be load context')
_not_implemented(decorator.id in ('exported', 'imported'), 'Custom decorators')
if decorator.id == 'exported':
function.exported = True
else:
function.imported = 'imports'
if node.returns is not None: # Note: `-> None` would be a ast.Constant
function.returns_type3 = self.visit_type(module, node.returns)