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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use crate::from_wasm::*;
use crate::to_wasm::*;
use crate::{LiveId,live_id};
use makepad_derive_wasm_bridge::*;
pub struct WasmDataU8(Vec<u8>);

impl WasmDataU8 {
    pub fn new_and_release_ownership(capacity: usize) -> u32 {
        let mut v = Vec::<u8>::new();
        v.reserve_exact(capacity);
        let mut v = std::mem::ManuallyDrop::new(v);
        let ptr = v.as_mut_ptr();
        let cap = v.capacity();
        if cap != capacity {panic!()};
        ptr as u32
    }
    
    pub fn take_ownership(ptr:u32, len:u32, cap:u32)->Self{
        unsafe {
            Self(Vec::from_raw_parts(ptr as *mut u8, len as usize, cap as usize))
        }
    }
    
    pub fn from_vec_u8(v:Vec<u8>)->Self{
        Self(v)
    }
    
    pub fn into_vec_u8(self)->Vec<u8>{
        self.0
    }
}

impl ToWasm for WasmDataU8 {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        
        let ptr = inp.read_u32();
        let len = inp.read_u32() as usize;
        unsafe {
            Self (Vec::from_raw_parts(ptr as *mut u8, len, len))
        }
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("this.push_data_u8({});", prop));
    }
    
    fn u32_size() -> usize {2}
}

impl FromWasm for WasmDataU8 {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = {{ptr:app.u32[this.u32_offset++],len:app.u32[this.u32_offset++],capacity:app.u32[this.u32_offset++]}};", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        let mut v = std::mem::ManuallyDrop::new(self.0);
        out.push_u32(v.as_mut_ptr() as u32);
        out.push_u32(v.len() as u32);
        out.push_u32(v.capacity() as u32);
    }
}
/*
impl FromWasm for WasmDataU8 {
    fn write_from_wasm(&self, out: &mut FromWasmMsg) {
        let swap = Vec::new();
        
        let mut v = std::mem::ManuallyDrop::new(v);
        let ptr = v.as_mut_ptr();
        let cap = v.capacity();
        
        out.push_u32()
        let ptr = inp.read_u32();
        let len = inp.read_u32() as usize;
        unsafe {
            Self (Vec::from_raw_parts(ptr as *mut u8, len, len))
        }
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("this.push_data_u8({});", prop));
    }
    
    fn u32_size() -> usize {2}
}*/

pub struct WasmPtrF32(u32);

impl WasmPtrF32 {
    pub fn new(ptr: *const f32) -> Self {
        Self (ptr as u32)
    }
}

impl FromWasm for WasmPtrF32 {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = app.u32[this.u32_offset++];", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_u32(self.0)
    }
}


#[derive(FromWasm)]
pub struct WasmDataF32 {
    pub ptr: WasmPtrF32,
    pub len: usize
}

impl WasmDataF32 {
    pub fn new(data:&[f32]) -> Self {
        if !data.is_empty(){
            Self{ptr:WasmPtrF32::new(data.as_ptr()), len:data.len()}
        }
        else{
            Self{ptr:WasmPtrF32::new(std::ptr::null::<f32>()), len:0}
        }
    }
}

#[derive(FromWasm)]
pub struct WasmDataU32 {
    pub ptr: WasmPtrU32,
    pub len: usize
}

impl WasmDataU32 {
    pub fn new(data:&[u32]) -> Self {
        if !data.is_empty(){
            Self{ptr:WasmPtrU32::new(data.as_ptr()), len:data.len()}
        }
        else{
            Self{ptr:WasmPtrU32::new(std::ptr::null::<u32>()), len:0}
        }
    }
}


pub struct WasmPtrU32(u32);

impl WasmPtrU32 {
    pub fn new(ptr: *const u32) -> Self {
        Self (ptr as u32)
    }
}

impl FromWasm for WasmPtrU32 {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = app.u32[this.u32_offset++];", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_u32(self.0)
    }
}





impl FromWasm for String {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = this.read_str();", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_str(&self);
    }
}

impl FromWasm for &str {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = this.read_str();", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_str(self);
    }
}

impl ToWasm for String {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        inp.read_string()
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("this.push_str({});", prop));
    }
    
    fn u32_size() -> usize {1}
}



impl FromWasm for bool {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = app.u32[this.u32_offset++]!==0?true:false;", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_u32(if self {1} else {0})
    }
}

impl ToWasm for bool {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        inp.read_u32() != 0
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("app.u32[this.u32_offset++] = {};", prop));
    }
    fn u32_size() -> usize {1}
}


impl FromWasm for usize {
    
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = app.u32[this.u32_offset++];", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_u32(self as u32)
    }
}

impl ToWasm for usize {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        inp.read_u32() as usize
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("app.u32[this.u32_offset++] = {};", prop));
    }
    fn u32_size() -> usize {1}
}



impl FromWasm for u32 {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = app.u32[this.u32_offset++];", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_u32(self)
    }
}

impl ToWasm for u32 {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        inp.read_u32()
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("app.u32[this.u32_offset++] = {};", prop));
    }
    fn u32_size() -> usize {1}
}




impl FromWasm for f32 {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("{} = app.f32[this.u32_offset++];", prop));
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_f32(self)
    }
}

impl ToWasm for f32 {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        inp.read_f32()
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, &format!("app.f32[this.u32_offset++] = {};", prop));
    }
    fn u32_size() -> usize {1}
}




impl FromWasm for f64 {
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, "this.u32_offset += this.u32_offset&1;");
        out.push_ln(slot, &format!("{} = app.f64[this.u32_offset>>1];", prop));
        out.push_ln(slot, "this.u32_offset += 2;");
    }
    
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_f64(self)
    }
}

impl ToWasm for f64 {
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        inp.read_f64()
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, "this.u32_offset += this.u32_offset&1;");
        out.push_ln(slot, &format!("app.f64[this.u32_offset>>1] = {};", prop));
        out.push_ln(slot, "this.u32_offset += 2;");
    }
    
    fn u32_size() -> usize {3}
}





impl<T, const N: usize> FromWasm for [T; N] where T: FromWasm {
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        for item in self {
            item.from_wasm_inner(out);
        }
    }
    
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, is_recur: bool, prop: &str, temp: usize) {
        out.push_ln(slot, &format!("if({0} === undefined) {0} = [];", prop));
        out.push_ln(slot, &format!("let t{} = {};", temp, prop));
        out.push_ln(slot, &format!("for(let i{0} = 0; i{0} < {1}; i{0}++){{", temp, N));
        let new_temp = out.alloc_temp();
        T::from_wasm_js_body(out, slot, is_recur, &format!("t{0}[i{0}]", temp), new_temp);
        out.push_ln(slot, "}");
    }
}

impl<T, const N: usize> ToWasm for [T; N] where T: ToWasm {
    fn u32_size() -> usize {T::u32_size() * N}
    
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        unsafe {
            let mut to = std::mem::MaybeUninit::<[T; N]>::uninit();
            let top: *mut T = &mut to as *mut _ as *mut T;
            for i in 0..N {
                top.add(i).write(ToWasm::read_to_wasm(inp));
            }
            to.assume_init()
        }
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, is_recur: bool, prop: &str, temp: usize) {
        out.push_ln(slot, &format!("let t{} = {}", temp, prop));
        out.push_ln(slot, &format!("for(let i{0} = 0; i{0} < {1}; i{0}++){{", temp, N));
        let new_temp = out.alloc_temp();
        T::to_wasm_js_body(out, slot, is_recur, &format!("t{0}[i{0}]", temp), new_temp);
        out.push_ln(slot, "}");
    }
}

impl<T> FromWasm for Vec<T> where T: FromWasm {
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        out.push_u32(self.len() as u32);
        for item in self {
            item.from_wasm_inner(out);
        }
    }
    
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, temp: usize) {
        out.push_ln(slot, &format!("let t{} = {} = [];", temp, prop));
        out.push_ln(slot, &format!("t{}.length = app.u32[this.u32_offset++];", temp));
        out.push_ln(slot, &format!("for(let i{0} = 0; i{0} < t{0}.length; i{0}++){{", temp));
        let new_temp = out.alloc_temp();
        T::from_wasm_js_body(out, slot, true, &format!("t{0}[i{0}]", temp), new_temp);
        out.push_ln(slot, "}");
    }
}

impl<T> ToWasm for Vec<T> where T: ToWasm {
    fn u32_size() -> usize {1}
    
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        let len = inp.read_u32();
        let mut ret = Vec::new();
        for _ in 0..len {
            ret.push(ToWasm::read_to_wasm(inp));
        }
        ret
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, temp: usize) {
        let item_size = T::u32_size();
        
        out.push_ln(slot, &format!("let t{} = {};", temp, prop));
        out.push_ln(slot, &format!("if(Array.isArray(t{})){{", temp));
        out.push_ln(slot, &format!("app.u32[this.u32_offset ++] = t{}.length;", temp));
        out.push_ln(slot, &format!("this.reserve_u32({} * t{}.length);", item_size, temp));
        out.push_ln(slot, &format!("for(let i{0} = 0; i{0} < t{0}.length; i{0}++){{", temp));
        let new_temp = out.alloc_temp();
        T::to_wasm_js_body(out, slot, true, &format!("t{0}[i{0}]", temp), new_temp);
        out.push_ln(slot, "}} else {");
        out.push_ln(slot, "   app.u32[this.u32_offset ++] = 0;");
        out.push_ln(slot, "}");
        
    }
}

impl<T> FromWasm for Box<T> where T: FromWasm {
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        (*self).from_wasm_inner(out);
    }
    
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, _temp: usize) {
        let new_temp = out.alloc_temp();
        T::from_wasm_js_body(out, slot, true, prop, new_temp);
    }
}

impl<T> ToWasm for Box<T> where T: ToWasm {
    fn u32_size() -> usize {0}
    
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        Self::new(ToWasm::read_to_wasm(inp))

    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, _is_recur: bool, prop: &str, temp: usize) {
        let item_size = T::u32_size();
        out.push_ln(slot, &format!("this.reserve_u32({});", item_size));
        T::to_wasm_js_body(out, slot, true, prop, temp);
    }
}

impl<T> FromWasm for Option<T> where T: FromWasm {
    fn from_wasm_inner(self, out: &mut FromWasmMsg) {
        if let Some(val) = self {
            out.push_u32(1);
            val.from_wasm_inner(out);
        }
        else {
            out.push_u32(0);
        }
    }
    
    fn from_wasm_js_body(out: &mut WasmJSOutput, slot: usize, is_recur: bool, prop: &str, _temp: usize) {
        out.push_ln(slot, "if(app.u32[this.u32_offset++] !== 0){");
        let new_temp = out.alloc_temp();
        T::from_wasm_js_body(out, slot, is_recur, prop, new_temp);
        out.push_ln(slot, "} else {");
        out.push_ln(slot, &format!("{} = undefined;", prop));
        out.push_ln(slot, "}");
    }
}

impl<T> ToWasm for Option<T> where T: ToWasm {
    fn u32_size() -> usize {1 + T::u32_size()}
    
    fn read_to_wasm(inp: &mut ToWasmMsgRef) -> Self {
        if inp.read_u32() == 0 {
            None
        }
        else {
            Some(ToWasm::read_to_wasm(inp))
        }
    }
    
    fn to_wasm_js_body(out: &mut WasmJSOutput, slot: usize, is_recur: bool, prop: &str, temp: usize) {
        out.push_ln(slot, &format!("if({0} === undefined){{", prop));
        out.push_ln(slot, "app.u32[this.u32_offset ++] = 0;");
        out.push_ln(slot, "} else {");
        out.push_ln(slot, "app.u32[this.u32_offset ++] = 1;");
        T::to_wasm_js_body(out, slot, is_recur, prop, temp);
        out.push_ln(slot, "}");
    }
}