forked from bytecodealliance/javy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from uditdc/main
Init FetchIo api module, based on Blockless host calls
- Loading branch information
Showing
8 changed files
with
492 additions
and
34 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::APIConfig; | ||
|
||
// Use crate visibility to avoid exposing the property outside the crate | ||
#[derive(Debug)] | ||
pub(crate) struct FetchIOConfig { | ||
pub(super) prefix: String, | ||
} | ||
|
||
// Always have a default value for every config. | ||
impl Default for FetchIOConfig { | ||
fn default() -> Self { | ||
Self { | ||
prefix: "Default prefix: ".to_string(), | ||
} | ||
} | ||
} | ||
|
||
// Define one or more methods on `APIConfig`, not `FetchIOConfig`, to set properties. | ||
impl APIConfig { | ||
/// Sets the prefix for `Javy.Env.print`. | ||
pub fn prefix(&mut self, prefix: String) -> &mut Self { | ||
self.fetch_io.prefix = prefix; | ||
self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Wrap everything in an anonymous function to avoid leaking local variables into the global scope. | ||
(function () { | ||
// Get a reference to the function before we delete it from `globalThis`. | ||
const __javy_fetchio_get = globalThis.__javy_fetchio_get; | ||
globalThis.Javy.FetchIO = { | ||
get(url) { | ||
__javy_fetchio_get(url); | ||
}, | ||
}; | ||
// Delete the function from `globalThis` so it doesn't leak. | ||
Reflect.deleteProperty(globalThis, "__javy_fetchio_get"); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//use std::collections::HashMap; | ||
|
||
use anyhow::{bail, Result}; | ||
use javy::{quickjs::JSValue, Runtime}; | ||
use blockless_sdk::*; | ||
|
||
use crate::{APIConfig, JSApiSet}; | ||
pub(super) use config::FetchIOConfig; | ||
|
||
mod config; | ||
|
||
pub(super) struct FetchIO; | ||
|
||
impl JSApiSet for FetchIO { | ||
fn register(&self, runtime: &Runtime, config: &APIConfig) -> Result<()> { | ||
let context = runtime.context(); | ||
|
||
let global = context.global_object()?; | ||
|
||
let mut javy_object = global.get_property("Javy")?; | ||
|
||
// If you're defining something on the `Javy` object, ensure it exists. | ||
if javy_object.is_undefined() { | ||
javy_object = context.object_value()?; | ||
global.set_property("Javy", javy_object)?; | ||
} | ||
|
||
// `wrap_callback`` has a static lifetime so you can't use references to the config in its body. | ||
global.set_property( | ||
"__javy_fetchio_get", | ||
context.wrap_callback(move |_ctx, _this, args| { | ||
let [url] = args else { | ||
bail!("Incorrect number of arguments"); | ||
}; | ||
// Convert JSValueRefs to Rust types. | ||
let url: String = url.try_into()?; | ||
|
||
let opts = HttpOptions::new("GET", 30, 10); | ||
let http = BlocklessHttp::open(&url, &opts); | ||
let http = http.unwrap(); | ||
let body = http.get_all_body().unwrap(); | ||
let body = String::from_utf8(body).unwrap(); | ||
let data = match json::parse(&body).unwrap() { | ||
json::JsonValue::Object(o) => o, | ||
_ => panic!("must be object"), | ||
}; | ||
|
||
println!("Http Data: {:?}", data); | ||
|
||
Ok(JSValue::Undefined) | ||
})?, | ||
)?; | ||
|
||
context.eval_global("fetch.js", include_str!("fetch.js"))?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
// Automated tests are highly recommended | ||
#[cfg(test)] | ||
mod tests { | ||
use std::env; | ||
|
||
use crate::{APIConfig, JSApiSet}; | ||
use anyhow::Result; | ||
use javy::Runtime; | ||
|
||
use super::FetchIO; | ||
|
||
#[test] | ||
fn test_print_env_var() -> Result<()> { | ||
let runtime = Runtime::default(); | ||
let context = runtime.context(); | ||
FetchIO.register(&runtime, &APIConfig::default())?; | ||
env::set_var("HELLO", "there"); | ||
let _ = context.eval_global("main", "Javy.Env.print('HELLO');")?; | ||
env::remove_var("HELLO"); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters