boa/syntax/ast/node/field/get_const_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/// [dot notation][mdn].
14///
15/// In the object.property syntax, the property must be a valid JavaScript identifier.
16/// (In the ECMAScript standard, the names of properties are technically "IdentifierNames", not
17/// "Identifiers", so reserved words can be used but are not recommended).
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/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#Dot_notation
33#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
34#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
35pub struct GetConstField {
36 obj: Box<Node>,
37 field: Box<str>,
38}
39
40impl GetConstField {
41 /// Creates a `GetConstField` AST node.
42 pub fn new<V, L>(value: V, label: L) -> Self
43 where
44 V: Into<Node>,
45 L: Into<Box<str>>,
46 {
47 Self {
48 obj: Box::new(value.into()),
49 field: label.into(),
50 }
51 }
52
53 /// Gets the original object from where to get the field from.
54 pub fn obj(&self) -> &Node {
55 &self.obj
56 }
57
58 /// Gets the name of the field to retrieve.
59 pub fn field(&self) -> &str {
60 &self.field
61 }
62}
63
64impl Executable for GetConstField {
65 fn run(&self, context: &mut Context) -> JsResult<JsValue> {
66 let mut obj = self.obj().run(context)?;
67 if !obj.is_object() {
68 obj = JsValue::Object(obj.to_object(context)?);
69 }
70
71 obj.get_field(self.field(), context)
72 }
73}
74
75impl fmt::Display for GetConstField {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 write!(f, "{}.{}", self.obj(), self.field())
78 }
79}
80
81impl From<GetConstField> for Node {
82 fn from(get_const_field: GetConstField) -> Self {
83 Self::GetConstField(get_const_field)
84 }
85}