Skip to content

Commit

Permalink
only add http to the linker if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Nelson committed Jan 30, 2025
1 parent 506d386 commit de562f3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
5 changes: 4 additions & 1 deletion native/wasmex/src/component_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ pub fn new_instance(

let mut linker = Linker::new(store.engine());
let _ = wasmtime_wasi::add_to_linker_sync(&mut linker);
let _ = wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker);
if store.data().http.is_some() {
let _ = wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker);
}

// Instantiate the component

// Handle imports
Expand Down
9 changes: 8 additions & 1 deletion native/wasmex/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,19 @@ pub fn component_store_new_wasi(
} else {
StoreLimits::default()
};

let http_option = if options.allow_http {
Some(WasiHttpCtx::new())
} else {
None
};

let mut store = Store::new(
&engine,
ComponentStoreData {
ctx: Some(wasi_ctx_builder.build()),
limits,
http: Some(WasiHttpCtx::new()),
http: http_option,
table: wasmtime_wasi::ResourceTable::new(),
},
);
Expand Down
4 changes: 3 additions & 1 deletion test/components/component_server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ defmodule Wasmex.Components.GenServerTest do
component_bytes = File.read!("test/component_fixtures/hello_world/hello_world.wasm")

component_pid =
start_supervised!({HelloWorld, bytes: component_bytes, wasi: %WasiP2Options{}})
start_supervised!(
{HelloWorld, bytes: component_bytes, wasi: %WasiP2Options{allow_http: true}}
)

assert {:ok, "Hello, Elixir from a function defined in the module!"} =
HelloWorld.greet(component_pid, "Elixir")
Expand Down
4 changes: 3 additions & 1 deletion test/components/component_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule Wasmex.WasmComponentsTest do
component_bytes = File.read!("test/component_fixtures/hello_world/hello_world.wasm")

instance =
start_supervised!({HelloWorld, bytes: component_bytes, wasi: %WasiP2Options{}})
start_supervised!(
{HelloWorld, bytes: component_bytes, wasi: %WasiP2Options{allow_http: true}}
)

%{instance: instance}
end
Expand Down
4 changes: 3 additions & 1 deletion test/components/component_types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ defmodule Wasm.Components.ComponentTypesTest do
component_bytes = File.read!("test/component_fixtures/hello_world/hello_world.wasm")

instance =
start_supervised!({HelloWorld, bytes: component_bytes, wasi: %WasiP2Options{}})
start_supervised!(
{HelloWorld, bytes: component_bytes, wasi: %WasiP2Options{allow_http: true}}
)

assert {:ok, %{kebab_field: "foo"}} =
Wasmex.Components.call_function(instance, "echo-kebab", [
Expand Down
4 changes: 3 additions & 1 deletion test/components/import_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ defmodule Wasmex.Components.ImportTest do
component_pid =
start_supervised!(
{Wasmex.Components,
bytes: component_bytes, wasi: %WasiP2Options{inherit_stdout: true}, imports: imports}
bytes: component_bytes,
wasi: %WasiP2Options{inherit_stdout: true, allow_http: true},
imports: imports}
)

assert {:ok, "7 foo 42 hi,there x: 1 y: 2"} =
Expand Down

0 comments on commit de562f3

Please sign in to comment.