forked from bytecodealliance/componentize-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
251 lines (213 loc) · 10.1 KB
/
app.py
File metadata and controls
251 lines (213 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import traceback
import tests
import resource_borrow_export
import resource_aggregates
import resource_alias1
import resource_borrow_in_record
import componentize_py_async_support
import streams_and_futures as my_streams_and_futures
from componentize_py_types import Result, Ok, Err
from tests import exports, imports
from tests.imports import resource_borrow_import
from tests.imports import simple_import_and_export
from tests.imports import simple_async_import_and_export
from tests.imports import host_thing_interface
from tests.exports import resource_alias2
from tests.exports import streams_and_futures
from typing import Tuple, List, Optional
from foo_sdk.wit import exports as foo_exports
from foo_sdk.wit.imports.foo_interface import test as foo_test
from bar_sdk.wit import exports as bar_exports
from bar_sdk.wit.imports.foo_interface import test as bar_test
class SimpleExport(exports.SimpleExport):
def foo(self, v: int) -> int:
return v + 3
class SimpleImportAndExport(exports.SimpleImportAndExport):
def foo(self, v: int) -> int:
return simple_import_and_export.foo(v) + 3
class SimpleAsyncExport(exports.SimpleAsyncExport):
async def foo(self, v: int) -> int:
return v + 3
class SimpleAsyncImportAndExport(exports.SimpleAsyncImportAndExport):
async def foo(self, v: int) -> int:
return (await simple_async_import_and_export.foo(v)) + 3
class ResourceImportAndExport(exports.ResourceImportAndExport):
pass
class ResourceBorrowExport(exports.ResourceBorrowExport):
def foo(self, v: resource_borrow_export.Thing) -> int:
return v.value + 2
class ResourceWithLists(exports.ResourceWithLists):
pass
class ResourceAggregates(exports.ResourceAggregates):
def foo(
self,
r1: exports.resource_aggregates.R1,
r2: exports.resource_aggregates.R2,
r3: exports.resource_aggregates.R3,
t1: Tuple[resource_aggregates.Thing, exports.resource_aggregates.R1],
t2: Tuple[resource_aggregates.Thing],
v1: exports.resource_aggregates.V1,
v2: exports.resource_aggregates.V2,
l1: List[resource_aggregates.Thing],
l2: List[resource_aggregates.Thing],
o1: Optional[resource_aggregates.Thing],
o2: Optional[resource_aggregates.Thing],
result1: Result[resource_aggregates.Thing, None],
result2: Result[resource_aggregates.Thing, None]
) -> int:
if o1 is None:
host_o1 = None
else:
host_o1 = o1.value
if o2 is None:
host_o2 = None
else:
host_o2 = o2.value
if isinstance(result1, Ok):
host_result1 = Ok(result1.value.value)
else:
host_result1 = result1
if isinstance(result2, Ok):
host_result2 = Ok(result2.value.value)
else:
host_result2 = result2
return imports.resource_aggregates.foo(
imports.resource_aggregates.R1(r1.thing.value),
imports.resource_aggregates.R2(r2.thing.value),
imports.resource_aggregates.R3(r3.thing1.value, r3.thing2.value),
(t1[0].value, imports.resource_aggregates.R1(t1[1].thing.value)),
(t2[0].value,),
imports.resource_aggregates.V1_Thing(v1.value.value),
imports.resource_aggregates.V2_Thing(v2.value.value),
list(map(lambda x: x.value, l1)),
list(map(lambda x: x.value, l2)),
host_o1,
host_o2,
host_result1,
host_result2
) + 4
class ResourceAlias1(exports.ResourceAlias1):
def a(self, f: exports.resource_alias1.Foo) -> List[resource_alias1.Thing]:
return list(
map(
resource_alias1.wrap_thing,
imports.resource_alias1.a(imports.resource_alias1.Foo(f.thing.value))
)
)
class ResourceAlias2(exports.ResourceAlias2):
def b(self, f: exports.resource_alias2.Foo, g: exports.resource_alias1.Foo) -> List[resource_alias1.Thing]:
return list(
map(
resource_alias1.wrap_thing,
imports.resource_alias2.b(
imports.resource_alias2.Foo(f.thing.value),
exports.resource_alias1.Foo(g.thing.value)
)
)
)
class ResourceBorrowInRecord(exports.ResourceBorrowInRecord):
def test(self, a: List[exports.resource_borrow_in_record.Foo]) -> List[resource_borrow_in_record.Thing]:
return list(
map(
resource_borrow_in_record.wrap_thing,
imports.resource_borrow_in_record.test(
list(map(lambda x: imports.resource_borrow_in_record.Foo(x.thing.value), a))
)
)
)
async def pipe_bytes(rx: ByteStreamReader, tx: ByteStreamWriter):
while not (rx.writer_dropped or tx.reader_dropped):
await tx.write_all(await rx.read(1024))
async def pipe_strings(rx: FutureReader[str], tx: StreamReader[str]):
await tx.write(await rx.read())
async def pipe_things(rx: StreamReader[streams_and_futures.Thing], tx: StreamWriter[streams_and_futures.Thing]):
# Read the things one at a time, forcing the host to re-take ownership of
# any unwritten items between writes.
things = []
while not rx.writer_dropped:
things += await rx.read(1)
# Write the things all at once. The host will read them only one at a time,
# forcing us to re-take ownership of any unwritten items between writes.
await tx.write_all(things)
async def pipe_host_things(rx: StreamReader[host_thing_interface.HostThing], tx: StreamWriter[host_thing_interface.HostThing]):
# Read the things one at a time, forcing the host to re-take ownership of
# any unwritten items between writes.
things = []
while not rx.writer_dropped:
things += await rx.read(1)
# Write the things all at once. The host will read them only one at a time,
# forcing us to re-take ownership of any unwritten items between writes.
await tx.write_all(things)
async def write_thing(thing: my_streams_and_futures.Thing,
tx1: FutureWriter[streams_and_futures.Thing],
tx2: FutureWriter[streams_and_futures.Thing]):
# The host will drop the first reader without reading, which should give us
# back ownership of `thing`.
wrote = await tx1.write(thing)
assert not wrote
# The host will read from the second reader, though.
wrote = await tx2.write(thing)
assert wrote
async def write_host_thing(thing: host_thing_interface.HostThing,
tx1: FutureWriter[host_thing_interface.HostThing],
tx2: FutureWriter[host_thing_interface.HostThing]):
# The host will drop the first reader without reading, which should give us
# back ownership of `thing`.
wrote = await tx1.write(thing)
assert not wrote
# The host will read from the second reader, though.
wrote = await tx2.write(thing)
assert wrote
def unreachable() -> str:
raise AssertionError
class StreamsAndFutures(exports.StreamsAndFutures):
async def echo_stream_u8(self, stream: ByteStreamReader) -> ByteStreamReader:
tx, rx = tests.byte_stream()
componentize_py_async_support.spawn(pipe_bytes(stream, tx))
return rx
async def echo_future_string(self, future: FutureReader[str]) -> FutureReader[str]:
tx, rx = tests.string_future(unreachable)
componentize_py_async_support.spawn(pipe_strings(future, tx))
return rx
async def short_reads(self, stream: StreamReader[streams_and_futures.Thing]) -> StreamReader[streams_and_futures.Thing]:
tx, rx = tests.streams_and_futures_thing_stream()
componentize_py_async_support.spawn(pipe_things(stream, tx))
return rx
async def short_reads_host(self, stream: StreamReader[host_thing_interface.HostThing]) -> StreamReader[host_thing_interface.HostThing]:
tx, rx = tests.host_thing_interface_host_thing_stream()
componentize_py_async_support.spawn(pipe_host_things(stream, tx))
return rx
async def dropped_future_reader(self, value: str) -> tuple[FutureReader[streams_and_futures.Thing], FutureReader[streams_and_futures.Thing]]:
tx1, rx1 = tests.streams_and_futures_thing_future(unreachable)
tx2, rx2 = tests.streams_and_futures_thing_future(unreachable)
componentize_py_async_support.spawn(write_thing(my_streams_and_futures.Thing(value), tx1, tx2))
return (rx1, rx2)
async def dropped_future_reader_host(self, value: str) -> tuple[FutureReader[host_thing_interface.HostThing], FutureReader[host_thing_interface.HostThing]]:
tx1, rx1 = tests.host_thing_interface_host_thing_future(unreachable)
tx2, rx2 = tests.host_thing_interface_host_thing_future(unreachable)
componentize_py_async_support.spawn(write_host_thing(host_thing_interface.HostThing(value), tx1, tx2))
return (rx1, rx2)
class Tests(tests.Tests):
def test_resource_borrow_import(self, v: int) -> int:
return resource_borrow_import.foo(resource_borrow_import.Thing(v + 1)) + 4
def test_resource_alias(self, things: List[imports.resource_alias1.Thing]) -> List[imports.resource_alias1.Thing]:
return things
def add(self, a: imports.resource_floats.Float, b: imports.resource_floats.Float) -> imports.resource_floats.Float:
return imports.resource_floats.Float(a.get() + b.get() + 5)
def read_file(self, path: str) -> bytes:
try:
with open(file=path, mode="rb") as f:
return f.read()
except:
raise Err(traceback.format_exc())
def test_refcounts(self):
# Retrieve 5GiB in chunks of 1MiB, which should _not_ lead to a
# `MemoryError` if we're handling refcounts correctly in the runtime.
for _ in range(5 * 1024):
chunk = tests.get_bytes(1024 * 1024)
class FooInterface(foo_exports.FooInterface):
def test(self, s: str) -> str:
return foo_test(f"{s} FooInterface.test")
class BarInterface(bar_exports.BarInterface):
def test(self, s: str) -> str:
return bar_test(f"{s} BarInterface.test")