Browse Source

Added some documentation to value

pull/293/head
HalidOdat 4 years ago
parent
commit
4a6276fc93
  1. 4
      boa/src/builtins/mod.rs
  2. 14
      boa/src/builtins/number/mod.rs
  3. 30
      boa/src/builtins/value/mod.rs

4
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;

14
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:

30
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<Value, Value>;
/// A Garbage-collected Javascript value as represented in the interpreter
/// A Garbage-collected Javascript value as represented in the interpreter.
pub type Value = Gc<ValueData>;
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.
///
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal would turn extensible to false/>
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze would also turn extensible to false/>
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<Property> {
// 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 {

Loading…
Cancel
Save