Skip to content

Commit 49b1be9

Browse files
committed
libcore: pass return type of method from stub to proxy via type annotation
1 parent 4f1b9b1 commit 49b1be9

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

amitools/vamos/libcore/proxy.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import get_type_hints
2+
13
from amitools.vamos.machine import Code, REG_D0, REG_D1
24
from amitools.vamos.libtypes import TagList
35

@@ -18,11 +20,12 @@ def __init__(self, ctx, base_addr=None, run_sp=None):
1820

1921

2022
class LibProxyRegs:
21-
def __init__(self, ctx, args, arg_regs, kwargs):
23+
def __init__(self, ctx, args, arg_regs, kwargs, stub_method=None):
2224
assert len(args) == len(arg_regs)
2325
self.ctx = ctx
2426
self.args = args
2527
self.arg_regs = arg_regs
28+
self.stub_method = stub_method
2629
# shall we return d1 as well?
2730
self.ret_d1 = kwargs.pop("ret_d1", False)
2831
# shall we wrap the return value into a type
@@ -83,7 +86,14 @@ def return_d1(self):
8386
return self.ret_d1
8487

8588
def wrap_result(self):
86-
return self.wrap_res
89+
# either use forced type in proxy call argument
90+
result_type = self.wrap_res
91+
# or use from type hint of stub method (if any)
92+
if not result_type and self.stub_method:
93+
hints = get_type_hints(self.stub_method)
94+
if hints and "return" in hints:
95+
result_type = hints["return"]
96+
return result_type
8797

8898
def cleanup(self):
8999
for mem in self.auto_strings:
@@ -109,7 +119,7 @@ def _gen_arg_regs(self, func_def):
109119
def _gen_stub_call(self, arg_regs, stub_method):
110120
def stub_call(self, *args, **kwargs):
111121
"""proxy function to call lib stub directly"""
112-
regs = LibProxyRegs(self.ctx, args, arg_regs, kwargs)
122+
regs = LibProxyRegs(self.ctx, args, arg_regs, kwargs, stub_method)
113123

114124
# fill registers with arg values
115125
# (lib call may depend on it)

amitools/vamos/libcore/stub.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from types import MethodType
2+
from typing import get_type_hints
23
import time
34
import traceback
45

@@ -358,6 +359,19 @@ def profile_func(this, *args, **kwargs):
358359

359360
return profile_func
360361

362+
def _copy_return_type_hint(self, in_func, out_func):
363+
# get type hint
364+
hints = get_type_hints(in_func)
365+
366+
# is a return type defined
367+
if hints and "return" in hints:
368+
return_type = hints["return"]
369+
# attach new return annotation
370+
ann = out_func.__annotations__
371+
if ann is None:
372+
ann = out_func.__annotations__ = {}
373+
ann["return"] = return_type
374+
361375
def _wrap_func(self, stub, impl_func, ctx, profile):
362376
"""create a stub func for a valid impl func
363377
returns an unbound method for the stub instaance
@@ -389,4 +403,7 @@ def _wrap_func(self, stub, impl_func, ctx, profile):
389403
if profile:
390404
func = self._gen_profile_func(fd_func, profile, func)
391405

406+
# copy return hint from method to wrap func for later use in proxy
407+
self._copy_return_type_hint(method, func)
408+
392409
return func

0 commit comments

Comments
 (0)