feat: wit-bindgen-sample
This commit is contained in:
@@ -1,3 +1,12 @@
|
|||||||
|
|
||||||
|
Requires:
|
||||||
|
* Rust (with target wasm32-unknown-unknown)
|
||||||
|
|
||||||
|
|
||||||
|
Check out wit-bindgen:
|
||||||
|
```shell
|
||||||
git clone https://github.com/bytecodealliance/wit-bindgen.git
|
git clone https://github.com/bytecodealliance/wit-bindgen.git
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ impl FnResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn success(result: String) -> Self {
|
pub fn success(result: impl Into<String>) -> Self {
|
||||||
FnResult {
|
FnResult {
|
||||||
result: Some(result),
|
result: Some(result.into()),
|
||||||
error: None,
|
error: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,16 +96,16 @@ pub fn do_fetch(params: &str) -> FnResult {
|
|||||||
|
|
||||||
let mut result_map = Map::new();
|
let mut result_map = Map::new();
|
||||||
result_map.insert("status".to_string(), Value::Number(Number::from(status)));
|
result_map.insert("status".to_string(), Value::Number(Number::from(status)));
|
||||||
let mut header_map = Map::new();
|
let mut headers_map = Map::new();
|
||||||
for (k, v) in headers {
|
for (k, v) in headers {
|
||||||
let header_key = k.map(|n| n.as_str().to_string()).unwrap_or_else(|| "".to_string());
|
let header_key = k.map(|n| n.as_str().to_string()).unwrap_or_else(|| "".to_string());
|
||||||
let header_value = match v.to_str() {
|
let header_value = match v.to_str() {
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
Ok(v) => v.to_string(),
|
Ok(v) => v.to_string(),
|
||||||
};
|
};
|
||||||
header_map.insert(header_key, Value::String(header_value));
|
headers_map.insert(header_key, Value::String(header_value));
|
||||||
}
|
}
|
||||||
result_map.insert("headers".to_string(), Value::Object(header_map));
|
result_map.insert("headers".to_string(), Value::Object(headers_map));
|
||||||
result_map.insert("body".to_string(), Value::String(String::from_utf8_lossy(&body_buff).to_string()));
|
result_map.insert("body".to_string(), Value::String(String::from_utf8_lossy(&body_buff).to_string()));
|
||||||
|
|
||||||
FnResult::success(format!("{}", Value::Object(result_map)))
|
FnResult::success(format!("{}", Value::Object(result_map)))
|
||||||
|
|||||||
@@ -30,15 +30,15 @@ fn main() -> Result<()> {
|
|||||||
)?;
|
)?;
|
||||||
let a = exports.eval_javascript(&mut store, r##"
|
let a = exports.eval_javascript(&mut store, r##"
|
||||||
function hi(name) { return "hi: " + name; }
|
function hi(name) { return "hi: " + name; }
|
||||||
let a = [];
|
// let a = [];
|
||||||
a.push(fetch('https://hatter.ink/util/print_request.action'));
|
// a.push(fetch('https://hatter.ink/util/print_request.action'));
|
||||||
for (let i = 0; i < 3; i++) { a.push(i); }
|
// for (let i = 0; i < 3; i++) { a.push(i); }
|
||||||
a.push({
|
// a.push({
|
||||||
id: 1, name: 'hatter'
|
// id: 1, name: 'hatter'
|
||||||
});
|
// });
|
||||||
a.push(hi('001'));
|
// a.push(hi('001'));
|
||||||
a.push(hi('002'));
|
// a.push(hi('002'));
|
||||||
// let a = fetch('https://hatter.ink/util/print_request.action');
|
let a = fetch('https://hatter.ink/util/print_request.action');
|
||||||
JSON.stringify(a)
|
JSON.stringify(a)
|
||||||
// a
|
// a
|
||||||
"##);
|
"##);
|
||||||
|
|||||||
@@ -9,15 +9,12 @@ struct Exports;
|
|||||||
impl exports::Exports for Exports {
|
impl exports::Exports for Exports {
|
||||||
fn eval_javascript(script: String) -> String {
|
fn eval_javascript(script: String) -> String {
|
||||||
let mut ctx = Context::default();
|
let mut ctx = Context::default();
|
||||||
ctx.register_global_function("fetch", 0, fetch_fn);
|
ctx.register_global_function("fetch", 0, do_fetch);
|
||||||
let o = match ctx.eval(&script) {
|
let o = match ctx.eval(&script) {
|
||||||
JsResult::Ok(o) => o,
|
JsResult::Ok(o) => o,
|
||||||
JsResult::Err(o) => o,
|
JsResult::Err(o) => o,
|
||||||
};
|
};
|
||||||
format!("{}", o.to_json(&mut ctx).unwrap())
|
format!("{}", o.to_json(&mut ctx).expect("To JSON error"))
|
||||||
// let a = container::fetch("");
|
|
||||||
// let a = fetch_from_host("<CONFIG>");
|
|
||||||
// format!("eval: {}, fetched: {}", s, a)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +24,10 @@ struct FetchResult {
|
|||||||
result: Option<String>,
|
result: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_fn(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
|
fn do_fetch(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue> {
|
||||||
|
if args.is_empty() {
|
||||||
|
return JsResult::Err(JsValue::String(JsString::from("Requires at least one argument")));
|
||||||
|
}
|
||||||
let mut fetch_params_map = Map::new();
|
let mut fetch_params_map = Map::new();
|
||||||
fetch_params_map.insert("url".to_string(), args[0].to_json(ctx).expect("error"));
|
fetch_params_map.insert("url".to_string(), args[0].to_json(ctx).expect("error"));
|
||||||
if args.len() > 1 {
|
if args.len() > 1 {
|
||||||
@@ -42,5 +42,5 @@ fn fetch_fn(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValu
|
|||||||
let v = JsValue::from_json(&r, ctx).unwrap();
|
let v = JsValue::from_json(&r, ctx).unwrap();
|
||||||
return JsResult::Ok(v);
|
return JsResult::Ok(v);
|
||||||
}
|
}
|
||||||
JsResult::Err(JsValue::String(JsString::from(format!("Invoke fetch error: {}", fetch_result.error.unwrap_or("".to_string())))))
|
JsResult::Err(JsValue::String(JsString::from(format!("Invoke fetch error: {}", fetch_result.error.unwrap_or("<unknown>".to_string())))))
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user