boa/syntax/ast/node/identifier/
mod.rs

1//! Local identifier node.
2
3use crate::{
4    exec::Executable,
5    gc::{Finalize, Trace},
6    syntax::ast::node::Node,
7    BoaProfiler, Context, JsResult, JsValue,
8};
9use std::fmt;
10
11#[cfg(feature = "deser")]
12use serde::{Deserialize, Serialize};
13/// An `identifier` is a sequence of characters in the code that identifies a variable,
14/// function, or property.
15///
16/// In JavaScript, identifiers are case-sensitive and can contain Unicode letters, $, _, and
17/// digits (0-9), but may not start with a digit.
18///
19/// An identifier differs from a string in that a string is data, while an identifier is part
20/// of the code. In JavaScript, there is no way to convert identifiers to strings, but
21/// sometimes it is possible to parse strings into identifiers.
22///
23/// More information:
24///  - [ECMAScript reference][spec]
25///  - [MDN documentation][mdn]
26///
27/// [spec]: https://tc39.es/ecma262/#prod-Identifier
28/// [mdn]: https://developer.mozilla.org/en-US/docs/Glossary/Identifier
29#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
30#[cfg_attr(feature = "deser", serde(transparent))]
31#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
32pub struct Identifier {
33    ident: Box<str>,
34}
35
36impl Executable for Identifier {
37    fn run(&self, context: &mut Context) -> JsResult<JsValue> {
38        let _timer = BoaProfiler::global().start_event("Identifier", "exec");
39        context.get_binding_value(self.as_ref())
40    }
41}
42
43impl fmt::Display for Identifier {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        fmt::Display::fmt(&self.ident, f)
46    }
47}
48
49impl AsRef<str> for Identifier {
50    fn as_ref(&self) -> &str {
51        &self.ident
52    }
53}
54
55impl<T> From<T> for Identifier
56where
57    T: Into<Box<str>>,
58{
59    fn from(stm: T) -> Self {
60        Self { ident: stm.into() }
61    }
62}
63
64impl From<Identifier> for Node {
65    fn from(local: Identifier) -> Self {
66        Self::Identifier(local)
67    }
68}