feat: fetch
This commit is contained in:
25
__wasm/wit-bindgen-sample/container/Cargo.lock
generated
25
__wasm/wit-bindgen-sample/container/Cargo.lock
generated
@@ -142,6 +142,8 @@ name = "container"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"wasmtime",
|
"wasmtime",
|
||||||
"wit-bindgen-wasmtime",
|
"wit-bindgen-wasmtime",
|
||||||
]
|
]
|
||||||
@@ -512,6 +514,12 @@ dependencies = [
|
|||||||
"either",
|
"either",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ittapi-rs"
|
name = "ittapi-rs"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -839,6 +847,12 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ryu"
|
||||||
|
version = "1.0.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
@@ -865,6 +879,17 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.82"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"ryu",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sha2"
|
name = "sha2"
|
||||||
version = "0.9.9"
|
version = "0.9.9"
|
||||||
|
|||||||
@@ -9,3 +9,5 @@ edition = "2021"
|
|||||||
anyhow = "1.0.58"
|
anyhow = "1.0.58"
|
||||||
wasmtime = "0.38.1"
|
wasmtime = "0.38.1"
|
||||||
wit-bindgen-wasmtime = { path = '../wit-bindgen/crates/wasmtime' }
|
wit-bindgen-wasmtime = { path = '../wit-bindgen/crates/wasmtime' }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
|||||||
@@ -1,19 +1,37 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
||||||
|
|
||||||
use container::*;
|
use container::*;
|
||||||
|
|
||||||
wit_bindgen_wasmtime::export!("../container.wit");
|
wit_bindgen_wasmtime::export!("../container.wit");
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
struct FetchResult {
|
||||||
|
error: Option<String>,
|
||||||
|
result: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
struct JsResult {
|
||||||
|
message: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct MyContainer;
|
pub struct MyContainer;
|
||||||
|
|
||||||
impl Container for MyContainer {
|
impl Container for MyContainer {
|
||||||
fn fetch(&mut self, s: &str) -> String {
|
fn fetch(&mut self, s: &str) -> String {
|
||||||
// format!("FETCHED: {}", s)
|
// // format!("FETCHED: {}", s)
|
||||||
let r = "{\"result\":\"{}\"}".into();
|
// let r = "{\"result\":\"{}\"}".into();
|
||||||
println!(">>>> {}", r);
|
// println!(">>>> {}", r);
|
||||||
r
|
// r
|
||||||
|
serde_json::to_string(&FetchResult {
|
||||||
|
error: None,
|
||||||
|
result: Some(serde_json::to_string(&JsResult {
|
||||||
|
message: format!("inputs: {}", s),
|
||||||
|
}).expect("to json failed.1")),
|
||||||
|
}).expect("to json failed.2")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +46,16 @@ fn main() -> Result<()> {
|
|||||||
|linker| container::add_to_linker(linker, |cx| -> &mut MyContainer { &mut cx.imports }),
|
|linker| container::add_to_linker(linker, |cx| -> &mut MyContainer { &mut cx.imports }),
|
||||||
|store, module, linker| Exports::instantiate(store, module, linker, |cx| &mut cx.exports),
|
|store, module, linker| Exports::instantiate(store, module, linker, |cx| &mut cx.exports),
|
||||||
)?;
|
)?;
|
||||||
let a = exports.eval_javascript(&mut store, "let a = [];fetch('aaa')");
|
let a = exports.eval_javascript(&mut store, r##"
|
||||||
|
let a = [];
|
||||||
|
a.push(fetch('https://www.google.com/'));
|
||||||
|
for (let i = 0; i < 3; i++) { a.push(i); }
|
||||||
|
a.push({
|
||||||
|
id: 1, name: 'hatter'
|
||||||
|
});
|
||||||
|
//JSON.stringify(a)
|
||||||
|
a
|
||||||
|
"##);
|
||||||
|
|
||||||
match a {
|
match a {
|
||||||
Ok(a) => println!("Hello, world! {:?}", a),
|
Ok(a) => println!("Hello, world! {:?}", a),
|
||||||
|
|||||||
Reference in New Issue
Block a user