From 2c9e505f25fc14799fed2b81eb2050e501bcf2ff Mon Sep 17 00:00:00 2001 From: Brian Guarraci Date: Wed, 13 Dec 2023 12:00:26 -0700 Subject: [PATCH] fix IPC marshaling for KGLambda --- examples/db/server.kg | 6 +++++- klongpy/core.py | 2 +- klongpy/sys_fn_ipc.py | 13 ++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/db/server.kg b/examples/db/server.kg index b2a80f7..7c6dee1 100644 --- a/examples/db/server.kg +++ b/examples/db/server.kg @@ -50,7 +50,9 @@ curl "http://localhost:8080" To call the server via IPC and operate on the prices table: -start the web server with kgpy -s 8888 server.kg +start the web server: + +$ kgpy -s 8888 server.kg and on a client repl: @@ -66,4 +68,6 @@ Fun fact: you can copy the table locally via: You now have a local copy of the table. + + **** diff --git a/klongpy/core.py b/klongpy/core.py index 2339e81..ddbda6d 100644 --- a/klongpy/core.py +++ b/klongpy/core.py @@ -162,7 +162,7 @@ def __init__(self, i): reserved_fn_args = ['x','y','z'] -reserved_fn_symbols = {KGSym(n) for n in reserved_fn_args} +reserved_fn_symbols = [KGSym(n) for n in reserved_fn_args] reserved_fn_symbol_map = {n:KGSym(n) for n in reserved_fn_args} reserved_dot_f_symbol = KGSym('.f') diff --git a/klongpy/sys_fn_ipc.py b/klongpy/sys_fn_ipc.py index 7794fd5..882bf76 100644 --- a/klongpy/sys_fn_ipc.py +++ b/klongpy/sys_fn_ipc.py @@ -12,7 +12,7 @@ from klongpy.core import (KGCall, KGFn, KGFnWrapper, KGLambda, KGSym, KlongException, get_fn_arity_str, is_list, - reserved_fn_args, reserved_fn_symbol_map) + reserved_fn_args, reserved_fn_symbols, reserved_fn_symbol_map) class KlongIPCException(Exception): @@ -85,7 +85,11 @@ async def execute_server_command(future_loop, result_future, klong, command, nc) if isinstance(command, KGRemoteFnCall): r = klong[command.sym] if callable(r): - response = r(*command.params) + if isinstance(r,KGLambda): + ctx = {reserved_fn_symbols[i]:command.params[i] for i in range(min(len(reserved_fn_args),len(command.params)))} + response = r(klong, ctx) + else: + response = r(*command.params) else: raise KlongException(f"not callable: {command.sym}") elif isinstance(command, KGRemoteDictSetCall): @@ -99,6 +103,9 @@ async def execute_server_command(future_loop, result_future, klong, command, nc) response = klong(str(command)) if isinstance(response, KGFn): response = KGRemoteFnRef(response.arity) + elif isinstance(response, KGLambda): + # TODO: move to using .arity for KGLambda + response = KGRemoteFnRef(response.get_arity()) future_loop.call_soon_threadsafe(result_future.set_result, response) except KeyError as e: future_loop.call_soon_threadsafe(result_future.set_exception, KlongException(f"symbol not found: {e}")) @@ -449,7 +456,7 @@ def __call__(self, _, ctx): msg = KGRemoteFnCall(x[0], x[1:]) if is_list(x) and len(x) > 0 and isinstance(x[0],KGSym) else x response = self.call(msg) if isinstance(x,KGSym) and isinstance(response, KGRemoteFnRef): - response = KGRemoteFnProxy(self.nc, x, response.arity) + response = KGRemoteFnProxy(self, x, response.arity) return response except Exception as e: import traceback