boa/builtins/error/
syntax.rs

1//! This module implements the global `SyntaxError` object.
2//!
3//! The SyntaxError object represents an error when trying to interpret syntactically invalid code.
4//! It is thrown when the JavaScript context encounters tokens or token order that does not conform
5//! to the syntax of the language when parsing code.
6//!
7//! More information:
8//!  - [MDN documentation][mdn]
9//!  - [ECMAScript reference][spec]
10//!
11//! [spec]: https://tc39.es/ecma262/#sec-native-error-types-used-in-this-standard-syntaxerror
12//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError
13
14use crate::{
15    builtins::BuiltIn,
16    context::StandardObjects,
17    object::{internal_methods::get_prototype_from_constructor, ConstructorBuilder, ObjectData},
18    profiler::BoaProfiler,
19    property::Attribute,
20    Context, JsResult, JsValue,
21};
22
23/// JavaScript `SyntaxError` impleentation.
24#[derive(Debug, Clone, Copy)]
25pub(crate) struct SyntaxError;
26
27impl BuiltIn for SyntaxError {
28    const NAME: &'static str = "SyntaxError";
29
30    fn attribute() -> Attribute {
31        Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE
32    }
33
34    fn init(context: &mut Context) -> (&'static str, JsValue, Attribute) {
35        let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
36
37        let error_prototype = context.standard_objects().error_object().prototype();
38        let attribute = Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE;
39        let syntax_error_object = ConstructorBuilder::with_standard_object(
40            context,
41            Self::constructor,
42            context.standard_objects().syntax_error_object().clone(),
43        )
44        .name(Self::NAME)
45        .length(Self::LENGTH)
46        .inherit(error_prototype.into())
47        .property("name", Self::NAME, attribute)
48        .property("message", "", attribute)
49        .build();
50
51        (Self::NAME, syntax_error_object.into(), Self::attribute())
52    }
53}
54
55impl SyntaxError {
56    /// The amount of arguments this function object takes.
57    pub(crate) const LENGTH: usize = 1;
58
59    /// Create a new error object.
60    pub(crate) fn constructor(
61        new_target: &JsValue,
62        args: &[JsValue],
63        context: &mut Context,
64    ) -> JsResult<JsValue> {
65        let prototype =
66            get_prototype_from_constructor(new_target, StandardObjects::error_object, context)?;
67        let obj = context.construct_object();
68        obj.set_prototype_instance(prototype.into());
69        let this = JsValue::new(obj);
70        if let Some(message) = args.get(0) {
71            if !message.is_undefined() {
72                this.set_field("message", message.to_string(context)?, false, context)?;
73            }
74        }
75
76        // This value is used by console.log and other routines to match Object type
77        // to its Javascript Identifier (global constructor method name)
78        this.set_data(ObjectData::error());
79        Ok(this)
80    }
81}