feat: works

This commit is contained in:
2022-07-17 10:11:20 +08:00
parent 4ba63b4c2e
commit 74a202f1ed
458 changed files with 125067 additions and 8 deletions

View File

@@ -1,16 +1,47 @@
use boa_engine::{Context, JsResult, JsString, JsValue};
use serde::{Deserialize, Serialize};
use serde_json::Value;
wit_bindgen_rust::export!("../exports.wit");
wit_bindgen_rust::import!("../container.wit");
struct Exports;
impl exports::Exports for Exports {
fn eval_javascript(s: String) -> String {
fn eval_javascript(script: String) -> String {
let mut ctx = Context::default();
ctx.register_global_function("fetch", 0, fetch_fn);
let o = match ctx.eval(&script) {
JsResult::Ok(o) => o,
JsResult::Err(o) => o,
};
format!("{}", o.to_json(&mut ctx).unwrap())
// let a = container::fetch("");
let a = fetch_from_host("<CONFIG>");
format!("eval: {}, fetched: {}", s, a)
// let a = fetch_from_host("<CONFIG>");
// format!("eval: {}, fetched: {}", s, a)
}
}
fn fetch_from_host(config: &str) -> String {
container::fetch(config)
#[derive(Clone, Debug, Serialize, Deserialize)]
struct FetchResult {
error: Option<String>,
result: Option<String>,
}
fn fetch_fn(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
let mut options = String::new();
options.push('[');
args.iter().enumerate().for_each(|(i, arg)| {
if i > 0 { options.push(','); }
options.push_str(arg.to_json(ctx).expect("to json error").as_str().expect("as str error"))
});
options.push(']');
// let r = serde_json::to_string(args).unwrap();
let fetch_result_string = container::fetch(&options);
let fetch_result: FetchResult = serde_json::from_str(&fetch_result_string).expect("from str error");
if let Some(result) = fetch_result.result {
let r: Value = serde_json::from_str(&result).expect("result from str error");
let v = JsValue::from_json(&r, ctx).unwrap();
return JsResult::Ok(v);
}
JsResult::Err(JsValue::String(JsString::from(format!("Invoke fetch error: {}", fetch_result.error.unwrap_or("".to_string())))))
}