Skip to content

Commit 82825a8

Browse files
authored
chore(tesseract): Native utils refactoring (#9685)
1 parent 1aeea98 commit 82825a8

File tree

12 files changed

+581
-330
lines changed

12 files changed

+581
-330
lines changed

packages/cubejs-backend-native/src/node_export.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,15 @@ pub fn reset_logger(mut cx: FunctionContext) -> JsResult<JsUndefined> {
604604

605605
fn build_sql_and_params(cx: FunctionContext) -> JsResult<JsValue> {
606606
neon_run_with_guarded_lifetime(cx, |neon_context_holder| {
607-
let options =
608-
NativeObjectHandle::<NeonInnerTypes<FunctionContext<'static>>>::new(NeonObject::new(
607+
let options = NativeObjectHandle::<NeonInnerTypes<FunctionContext<'static>>>::new(
608+
NeonObject::new(
609609
neon_context_holder.clone(),
610610
neon_context_holder
611611
.with_context(|cx| cx.argument::<JsValue>(0))
612612
.unwrap()?,
613-
));
613+
)
614+
.unwrap(),
615+
);
614616

615617
let safe_call_fn = neon_context_holder
616618
.with_context(|cx| {
@@ -635,7 +637,7 @@ fn build_sql_and_params(cx: FunctionContext) -> JsResult<JsValue> {
635637
let res = base_query.build_sql_and_params();
636638

637639
let result: NeonObject<FunctionContext<'static>> = res.into_object();
638-
let result = result.into_object();
640+
let result = result.get_object().unwrap();
639641
Ok(result)
640642
})
641643
}

rust/cubenativeutils/src/wrappers/neon/context.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,47 +229,47 @@ impl<C: Context<'static> + 'static> NativeContext<NeonInnerTypes<C>> for Context
229229
let obj = NeonObject::new(
230230
self.clone(),
231231
self.with_context(|cx| cx.boolean(v).upcast())?,
232-
);
232+
)?;
233233
obj.into_boolean()
234234
}
235235

236236
fn string(&self, v: String) -> Result<NeonString<C>, CubeError> {
237-
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.string(v).upcast())?);
237+
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.string(v).upcast())?)?;
238238
obj.into_string()
239239
}
240240

241241
fn number(&self, v: f64) -> Result<NeonNumber<C>, CubeError> {
242-
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.number(v).upcast())?);
242+
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.number(v).upcast())?)?;
243243
obj.into_number()
244244
}
245245

246246
fn undefined(&self) -> Result<NativeObjectHandle<NeonInnerTypes<C>>, CubeError> {
247247
Ok(NativeObjectHandle::new(NeonObject::new(
248248
self.clone(),
249249
self.with_context(|cx| cx.undefined().upcast())?,
250-
)))
250+
)?))
251251
}
252252

253253
fn null(&self) -> Result<NativeObjectHandle<NeonInnerTypes<C>>, CubeError> {
254254
Ok(NativeObjectHandle::new(NeonObject::new(
255255
self.clone(),
256256
self.with_context(|cx| cx.null().upcast())?,
257-
)))
257+
)?))
258258
}
259259

260260
fn empty_array(&self) -> Result<NeonArray<C>, CubeError> {
261261
let obj = NeonObject::new(
262262
self.clone(),
263263
self.with_context(|cx| cx.empty_array().upcast())?,
264-
);
264+
)?;
265265
obj.into_array()
266266
}
267267

268268
fn empty_struct(&self) -> Result<NeonStruct<C>, CubeError> {
269269
let obj = NeonObject::new(
270270
self.clone(),
271271
self.with_context(|cx| cx.empty_object().upcast())?,
272-
);
272+
)?;
273273
obj.into_struct()
274274
}
275275
fn to_string_fn(&self, result: String) -> Result<NeonFunction<C>, CubeError> {
@@ -280,7 +280,7 @@ impl<C: Context<'static> + 'static> NativeContext<NeonInnerTypes<C>> for Context
280280
.unwrap()
281281
.upcast()
282282
})?,
283-
);
283+
)?;
284284
obj.into_function()
285285
}
286286
}
Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,84 @@
1-
use super::{NeonObject, NeonTypeHandle};
1+
use super::primitive_root_holder::*;
2+
use super::{NeonObject, RootHolder};
23
use crate::wrappers::neon::inner_types::NeonInnerTypes;
3-
use std::marker::PhantomData;
44

5-
use crate::wrappers::object::{NativeBoolean, NativeBox, NativeNumber, NativeString, NativeType};
5+
use crate::wrappers::object::{NativeBoolean, NativeNumber, NativeString, NativeType};
66
use cubesql::CubeError;
77
use neon::prelude::*;
8-
use std::ops::Deref;
98

109
pub struct NeonString<C: Context<'static>> {
11-
object: NeonTypeHandle<C, JsString>,
10+
holder: PrimitiveNeonTypeHolder<C, JsString>,
1211
}
1312

1413
impl<C: Context<'static>> NeonString<C> {
15-
pub fn new(object: NeonTypeHandle<C, JsString>) -> Self {
16-
Self { object }
14+
pub fn new(holder: PrimitiveNeonTypeHolder<C, JsString>) -> Self {
15+
Self { holder }
1716
}
1817
}
1918

2019
impl<C: Context<'static> + 'static> NativeType<NeonInnerTypes<C>> for NeonString<C> {
2120
fn into_object(self) -> NeonObject<C> {
22-
self.object.upcast()
21+
let root_holder = RootHolder::from_typed(self.holder);
22+
NeonObject::form_root(root_holder)
2323
}
2424
}
2525

2626
impl<C: Context<'static> + 'static> NativeString<NeonInnerTypes<C>> for NeonString<C> {
2727
fn value(&self) -> Result<String, CubeError> {
28-
self.object
28+
self.holder
2929
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))?
3030
}
3131
}
3232

3333
pub struct NeonNumber<C: Context<'static>> {
34-
object: NeonTypeHandle<C, JsNumber>,
34+
holder: PrimitiveNeonTypeHolder<C, JsNumber>,
3535
}
3636

3737
impl<C: Context<'static>> NeonNumber<C> {
38-
pub fn new(object: NeonTypeHandle<C, JsNumber>) -> Self {
39-
Self { object }
38+
pub fn new(holder: PrimitiveNeonTypeHolder<C, JsNumber>) -> Self {
39+
Self { holder }
4040
}
4141
}
4242

4343
impl<C: Context<'static> + 'static> NativeType<NeonInnerTypes<C>> for NeonNumber<C> {
4444
fn into_object(self) -> NeonObject<C> {
45-
self.object.upcast()
45+
let root_holder = RootHolder::from_typed(self.holder);
46+
NeonObject::form_root(root_holder)
4647
}
4748
}
4849

4950
impl<C: Context<'static> + 'static> NativeNumber<NeonInnerTypes<C>> for NeonNumber<C> {
5051
fn value(&self) -> Result<f64, CubeError> {
51-
self.object
52+
self.holder
5253
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))?
5354
}
5455
}
5556

5657
pub struct NeonBoolean<C: Context<'static>> {
57-
object: NeonTypeHandle<C, JsBoolean>,
58+
holder: PrimitiveNeonTypeHolder<C, JsBoolean>,
5859
}
5960

6061
impl<C: Context<'static>> NeonBoolean<C> {
61-
pub fn new(object: NeonTypeHandle<C, JsBoolean>) -> Self {
62-
Self { object }
62+
pub fn new(holder: PrimitiveNeonTypeHolder<C, JsBoolean>) -> Self {
63+
Self { holder }
6364
}
6465
}
6566

6667
impl<C: Context<'static> + 'static> NativeType<NeonInnerTypes<C>> for NeonBoolean<C> {
6768
fn into_object(self) -> NeonObject<C> {
68-
self.object.upcast()
69+
let root_holder = RootHolder::from_typed(self.holder);
70+
NeonObject::form_root(root_holder)
6971
}
7072
}
7173

7274
impl<C: Context<'static> + 'static> NativeBoolean<NeonInnerTypes<C>> for NeonBoolean<C> {
7375
fn value(&self) -> Result<bool, CubeError> {
74-
self.object
76+
self.holder
7577
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))?
7678
}
7779
}
7880

79-
pub struct NeonBox<C: Context<'static>, T: 'static> {
81+
/* pub struct NeonBox<C: Context<'static>, T: 'static> {
8082
object: NeonTypeHandle<C, JsBox<T>>,
8183
_marker: PhantomData<T>,
8284
}
@@ -100,4 +102,4 @@ impl<C: Context<'static> + 'static, T: 'static> NativeBox<NeonInnerTypes<C>, T>
100102
fn deref_value(&self) -> &T {
101103
self.object.get_object_ref().deref()
102104
}
103-
}
105+
} */
Lines changed: 9 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,13 @@
11
pub mod base_types;
22
pub mod neon_array;
33
pub mod neon_function;
4+
pub mod neon_object;
45
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

Comments
 (0)