use std::convert::TryFrom;
use std::{io, ptr};
use devtools_traits::{ConsoleMessage, LogLevel, ScriptToDevtoolsControlMsg, StackFrame};
use js::jsapi::{self, ESClass, PropertyDescriptor};
use js::jsval::UndefinedValue;
use js::rust::wrappers::{
GetBuiltinClass, GetPropertyKeys, JS_GetOwnPropertyDescriptorById, JS_GetPropertyById,
JS_IdToValue, JS_ValueToSource,
};
use js::rust::{describe_scripted_caller, CapturedJSStack, HandleValue, IdVector};
use crate::dom::bindings::codegen::Bindings::ConsoleBinding::consoleMethods;
use crate::dom::bindings::conversions::jsstring_to_str;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::str::DOMString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::script_runtime::JSContext;
const MAX_LOG_DEPTH: usize = 10;
const MAX_LOG_CHILDREN: usize = 15;
pub struct Console;
impl Console {
#[allow(unsafe_code)]
fn send_to_devtools(global: &GlobalScope, level: LogLevel, message: String) {
if let Some(chan) = global.devtools_chan() {
let caller =
unsafe { describe_scripted_caller(*GlobalScope::get_cx()) }.unwrap_or_default();
let console_message = ConsoleMessage {
message,
log_level: level,
filename: caller.filename,
line_number: caller.line as usize,
column_number: caller.col as usize,
stacktrace: get_js_stack(*GlobalScope::get_cx()),
};
let worker_id = global
.downcast::<WorkerGlobalScope>()
.map(|worker| worker.get_worker_id());
let devtools_message = ScriptToDevtoolsControlMsg::ConsoleAPI(
global.pipeline_id(),
console_message,
worker_id,
);
chan.send(devtools_message).unwrap();
}
}
pub fn internal_warn(global: &GlobalScope, message: DOMString) {
console_message(global, message, LogLevel::Warn)
}
}
fn with_stderr_lock<F>(f: F)
where
F: FnOnce(),
{
let stderr = io::stderr();
let _handle = stderr.lock();
f()
}
#[allow(unsafe_code)]
unsafe fn handle_value_to_string(cx: *mut jsapi::JSContext, value: HandleValue) -> DOMString {
rooted!(in(cx) let mut js_string = std::ptr::null_mut::<jsapi::JSString>());
match std::ptr::NonNull::new(JS_ValueToSource(cx, value)) {
Some(js_str) => {
js_string.set(js_str.as_ptr());
jsstring_to_str(cx, js_str)
},
None => "<error converting value to string>".into(),
}
}
#[allow(unsafe_code)]
fn stringify_handle_value(message: HandleValue) -> DOMString {
let cx = *GlobalScope::get_cx();
unsafe {
if message.is_string() {
return jsstring_to_str(cx, std::ptr::NonNull::new(message.to_string()).unwrap());
}
unsafe fn stringify_object_from_handle_value(
cx: *mut jsapi::JSContext,
value: HandleValue,
parents: Vec<u64>,
) -> DOMString {
rooted!(in(cx) let mut obj = value.to_object());
let mut object_class = ESClass::Other;
if !GetBuiltinClass(cx, obj.handle(), &mut object_class as *mut _) {
return DOMString::from("/* invalid */");
}
let mut ids = IdVector::new(cx);
if !GetPropertyKeys(
cx,
obj.handle(),
jsapi::JSITER_OWNONLY | jsapi::JSITER_SYMBOLS,
ids.handle_mut(),
) {
return DOMString::from("/* invalid */");
}
let truncate = ids.len() > MAX_LOG_CHILDREN;
if object_class != ESClass::Array && object_class != ESClass::Object {
if truncate {
return DOMString::from("…");
} else {
return handle_value_to_string(cx, value);
}
}
let mut explicit_keys = object_class == ESClass::Object;
let mut props = Vec::with_capacity(ids.len());
for id in ids.iter().take(MAX_LOG_CHILDREN) {
rooted!(in(cx) let id = *id);
rooted!(in(cx) let mut desc = PropertyDescriptor::default());
let mut is_none = false;
if !JS_GetOwnPropertyDescriptorById(
cx,
obj.handle(),
id.handle(),
desc.handle_mut(),
&mut is_none,
) {
return DOMString::from("/* invalid */");
}
rooted!(in(cx) let mut property = UndefinedValue());
if !JS_GetPropertyById(cx, obj.handle(), id.handle(), property.handle_mut()) {
return DOMString::from("/* invalid */");
}
if !explicit_keys {
if id.is_int() {
if let Ok(id_int) = usize::try_from(id.to_int()) {
explicit_keys = props.len() != id_int;
} else {
explicit_keys = false;
}
} else {
explicit_keys = false;
}
}
let value_string = stringify_inner(cx, property.handle(), parents.clone());
if explicit_keys {
let key = if id.is_string() || id.is_symbol() || id.is_int() {
rooted!(in(cx) let mut key_value = UndefinedValue());
let raw_id: jsapi::HandleId = id.handle().into();
if !JS_IdToValue(cx, *raw_id.ptr, key_value.handle_mut()) {
return DOMString::from("/* invalid */");
}
handle_value_to_string(cx, key_value.handle())
} else {
return DOMString::from("/* invalid */");
};
props.push(format!("{}: {}", key, value_string,));
} else {
props.push(value_string.to_string());
}
}
if truncate {
props.push("…".to_string());
}
if object_class == ESClass::Array {
DOMString::from(format!("[{}]", itertools::join(props, ", ")))
} else {
DOMString::from(format!("{{{}}}", itertools::join(props, ", ")))
}
}
unsafe fn stringify_inner(
cx: *mut jsapi::JSContext,
value: HandleValue,
mut parents: Vec<u64>,
) -> DOMString {
if parents.len() >= MAX_LOG_DEPTH {
return DOMString::from("...");
}
let value_bits = value.asBits_;
if parents.contains(&value_bits) {
return DOMString::from("[circular]");
}
if value.is_undefined() {
return DOMString::from("undefined");
} else if !value.is_object() {
return handle_value_to_string(cx, value);
}
parents.push(value_bits);
stringify_object_from_handle_value(cx, value, parents)
}
stringify_inner(cx, message, Vec::new())
}
}
fn stringify_handle_values(messages: Vec<HandleValue>) -> DOMString {
DOMString::from(itertools::join(
messages.into_iter().map(stringify_handle_value),
" ",
))
}
fn console_messages(global: &GlobalScope, messages: Vec<HandleValue>, level: LogLevel) {
let message = stringify_handle_values(messages);
console_message(global, message, level)
}
fn console_message(global: &GlobalScope, message: DOMString, level: LogLevel) {
with_stderr_lock(move || {
let prefix = global.current_group_label().unwrap_or_default();
let message = format!("{}{}", prefix, message);
println!("{}", message);
Console::send_to_devtools(global, level, message);
})
}
impl consoleMethods<crate::DomTypeHolder> for Console {
fn Log(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
console_messages(global, messages, LogLevel::Log)
}
fn Clear(global: &GlobalScope) {
let message: Vec<HandleValue> = Vec::new();
console_messages(global, message, LogLevel::Clear)
}
fn Debug(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
console_messages(global, messages, LogLevel::Debug)
}
fn Info(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
console_messages(global, messages, LogLevel::Info)
}
fn Warn(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
console_messages(global, messages, LogLevel::Warn)
}
fn Error(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
console_messages(global, messages, LogLevel::Error)
}
fn Trace(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
console_messages(global, messages, LogLevel::Trace)
}
fn Assert(_cx: JSContext, global: &GlobalScope, condition: bool, messages: Vec<HandleValue>) {
if !condition {
let message = DOMString::from(format!(
"Assertion failed: {}",
stringify_handle_values(messages)
));
console_message(global, message, LogLevel::Error);
}
}
fn Time(global: &GlobalScope, label: DOMString) {
if let Ok(()) = global.time(label.clone()) {
let message = DOMString::from(format!("{label}: timer started"));
console_message(global, message, LogLevel::Log);
}
}
fn TimeLog(_cx: JSContext, global: &GlobalScope, label: DOMString, data: Vec<HandleValue>) {
if let Ok(delta) = global.time_log(&label) {
let message = DOMString::from(format!(
"{label}: {delta}ms {}",
stringify_handle_values(data)
));
console_message(global, message, LogLevel::Log);
}
}
fn TimeEnd(global: &GlobalScope, label: DOMString) {
if let Ok(delta) = global.time_end(&label) {
let message = DOMString::from(format!("{label}: {delta}ms"));
console_message(global, message, LogLevel::Log);
}
}
fn Group(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
global.push_console_group(stringify_handle_values(messages));
}
fn GroupCollapsed(_cx: JSContext, global: &GlobalScope, messages: Vec<HandleValue>) {
global.push_console_group(stringify_handle_values(messages));
}
fn GroupEnd(global: &GlobalScope) {
global.pop_console_group();
}
fn Count(global: &GlobalScope, label: DOMString) {
let count = global.increment_console_count(&label);
let message = DOMString::from(format!("{label}: {count}"));
console_message(global, message, LogLevel::Log);
}
fn CountReset(global: &GlobalScope, label: DOMString) {
if global.reset_console_count(&label).is_err() {
Self::internal_warn(
global,
DOMString::from(format!("Counter “{label}” doesn’t exist.")),
)
}
}
}
#[allow(unsafe_code)]
fn get_js_stack(cx: *mut jsapi::JSContext) -> Vec<StackFrame> {
const MAX_FRAME_COUNT: u32 = 128;
let mut frames = vec![];
rooted!(in(cx) let mut handle = ptr::null_mut());
let captured_js_stack = unsafe { CapturedJSStack::new(cx, handle, Some(MAX_FRAME_COUNT)) };
let Some(captured_js_stack) = captured_js_stack else {
return frames;
};
captured_js_stack.for_each_stack_frame(|frame| {
rooted!(in(cx) let mut result: *mut jsapi::JSString = ptr::null_mut());
unsafe {
jsapi::GetSavedFrameFunctionDisplayName(
cx,
ptr::null_mut(),
frame.into(),
result.handle_mut().into(),
jsapi::SavedFrameSelfHosted::Include,
);
}
let function_name = if let Some(nonnull_result) = ptr::NonNull::new(*result) {
unsafe { jsstring_to_str(cx, nonnull_result) }.into()
} else {
"<anonymous>".into()
};
result.set(ptr::null_mut());
unsafe {
jsapi::GetSavedFrameSource(
cx,
ptr::null_mut(),
frame.into(),
result.handle_mut().into(),
jsapi::SavedFrameSelfHosted::Include,
);
}
let filename = if let Some(nonnull_result) = ptr::NonNull::new(*result) {
unsafe { jsstring_to_str(cx, nonnull_result) }.into()
} else {
"<anonymous>".into()
};
let mut line_number = 0;
unsafe {
jsapi::GetSavedFrameLine(
cx,
ptr::null_mut(),
frame.into(),
&mut line_number,
jsapi::SavedFrameSelfHosted::Include,
);
}
let mut column_number = jsapi::JS::TaggedColumnNumberOneOrigin { value_: 0 };
unsafe {
jsapi::GetSavedFrameColumn(
cx,
ptr::null_mut(),
frame.into(),
&mut column_number,
jsapi::SavedFrameSelfHosted::Include,
);
}
let frame = StackFrame {
filename,
function_name,
line_number,
column_number: column_number.value_,
};
frames.push(frame);
});
frames
}