@@ -6,12 +6,15 @@ use crate::{
6
6
object:: Object ,
7
7
result:: { JsResult , NeonResult } ,
8
8
thread:: LocalKey ,
9
- types:: { JsFunction , JsObject } ,
9
+ types:: { extract :: TryIntoJs , function :: BindOptions , JsFunction , JsObject } ,
10
10
} ;
11
11
12
12
static CONSOLE : LocalKey < Root < JsObject > > = LocalKey :: new ( ) ;
13
13
static LOG : LocalKey < Root < JsFunction > > = LocalKey :: new ( ) ;
14
14
static ERROR : LocalKey < Root < JsFunction > > = LocalKey :: new ( ) ;
15
+ static INFO : LocalKey < Root < JsFunction > > = LocalKey :: new ( ) ;
16
+ static WARN : LocalKey < Root < JsFunction > > = LocalKey :: new ( ) ;
17
+ static CLEAR : LocalKey < Root < JsFunction > > = LocalKey :: new ( ) ;
15
18
16
19
pub struct Console < ' a , ' cx : ' a , C : Context < ' cx > > {
17
20
pub ( crate ) cx : & ' a mut C ,
@@ -37,6 +40,22 @@ impl<'a, 'cx: 'a, C: Context<'cx>> Console<'a, 'cx, C> {
37
40
Ok ( v. to_inner ( self . cx ) )
38
41
}
39
42
43
+ fn memo_method < F > (
44
+ & mut self ,
45
+ cache : & ' static LocalKey < Root < JsFunction > > ,
46
+ get_container : F ,
47
+ key : & str ,
48
+ ) -> NeonResult < BindOptions < ' _ , ' cx > >
49
+ where
50
+ F : FnOnce ( & mut Self ) -> JsResult < ' cx , JsObject > ,
51
+ {
52
+ let container = get_container ( self ) ?;
53
+ let function = self . memo ( cache, |_| Ok ( container) , key) ?;
54
+ let mut method = function. bind ( self . cx . cx_mut ( ) ) ;
55
+ method. this ( container) ?;
56
+ Ok ( method)
57
+ }
58
+
40
59
pub ( crate ) fn new ( cx : & ' a mut C ) -> Self {
41
60
Self {
42
61
cx,
@@ -48,37 +67,23 @@ impl<'a, 'cx: 'a, C: Context<'cx>> Console<'a, 'cx, C> {
48
67
self . memo ( & CONSOLE , |c| Ok ( c. cx . global_object ( ) ) , "console" )
49
68
}
50
69
51
- fn log_function ( & mut self ) -> JsResult < ' cx , JsFunction > {
52
- self . memo ( & LOG , |c| c. console_object ( ) , "log" )
70
+ pub fn log < T : TryIntoJs < ' cx > > ( & mut self , msg : T ) -> NeonResult < ( ) > {
71
+ self . memo_method ( & LOG , |c| c. console_object ( ) , "log" ) ? . arg ( msg ) ? . exec ( )
53
72
}
54
73
55
- fn error_function ( & mut self ) -> JsResult < ' cx , JsFunction > {
56
- let console = self . console_object ( ) ?;
57
- let function = ERROR . get_or_try_init ( self . cx , |cx| {
58
- let log: Handle < JsFunction > = console. get ( cx, "error" ) ?;
59
- Ok ( log. root ( cx) )
60
- } ) ?;
61
- Ok ( function. to_inner ( self . cx ) )
74
+ pub fn error < T : TryIntoJs < ' cx > > ( & mut self , msg : T ) -> NeonResult < ( ) > {
75
+ self . memo_method ( & ERROR , |c| c. console_object ( ) , "error" ) ?. arg ( msg) ?. exec ( )
76
+ }
77
+
78
+ pub fn info < T : TryIntoJs < ' cx > > ( & mut self , msg : T ) -> NeonResult < ( ) > {
79
+ self . memo_method ( & INFO , |c| c. console_object ( ) , "info" ) ?. arg ( msg) ?. exec ( )
62
80
}
63
81
64
- // FIXME: when we land #1056, this can get simplified
65
- // FIXME: msg should be a generic TryIntoJs
66
- pub fn log ( & mut self , msg : & str ) -> NeonResult < ( ) > {
67
- let function = self . log_function ( ) ?;
68
- let console = self . console_object ( ) ?;
69
- let msg = self . cx . string ( msg) ;
70
- let args = vec ! [ msg. upcast( ) ] ;
71
- function. call ( self . cx , console, args) ?;
72
- Ok ( ( ) )
82
+ pub fn warn < T : TryIntoJs < ' cx > > ( & mut self , msg : T ) -> NeonResult < ( ) > {
83
+ self . memo_method ( & WARN , |c| c. console_object ( ) , "warn" ) ?. arg ( msg) ?. exec ( )
73
84
}
74
85
75
- // FIXME: msg should be a generic TryIntoJs
76
- pub fn error ( & mut self , msg : & str ) -> NeonResult < ( ) > {
77
- let function = self . error_function ( ) ?;
78
- let console = self . console_object ( ) ?;
79
- let msg = self . cx . string ( msg) ;
80
- let args = vec ! [ msg. upcast( ) ] ;
81
- function. call ( self . cx , console, args) ?;
82
- Ok ( ( ) )
86
+ pub fn clear < T : TryIntoJs < ' cx > > ( & mut self ) -> NeonResult < ( ) > {
87
+ self . memo_method ( & CLEAR , |c| c. console_object ( ) , "clear" ) ?. exec ( )
83
88
}
84
89
}
0 commit comments