use crate::host::{with_host, JsObj};
use fusevm::Value;
use indexmap::IndexMap;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::io::IsTerminal;
use std::time::Instant;
pub const METHODS: &[&str] = &[
"log",
"info",
"debug",
"error",
"warn",
"dir",
"dirxml",
"trace",
"assert",
"count",
"countReset",
"group",
"groupCollapsed",
"groupEnd",
"time",
"timeEnd",
"timeLog",
"table",
"clear",
"timeStamp",
"profile",
"profileEnd",
];
pub const CONSOLE_METHODS: &[&str] = METHODS;
thread_local! {
static GROUP_DEPTH: Cell<usize> = const { Cell::new(0) };
static COUNTS: RefCell<HashMap<String, u64>> = RefCell::new(HashMap::new());
static TIMERS: RefCell<HashMap<String, Instant>> = RefCell::new(HashMap::new());
static SINK: RefCell<Option<(Value, Value)>> = const { RefCell::new(None) };
}
pub fn call(method: &str, args: &[Value]) -> Option<Result<Value, String>> {
Some(match method {
"log" | "info" | "debug" | "dirxml" => {
match format_args(args) {
Ok(s) => {
emit(&s, false);
Ok(Value::Undef)
}
Err(e) => Err(e),
}
}
"error" | "warn" => match format_args(args) {
Ok(s) => {
emit(&s, true);
Ok(Value::Undef)
}
Err(e) => Err(e),
},
"dir" => {
let opts = dir_options(args.get(1));
let target = args.first().cloned().unwrap_or(Value::Undef);
match crate::stdlib::util::call("inspect", &[target, opts]) {
Some(Ok(s)) => {
emit(&with_host(|h| h.str_of(&s)), false);
Ok(Value::Undef)
}
Some(Err(e)) => Err(e),
None => Ok(Value::Undef),
}
}
"trace" => {
let msg = match format_args(args) {
Ok(s) => s,
Err(e) => return Some(Err(e)),
};
let line = if msg.is_empty() {
"Trace".to_string()
} else {
format!("Trace: {msg}")
};
emit(&line, true);
Ok(Value::Undef)
}
"assert" => {
let ok = with_host(|h| h.truthy(&args.first().cloned().unwrap_or(Value::Undef)));
if !ok {
let msg = match format_args(&args[1.min(args.len())..]) {
Ok(s) => s,
Err(e) => return Some(Err(e)),
};
let line = if msg.is_empty() {
"Assertion failed".to_string()
} else {
format!("Assertion failed: {msg}")
};
emit(&line, true);
}
Ok(Value::Undef)
}
"count" => {
let label = label_arg(args, "default");
let n = COUNTS.with(|c| {
let mut m = c.borrow_mut();
let e = m.entry(label.clone()).or_insert(0);
*e += 1;
*e
});
emit(&format!("{label}: {n}"), false);
Ok(Value::Undef)
}
"countReset" => {
let label = label_arg(args, "default");
COUNTS.with(|c| c.borrow_mut().remove(&label));
Ok(Value::Undef)
}
"group" | "groupCollapsed" => {
if !args.is_empty() {
match format_args(args) {
Ok(s) => emit(&s, false),
Err(e) => return Some(Err(e)),
}
}
GROUP_DEPTH.with(|d| d.set(d.get() + 1));
Ok(Value::Undef)
}
"groupEnd" => {
GROUP_DEPTH.with(|d| d.set(d.get().saturating_sub(1)));
Ok(Value::Undef)
}
"time" => {
let label = label_arg(args, "default");
TIMERS.with(|t| t.borrow_mut().insert(label, Instant::now()));
Ok(Value::Undef)
}
"timeEnd" | "timeLog" => {
let label = label_arg(args, "default");
let elapsed = TIMERS.with(|t| {
let m = t.borrow();
m.get(&label).map(|start| start.elapsed())
});
match elapsed {
Some(d) => {
let ms = d.as_secs_f64() * 1000.0;
let extra = if args.len() > 1 {
match format_args(&args[1..]) {
Ok(s) => format!(" {s}"),
Err(e) => return Some(Err(e)),
}
} else {
String::new()
};
emit(&format!("{label}: {ms:.3}ms{extra}"), false);
if method == "timeEnd" {
TIMERS.with(|t| t.borrow_mut().remove(&label));
}
}
None => emit(
&format!("Warning: No such label '{label}' for console.{method}()"),
true,
),
}
Ok(Value::Undef)
}
"table" => {
match render_table(args) {
Err(e) => return Some(Err(e)),
Ok(Some(t)) => emit(&t, false),
Ok(None) => match format_args(&args[..args.len().min(1)]) {
Ok(s) => emit(&s, false),
Err(e) => return Some(Err(e)),
},
}
Ok(Value::Undef)
}
"clear" => {
let is_tty = SINK.with(|s| s.borrow().is_some()) || std::io::stdout().is_terminal();
if is_tty {
emit("\u{1b}[2J\u{1b}[0f", false);
}
Ok(Value::Undef)
}
"timeStamp" | "profile" | "profileEnd" => Ok(Value::Undef),
_ => return None,
})
}
pub fn construct(args: &[Value]) -> Result<Value, String> {
let first = args.first().cloned().unwrap_or(Value::Undef);
let is_options = with_host(|h| match h.get(&first) {
Some(JsObj::Object(m)) => m.contains_key("stdout"),
_ => false,
});
let (stdout, stderr) = if is_options {
let out = crate::builtins::get_property(&first, "stdout").unwrap_or(Value::Undef);
let err = match crate::builtins::get_property(&first, "stderr") {
Ok(Value::Undef) | Err(_) => out.clone(),
Ok(v) => v,
};
(out, err)
} else {
let err = args
.get(1)
.cloned()
.filter(|v| !matches!(v, Value::Undef))
.unwrap_or_else(|| first.clone());
(first, err)
};
Ok(with_host(|h| {
let mut m = IndexMap::new();
m.insert("@@native".into(), h.new_str("Console"));
m.insert("@@stdout".into(), stdout);
m.insert("@@stderr".into(), stderr);
h.new_object(m)
}))
}
pub fn instance_call(recv: &Value, method: &str, args: Vec<Value>) -> Result<Value, String> {
let streams = with_host(|h| match h.get(recv) {
Some(JsObj::Object(m)) => Some((
m.get("@@stdout").cloned().unwrap_or(Value::Undef),
m.get("@@stderr").cloned().unwrap_or(Value::Undef),
)),
_ => None,
});
let prev = SINK.with(|s| s.borrow_mut().take());
SINK.with(|s| *s.borrow_mut() = streams);
let r = call(method, &args).unwrap_or(Ok(Value::Undef));
SINK.with(|s| *s.borrow_mut() = prev);
r
}
fn format_args(args: &[Value]) -> Result<String, String> {
super::util::format(args)
}
fn dir_options(options: Option<&Value>) -> Value {
let mut props: IndexMap<String, Value> = IndexMap::new();
props.insert("customInspect".into(), Value::Bool(false));
if let Some(o) = options.filter(|o| matches!(o, Value::Obj(_))) {
for (k, v) in crate::host::own_enum_entries_deep(o).unwrap_or_default() {
props.insert(k, v);
}
}
with_host(|h| h.new_object(props))
}
fn label_arg(args: &[Value], fallback: &str) -> String {
match args.first() {
Some(v) => with_host(|h| h.str_of(v)),
None => fallback.to_string(),
}
}
fn emit(line: &str, stderr: bool) {
let depth = GROUP_DEPTH.with(|d| d.get());
let out = if depth == 0 {
line.to_string()
} else {
let pad = " ".repeat(depth);
format!("{pad}{}", line.replace('\n', &format!("\n{pad}")))
};
let stream = SINK.with(|s| {
s.borrow().as_ref().and_then(|(o, e)| {
let target = if stderr { e } else { o };
matches!(target, Value::Obj(_)).then(|| target.clone())
})
});
if let Some(stream) = stream {
let payload = with_host(|h| h.new_str(format!("{out}\n")));
if crate::host::call_method(&stream, "write", vec![payload]).is_ok() {
return;
}
}
with_host(|h| h.write_out(&format!("{out}\n"), stderr));
}
type TableEntries = Vec<(Value, Value)>;
fn render_table(args: &[Value]) -> Result<Option<String>, String> {
let data = args.first().cloned().unwrap_or(Value::Undef);
let properties = args.get(1).cloned().unwrap_or(Value::Undef);
let properties: Option<Vec<Value>> = match with_host(|h| h.get(&properties).cloned()) {
_ if matches!(properties, Value::Undef) => None,
Some(JsObj::Array(items)) => Some(items),
_ => {
return Err(crate::host::coded_error(
"TypeError",
"ERR_INVALID_ARG_TYPE",
&format!(
"The \"properties\" argument must be an instance of Array. Received {}",
crate::stdlib::received_desc(&properties)
),
))
}
};
if with_host(|h| crate::host::is_primitive(h, &data) || h.is_null(&data)) {
return Ok(None);
}
let index_array = |n: usize| -> Result<Vec<Option<String>>, String> {
(0..n)
.map(|i| table_inspect(&Value::Float(i as f64)).map(Some))
.collect()
};
let view = with_host(|h| crate::builtins::collection_iterator_view(h, &data));
let (map_entries, set_values): (Option<TableEntries>, Option<Vec<Value>>) =
match (view, with_host(|h| h.get(&data).cloned())) {
(Some(("Map Entries", rest)), _) => (Some(rest), None),
(Some(("Set Entries", rest)), _) => (
None,
Some(rest.into_iter().flat_map(|(k, v)| [k, v]).collect()),
),
(Some((_, rest)), _) => (None, Some(rest.into_iter().map(|(k, _)| k).collect())),
(None, Some(JsObj::Map { entries, .. })) => {
(Some(entries.values().cloned().collect()), None)
}
(None, Some(JsObj::Set { entries, .. })) => {
(None, Some(entries.values().cloned().collect()))
}
_ => (None, None),
};
if let Some(pairs) = map_entries {
let mut keys = Vec::with_capacity(pairs.len());
let mut values = Vec::with_capacity(pairs.len());
for (k, v) in &pairs {
keys.push(Some(table_inspect(k)?));
values.push(Some(table_inspect(v)?));
}
let head = ["(iteration index)", "Key", "Values"].map(str::to_string);
return Ok(Some(cli_table(
&head,
&[index_array(pairs.len())?, keys, values],
)));
}
if let Some(items) = set_values {
let values = items
.iter()
.map(|v| table_inspect(v).map(Some))
.collect::<Result<Vec<_>, _>>()?;
let head = ["(iteration index)", "Values"].map(str::to_string);
return Ok(Some(cli_table(&head, &[index_array(items.len())?, values])));
}
let index_keys = object_keys(&data)?;
let mut map: Vec<(String, Vec<Option<String>>)> = Vec::new();
let mut has_primitives = false;
let mut values_column: Vec<Option<String>> = Vec::new();
for (i, k) in index_keys.iter().enumerate() {
let item = crate::builtins::get_property(&data, k)?;
let primitive = with_host(|h| h.is_nullish(&item) || crate::host::is_primitive(h, &item));
if properties.is_none() && primitive {
has_primitives = true;
set_cell(&mut values_column, i, table_inspect(&item)?);
continue;
}
let keys: Vec<String> = match &properties {
Some(p) => p
.iter()
.map(crate::host::to_property_key)
.collect::<Result<_, _>>()?,
None => object_keys(&item)?,
};
for key in keys {
let pos = match map.iter().position(|(k, _)| *k == key) {
Some(p) => p,
None => {
map.push((key.clone(), Vec::new()));
map.len() - 1
}
};
let cell = if (primitive && properties.is_some()) || !has_own(&item, &key)? {
String::new()
} else {
table_inspect(&crate::builtins::get_property(&item, &key)?)?
};
set_cell(&mut map[pos].1, i, cell);
}
}
let (mut ordered, rest): (Vec<_>, Vec<_>) = map
.into_iter()
.partition(|(k, _)| crate::host::array_index(k).is_some());
ordered.sort_by_key(|(k, _)| crate::host::array_index(k));
ordered.extend(rest);
let mut head = vec!["(index)".to_string()];
let mut columns = vec![index_keys.into_iter().map(Some).collect::<Vec<_>>()];
for (k, col) in ordered {
head.push(k);
columns.push(col);
}
if has_primitives {
head.push("Values".to_string());
columns.push(values_column);
}
Ok(Some(cli_table(&head, &columns)))
}
fn set_cell(column: &mut Vec<Option<String>>, i: usize, cell: String) {
if column.len() <= i {
column.resize(i + 1, None);
}
column[i] = Some(cell);
}
fn object_keys(v: &Value) -> Result<Vec<String>, String> {
let keys = crate::builtins::call_builtin_function("Object.keys", vec![v.clone()])?;
Ok(with_host(|h| match h.get(&keys) {
Some(JsObj::Array(items)) => items.iter().map(|k| h.str_of(k)).collect(),
_ => Vec::new(),
}))
}
fn has_own(v: &Value, key: &str) -> Result<bool, String> {
let k = with_host(|h| h.new_str(key.to_string()));
let r = crate::builtins::object_builtin_method(v, "hasOwnProperty", vec![k])?;
Ok(with_host(|h| h.truthy(&r)))
}
fn table_inspect(v: &Value) -> Result<String, String> {
let view = matches!(
crate::stdlib::native_tag(v).as_deref(),
Some("TypedArray" | "Buffer")
);
let collapse = !view
&& with_host(|h| {
!crate::host::is_primitive(h, v)
&& !h.is_nullish(v)
&& !matches!(h.get(v), Some(JsObj::Array(_)))
})
&& object_keys(v)?.len() > 2;
let opts = with_host(|h| {
let mut m: IndexMap<String, Value> = IndexMap::new();
m.insert(
"depth".into(),
Value::Float(if collapse { -1.0 } else { 0.0 }),
);
m.insert("maxArrayLength".into(), Value::Float(3.0));
m.insert("breakLength".into(), Value::Float(f64::INFINITY));
h.new_object(m)
});
let out = crate::stdlib::util::call("inspect", &[v.clone(), opts])
.unwrap_or_else(|| Ok(with_host(|h| h.new_str(h.inspect(v)))))?;
Ok(with_host(|h| h.str_of(&out)))
}
fn string_width(s: &str) -> usize {
use unicode_normalization::UnicodeNormalization;
let s: String = crate::stdlib::util::strip_vt(s).nfc().collect();
s.chars()
.map(|c| {
let code = c as u32;
if is_full_width(code) {
2
} else if is_zero_width(code) {
0
} else {
1
}
})
.sum()
}
fn is_zero_width(code: u32) -> bool {
code <= 0x1F
|| (0x7F..=0x9F).contains(&code)
|| (0x300..=0x36F).contains(&code)
|| (0x200B..=0x200F).contains(&code)
|| (0x20D0..=0x20FF).contains(&code)
|| (0xFE00..=0xFE0F).contains(&code)
|| (0xFE20..=0xFE2F).contains(&code)
|| (0xE0100..=0xE01EF).contains(&code)
}
fn is_full_width(code: u32) -> bool {
code >= 0x1100
&& (code <= 0x115f
|| code == 0x2329
|| code == 0x232a
|| ((0x2e80..=0x3247).contains(&code) && code != 0x303f)
|| (0x3250..=0x4dbf).contains(&code)
|| (0x4e00..=0xa4c6).contains(&code)
|| (0xa960..=0xa97c).contains(&code)
|| (0xac00..=0xd7a3).contains(&code)
|| (0xf900..=0xfaff).contains(&code)
|| (0xfe10..=0xfe19).contains(&code)
|| (0xfe30..=0xfe6b).contains(&code)
|| (0xff01..=0xff60).contains(&code)
|| (0xffe0..=0xffe6).contains(&code)
|| (0x1b000..=0x1b001).contains(&code)
|| (0x1f200..=0x1f251).contains(&code)
|| (0x1f300..=0x1f64f).contains(&code)
|| (0x20000..=0x3fffd).contains(&code))
}
fn cli_table(head: &[String], columns: &[Vec<Option<String>>]) -> String {
let longest = columns.iter().map(Vec::len).max().unwrap_or(0);
let mut widths: Vec<usize> = head.iter().map(|h| string_width(h)).collect();
let mut rows: Vec<Vec<String>> = vec![Vec::with_capacity(head.len()); longest];
for (i, column) in columns.iter().enumerate() {
for (j, row) in rows.iter_mut().enumerate() {
let value = column.get(j).cloned().flatten().unwrap_or_default();
widths[i] = widths[i].max(string_width(&value));
row.push(value);
}
}
let render_row = |row: &[String]| -> String {
let cells: Vec<String> = row
.iter()
.zip(&widths)
.map(|(cell, w)| format!("{cell}{}", " ".repeat(w.saturating_sub(string_width(cell)))))
.collect();
format!("│ {} │", cells.join(" │ "))
};
let divider: Vec<String> = widths.iter().map(|w| "─".repeat(w + 2)).collect();
let mut out = format!(
"┌{}┐\n{}\n├{}┤\n",
divider.join("┬"),
render_row(head),
divider.join("┼")
);
for row in &rows {
out.push_str(&render_row(row));
out.push('\n');
}
out.push_str(&format!("└{}┘", divider.join("┴")));
out
}