Skip to content

Conversation

@jozanza
Copy link
Contributor

@jozanza jozanza commented Jun 28, 2023

I was running through the documentation for invoking functions and ran into trouble reading env vars and stdin from Rust.

After some exploration of the AssemblyScript bindings, I realized that std::io was not the expected way to gather these inputs. It's actually done via two externs: memory_read and env_var_read in the blockless_memory wasm import module.

Locally, I added the externs and some basic wrapper functions to read the data. There aren't any utility methods for reading to strings and json in the module, and I wasn't sure what the error code enumeration was 😅. But I'm using this code locally right now, so I thought I'd share.

Example

If this code were merged, you could read stdin and env vars like so:

use blockless_sdk::*;

fn main() -> std::io::Result<()> {
    let mut buf = [0; 1024];
    let len = memory::read_stdin(&mut buf)?;
    let str = std::str::from_utf8(&buf[..len as usize]);
    println!("STDIN: {:?}", str);

    let mut buf = [0; 1024];
    let len = memory::read_env_vars(&mut buf)?;
    let str = std::str::from_utf8(&buf[..len as usize]);
    println!("ENV: {:?}", str);

    Ok(())
}

Invoking the function with stdin would yield the following:

bls function invoke --stdin ARG1=VAL1 --env FOO=BAR

STDIN: Ok("ARG1=VAL1\n")
ENV: Ok("{\"FOO\": \"BAR\"}")

@Joinhack Joinhack requested a review from dmikey June 28, 2023 22:54
@Joinhack
Copy link
Collaborator

thank you.

@Joinhack Joinhack merged commit 4ac8277 into blocklessnetwork:main Jun 28, 2023
@jozanza
Copy link
Contributor Author

jozanza commented Jun 29, 2023

np!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants