Skip to content

Commit 4df213c

Browse files
committed
in work
1 parent 542859a commit 4df213c

File tree

9 files changed

+189
-136
lines changed

9 files changed

+189
-136
lines changed

rust/cubenativeutils/src/wrappers/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::inner_types::InnerTypes;
2+
use super::object::NativeBox;
23
use super::object_handle::NativeObjectHandle;
34

45
pub trait NativeContext<IT: InnerTypes>: Clone {
@@ -8,6 +9,7 @@ pub trait NativeContext<IT: InnerTypes>: Clone {
89
fn undefined(&self) -> NativeObjectHandle<IT>;
910
fn empty_array(&self) -> IT::Array;
1011
fn empty_struct(&self) -> IT::Struct;
12+
//fn boxed<T: 'static>(&self, value: T) -> impl NativeBox<IT, T>;
1113
fn to_string_fn(&self, result: String) -> IT::Function;
1214
}
1315

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ impl<'cx, C: Context<'cx> + 'cx> ContextHolder<'cx, C> {
6161
context.with_context(f)
6262
}
6363

64-
/* pub fn as_native_context_holder(&self) -> NativeContextHolder {
65-
NativeContextHolder::new(Box::new(self.clone()))
66-
} */
67-
6864
pub fn weak(&self) -> WeakContextHolder<'cx, C> {
6965
WeakContextHolder {
7066
context: Rc::downgrade(&self.context),
Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,107 @@
1-
use super::NeonObject;
1+
use super::{NeonObject, NeonTypeHandle};
22
use crate::wrappers::neon::inner_types::NeonInnerTypes;
3+
use std::marker::PhantomData;
34

4-
use crate::wrappers::object::{NativeBoolean, NativeNumber, NativeString, NativeType};
5+
use crate::wrappers::object::{NativeBoolean, NativeBox, NativeNumber, NativeString, NativeType};
56
use cubesql::CubeError;
67
use neon::prelude::*;
8+
use std::ops::Deref;
79

810
pub struct NeonString<'cx: 'static, C: Context<'cx>> {
9-
object: NeonObject<'cx, C>,
11+
object: NeonTypeHandle<'cx, C, JsString>,
1012
}
1113

1214
impl<'cx, C: Context<'cx>> NeonString<'cx, C> {
13-
pub fn new(object: NeonObject<'cx, C>) -> Self {
15+
pub fn new(object: NeonTypeHandle<'cx, C, JsString>) -> Self {
1416
Self { object }
1517
}
1618
}
1719

1820
impl<'cx, C: Context<'cx> + 'cx> NativeType<NeonInnerTypes<'cx, C>> for NeonString<'cx, C> {
1921
fn into_object(self) -> NeonObject<'cx, C> {
20-
self.object
22+
self.object.upcast()
2123
}
2224
}
2325

2426
impl<'cx, C: Context<'cx> + 'cx> NativeString<NeonInnerTypes<'cx, C>> for NeonString<'cx, C> {
2527
fn value(&self) -> Result<String, CubeError> {
2628
self.object
27-
.map_downcast_neon_object::<JsString, _, _>(|cx, object| Ok(object.value(cx)))
29+
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))
2830
}
2931
}
3032

3133
pub struct NeonNumber<'cx: 'static, C: Context<'cx>> {
32-
object: NeonObject<'cx, C>,
34+
object: NeonTypeHandle<'cx, C, JsNumber>,
3335
}
3436

3537
impl<'cx, C: Context<'cx>> NeonNumber<'cx, C> {
36-
pub fn new(object: NeonObject<'cx, C>) -> Self {
38+
pub fn new(object: NeonTypeHandle<'cx, C, JsNumber>) -> Self {
3739
Self { object }
3840
}
3941
}
4042

4143
impl<'cx, C: Context<'cx> + 'cx> NativeType<NeonInnerTypes<'cx, C>> for NeonNumber<'cx, C> {
4244
fn into_object(self) -> NeonObject<'cx, C> {
43-
self.object
45+
self.object.upcast()
4446
}
4547
}
4648

4749
impl<'cx, C: Context<'cx> + 'cx> NativeNumber<NeonInnerTypes<'cx, C>> for NeonNumber<'cx, C> {
4850
fn value(&self) -> Result<f64, CubeError> {
4951
self.object
50-
.map_downcast_neon_object::<JsNumber, _, _>(|cx, object| Ok(object.value(cx)))
52+
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))
5153
}
5254
}
5355

5456
pub struct NeonBoolean<'cx: 'static, C: Context<'cx>> {
55-
object: NeonObject<'cx, C>,
57+
object: NeonTypeHandle<'cx, C, JsBoolean>,
5658
}
5759

5860
impl<'cx, C: Context<'cx>> NeonBoolean<'cx, C> {
59-
pub fn new(object: NeonObject<'cx, C>) -> Self {
61+
pub fn new(object: NeonTypeHandle<'cx, C, JsBoolean>) -> Self {
6062
Self { object }
6163
}
6264
}
6365

6466
impl<'cx, C: Context<'cx> + 'cx> NativeType<NeonInnerTypes<'cx, C>> for NeonBoolean<'cx, C> {
6567
fn into_object(self) -> NeonObject<'cx, C> {
66-
self.object
68+
self.object.upcast()
6769
}
6870
}
6971

7072
impl<'cx, C: Context<'cx> + 'cx> NativeBoolean<NeonInnerTypes<'cx, C>> for NeonBoolean<'cx, C> {
7173
fn value(&self) -> Result<bool, CubeError> {
7274
self.object
73-
.map_downcast_neon_object::<JsBoolean, _, _>(|cx, object| Ok(object.value(cx)))
75+
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))
76+
}
77+
}
78+
79+
pub struct NeonBox<'cx: 'static, C: Context<'cx>, T: 'static> {
80+
object: NeonTypeHandle<'cx, C, JsBox<T>>,
81+
_marker: PhantomData<T>,
82+
}
83+
84+
impl<'cx: 'static, C: Context<'cx>, T: 'static> NeonBox<'cx, C, T> {
85+
pub fn new(object: NeonTypeHandle<'cx, C, JsBox<T>>) -> Self {
86+
Self {
87+
object,
88+
_marker: PhantomData::default(),
89+
}
90+
}
91+
}
92+
93+
impl<'cx: 'static, C: Context<'cx> + 'cx, T: 'static> NativeType<NeonInnerTypes<'cx, C>>
94+
for NeonBox<'cx, C, T>
95+
{
96+
fn into_object(self) -> NeonObject<'cx, C> {
97+
self.object.upcast()
98+
}
99+
}
100+
101+
impl<'cx: 'static, C: Context<'cx> + 'cx, T: 'static> NativeBox<NeonInnerTypes<'cx, C>, T>
102+
for NeonBox<'cx, C, T>
103+
{
104+
fn deref_value(&self) -> &T {
105+
self.object.get_object_ref().deref()
74106
}
75107
}

rust/cubenativeutils/src/wrappers/neon/object/mod.rs

Lines changed: 86 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,43 @@ use crate::wrappers::object::NativeObject;
1313
use cubesql::CubeError;
1414
use neon::prelude::*;
1515

16-
pub struct NeonObject<'cx: 'static, C: Context<'cx>> {
16+
pub struct NeonTypeHandle<'cx, C: Context<'cx>, V: Value + 'cx> {
1717
context: ContextHolder<'cx, C>,
18-
object: Handle<'cx, JsValue>,
18+
object: Handle<'cx, V>,
1919
}
2020

21-
impl<'cx: 'static, C: Context<'cx> + 'cx> NeonObject<'cx, C> {
22-
pub fn new(context: ContextHolder<'cx, C>, object: Handle<'cx, JsValue>) -> Self {
21+
impl<'cx: 'static, C: Context<'cx> + 'cx, V: Value + 'cx> NeonTypeHandle<'cx, C, V> {
22+
pub fn new(context: ContextHolder<'cx, C>, object: Handle<'cx, V>) -> Self {
2323
Self { context, object }
2424
}
2525

26-
pub fn get_object(&self) -> Handle<'cx, JsValue> {
26+
fn get_context(&self) -> ContextHolder<'cx, C> {
27+
self.context.clone()
28+
}
29+
30+
pub fn get_object(&self) -> Handle<'cx, V> {
2731
self.object.clone()
2832
}
2933

30-
pub fn get_object_ref(&self) -> &Handle<'cx, JsValue> {
34+
pub fn get_object_ref(&self) -> &Handle<'cx, V> {
3135
&self.object
3236
}
3337

34-
pub fn into_object(self) -> Handle<'cx, JsValue> {
38+
pub fn into_object(self) -> Handle<'cx, V> {
3539
self.object
3640
}
3741

42+
pub fn upcast(&self) -> NeonObject<'cx, C> {
43+
NeonObject::new(self.context.clone(), self.object.upcast())
44+
}
45+
3846
pub fn map_neon_object<T, F>(&self, f: F) -> T
3947
where
40-
F: FnOnce(&mut C, &Handle<'cx, JsValue>) -> T,
48+
F: FnOnce(&mut C, &Handle<'cx, V>) -> T,
4149
{
4250
self.context.with_context(|cx| f(cx, &self.object))
4351
}
52+
4453
pub fn map_downcast_neon_object<JT: Value, T, F>(&self, f: F) -> Result<T, CubeError>
4554
where
4655
F: FnOnce(&mut C, &Handle<'cx, JT>) -> Result<T, CubeError>,
@@ -59,6 +68,63 @@ impl<'cx: 'static, C: Context<'cx> + 'cx> NeonObject<'cx, C> {
5968
}
6069
}
6170

71+
impl<'cx: 'static, C: Context<'cx>, V: Value + 'cx> Clone for NeonTypeHandle<'cx, C, V> {
72+
fn clone(&self) -> Self {
73+
Self {
74+
context: self.context.clone(),
75+
object: self.object.clone(),
76+
}
77+
}
78+
}
79+
80+
pub struct NeonObject<'cx: 'static, C: Context<'cx>> {
81+
context: ContextHolder<'cx, C>,
82+
object: Handle<'cx, JsValue>,
83+
}
84+
85+
impl<'cx: 'static, C: Context<'cx> + 'cx> NeonObject<'cx, C> {
86+
pub fn new(context: ContextHolder<'cx, C>, object: Handle<'cx, JsValue>) -> Self {
87+
Self { context, object }
88+
}
89+
90+
pub fn get_object(&self) -> Handle<'cx, JsValue> {
91+
self.object.clone()
92+
}
93+
94+
pub fn get_object_ref(&self) -> &Handle<'cx, JsValue> {
95+
&self.object
96+
}
97+
98+
pub fn into_object(self) -> Handle<'cx, JsValue> {
99+
self.object
100+
}
101+
102+
pub fn is_a<U: Value>(&self) -> bool {
103+
self.context.with_context(|cx| self.object.is_a::<U, _>(cx))
104+
}
105+
106+
pub fn downcast<U: Value>(&self) -> Result<NeonTypeHandle<'cx, C, U>, CubeError> {
107+
let obj = self.context.with_context(|cx| {
108+
self.object
109+
.downcast::<U, _>(cx)
110+
.map_err(|_| CubeError::internal("Downcast error".to_string()))
111+
})?;
112+
Ok(NeonTypeHandle::new(self.context.clone(), obj))
113+
}
114+
115+
pub fn downcast_with_err_msg<U: Value>(
116+
&self,
117+
msg: &str,
118+
) -> Result<NeonTypeHandle<'cx, C, U>, CubeError> {
119+
let obj = self.context.with_context(|cx| {
120+
self.object
121+
.downcast::<U, _>(cx)
122+
.map_err(|_| CubeError::internal(msg.to_string()))
123+
})?;
124+
Ok(NeonTypeHandle::new(self.context.clone(), obj))
125+
}
126+
}
127+
62128
impl<'cx: 'static, C: Context<'cx> + 'cx> NativeObject<NeonInnerTypes<'cx, C>>
63129
for NeonObject<'cx, C>
64130
{
@@ -67,52 +133,28 @@ impl<'cx: 'static, C: Context<'cx> + 'cx> NativeObject<NeonInnerTypes<'cx, C>>
67133
}
68134

69135
fn into_struct(self) -> Result<NeonStruct<'cx, C>, CubeError> {
70-
if !self.is_a::<JsObject>() {
71-
return Err(CubeError::internal(format!(
72-
"NeonObject is not the JsObject"
73-
)));
74-
}
75-
Ok(NeonStruct::new(self))
136+
let obj = self.downcast_with_err_msg::<JsObject>("NeonObject is not the JsObject")?;
137+
Ok(NeonStruct::new(obj))
76138
}
77139
fn into_function(self) -> Result<NeonFunction<'cx, C>, CubeError> {
78-
if !self.is_a::<JsFunction>() {
79-
return Err(CubeError::internal(format!(
80-
"NeonObject is not the JsFunction"
81-
)));
82-
}
83-
Ok(NeonFunction::new(self))
140+
let obj = self.downcast_with_err_msg::<JsFunction>("NeonObject is not the JsArray")?;
141+
Ok(NeonFunction::new(obj))
84142
}
85143
fn into_array(self) -> Result<NeonArray<'cx, C>, CubeError> {
86-
if !self.is_a::<JsArray>() {
87-
return Err(CubeError::internal(format!(
88-
"NeonObject is not the JsArray"
89-
)));
90-
}
91-
Ok(NeonArray::new(self))
144+
let obj = self.downcast_with_err_msg::<JsArray>("NeonObject is not the JsArray")?;
145+
Ok(NeonArray::new(obj))
92146
}
93147
fn into_string(self) -> Result<NeonString<'cx, C>, CubeError> {
94-
if !self.is_a::<JsString>() {
95-
return Err(CubeError::internal(format!(
96-
"NeonObject is not the JsString"
97-
)));
98-
}
99-
Ok(NeonString::new(self))
148+
let obj = self.downcast_with_err_msg::<JsString>("NeonObject is not the JsString")?;
149+
Ok(NeonString::new(obj))
100150
}
101151
fn into_number(self) -> Result<NeonNumber<'cx, C>, CubeError> {
102-
if !self.is_a::<JsNumber>() {
103-
return Err(CubeError::internal(format!(
104-
"NeonObject is not the JsNumber"
105-
)));
106-
}
107-
Ok(NeonNumber::new(self))
152+
let obj = self.downcast_with_err_msg::<JsNumber>("NeonObject is not the JsNumber")?;
153+
Ok(NeonNumber::new(obj))
108154
}
109155
fn into_boolean(self) -> Result<NeonBoolean<'cx, C>, CubeError> {
110-
if !self.is_a::<JsBoolean>() {
111-
return Err(CubeError::internal(format!(
112-
"NeonObject is not the JsBoolean"
113-
)));
114-
}
115-
Ok(NeonBoolean::<C>::new(self))
156+
let obj = self.downcast_with_err_msg::<JsBoolean>("NeonObject is not the JsBoolean")?;
157+
Ok(NeonBoolean::new(obj))
116158
}
117159

118160
fn is_null(&self) -> bool {

0 commit comments

Comments
 (0)