1#[no_mangle]
2pub extern "C" fn sapp_jsutils_crate_version() -> u32 {
3 1
4}
5
6#[repr(transparent)]
10pub struct JsObject(i32);
11
12impl JsObject {
13 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
32extern "C" {
34 fn js_create_string(buf: *const u8, max_len: u32) -> JsObject;
37
38 fn js_create_buffer(buf: *const u8, max_len: u32) -> JsObject;
40
41 fn js_create_object() -> JsObject;
44
45 fn js_free_object(js_object: JsObjectWeak);
47
48 fn js_unwrap_to_str(js_object: JsObjectWeak, buf: *mut u8, max_len: u32);
50
51 fn js_unwrap_to_buf(js_object: JsObjectWeak, buf: *mut u8, max_len: u32);
53
54 fn js_string_length(js_object: JsObjectWeak) -> u32;
57 fn js_buf_length(js_object: JsObjectWeak) -> u32;
58
59 fn js_have_field(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> bool;
61
62 fn js_field(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> JsObject;
64
65 fn js_field_f32(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> f32;
67
68 fn js_field_u32(js_object: JsObjectWeak, buf: *mut u8, len: u32) -> u32;
70
71 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 fn js_set_field_f32(js_object: JsObjectWeak, buf: *mut u8, len: u32, data: f32);
82
83 fn js_set_field_u32(js_object: JsObjectWeak, buf: *mut u8, len: u32, data: u32);
85
86}
87
88impl JsObject {
89 pub fn string(string: &str) -> JsObject {
91 unsafe { js_create_string(string.as_ptr() as _, string.len() as _) }
92 }
93
94 pub fn buffer(data: &[u8]) -> JsObject {
96 unsafe { js_create_buffer(data.as_ptr() as _, data.len() as _) }
97 }
98
99 pub fn object() -> JsObject {
101 unsafe { js_create_object() }
102 }
103
104 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 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 pub fn field(&self, field: &str) -> JsObject {
129 unsafe { js_field(self.weak(), field.as_ptr() as _, field.len() as _) }
130 }
131
132 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 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 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 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 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 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 pub fn is_nil(&self) -> bool {
178 self.0 == Self::NULL.0
179 }
180
181 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}