From 4a6276fc9329128083099a7b8daaa7af877bdcdf Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Sat, 25 Apr 2020 23:15:33 +0200 Subject: [PATCH] Added some documentation to value --- boa/src/builtins/mod.rs | 4 ++-- boa/src/builtins/number/mod.rs | 14 +++++++++++++- boa/src/builtins/value/mod.rs | 30 +++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/boa/src/builtins/mod.rs b/boa/src/builtins/mod.rs index 88d1527a46..ac50ac3ddd 100644 --- a/boa/src/builtins/mod.rs +++ b/boa/src/builtins/mod.rs @@ -1,6 +1,7 @@ //! Builtins live here, such as Object, String, Math etc -/// Macro to create a new member function of a prototype +/// Macro to create a new member function of a prototype. +/// /// If no length is provided, the length will be set to 0. macro_rules! make_builtin_fn { ($fn:ident, named $name:expr, with length $l:tt, of $p:ident) => { @@ -26,5 +27,4 @@ pub mod property; pub mod regexp; pub mod string; pub mod symbol; -/// Javascript values, utility methods and conversion between Javascript values and Rust values pub mod value; diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index b8a34cd600..42841e8eee 100644 --- a/boa/src/builtins/number/mod.rs +++ b/boa/src/builtins/number/mod.rs @@ -57,7 +57,7 @@ fn num_to_exponential(n: f64) -> String { } } -/// Create a new number [[Construct]] +/// Create a new number `[[Construct]]` pub fn make_number(this: &Value, args: &[Value], _ctx: &mut Interpreter) -> ResultValue { let data = match args.get(0) { Some(ref value) => to_number(value), @@ -78,6 +78,8 @@ pub fn call_number(_this: &Value, args: &[Value], _ctx: &mut Interpreter) -> Res Ok(data) } +/// `Number.prototype.toExponential( [fractionDigits] )` +/// /// The `toExponential()` method returns a string representing the Number object in exponential notation. /// /// More information: @@ -92,6 +94,8 @@ pub fn to_exponential(this: &Value, _args: &[Value], _ctx: &mut Interpreter) -> Ok(to_value(this_str_num)) } +/// `Number.prototype.toFixed( [digits] )` +/// /// The `toFixed()` method formats a number using fixed-point notation /// /// More information: @@ -113,6 +117,8 @@ pub fn to_fixed(this: &Value, args: &[Value], _ctx: &mut Interpreter) -> ResultV Ok(to_value(this_fixed_num)) } +/// `Number.prototype.toLocaleString( [locales [, options]] )` +/// /// The `toLocaleString()` method returns a string with a language-sensitive representation of this number. /// /// Note that while this technically conforms to the Ecma standard, it does no actual @@ -130,6 +136,8 @@ pub fn to_locale_string(this: &Value, _args: &[Value], _ctx: &mut Interpreter) - Ok(to_value(this_str_num)) } +/// `Number.prototype.toPrecision( [precision] )` +/// /// The `toPrecision()` method returns a string representing the Number object to the specified precision. /// /// More information: @@ -152,6 +160,8 @@ pub fn to_precision(this: &Value, args: &[Value], _ctx: &mut Interpreter) -> Res unimplemented!("TODO: Implement toPrecision"); } +/// `Number.prototype.toString( [radix] )` +/// /// The `toString()` method returns a string representing the specified Number object. /// /// More information: @@ -164,6 +174,8 @@ pub fn to_string(this: &Value, _args: &[Value], _ctx: &mut Interpreter) -> Resul Ok(to_value(format!("{}", to_number(this).to_num()))) } +/// `Number.prototype.toString()` +/// /// The `valueOf()` method returns the wrapped primitive value of a Number object. /// /// More information: diff --git a/boa/src/builtins/value/mod.rs b/boa/src/builtins/value/mod.rs index 7486d214c6..ef307e5fc5 100644 --- a/boa/src/builtins/value/mod.rs +++ b/boa/src/builtins/value/mod.rs @@ -1,3 +1,7 @@ +//! This module implements the JavaScript Value. +//! +//! Javascript values, utility methods and conversion between Javascript values and Rust values. + #[cfg(test)] mod tests; @@ -24,7 +28,8 @@ use std::{ /// The result of a Javascript expression is represented like this so it can succeed (`Ok`) or fail (`Err`) #[must_use] pub type ResultValue = Result; -/// A Garbage-collected Javascript value as represented in the interpreter + +/// A Garbage-collected Javascript value as represented in the interpreter. pub type Value = Gc; pub fn undefined() -> Value { @@ -80,8 +85,13 @@ impl ValueData { Gc::new(ValueData::Object(GcCell::new(obj))) } - /// This will tell us if we can exten an object or not, not properly implemented yet, for now always returns true - /// For scalar types it should be false, for objects check the private field for extensibilaty. By default true + /// This will tell us if we can exten an object or not, not properly implemented yet + /// + /// For now always returns true. + /// + /// For scalar types it should be false, for objects check the private field for extensibilaty. + /// By default true. + /// /// /// pub fn is_extensible(&self) -> bool { @@ -167,6 +177,7 @@ impl ValueData { } /// Returns true if the value is true + /// /// [toBoolean](https://tc39.es/ecma262/#sec-toboolean) pub fn is_true(&self) -> bool { match *self { @@ -217,6 +228,7 @@ impl ValueData { } /// remove_prop removes a property from a Value object. + /// /// It will return a boolean based on if the value was removed, if there was no value to remove false is returned pub fn remove_prop(&self, field: &str) { match *self { @@ -230,8 +242,9 @@ impl ValueData { }; } - /// Resolve the property in the object - /// Returns a copy of the Property + /// Resolve the property in the object. + /// + /// A copy of the Property is returned. pub fn get_prop(&self, field: &str) -> Option { // Spidermonkey has its own GetLengthProperty: https://searchfox.org/mozilla-central/source/js/src/vm/Interpreter-inl.h#154 // This is only for primitive strings, String() objects have their lengths calculated in string.rs @@ -301,8 +314,9 @@ impl ValueData { } } - /// Resolve the property in the object - /// Returns a copy of the Property + /// Resolve the property in the object. + /// + /// Returns a copy of the Property. pub fn get_internal_slot(&self, field: &str) -> Value { let obj: Object = match *self { ValueData::Object(ref obj) => { @@ -578,6 +592,7 @@ impl ValueData { } } + /// Conversts the `Value` to `JSON`. pub fn to_json(&self) -> JSONValue { match *self { ValueData::Null @@ -603,6 +618,7 @@ impl ValueData { } /// Get the type of the value + /// /// https://tc39.es/ecma262/#sec-typeof-operator pub fn get_type(&self) -> &'static str { match *self {