Skip to content

Commit 7767fe8

Browse files
committed
feat: add support for env
1 parent b3871ec commit 7767fe8

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

packages/polywrap-client/polywrap_client/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ async def invoke(self, options: InvokerOptions) -> InvokeResult:
123123
)
124124
).unwrap()
125125

126+
env = self.get_env_by_uri(options.uri)
127+
options.env = options.env or (env.env if env else None)
128+
126129
result = await wrapper.invoke(options, invoker=self)
127130
if options.encode_result and not result.encoded:
128131
encoded = msgpack_encode(result.result)
629 Bytes
Binary file not shown.
40.5 KB
Binary file not shown.

packages/polywrap-client/tests/test_client.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pathlib import Path
22

33
from polywrap_client import PolywrapClient
4-
from polywrap_core import Uri, InvokerOptions, InterfaceImplementations
4+
from polywrap_core import Uri, InvokerOptions, InterfaceImplementations, Env
55
from polywrap_uri_resolvers import BaseUriResolver, SimpleFileReader
66

77
from polywrap_client.client import PolywrapClientConfig
@@ -67,7 +67,32 @@ async def test_interface_implementation():
6767
f'fs/{Path(__file__).parent.joinpath("cases", "simple-interface", "wrapper").absolute()}'
6868
)
6969
args = {"arg": {"str": "hello", "uint8": 2}}
70-
options = InvokerOptions(uri=uri, method="moduleMethod", args=args, encode_result=False)
70+
options = InvokerOptions(
71+
uri=uri, method="moduleMethod", args=args, encode_result=False
72+
)
7173
result = await client.invoke(options)
7274

7375
assert result.result == {"str": "hello", "uint8": 2}
76+
77+
78+
async def test_env():
79+
uri_resolver = BaseUriResolver(
80+
file_reader=SimpleFileReader(),
81+
redirects={},
82+
)
83+
84+
uri = Uri(f'fs/{Path(__file__).parent.joinpath("cases", "simple-env").absolute()}')
85+
env = {"externalArray": [1, 2, 3], "externalString": "hello"}
86+
87+
client = PolywrapClient(
88+
config=PolywrapClientConfig(
89+
envs=[Env(uri=uri, env=env)],
90+
resolver=uri_resolver,
91+
)
92+
)
93+
options = InvokerOptions(
94+
uri=uri, method="externalEnvMethod", args={}, encode_result=False
95+
)
96+
result = await client.invoke(options)
97+
98+
assert result.result == env

packages/polywrap-wasm/polywrap_wasm/imports.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ def wrap_abort(
7979
f"__wrap_abort: {msg}\nFile: {file}\nLocation: [{line},{column}]"
8080
)
8181

82+
wrap_load_env_type = FuncType([ValType.i32()], [])
83+
84+
def wrap_load_env(ptr: int) -> None:
85+
if not state.env:
86+
raise WasmAbortError("env: is not set")
87+
write_bytes(mem.data_ptr(store), mem.data_len(store), state.env, ptr)
88+
8289
wrap_invoke_args_type = FuncType([ValType.i32(), ValType.i32()], [])
8390

8491
def wrap_invoke_args(method_ptr: int, args_ptr: int) -> None:
@@ -309,6 +316,7 @@ def wrap_get_implementations_result(ptr: int) -> None:
309316
# TODO: use generics or any on wasmtime codebase to fix typings
310317
linker.define_func("wrap", "__wrap_debug_log", wrap_debug_log_type, wrap_debug_log) # type: ignore partially unknown
311318
linker.define_func("wrap", "__wrap_abort", wrap_abort_type, wrap_abort) # type: ignore partially unknown
319+
linker.define_func("wrap", "__wrap_load_env", wrap_load_env_type, wrap_load_env) # type: ignore partially unknown
312320

313321
# invoke
314322
linker.define_func("wrap", "__wrap_invoke_args", wrap_invoke_args_type, wrap_invoke_args) # type: ignore partially unknown

0 commit comments

Comments
 (0)