11use super :: { inner_types:: InnerTypes , object_handle:: NativeObjectHandle } ;
22use cubesql:: CubeError ;
3+ use std:: any:: Any ;
4+ use std:: rc:: Rc ;
35
46pub trait NativeContext < IT : InnerTypes > : Clone {
57 fn boolean ( & self , v : bool ) -> Result < IT :: Boolean , CubeError > ;
@@ -8,8 +10,30 @@ pub trait NativeContext<IT: InnerTypes>: Clone {
810 fn undefined ( & self ) -> Result < NativeObjectHandle < IT > , CubeError > ;
911 fn empty_array ( & self ) -> Result < IT :: Array , CubeError > ;
1012 fn empty_struct ( & self ) -> Result < IT :: Struct , CubeError > ;
11- //fn boxed<T: 'static>(&self, value: T) -> impl NativeBox<IT, T>;
1213 fn to_string_fn ( & self , result : String ) -> Result < IT :: Function , CubeError > ;
14+
15+ //IMPORTANT NOTE: Using of any native args in callback (as well as any native objects created
16+ //with NativeContextHolder passed to callback) outside of callback will cause error from
17+ //runtime lifetime check
18+ fn function < F > ( & self , function : F ) -> Result < IT :: Function , CubeError >
19+ where
20+ F : Fn (
21+ Rc < NativeContextHolder < IT :: FunctionIT > > ,
22+ Vec < NativeObjectHandle < IT :: FunctionIT > > ,
23+ ) -> Result < NativeObjectHandle < IT :: FunctionIT > , CubeError >
24+ + ' static ;
25+
26+ fn boxed < T : NativeFinalize + ' static > (
27+ & self ,
28+ object : T ,
29+ ) -> Result < impl NativeBox < IT , T > , CubeError > ;
30+ fn global ( & self , name : & str ) -> Result < NativeObjectHandle < IT > , CubeError > ;
31+ }
32+
33+ //Top level reference to ContextHolder for using in top level interfaces. Should be downcaster to
34+ //specific context for use
35+ pub trait NativeContextHolderRef {
36+ fn as_any ( self : Rc < Self > ) -> Rc < dyn Any > ;
1337}
1438
1539#[ derive( Clone ) ]
@@ -18,10 +42,10 @@ pub struct NativeContextHolder<IT: InnerTypes> {
1842}
1943
2044impl < IT : InnerTypes > NativeContextHolder < IT > {
21- pub fn new ( context : IT :: Context ) -> Self {
22- Self { context }
45+ pub fn new ( context : IT :: Context ) -> Rc < Self > {
46+ Rc :: new ( Self { context } )
2347 }
24- pub fn context ( & self ) -> & impl NativeContext < IT > {
48+ pub fn context ( & self ) -> & IT :: Context {
2549 & self . context
2650 }
2751 pub fn boolean ( & self , v : bool ) -> Result < IT :: Boolean , CubeError > {
@@ -42,8 +66,54 @@ impl<IT: InnerTypes> NativeContextHolder<IT> {
4266 pub fn empty_struct ( & self ) -> Result < IT :: Struct , CubeError > {
4367 self . context . empty_struct ( )
4468 }
45- #[ allow( dead_code) ]
4669 pub fn to_string_fn ( & self , result : String ) -> Result < IT :: Function , CubeError > {
4770 self . context . to_string_fn ( result)
4871 }
72+ pub fn function < F > ( & self , function : F ) -> Result < IT :: Function , CubeError >
73+ where
74+ F : Fn (
75+ Rc < NativeContextHolder < IT :: FunctionIT > > ,
76+ Vec < NativeObjectHandle < IT :: FunctionIT > > ,
77+ ) -> Result < NativeObjectHandle < IT :: FunctionIT > , CubeError >
78+ + ' static ,
79+ {
80+ self . context . function ( function)
81+ }
82+ pub fn boxed < T : NativeFinalize + ' static > (
83+ & self ,
84+ object : T ,
85+ ) -> Result < impl NativeBox < IT , T > + ' _ , CubeError > {
86+ self . context . boxed ( object)
87+ }
88+ pub fn global ( & self , name : & str ) -> Result < NativeObjectHandle < IT > , CubeError > {
89+ self . context . global ( name)
90+ }
91+ pub fn as_context_ref ( self : & Rc < Self > ) -> Rc < dyn NativeContextHolderRef > {
92+ self . clone ( )
93+ }
94+ }
95+
96+ impl < IT : InnerTypes > NativeContextHolderRef for NativeContextHolder < IT > {
97+ fn as_any ( self : Rc < Self > ) -> Rc < dyn Any > {
98+ self . clone ( )
99+ }
100+ }
101+
102+ //FIXME For now we don't allow js calls on finalize, so it's only to clean rust resources
103+ pub trait NativeFinalize : Sized {
104+ fn finalize ( self ) { }
105+ }
106+
107+ impl < T : NativeFinalize > NativeFinalize for std:: rc:: Rc < T > {
108+ fn finalize ( self ) {
109+ if let Ok ( v) = std:: rc:: Rc :: try_unwrap ( self ) {
110+ v. finalize ( ) ;
111+ }
112+ }
113+ }
114+
115+ impl < T : NativeFinalize > NativeFinalize for std:: cell:: RefCell < T > {
116+ fn finalize ( self ) {
117+ self . into_inner ( ) . finalize ( ) ;
118+ }
49119}
0 commit comments