@@ -13,34 +13,43 @@ use crate::wrappers::object::NativeObject;
1313use cubesql:: CubeError ;
1414use 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+
62128impl < ' 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