Skip to content

Commit 838b616

Browse files
authored
refactor: polywrap-wasm package (#183)
1 parent 0a85b2a commit 838b616

File tree

15 files changed

+169
-112
lines changed

15 files changed

+169
-112
lines changed
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
"""This module contains the runtime for executing Wasm wrappers."""
2-
from .buffer import *
32
from .errors import *
4-
from .exports import *
5-
from .imports import *
63
from .inmemory_file_reader import *
74
from .wasm_package import *
85
from .wasm_wrapper import *
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
"""This module contains the constants used by polywrap-wasm package."""
22
WRAP_MANIFEST_PATH = "wrap.info"
33
WRAP_MODULE_PATH = "wrap.wasm"
4+
5+
__all__ = ["WRAP_MANIFEST_PATH", "WRAP_MODULE_PATH"]

packages/polywrap-wasm/polywrap_wasm/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ class WasmExportNotFoundError(WasmError):
1212

1313
class WasmMemoryError(WasmError):
1414
"""Raises when the Wasm memory is not found."""
15+
16+
17+
__all__ = [
18+
"WasmError",
19+
"WasmExportNotFoundError",
20+
"WasmMemoryError",
21+
]

packages/polywrap-wasm/polywrap_wasm/imports/get_implementations.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def wrap_get_implementations(self, uri_ptr: int, uri_len: int) -> bool:
2424
uri_len,
2525
)
2626
)
27+
if not self.invoker:
28+
raise WrapAbortError(
29+
invoke_options=self.state.invoke_options,
30+
message="Expected invoker to be defined got None",
31+
)
2732
try:
2833
maybe_implementations = self.invoker.get_implementations(uri=uri)
2934
implementations: List[str] = (
@@ -35,8 +40,8 @@ def wrap_get_implementations(self, uri_ptr: int, uri_len: int) -> bool:
3540
return len(implementations) > 0
3641
except Exception as err:
3742
raise WrapAbortError(
38-
self.state.invoke_options,
39-
f"failed calling invoker.get_implementations({repr(uri)})",
43+
invoke_options=self.state.invoke_options,
44+
message=f"failed calling invoker.get_implementations({repr(uri)})",
4045
) from err
4146

4247
def wrap_get_implementations_result_len(self) -> int:
@@ -59,6 +64,12 @@ def wrap_get_implementations_result(self, ptr: int) -> None:
5964
self.write_bytes(ptr, result)
6065

6166
def _get_get_implementations_result(self, export_name: str):
67+
if not self.invoker:
68+
raise WrapAbortError(
69+
invoke_options=self.state.invoke_options,
70+
message="Expected invoker to be defined got None",
71+
)
72+
6273
if not self.state.get_implementations_result:
6374
raise WrapAbortError(
6475
self.state.invoke_options,

packages/polywrap-wasm/polywrap_wasm/imports/subinvoke.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
"""This module contains the subinvoke imports for the Wasm module."""
2-
import asyncio
3-
from concurrent.futures import ThreadPoolExecutor
4-
5-
from polywrap_core import InvokerOptions, Uri, WrapAbortError
2+
from polywrap_core import Uri, WrapAbortError
63
from polywrap_msgpack import msgpack_encode
74

85
from ..types import InvokeResult
96
from .types import BaseWrapImports
107

11-
pool = ThreadPoolExecutor()
12-
138

149
class WrapSubinvokeImports(BaseWrapImports):
1510
"""Defines the subinvoke family of imports for the Wasm module."""
@@ -42,18 +37,19 @@ def wrap_subinvoke(
4237
method = self._get_subinvoke_method(method_ptr, method_len)
4338
args = self._get_subinvoke_args(args_ptr, args_len)
4439

40+
if not self.invoker:
41+
raise WrapAbortError(
42+
invoke_options=self.state.invoke_options,
43+
message="Expected invoker to be defined got None",
44+
)
45+
4546
try:
46-
result = pool.submit(
47-
asyncio.run,
48-
self.invoker.invoke(
49-
InvokerOptions(
50-
uri=uri,
51-
method=method,
52-
args=args,
53-
encode_result=True,
54-
)
55-
),
56-
).result()
47+
result = self.invoker.invoke(
48+
uri=uri,
49+
method=method,
50+
args=args,
51+
encode_result=True,
52+
)
5753
if isinstance(result, bytes):
5854
self.state.subinvoke_result = InvokeResult(result=result)
5955
return True

packages/polywrap-wasm/polywrap_wasm/imports/types/base_wrap_imports.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
from __future__ import annotations
33

44
from abc import ABC
5+
from typing import Optional
56

6-
from polywrap_core import Invoker, UriPackageOrWrapper
7+
from polywrap_core import Invoker
78
from wasmtime import Memory, Store
89

910
from ...buffer import read_bytes, read_string, write_bytes, write_string
@@ -16,7 +17,7 @@ class BaseWrapImports(ABC):
1617
memory: Memory
1718
store: Store
1819
state: State
19-
invoker: Invoker[UriPackageOrWrapper]
20+
invoker: Optional[Invoker]
2021

2122
def read_string(self, ptr: int, length: int) -> str:
2223
"""Read a UTF-8 encoded string from the memory buffer."""

packages/polywrap-wasm/polywrap_wasm/imports/utils/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/polywrap-wasm/polywrap_wasm/imports/utils/unsync_invoke.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/polywrap-wasm/polywrap_wasm/imports/wrap_imports.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
# pylint: disable=too-many-ancestors
44
from __future__ import annotations
55

6-
from polywrap_core import Invoker, UriPackageOrWrapper
6+
from typing import Optional
7+
8+
from polywrap_core import Invoker
79
from wasmtime import Memory, Store
810

911
from ..types.state import State
@@ -39,7 +41,7 @@ def __init__(
3941
memory: Memory,
4042
store: Store,
4143
state: State,
42-
invoker: Invoker[UriPackageOrWrapper],
44+
invoker: Optional[Invoker],
4345
) -> None:
4446
"""Initialize the WrapImports instance.
4547

packages/polywrap-wasm/polywrap_wasm/inmemory_file_reader.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(
3131
self._wasm_manifest = wasm_manifest
3232
self._base_file_reader = base_file_reader
3333

34-
async def read_file(self, file_path: str) -> bytes:
34+
def read_file(self, file_path: str) -> bytes:
3535
"""Read a file from memory.
3636
3737
Args:
@@ -44,4 +44,7 @@ async def read_file(self, file_path: str) -> bytes:
4444
return self._wasm_module
4545
if file_path == WRAP_MANIFEST_PATH and self._wasm_manifest:
4646
return self._wasm_manifest
47-
return await self._base_file_reader.read_file(file_path=file_path)
47+
return self._base_file_reader.read_file(file_path=file_path)
48+
49+
50+
__all__ = ["InMemoryFileReader"]

0 commit comments

Comments
 (0)