Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add environment variable host functions. #224

Merged
merged 2 commits into from
Oct 2, 2023
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
- [Rust](https://github.com/moonrepo/rust-plugin/blob/master/CHANGELOG.md)
- [Schema](https://github.com/moonrepo/schema-plugin/blob/master/CHANGELOG.md)

## Unreleased

#### 🚀 Updates

- WASM API
- Added `get_env_var` and `set_env_var` host functions.
- Added `host_env!` macro.

## 0.19.0

#### 💥 Breaking
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub enum ProtoError {
code(proto::version::undetected),
help = "Has the tool been installed?"
)]
#[error("Failed to detect an applicable version to run {} with. Try pinning a local or global version, or passing the version as an argument.", .tool.style(Style::Id))]
#[error("Failed to detect an applicable version to run {} with. Try pinning a version or passing the version as an argument.", .tool.style(Style::Id))]
VersionDetectFailed { tool: Id },

#[diagnostic(code(proto::version::unresolved))]
Expand Down
18 changes: 18 additions & 0 deletions crates/pdk/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ macro_rules! exec_command {
};
}

#[macro_export]
macro_rules! host_env {
($name:literal, $value:expr) => {
unsafe { set_env_var($name, $value)? };
};
($name:literal) => {
unsafe {
let inner = get_env_var($name)?;

if inner.is_empty() {
None
} else {
Some(inner)
}
}
};
}

#[macro_export]
macro_rules! host_log {
($($arg:tt)+) => {
Expand Down
67 changes: 64 additions & 3 deletions crates/wasm-plugin/src/host_funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ pub struct HostData {

pub fn create_host_functions(data: HostData) -> Vec<Function> {
vec![
Function::new("host_log", [ValType::I64], [], None, host_log),
Function::new(
"exec_command",
[ValType::I64],
[ValType::I64],
Some(UserData::new(data)),
exec_command,
),
// Backwards compatibility
Function::new("trace", [ValType::I64], [], None, host_log),
Function::new(
"get_env_var",
[ValType::I64],
[ValType::I64],
None,
get_env_var,
),
Function::new("host_log", [ValType::I64], [], None, host_log),
Function::new(
"set_env_var",
[ValType::I64, ValType::I64],
[],
None,
set_env_var,
),
]
}

Expand Down Expand Up @@ -136,3 +148,52 @@ fn exec_command(

Ok(())
}

fn get_env_var(
plugin: &mut CurrentPlugin,
inputs: &[Val],
outputs: &mut [Val],
_user_data: UserData,
) -> Result<(), Error> {
let name = plugin.memory_read_str(inputs[0].unwrap_i64() as u64)?;
let value = env::var(name).unwrap_or_default();

trace!(
target: "proto_wasm::get_env_var",
name,
value = &value,
"Reading environment variable from host"
);

let ptr = plugin.memory_alloc_bytes(value)?;

outputs[0] = Val::I64(ptr as i64);

Ok(())
}

fn set_env_var(
plugin: &mut CurrentPlugin,
inputs: &[Val],
_outputs: &mut [Val],
_user_data: UserData,
) -> Result<(), Error> {
let name = plugin
.memory_read_str(inputs[0].unwrap_i64() as u64)?
.to_owned();

let value = plugin
.memory_read_str(inputs[1].unwrap_i64() as u64)?
.to_owned();

trace!(
target: "proto_wasm::set_env_var",
name = &name,
value = &value,
"Writing environment variable to host"
);

env::set_var(name, value);

Ok(())
}
19 changes: 10 additions & 9 deletions plugins/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion plugins/wasm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ use std::collections::HashMap;

#[host_fn]
extern "ExtismHost" {
fn host_log(input: Json<HostLogInput>);
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
fn get_env_var(name: &str) -> String;
fn host_log(input: Json<HostLogInput>);
fn set_env_var(name: &str, value: &str);
}

#[plugin_fn]
pub fn register_tool(_: ()) -> FnResult<Json<ToolMetadataOutput>> {
host_log!("Registering tool");

let value = host_env!("WASM_KEY");

host_log!("WASM_KEY = {:?}", value);
host_env!("WASM_SOURCE", "guest");

Ok(Json(ToolMetadataOutput {
name: "WASM Test".into(),
type_of: PluginType::CLI,
Expand Down