sapp_jsutils/
lib.rs

1#[no_mangle]
2pub extern "C" fn sapp_jsutils_crate_version() -> u32 {
3	1
4}
5
6/// Pointer type for Js allocated object
7/// Consider this as a Box, but pointing into JS memory
8/// -1 is nil
9#[repr(transparent)]
10pub struct JsObject(i32);
11
12impl JsObject {
13    /// Get a weak reference to js memory
14    /// No guarantees against js garbage collector
15    pub fn weak(&self) -> JsObjectWeak {
16        JsObjectWeak(self.0)
17    }
18}
19
20#[derive(Clone, Copy)]
21#[repr(transparent)]
22pub struct JsObjectWeak(i32);
23
24impl Drop for JsObject {
25    fn drop(&mut self) {
26        unsafe {
27            js_free_object(self.weak());
28        }
29    }
30}
31
32// Private unsafe JS api
33extern "C" {
34    /// Allocate new js object with data providen.
35    /// Returned JsObject is safe to use and will follow usual JsObject ownership rules
36    fn js_create_string(buf: *const u8, max_len: u32) -> JsObject;
37
38    /// Same as create_string, on both api and implementation, however will not perform UTF8 conversion on the JS side.
39    fn js_create_buffer(buf: *const u8, max_len: u32) -> JsObject;
40
41    /// Allocate new empty js object. Like "var obj = {};".
42    /// Returned JsObject is safe to use and will follow usual JsObject ownership rules
43    fn js_create_object() -> JsObject;
44
45    /// This will not delete or delallocate JS object, but will stop saving it from JS garbage collector
46    fn js_free_object(js_object: JsObjectWeak);
47
48    /// Will read object byte by byte into rust memory assuming that object is an array
49    fn js_unwrap_to_str(js_object: JsObjectWeak, buf: *mut u8, max_len: u32);
50
51    /// Will read object byte by byte into rust memory assuming that object is an array
52    fn js_unwrap_to_buf(js_object: JsObjectWeak, buf: *mut u8, max_len: u32);
53
54    // Will panic if js_object is not a string
55    // Will calculate the length of string bytes representation
56    fn js_string_length(js_object: JsObjectWeak) -> u32;
57    fn js_buf_length(js_object: JsObjectWeak) -> u32;
58
59    /// .field or ["field"] == undefined
60    fn js_have_field(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> bool;
61
62    /// Get .field or ["field"] of given JsObject
63    fn js_field(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> JsObject;
64
65    /// Get a numerical value of .field or ["field"] of given JsObject
66    fn js_field_f32(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> f32;
67
68    /// Get a u32 value of .field or ["field"] of given JsObject
69    fn js_field_u32(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> u32;
70
71    /// Set .field or ["field"] to given string, like "object.field = "data"";
72    fn js_set_field_string(
73        js_object: JsObjectWeak,
74        buf: *mut u8,
75        len: u32,
76        data_buf: *mut u8,
77        data_len: u32,
78    );
79
80    /// Set .field or ["field"] to given f32, like "object.field = data";
81    fn js_set_field_f32(js_object: JsObjectWeak, buf: *mut u8, len: u32, data: f32);
82
83    /// Set .field or ["field"] to given u32, like "object.field = data";
84    fn js_set_field_u32(js_object: JsObjectWeak, buf: *mut u8, len: u32, data: u32);
85
86}
87
88impl JsObject {
89    /// Allocate new javascript object with string type
90    pub fn string(string: &str) -> JsObject {
91        unsafe { js_create_string(string.as_ptr() as _, string.len() as _) }
92    }
93
94    /// Allocate new javascript object with Uint8Array type
95    pub fn buffer(data: &[u8]) -> JsObject {
96        unsafe { js_create_buffer(data.as_ptr() as _, data.len() as _) }
97    }
98
99    /// Allocate new javascript object with object type. Like "var object = {}" in JS.
100    pub fn object() -> JsObject {
101        unsafe { js_create_object() }
102    }
103
104    /// Read JS object content to a given string
105    /// Will panic if object is not a string
106    /// Will not allocate memory if string is large enough, will use "String::reserve" otherwise
107    pub fn to_string(&self, buf: &mut String) {
108        let len = unsafe { js_string_length(self.weak()) };
109
110        if len as usize > buf.len() {
111            buf.reserve(len as usize - buf.len());
112        }
113        unsafe { buf.as_mut_vec().set_len(len as usize) };
114        unsafe { js_unwrap_to_str(self.weak(), buf.as_mut_vec().as_mut_ptr(), len as u32) };
115    }
116
117    /// Read JS object content to a given bytes buffer
118    /// Will panic if object is not a buffer
119    /// Will use .resize() on "buf", so if "buf" is large enough - no memory is going to be allocated here
120    pub fn to_byte_buffer(&self, buf: &mut Vec<u8>) {
121        let len = unsafe { js_buf_length(self.weak()) };
122        buf.resize(len as usize, 0u8);
123        unsafe { js_unwrap_to_buf(self.weak(), buf.as_mut_ptr(), len as u32) };
124    }
125
126    /// Get a new JsObject from this object .field
127    /// Will panic if self is not an object or map
128    pub fn field(&self, field: &str) -> JsObject {
129        unsafe { js_field(self.weak(), field.as_ptr() as _, field.len() as _) }
130    }
131
132    /// Get a value from this object .field
133    /// Will panic if self is not an object or map
134    pub fn field_u32(&self, field: &str) -> u32 {
135        unsafe { js_field_u32(self.weak(), field.as_ptr() as _, field.len() as _) }
136    }
137
138    /// .field == undefined
139    pub fn have_field(&self, field: &str) -> bool {
140        unsafe { js_have_field(self.weak(), field.as_ptr() as _, field.len() as _) }
141    }
142
143    /// Get a value from this object .field
144    /// Will panic if self is not an object or map
145    pub fn field_f32(&self, field: &str) -> f32 {
146        unsafe { js_field_f32(self.weak(), field.as_ptr() as _, field.len() as _) }
147    }
148
149    /// Set .field or ["field"] to given f32, like "object.field = data";
150    /// Will panic if self is not an object or map
151    pub fn set_field_f32(&self, field: &str, data: f32) {
152        unsafe { js_set_field_f32(self.weak(), field.as_ptr() as _, field.len() as _, data) }
153    }
154
155    /// Set .field or ["field"] to given u32, like "object.field = data";
156    /// Will panic if self is not an object or map
157    pub fn set_field_u32(&self, field: &str, data: u32) {
158        unsafe { js_set_field_u32(self.weak(), field.as_ptr() as _, field.len() as _, data) }
159    }
160
161    /// Set .field or ["field"] to given string, like "object.field = data";
162    /// Will panic if self is not an object or map
163
164    pub fn set_field_string(&self, field: &str, data: &str) {
165        unsafe {
166            js_set_field_string(
167                self.weak(),
168                field.as_ptr() as _,
169                field.len() as _,
170                data.as_ptr() as _,
171                data.len() as _,
172            )
173        }
174    }
175
176    /// Checks whether the JsObject handle is actually null
177    pub fn is_nil(&self) -> bool {
178        self.0 == Self::NULL.0
179    }
180
181    /// Checks whether the JsObject handle is undefined
182    pub fn is_undefined(&self) -> bool {
183        self.0 == Self::UNDEFINED.0
184    }
185
186    pub const NULL: Self = Self(-1);
187    pub const UNDEFINED: Self = Self(-2);
188}