Browse Source

Added documentation to builtins/json

pull/293/head
HalidOdat 5 years ago
parent
commit
3e2e241f3e
  1. 33
      boa/src/builtins/json.rs
  2. 1
      boa/src/builtins/mod.rs
  3. 3
      boa/src/syntax/lexer/mod.rs

33
boa/src/builtins/json.rs

@ -1,6 +1,6 @@
//! This module implements the global `JSON` object. //! This module implements the global `JSON` object.
//! //!
//! The `JSON` object contains methods for parsing [JavaScript Object Notation (JSON)][json] //! The `JSON` object contains methods for parsing [JavaScript Object Notation (JSON)][spec]
//! and converting values to JSON. It can't be called or constructed, and aside from its //! and converting values to JSON. It can't be called or constructed, and aside from its
//! two method properties, it has no interesting functionality of its own. //! two method properties, it has no interesting functionality of its own.
//! //!
@ -14,12 +14,22 @@
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON //! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON
use crate::builtins::function::NativeFunctionData; use crate::builtins::function::NativeFunctionData;
use crate::builtins::object::{Object, ObjectKind, PROTOTYPE};
use crate::builtins::value::{to_value, ResultValue, Value, ValueData}; use crate::builtins::value::{to_value, ResultValue, Value, ValueData};
use crate::exec::Interpreter; use crate::exec::Interpreter;
use serde_json::{self, Value as JSONValue}; use serde_json::{self, Value as JSONValue};
/// Parse a JSON string into a Javascript object /// The `JSON` method parses a JSON string, constructing the JavaScript value or object described by the string.
/// <https://tc39.es/ecma262/#sec-json.parse> ///
/// An optional `reviver` function can be provided to perform a transformation on the resulting object before it is returned.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-json.parse
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
// TODO: implement optional revever argument.
pub fn parse(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub fn parse(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
match serde_json::from_str::<JSONValue>( match serde_json::from_str::<JSONValue>(
&args &args
@ -33,14 +43,27 @@ pub fn parse(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
} }
} }
/// Process a Javascript object into a JSON string /// The `JSON` method converts a JavaScript object or value to a JSON string.
///
/// This medhod optionally replaces values if a `replacer` function is specified or
/// optionally including only the specified properties if a replacer array is specified.
///
/// An optional `space` argument can be supplied of type `String` or `Number` that's used to insert
/// white space into the output JSON string for readability purposes.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-json.stringify
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
let obj = args.get(0).expect("cannot get argument for JSON.stringify"); let obj = args.get(0).expect("cannot get argument for JSON.stringify");
let json = obj.to_json().to_string(); let json = obj.to_json().to_string();
Ok(to_value(json)) Ok(to_value(json))
} }
/// Create a new `JSON` object /// Create a new `JSON` object.
pub fn create_constructor(global: &Value) -> Value { pub fn create_constructor(global: &Value) -> Value {
let json = ValueData::new_obj(Some(global)); let json = ValueData::new_obj(Some(global));

1
boa/src/builtins/mod.rs

@ -22,7 +22,6 @@ pub mod console;
pub mod error; pub mod error;
/// The global `Function` object and function value representations /// The global `Function` object and function value representations
pub mod function; pub mod function;
/// The global `JSON` object
pub mod json; pub mod json;
/// The global `Math` object /// The global `Math` object
pub mod math; pub mod math;

3
boa/src/syntax/lexer/mod.rs

@ -135,7 +135,6 @@ impl<'a> Lexer<'a> {
/// ///
/// * `buffer` - A string slice that holds the source code. /// * `buffer` - A string slice that holds the source code.
/// The buffer needs to have a lifetime as long as the Lexer instance itself /// The buffer needs to have a lifetime as long as the Lexer instance itself
///
pub fn new(buffer: &'a str) -> Lexer<'a> { pub fn new(buffer: &'a str) -> Lexer<'a> {
Lexer { Lexer {
tokens: Vec::new(), tokens: Vec::new(),
@ -145,7 +144,7 @@ impl<'a> Lexer<'a> {
} }
} }
/// Push a token onto the token queue /// Push a token onto the token queue.
fn push_token(&mut self, tk: TokenKind) { fn push_token(&mut self, tk: TokenKind) {
self.tokens self.tokens
.push(Token::new(tk, self.line_number, self.column_number)) .push(Token::new(tk, self.line_number, self.column_number))

Loading…
Cancel
Save