boa/syntax/ast/node/field/get_field/
mod.rs

1use crate::{
2    exec::Executable,
3    gc::{Finalize, Trace},
4    syntax::ast::node::Node,
5    Context, JsResult, JsValue,
6};
7use std::fmt;
8
9#[cfg(feature = "deser")]
10use serde::{Deserialize, Serialize};
11
12/// This property accessor provides access to an object's properties by using the
13/// [bracket notation][mdn].
14///
15/// In the object\[property_name\] syntax, the property_name is just a string or
16/// [Symbol][symbol]. So, it can be any string, including '1foo', '!bar!', or even ' ' (a
17/// space).
18///
19/// One can think of an object as an associative array (a.k.a. map, dictionary, hash, lookup
20/// table). The keys in this array are the names of the object's properties.
21///
22/// It's typical when speaking of an object's properties to make a distinction between
23/// properties and methods. However, the property/method distinction is little more than a
24/// convention. A method is simply a property that can be called (for example, if it has a
25/// reference to a Function instance as its value).
26///
27/// More information:
28///  - [ECMAScript reference][spec]
29///  - [MDN documentation][mdn]
30///
31/// [spec]: https://tc39.es/ecma262/#sec-property-accessors
32/// [symbol]: https://developer.mozilla.org/en-US/docs/Glossary/Symbol
33/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#Bracket_notation
34#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
35#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
36pub struct GetField {
37    obj: Box<Node>,
38    field: Box<Node>,
39}
40
41impl GetField {
42    pub fn obj(&self) -> &Node {
43        &self.obj
44    }
45
46    pub fn field(&self) -> &Node {
47        &self.field
48    }
49
50    /// Creates a `GetField` AST node.
51    pub fn new<V, F>(value: V, field: F) -> Self
52    where
53        V: Into<Node>,
54        F: Into<Node>,
55    {
56        Self {
57            obj: Box::new(value.into()),
58            field: Box::new(field.into()),
59        }
60    }
61}
62
63impl Executable for GetField {
64    fn run(&self, context: &mut Context) -> JsResult<JsValue> {
65        let mut obj = self.obj().run(context)?;
66        if !obj.is_object() {
67            obj = JsValue::Object(obj.to_object(context)?);
68        }
69        let field = self.field().run(context)?;
70
71        obj.get_field(field.to_property_key(context)?, context)
72    }
73}
74
75impl fmt::Display for GetField {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "{}[{}]", self.obj(), self.field())
78    }
79}
80
81impl From<GetField> for Node {
82    fn from(get_field: GetField) -> Self {
83        Self::GetField(get_field)
84    }
85}