boa/builtins/error/
syntax.rs1use 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#[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 pub(crate) const LENGTH: usize = 1;
58
59 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.set_data(ObjectData::error());
79 Ok(this)
80 }
81}