|
1 | 1 | pub mod base_types; |
2 | 2 | pub mod neon_array; |
3 | 3 | pub mod neon_function; |
| 4 | +pub mod neon_object; |
4 | 5 | pub mod neon_struct; |
5 | | - |
6 | | -use self::{ |
7 | | - base_types::{NeonBoolean, NeonNumber, NeonString}, |
8 | | - neon_array::NeonArray, |
9 | | - neon_function::NeonFunction, |
10 | | - neon_struct::NeonStruct, |
11 | | -}; |
12 | | -use super::inner_types::NeonInnerTypes; |
13 | | -use crate::wrappers::{ |
14 | | - neon::context::{ContextHolder, SafeCallFn}, |
15 | | - object::NativeObject, |
16 | | -}; |
17 | | -use cubesql::CubeError; |
18 | | -use neon::prelude::*; |
19 | | - |
20 | | -pub struct NeonTypeHandle<C: Context<'static>, V: Value + 'static> { |
21 | | - context: ContextHolder<C>, |
22 | | - object: Handle<'static, V>, |
23 | | -} |
24 | | - |
25 | | -impl<C: Context<'static> + 'static, V: Value + 'static> NeonTypeHandle<C, V> { |
26 | | - pub fn new(context: ContextHolder<C>, object: Handle<'static, V>) -> Self { |
27 | | - Self { context, object } |
28 | | - } |
29 | | - |
30 | | - fn get_context(&self) -> ContextHolder<C> { |
31 | | - self.context.clone() |
32 | | - } |
33 | | - |
34 | | - pub fn get_object(&self) -> Handle<'static, V> { |
35 | | - self.object |
36 | | - } |
37 | | - |
38 | | - pub fn get_object_ref(&self) -> &Handle<'static, V> { |
39 | | - &self.object |
40 | | - } |
41 | | - |
42 | | - pub fn into_object(self) -> Handle<'static, V> { |
43 | | - self.object |
44 | | - } |
45 | | - |
46 | | - pub fn upcast(&self) -> NeonObject<C> { |
47 | | - NeonObject::new(self.context.clone(), self.object.upcast()) |
48 | | - } |
49 | | - |
50 | | - pub fn map_neon_object<T, F>(&self, f: F) -> Result<T, CubeError> |
51 | | - where |
52 | | - F: FnOnce(&mut C, &Handle<'static, V>) -> T, |
53 | | - { |
54 | | - self.context.with_context(|cx| f(cx, &self.object)) |
55 | | - } |
56 | | - |
57 | | - pub fn map_downcast_neon_object<JT: Value, T, F>(&self, f: F) -> Result<T, CubeError> |
58 | | - where |
59 | | - F: FnOnce(&mut C, &Handle<'static, JT>) -> Result<T, CubeError>, |
60 | | - { |
61 | | - self.context.with_context(|cx| { |
62 | | - let obj = self |
63 | | - .object |
64 | | - .downcast::<JT, _>(cx) |
65 | | - .map_err(|_| CubeError::internal("Downcast error".to_string()))?; |
66 | | - f(cx, &obj) |
67 | | - })? |
68 | | - } |
69 | | - |
70 | | - pub fn map_neon_object_with_safe_call_fn<T, F>(&self, f: F) -> Result<T, CubeError> |
71 | | - where |
72 | | - F: FnOnce(&mut C, &Handle<'static, V>, SafeCallFn) -> T, |
73 | | - { |
74 | | - self.context |
75 | | - .with_context_and_safe_fn(|cx, safe_call_fn| f(cx, &self.object, safe_call_fn)) |
76 | | - } |
77 | | - |
78 | | - pub fn is_a<U: Value>(&self) -> Result<bool, CubeError> { |
79 | | - self.context.with_context(|cx| self.object.is_a::<U, _>(cx)) |
80 | | - } |
81 | | -} |
82 | | - |
83 | | -impl<C: Context<'static>, V: Value + 'static> Clone for NeonTypeHandle<C, V> { |
84 | | - fn clone(&self) -> Self { |
85 | | - Self { |
86 | | - context: self.context.clone(), |
87 | | - object: self.object, |
88 | | - } |
89 | | - } |
90 | | -} |
91 | | - |
92 | | -pub struct NeonObject<C: Context<'static>> { |
93 | | - context: ContextHolder<C>, |
94 | | - object: Handle<'static, JsValue>, |
95 | | -} |
96 | | - |
97 | | -impl<C: Context<'static> + 'static> NeonObject<C> { |
98 | | - pub fn new(context: ContextHolder<C>, object: Handle<'static, JsValue>) -> Self { |
99 | | - Self { context, object } |
100 | | - } |
101 | | - |
102 | | - pub fn get_object(&self) -> Handle<'static, JsValue> { |
103 | | - self.object |
104 | | - } |
105 | | - |
106 | | - pub fn get_object_ref(&self) -> &Handle<'static, JsValue> { |
107 | | - &self.object |
108 | | - } |
109 | | - |
110 | | - pub fn into_object(self) -> Handle<'static, JsValue> { |
111 | | - self.object |
112 | | - } |
113 | | - |
114 | | - pub fn is_a<U: Value>(&self) -> Result<bool, CubeError> { |
115 | | - self.context.with_context(|cx| self.object.is_a::<U, _>(cx)) |
116 | | - } |
117 | | - |
118 | | - pub fn downcast<U: Value>(&self) -> Result<NeonTypeHandle<C, U>, CubeError> { |
119 | | - let obj = self.context.with_context(|cx| { |
120 | | - self.object |
121 | | - .downcast::<U, _>(cx) |
122 | | - .map_err(|_| CubeError::internal("Downcast error".to_string())) |
123 | | - })??; |
124 | | - Ok(NeonTypeHandle::new(self.context.clone(), obj)) |
125 | | - } |
126 | | - |
127 | | - pub fn downcast_with_err_msg<U: Value>( |
128 | | - &self, |
129 | | - msg: &str, |
130 | | - ) -> Result<NeonTypeHandle<C, U>, CubeError> { |
131 | | - let obj = self.context.with_context(|cx| { |
132 | | - self.object |
133 | | - .downcast::<U, _>(cx) |
134 | | - .map_err(|_| CubeError::internal(msg.to_string())) |
135 | | - })??; |
136 | | - Ok(NeonTypeHandle::new(self.context.clone(), obj)) |
137 | | - } |
138 | | -} |
139 | | - |
140 | | -impl<C: Context<'static> + 'static> NativeObject<NeonInnerTypes<C>> for NeonObject<C> { |
141 | | - fn get_context(&self) -> ContextHolder<C> { |
142 | | - self.context.clone() |
143 | | - } |
144 | | - |
145 | | - fn into_struct(self) -> Result<NeonStruct<C>, CubeError> { |
146 | | - let obj = self.downcast_with_err_msg::<JsObject>("NeonObject is not the JsObject")?; |
147 | | - Ok(NeonStruct::new(obj)) |
148 | | - } |
149 | | - fn into_function(self) -> Result<NeonFunction<C>, CubeError> { |
150 | | - let obj = self.downcast_with_err_msg::<JsFunction>("NeonObject is not the JsArray")?; |
151 | | - Ok(NeonFunction::new(obj)) |
152 | | - } |
153 | | - fn into_array(self) -> Result<NeonArray<C>, CubeError> { |
154 | | - let obj = self.downcast_with_err_msg::<JsArray>("NeonObject is not the JsArray")?; |
155 | | - Ok(NeonArray::new(obj)) |
156 | | - } |
157 | | - fn into_string(self) -> Result<NeonString<C>, CubeError> { |
158 | | - let obj = self.downcast_with_err_msg::<JsString>("NeonObject is not the JsString")?; |
159 | | - Ok(NeonString::new(obj)) |
160 | | - } |
161 | | - fn into_number(self) -> Result<NeonNumber<C>, CubeError> { |
162 | | - let obj = self.downcast_with_err_msg::<JsNumber>("NeonObject is not the JsNumber")?; |
163 | | - Ok(NeonNumber::new(obj)) |
164 | | - } |
165 | | - fn into_boolean(self) -> Result<NeonBoolean<C>, CubeError> { |
166 | | - let obj = self.downcast_with_err_msg::<JsBoolean>("NeonObject is not the JsBoolean")?; |
167 | | - Ok(NeonBoolean::new(obj)) |
168 | | - } |
169 | | - |
170 | | - fn is_null(&self) -> Result<bool, CubeError> { |
171 | | - self.is_a::<JsNull>() |
172 | | - } |
173 | | - |
174 | | - fn is_undefined(&self) -> Result<bool, CubeError> { |
175 | | - self.is_a::<JsUndefined>() |
176 | | - } |
177 | | -} |
178 | | - |
179 | | -impl<C: Context<'static>> Clone for NeonObject<C> { |
180 | | - fn clone(&self) -> Self { |
181 | | - Self { |
182 | | - context: self.context.clone(), |
183 | | - object: self.object, |
184 | | - } |
185 | | - } |
186 | | -} |
| 6 | +pub mod object_root_holder; |
| 7 | +pub mod primitive_root_holder; |
| 8 | +pub mod root_holder; |
| 9 | + |
| 10 | +pub use neon_object::*; |
| 11 | +use object_root_holder::*; |
| 12 | +use primitive_root_holder::*; |
| 13 | +use root_holder::*; |
0 commit comments