Skip to content

Commit

Permalink
fix IPC marshaling for KGLambda
Browse files Browse the repository at this point in the history
  • Loading branch information
briangu committed Dec 13, 2023
1 parent d1bbf5f commit 2c9e505
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 5 additions & 1 deletion examples/db/server.kg
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -66,4 +68,6 @@ Fun fact: you can copy the table locally via:

You now have a local copy of the table.

<TODO Example>

****
2 changes: 1 addition & 1 deletion klongpy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
13 changes: 10 additions & 3 deletions klongpy/sys_fn_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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}"))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2c9e505

Please sign in to comment.