From 366c3c07c8fe6467bb4efb1c14b4a0d63ca84728 Mon Sep 17 00:00:00 2001 From: Andreas Langenhagen Date: Fri, 21 Feb 2025 17:30:39 +0100 Subject: [PATCH 1/3] chore: Remove an unused import in a test file --- wandelscript/tests/test_runner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/wandelscript/tests/test_runner.py b/wandelscript/tests/test_runner.py index 1471917..1f86ed7 100644 --- a/wandelscript/tests/test_runner.py +++ b/wandelscript/tests/test_runner.py @@ -13,7 +13,6 @@ from wandelscript.exception import NameError_, ProgramSyntaxError from wandelscript.ffi import ForeignFunction from wandelscript.runtime import ExecutionContext -from wandelscript.serializer import Vector3d from wandelscript.simulation import get_robot_controller from wandelscript.utils.runtime import Tee From 14a3086b926407c7d144d8c328987f459543c2eb Mon Sep 17 00:00:00 2001 From: Andreas Langenhagen Date: Mon, 24 Feb 2025 16:59:07 +0100 Subject: [PATCH 2/3] chore: Mention async funcitons in ForeignFunction docstrings --- wandelscript/ffi.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wandelscript/ffi.py b/wandelscript/ffi.py index 1c79850..7391e98 100644 --- a/wandelscript/ffi.py +++ b/wandelscript/ffi.py @@ -13,7 +13,7 @@ class ForeignFunction: """References a function that can be used inside a Wandelscript. Args: - function: Handle to an arbitrary function. + function: Handle to an arbitrary function, even an async function. pass_context (bool): Whether or not the function takes the current wandelscript context as the first parameter. Defaults to False. """ @@ -27,10 +27,10 @@ def __post_init__(self): def ff(fn: Callable, pass_context: bool = False) -> ForeignFunction: - """Shortcut method to create a foreign function. + """Shortcut method to create a ForeignFunction. Args: - function: Handle to an arbitrary function. + function: Handle to an arbitrary function, even an async function. pass_context (bool): Whether or not the function takes the current wandelscript context as the first parameter. Defaults to False. """ From edbd547899f019f1f1e8d3dd3155785d0c6b9539 Mon Sep 17 00:00:00 2001 From: Andreas Langenhagen Date: Mon, 24 Feb 2025 16:40:14 +0100 Subject: [PATCH 3/3] chore: Add test showcasing async foreign function --- wandelscript/tests/test_runner.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/wandelscript/tests/test_runner.py b/wandelscript/tests/test_runner.py index 1f86ed7..44ac14f 100644 --- a/wandelscript/tests/test_runner.py +++ b/wandelscript/tests/test_runner.py @@ -1,3 +1,4 @@ +import asyncio import sys import time import uuid @@ -11,7 +12,7 @@ from wandelscript import ProgramRun, ProgramRunner, ProgramRunState, run from wandelscript.exception import NameError_, ProgramSyntaxError -from wandelscript.ffi import ForeignFunction +from wandelscript.ffi import ForeignFunction, ff from wandelscript.runtime import ExecutionContext from wandelscript.simulation import get_robot_controller from wandelscript.utils.runtime import Tee @@ -91,6 +92,29 @@ def test_run(): assert not isinstance(sys.stdout, Tee) +async def custom_async_function() -> int: + await asyncio.sleep(0.1) + return 42 + + +def test_run_with_foreign_async_function(): + """Showcase that Wandelscript supports also async foreign functions.""" + code = """ +a = custom_async_function() +print(a) +""" + + runner = run( + code, + robot_cell, + default_robot="0@controller", + default_tcp="flange", + foreign_functions={"custom_async_function": ff(custom_async_function)}, + ) + stdout = runner.program_run.stdout + assert "42\n" == stdout + + def test_program_runner(): program_runner = ProgramRunner("move via p2p() to [100, 0, 300, 0, pi, 0]", robot_cell) assert uuid.UUID(str(program_runner.id)) is not None