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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#![allow(dead_code)]

pub use lmfu;
pub use lmfu::json::{JsonFile, Path as JsonPath, Value as JsonValue};

use lmfu::{strpool::Pool, ArcStr};
use core::ptr::NonNull;

pub use moth_wasm_macros::moth_callback;

pub fn param(ptr: u64, len: u64) -> &'static str {
    use core::{slice, str};
    str::from_utf8(unsafe { slice::from_raw_parts(ptr as _, len as _) }).unwrap()
}

extern "C" {
    fn __read_table_entry(
        db_token: u64,
        in_tn_len: u64,
        in_tn_ptr: u64,
        in_key_len: u64,
        in_key_ptr: u64,
    ) -> /* out_json_ptr */ u64;

    fn __write_table_entry(
        db_token: u64,
        in_tn_len: u64,
        in_tn_ptr: u64,
        in_key_len: u64,
        in_key_ptr: u64,
        in_json_len: u64,
        in_json_ptr: u64,
    );

    fn __set_template_name(
        db_token: u64,
        in_name_len: u64,
        in_name_ptr: u64,
    );

    fn __set_template_param(
        db_token: u64,
        in_key_len: u64,
        in_key_ptr: u64,
        in_value_len: u64,
        in_value_ptr: u64,
    );
}

pub struct Request {
    db_token: u64,
    body: Option<Box<JsonFile>>,
}

impl Request {
    pub unsafe fn new(db_token: u64, body_ptr: u64) -> Self {
        Self {
            db_token,
            body: Some(Box::from_raw(body_ptr as *mut JsonFile)),
        }
    }

    pub fn take_body(&mut self) -> Box<JsonFile> {
        self.body.take().expect("Request body was already taken")
    }

    pub fn read_table_entry(&self, table: &str, key: &str) -> Option<Box<JsonFile>> {
        unsafe {
            let json_ptr = __read_table_entry(
                self.db_token,
                table.as_ptr() as _,
                table.len() as _,
                key.as_ptr() as _,
                key.len() as _,
            );

            match json_ptr {
                0 => None,
                p => Some(Box::from_raw(p as *mut JsonFile)),
            }
        }
    }

    pub fn write_table_entry(&self, table: &str, key: &str, json: &str) {
        unsafe {
            __write_table_entry(
                self.db_token,
                table.as_ptr() as _,
                table.len() as _,
                key.as_ptr() as _,
                key.len() as _,
                json.as_ptr() as _,
                json.len() as _,
            );
        }
    }

    pub fn set_template_name(&self, name: &str) {
        unsafe {
            __set_template_name(self.db_token, name.as_ptr() as _, name.len() as _);
        }
    }

    pub fn set_template_param(&self, key: &str, value: &str) {
        unsafe {
            __set_template_param(
                self.db_token,
                key.as_ptr() as _,
                key.len() as _,
                value.as_ptr() as _,
                value.len() as _,
            );
        }
    }
}

#[no_mangle]
extern "C" fn __rs_malloc(size: u64) -> /* ptr */ u64 {
    (Box::into_raw(vec![0u8; size as _].into_boxed_slice()) as *mut u8) as _
}

#[no_mangle]
extern "C" fn __rs_free(ptr: u64, size: u64) {
    let slice_ptr = core::ptr::slice_from_raw_parts_mut(ptr as *mut u8, size as _);
    unsafe { Box::from_raw(slice_ptr) };
}

#[no_mangle]
extern "C" fn __parse_json(in_str_ptr: u64, in_str_len: u64) -> /* out_json_ptr */ u64 {
    let string = param(in_str_ptr, in_str_len);
    match JsonFile::with_key_pool(Some(string), Pool::get_static_pool()) {
        Ok(parsed) => Box::into_raw(Box::new(parsed)) as _,
        Err(_) => 0,
    }
}

#[no_mangle]
extern "C" fn __dump_json(
    in_json_ptr: u64,
) -> /* arcstr_ptr */ u64 {
    // take back ownership
    let json = unsafe { Box::from_raw(in_json_ptr as *mut JsonFile) };

    // dump into an ArcStr
    let arcstr = json.dump(&JsonPath::new()).unwrap(/* panic = OOM */);
    let arcstr_ptr = ArcStr::into_raw(arcstr).as_ptr();

    arcstr_ptr as _
}

#[no_mangle]
extern "C" fn __json_dump_len(arcstr_ptr: u64) -> u64 {
    let arcstr = unsafe { ArcStr::from_raw(NonNull::new_unchecked(arcstr_ptr as _)) };
    let len = arcstr.len();
    core::mem::forget(arcstr);
    len as _
}

#[no_mangle]
extern "C" fn __json_dump_ptr(arcstr_ptr: u64) -> u64 {
    let arcstr = unsafe { ArcStr::from_raw(NonNull::new_unchecked(arcstr_ptr as _)) };
    let ptr = arcstr.as_str().as_ptr();
    core::mem::forget(arcstr);
    ptr as _
}

#[no_mangle]
extern "C" fn __free_json_dump(arcstr_ptr: u64) {
    unsafe { ArcStr::from_raw(NonNull::new_unchecked(arcstr_ptr as _)) };
}