wasm_val/js_ser/
mod.rs

1// Copyright 2019 Vladimir Iftodi <Vladimir.Iftodi@gmail.com>. 
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::io::Cursor;
10use ser_constants::TypeTag;
11
12use byteorder::{LittleEndian, WriteBytesExt,};
13
14use JsValue;
15
16mod js_val;
17mod num;
18mod num_slice;
19
20pub trait JsSerializable {
21    fn size(&self) -> u32;
22    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> ();
23}
24
25impl JsSerializable for bool {
26    fn size(&self) -> u32 { 1 }
27
28    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) {
29        match self {
30            false => cursor.write_u8(TypeTag::BoolFalse as u8).unwrap(),
31            true => cursor.write_u8(TypeTag::BoolTrue as u8).unwrap(),
32        }
33    }
34}
35
36impl<'a> JsSerializable for &'a str {
37    fn size(&self) -> u32 { 9 }
38
39    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> () {
40        cursor.write_u8(TypeTag::String as u8).unwrap();
41        cursor.write_u32::<LittleEndian>(self.len() as u32).unwrap();
42        cursor.write_u32::<LittleEndian>(self.as_ptr() as u32).unwrap();
43    }
44}
45
46impl<'a> JsSerializable for String {
47    fn size(&self) -> u32 { 9 }
48
49    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> () {
50        cursor.write_u8(TypeTag::String as u8).unwrap();
51        cursor.write_u32::<LittleEndian>(self.len() as u32).unwrap();
52        cursor.write_u32::<LittleEndian>(self.as_ptr() as u32).unwrap();
53    }
54}
55
56impl JsSerializable for &'static Fn() -> () {
57    fn size(&self) -> u32 { 5 }
58
59    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> () {
60        let id = super::register_callback(*self);
61
62        cursor.write_u8(TypeTag::Lambda as u8).unwrap();
63        cursor.write_u32::<LittleEndian>(id).unwrap();
64    }
65}
66
67impl JsSerializable for &'static Fn() -> (Box<JsSerializable>) {
68    fn size(&self) -> u32 { 5 }
69
70    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> () {
71        let id = super::register_callback_ret(*self);
72
73        cursor.write_u8(TypeTag::LambdaReturn as u8).unwrap();
74        cursor.write_u32::<LittleEndian>(id).unwrap();
75    }
76}
77
78impl JsSerializable for &'static Fn(JsValue) -> () {
79    fn size(&self) -> u32 { 5 }
80
81    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> () {
82        let id = super::register_callback_arg(*self);
83
84        cursor.write_u8(TypeTag::LambdaArg as u8).unwrap();
85        cursor.write_u32::<LittleEndian>(id).unwrap();
86    }
87}
88
89impl JsSerializable for &'static Fn(JsValue) -> (Box<JsSerializable>) {
90    fn size(&self) -> u32 { 5 }
91
92    fn ser(&self, cursor: &mut Cursor<Vec<u8>>) -> () {
93        let id = super::register_callback_ret_arg(*self);
94
95        cursor.write_u8(TypeTag::LambdaArgReturn as u8).unwrap();
96        cursor.write_u32::<LittleEndian>(id).unwrap();
97    }
98}