feat: Variable args (#1667)

* feat: Variable args

* chore: Make the assert error message not wrong
This commit is contained in:
J M
2022-07-18 15:46:32 -06:00
committed by GitHub
parent 009f7ddf84
commit 635c3627c9
4 changed files with 81 additions and 8 deletions

View File

@@ -68,6 +68,7 @@ const fetch = async (url, options = null) => {
const currentFunction = Deno.core.opSync("current_function");
const input = Deno.core.opSync("get_input");
const variable_args = Deno.core.opSync("get_variable_args");
const setState = (x) => Deno.core.opSync("set_value", x);
const effects = {
writeFile,
@@ -93,6 +94,6 @@ const runFunction = jsonPointerValue(mainModule, currentFunction);
error(`Expecting ${ currentFunction } to be a function`);
throw new Error(`Expecting ${ currentFunction } to be a function`);
}
const answer = await runFunction(effects, input);
const answer = await runFunction(effects, input, ...variable_args);
setState(answer);
})();

View File

@@ -87,6 +87,7 @@ struct JsContext {
package_id: PackageId,
volumes: Arc<dyn PathForVolumeId>,
input: Value,
variable_args: Vec<serde_json::Value>,
}
#[derive(Clone, Default)]
@@ -218,6 +219,7 @@ impl JsExecutionEnvironment {
self,
procedure_name: ProcedureName,
input: Option<I>,
variable_args: Vec<serde_json::Value>,
) -> Result<O, (JsError, String)> {
let input = match serde_json::to_value(input) {
Ok(a) => a,
@@ -231,7 +233,8 @@ impl JsExecutionEnvironment {
}
};
let safer_handle: NonDetachingJoinHandle<_> =
tokio::task::spawn_blocking(move || self.execute(procedure_name, input)).into();
tokio::task::spawn_blocking(move || self.execute(procedure_name, input, variable_args))
.into();
let output = safer_handle
.await
.map_err(|err| (JsError::Tokio, format!("Tokio gave us the error: {}", err)))??;
@@ -266,6 +269,7 @@ impl JsExecutionEnvironment {
fns::log_debug::decl(),
fns::log_info::decl(),
fns::get_input::decl(),
fns::get_variable_args::decl(),
fns::set_value::decl(),
fns::is_sandboxed::decl(),
]
@@ -275,6 +279,7 @@ impl JsExecutionEnvironment {
&self,
procedure_name: ProcedureName,
input: Value,
variable_args: Vec<serde_json::Value>,
) -> Result<Value, (JsError, String)> {
let base_directory = self.base_directory.clone();
let answer_state = AnswerState::default();
@@ -287,6 +292,7 @@ impl JsExecutionEnvironment {
version: self.version.clone(),
sandboxed: self.sandboxed,
input,
variable_args,
};
let ext = Extension::builder()
.ops(Self::declarations())
@@ -675,6 +681,11 @@ mod fns {
Ok(ctx.input.clone())
}
#[op]
fn get_variable_args(state: &mut OpState) -> Result<Vec<Value>, AnyError> {
let ctx = state.borrow::<JsContext>();
Ok(ctx.variable_args.clone())
}
#[op]
fn set_value(state: &mut OpState, value: Value) -> Result<(), AnyError> {
let mut answer = state.borrow::<AnswerState>().0.lock();
*answer = value;