1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! A Rust SDK for writing [Webassembly] for [W3bstream].
//!
//! [W3bstream] is a general framework for connecting data generated
//! in the physical world to the blockchain world. [Webassembly] is selected as the
//! programming language for data processing.
//!
//! At a high level, W3bstream provides several Application Binary Interfaces (ABIs)
//! to enhance the [Webassembly] ability for developers:
//!
//!  * [streaming]: Reading or Writing the data in the stream
//!  * [database]: Storing or accessing the data in the database
//!  * [blockchain]: Writing or reading the contract on the blockchain
//!  * [logging][log]: Logging information or errors
//!
//! Guide level documentation is found on the [website].
//!
//! [W3bstream]: https://w3bstream.com/
//! [Webassembly]: https://webassembly.org/
//! [streaming]: crate::stream
//! [database]: crate::database
//! [blockchain]: crate::blockchain
//! [log]: crate::log
//! [website]: https://docs.w3bstream.com/
//!
//! # Examples
//!
//! Say "Hello World!" to the w3bstream:
//!
//! ```no_run
//!
//! ```
//!
pub mod blockchain;
pub mod database;
pub mod host;
pub mod log;
pub mod stream;

use self::database::kv::*;
use self::log::log_info;
use anyhow::Result;
use std::mem;
#[no_mangle]
pub extern "C" fn start(rid: i32) -> i32 {
    match handle(rid) {
        Ok(_) => return 0,
        _ => return -1,
    };
}

// #[no_mangle]
// pub extern "C" fn alloc(size: i32) -> *mut u8 {
//     let mut buf: Vec<u8> = Vec::with_capacity(size as _);
//     let ptr = buf.as_mut_ptr();
//     mem::forget(buf);
//     log_info(&format!("PTR ADDR IN ALLOC {:?} SIZE {}", ptr, size));
//     return ptr;
// }

fn handle(_: i32) -> Result<()> {
    set("key_test", String::from("test").into_bytes())?;
    log_info("set key success_3")?;

    let value = get("key_test")?;
    log_info("get key success")?;

    log_info(&format!("value len {}", value.len()))?;
    log_info(&format!(
        "get data {} by key {}",
        String::from_utf8(value)?,
        "key_test"
    ))?;

    Ok(())
}