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
use makepad_live_id::*;
use crate::to_wasm::*;

pub trait FromWasm {
    fn type_name()->&'static str{panic!()}
    fn live_id()->LiveId{panic!()}

    fn write_from_wasm(self, out: &mut FromWasmMsg) where Self:Sized {
        out.push_u64(Self::live_id().0);
        let block_len_offset = out.data_len();
        out.push_u32(0);
        self.from_wasm_inner(out);
        out.or_even_u32(block_len_offset, (out.data_len() - block_len_offset + 1) as u32);
        // align it
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg);
    
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot:usize, is_recur: bool, prop:&str, temp:usize);
    
    fn from_wasm_js_reuse(wrapper: &mut String) {
        Self::from_wasm_js_inner(wrapper, true);
    }
    
    fn to_js_code()->String {
        let mut wrapper = String::new();
        Self::from_wasm_js_inner(&mut wrapper, false);
        wrapper
    }
    
    fn from_wasm_js_inner(wrapper:&mut String, reuse_arg:bool){
        let id = Self::live_id();

        wrapper.push_str(&format!("{}(){{\n", id.0 & 0xffff_ffff));
        wrapper.push_str("let app = this.app;\n");
        if reuse_arg{
            wrapper.push_str("let args = app.from_wasm_args;\n");
        }
        else{
            wrapper.push_str("let args = {};\n");
        }
        
        let mut out = WasmJSOutput{temp_alloc:0, fns:vec![WasmJSOutputFn{name:String::new(), body:String::new(), temp:0}]};
        let new_nest = out.alloc_temp();
        
        Self::from_wasm_js_body(&mut out, 0, false, &format!("args.{}", Self::type_name()), new_nest);
        
        for p in out.fns.iter().rev(){
            if p.name.is_empty(){
                wrapper.push_str(&p.body);
            }
            else{
                wrapper.push_str(&format!("let {} = (t{})=>{{\n{}}}\n", p.name, p.temp, p.body))
            }
        }
        
        wrapper.push_str(&format!("app.{0}(args.{0});\n", Self::type_name()));
        wrapper.push_str("}\n");
    }
}

pub struct FromWasmMsg {
    pub(crate) data: Vec<u64>,
    pub(crate) odd: bool
}

impl FromWasmMsg {
    pub fn new() -> Self {
        Self {
            data: vec![0],
            odd: false
        }
    }
    
    pub fn reserve_u64(mut self, capacity_u64: usize) -> Self {
        self.data.reserve(capacity_u64);
        self
    }

    pub fn data_len(& self)->usize{ self.data.len() }
    
    pub fn or_even_u32(&mut self, index:usize, data:u32){
        self.data[index] |= data as u64;
    }
    
    pub fn push_u32(&mut self, data: u32) {
        if self.odd {
            let len = self.data.len();
            self.data[len - 1] |= (data as u64) << 32;
            self.odd = false
        }
        else {
            self.data.push(data as u64);
            self.odd = true;
        }
    }
    
    pub fn push_f32(&mut self, data: f32) {
        self.push_u32(data.to_bits())
    }

    pub fn push_u64(&mut self, data: u64) {
        self.odd = false;
        self.data.push(data);
    }
    
    pub fn push_f64(&mut self, data: f64) {
        self.odd = false;
        self.data.push(data.to_bits());
    }
    
    pub fn push_str(&mut self, val: &str) {
        let chars = val.chars().count();
        self.push_u32(chars as u32);
        for c in val.chars() {
            self.push_u32(c as u32);
        }
    }
    
    pub fn release_ownership(self) -> u32 {
        unsafe {
            let mut v = std::mem::ManuallyDrop::new(self.data);
            let ptr = v.as_mut_ptr();
            let len = v.len();
            let cap = v.capacity();
            
            ptr.offset(0).write((len as u64) << 32 | cap as u64);

            ptr as u32
        }
    }

    pub fn from_wasm(&mut self, from_wasm:impl FromWasm){
        from_wasm.write_from_wasm(self);
    }

}

impl Default for FromWasmMsg {
    fn default() -> Self {
        Self::new()
    }
}