Skip to content

Commit ebfa9dc

Browse files
committed
- memo_method abstraction to implement methods as one-liners
- add console().info(), console().warn(), console().clear()
1 parent 77f89cc commit ebfa9dc

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

crates/neon/src/node/console.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use crate::{
66
object::Object,
77
result::{JsResult, NeonResult},
88
thread::LocalKey,
9-
types::{JsFunction, JsObject},
9+
types::{extract::TryIntoJs, function::BindOptions, JsFunction, JsObject},
1010
};
1111

1212
static CONSOLE: LocalKey<Root<JsObject>> = LocalKey::new();
1313
static LOG: LocalKey<Root<JsFunction>> = LocalKey::new();
1414
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();
1518

1619
pub struct Console<'a, 'cx: 'a, C: Context<'cx>> {
1720
pub(crate) cx: &'a mut C,
@@ -37,6 +40,22 @@ impl<'a, 'cx: 'a, C: Context<'cx>> Console<'a, 'cx, C> {
3740
Ok(v.to_inner(self.cx))
3841
}
3942

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+
4059
pub(crate) fn new(cx: &'a mut C) -> Self {
4160
Self {
4261
cx,
@@ -48,37 +67,23 @@ impl<'a, 'cx: 'a, C: Context<'cx>> Console<'a, 'cx, C> {
4867
self.memo(&CONSOLE, |c| Ok(c.cx.global_object()), "console")
4968
}
5069

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()
5372
}
5473

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()
6280
}
6381

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()
7384
}
7485

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()
8388
}
8489
}

0 commit comments

Comments
 (0)