Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion tests/component/test_linker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from wasmtime import Engine, WasmtimeError, Module, Store
from wasmtime import Engine, WasiConfig, WasmtimeError, Module, Store
from wasmtime.component import Linker, Component, LinkerInstance


Expand Down Expand Up @@ -143,6 +143,34 @@ def test_shadow_func(self):
with linker.root() as l:
l.add_func('x', lambda: None)

def test_add_wasi_http(self):
engine = Engine()
linker = Linker(engine)
linker.add_wasip2()
linker.add_wasi_http()

with linker.root():
with self.assertRaises(WasmtimeError):
linker.add_wasi_http()

linker.close()

with Linker(engine) as l2:
l2.add_wasip2()
l2.add_wasi_http()
with self.assertRaises(WasmtimeError):
l2.add_wasi_http()
l2.allow_shadowing = True
l2.add_wasi_http()

def test_set_wasi_http(self):
engine = Engine()
store = Store(engine)
wasi = WasiConfig()
wasi.inherit_stdout()
store.set_wasi(wasi)
store.set_wasi_http()

def test_define_unknown_imports_as_traps(self):
engine = Engine()
store = Store(engine)
Expand Down
12 changes: 12 additions & 0 deletions wasmtime/_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,12 @@ def wasmtime_context_get_fuel(context: Any, fuel: Any) -> ctypes._Pointer:
def wasmtime_context_set_wasi(context: Any, wasi: Any) -> ctypes._Pointer:
return _wasmtime_context_set_wasi(context, wasi) # type: ignore

_wasmtime_context_set_wasi_http = dll.wasmtime_context_set_wasi_http
_wasmtime_context_set_wasi_http.restype = None
_wasmtime_context_set_wasi_http.argtypes = [ctypes.POINTER(wasmtime_context_t)]
def wasmtime_context_set_wasi_http(context: Any) -> None:
return _wasmtime_context_set_wasi_http(context) # type: ignore

_wasmtime_context_set_epoch_deadline = dll.wasmtime_context_set_epoch_deadline
_wasmtime_context_set_epoch_deadline.restype = None
_wasmtime_context_set_epoch_deadline.argtypes = [ctypes.POINTER(wasmtime_context_t), ctypes.c_uint64]
Expand Down Expand Up @@ -4751,6 +4757,12 @@ def wasmtime_component_linker_instance_add_func(linker_instance: Any, name: Any,
def wasmtime_component_linker_add_wasip2(linker: Any) -> ctypes._Pointer:
return _wasmtime_component_linker_add_wasip2(linker) # type: ignore

_wasmtime_component_linker_add_wasi_http = dll.wasmtime_component_linker_add_wasi_http
_wasmtime_component_linker_add_wasi_http.restype = ctypes.POINTER(wasmtime_error_t)
_wasmtime_component_linker_add_wasi_http.argtypes = [ctypes.POINTER(wasmtime_component_linker_t)]
def wasmtime_component_linker_add_wasi_http(linker: Any) -> ctypes._Pointer:
return _wasmtime_component_linker_add_wasi_http(linker) # type: ignore

wasmtime_component_resource_destructor_t = ctypes.CFUNCTYPE(ctypes.c_size_t, ctypes.c_void_p, ctypes.POINTER(wasmtime_context_t), ctypes.c_uint32)

_wasmtime_component_linker_instance_add_resource = dll.wasmtime_component_linker_instance_add_resource
Expand Down
9 changes: 9 additions & 0 deletions wasmtime/_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def set_wasi(self, wasi: "WasiConfig") -> None:
if error:
raise WasmtimeError._from_ptr(error)

def set_wasi_http(self) -> None:
"""
Initializes the WASI HTTP context for this store.

Must be called before instantiating a component that uses `wasi:http`.
Requires WASI to be configured first via `set_wasi()`.
"""
ffi.wasmtime_context_set_wasi_http(self._context())

def set_epoch_deadline(self, ticks_after_current: int) -> None:
"""
Configures the relative epoch deadline, after the current engine's
Expand Down
12 changes: 12 additions & 0 deletions wasmtime/component/_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ def add_wasip2(self) -> None:
if err:
raise WasmtimeError._from_ptr(err)

def add_wasi_http(self) -> None:
"""
Adds the WASI HTTP API definitions to this linker.

This adds `wasi:http/types` and `wasi:http/outgoing-handler`.
Requires WASIp2 to be added first via `add_wasip2()`.
"""
self._assert_not_locked()
err = ffi.wasmtime_component_linker_add_wasi_http(self.ptr())
if err:
raise WasmtimeError._from_ptr(err)

def instantiate(self, store: Storelike, component: Component) -> Instance:
"""
Instantiates the given component using this linker within the provided
Expand Down
Loading