From 348edbf4dfdec1ac53b8e1df53ff039d1dea0adb Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Sun, 12 Apr 2020 21:41:45 +0200 Subject: [PATCH] Added some documentation to builtins/console --- boa/src/builtins/console.rs | 58 +++++++++++++++++++++++++++++++++++++ boa/src/builtins/mod.rs | 1 - 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 boa/src/builtins/console.rs diff --git a/boa/src/builtins/console.rs b/boa/src/builtins/console.rs new file mode 100644 index 0000000000..93a17a84ba --- /dev/null +++ b/boa/src/builtins/console.rs @@ -0,0 +1,58 @@ +//! This module implements the global `console` object. + +#![allow(clippy::print_stdout)] + +use crate::{ + builtins::{ + function::NativeFunctionData, + value::{from_value, log_string_from, to_value, ResultValue, Value, ValueData}, + }, + exec::Interpreter, +}; +use gc::Gc; +use std::{iter::FromIterator, ops::Deref}; + +/// This `console` method prints the javascript values to stdout. +/// +/// More information: +/// - [Whatwg reference][spec] +/// - [MDN documentation][mdn] +/// +/// [spec]: https://console.spec.whatwg.org/#log +/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Console/log +pub fn log(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { + // Welcome to console.log! The output here is what the developer sees, so its best matching through value types and stringifying to the correct output + // The input is a vector of Values, we generate a vector of strings then + // pass them to println! + let args: Vec = + FromIterator::from_iter(args.iter().map(|x| log_string_from(x.deref(), false))); + + println!("{}", args.join(" ")); + Ok(Gc::new(ValueData::Undefined)) +} + +/// This `console` method prints the javascript values to stderr. +/// +/// More information: +/// - [Whatwg reference][spec] +/// - [MDN documentation][mdn] +/// +/// [spec]: https://console.spec.whatwg.org/#error +/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Console/error +pub fn error(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { + let args: Vec = FromIterator::from_iter( + args.iter() + .map(|x| from_value::(x.clone()).expect("Could not convert value to String")), + ); + eprintln!("{}", args.join(" ")); + Ok(Gc::new(ValueData::Undefined)) +} + +/// Create a new `console` object. +pub fn create_constructor(global: &Value) -> Value { + let console = ValueData::new_obj(Some(global)); + console.set_field_slice("log", to_value(log as NativeFunctionData)); + console.set_field_slice("error", to_value(error as NativeFunctionData)); + console.set_field_slice("exception", to_value(error as NativeFunctionData)); + console +} diff --git a/boa/src/builtins/mod.rs b/boa/src/builtins/mod.rs index 18e79c7b0b..5e7c335c59 100644 --- a/boa/src/builtins/mod.rs +++ b/boa/src/builtins/mod.rs @@ -17,7 +17,6 @@ pub mod array; pub mod symbol; // The global `Boolean` object pub mod boolean; -/// The global `console` object pub mod console; /// The global `Error` object pub mod error;