Skip to content

Commit 2b7dc70

Browse files
authored
Merge pull request #12 from extism/typed-var-gets
feat: Typed Var.get functions
2 parents 3c81bdb + 72d95a3 commit 2b7dc70

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ Implemented so far:
208208
* Host.inputString
209209
* Host.outputBytes
210210
* Host.outputString
211-
* Var.get
211+
* Var.getBytes
212+
* Var.getString
212213
* Var.set
213214
* console.log
214215
* console.error

crates/core/src/globals.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,19 @@ fn build_var_object(context: &Context) -> anyhow::Result<Value> {
104104
}
105105
})?;
106106

107+
let var_get_str = context.wrap_callback(|ctx: &Context, _this: &Value, args: &[Value]| {
108+
let var_name = args.get(0).ok_or(anyhow!("Expected var_name argument"))?;
109+
let data = var::get::<String>(var_name.as_str()?)?;
110+
match data {
111+
Some(d) => ctx.value_from_str(d.as_str()),
112+
None => ctx.null_value(),
113+
}
114+
})?;
115+
107116
let var_object = context.object_value()?;
108117
var_object.set_property("set", var_set)?;
109-
var_object.set_property("get", var_get)?;
118+
var_object.set_property("getBytes", var_get)?;
119+
var_object.set_property("getString", var_get_str)?;
110120

111121
Ok(var_object)
112122
}

0 commit comments

Comments
 (0)