boa_engine/builtins/error/
reference.rs1use crate::{
13 Context, JsArgs, JsResult, JsString, JsValue,
14 builtins::{BuiltInBuilder, BuiltInConstructor, BuiltInObject, IntrinsicObject},
15 context::intrinsics::{Intrinsics, StandardConstructor, StandardConstructors},
16 js_string,
17 object::{JsObject, internal_methods::get_prototype_from_constructor},
18 property::Attribute,
19 realm::Realm,
20 string::StaticJsStrings,
21};
22
23use super::{Error, ErrorKind};
24
25#[derive(Debug, Clone, Copy)]
26pub(crate) struct ReferenceError;
27
28impl IntrinsicObject for ReferenceError {
29 fn init(realm: &Realm) {
30 let attribute = Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE;
31 BuiltInBuilder::from_standard_constructor::<Self>(realm)
32 .prototype(realm.intrinsics().constructors().error().constructor())
33 .inherits(Some(realm.intrinsics().constructors().error().prototype()))
34 .property(js_string!("name"), Self::NAME, attribute)
35 .property(js_string!("message"), js_string!(), attribute)
36 .build();
37 }
38
39 fn get(intrinsics: &Intrinsics) -> JsObject {
40 Self::STANDARD_CONSTRUCTOR(intrinsics.constructors()).constructor()
41 }
42}
43
44impl BuiltInObject for ReferenceError {
45 const NAME: JsString = StaticJsStrings::REFERENCE_ERROR;
46}
47
48impl BuiltInConstructor for ReferenceError {
49 const CONSTRUCTOR_ARGUMENTS: usize = 1;
50 const PROTOTYPE_STORAGE_SLOTS: usize = 2;
51 const CONSTRUCTOR_STORAGE_SLOTS: usize = 0;
52
53 const STANDARD_CONSTRUCTOR: fn(&StandardConstructors) -> &StandardConstructor =
54 StandardConstructors::reference_error;
55
56 fn constructor(
58 new_target: &JsValue,
59 args: &[JsValue],
60 context: &mut Context,
61 ) -> JsResult<JsValue> {
62 let new_target = &if new_target.is_undefined() {
64 context
65 .active_function_object()
66 .unwrap_or_else(|| {
67 context
68 .intrinsics()
69 .constructors()
70 .reference_error()
71 .constructor()
72 })
73 .into()
74 } else {
75 new_target.clone()
76 };
77 let prototype = get_prototype_from_constructor(
79 new_target,
80 StandardConstructors::reference_error,
81 context,
82 )?;
83 let o = JsObject::from_proto_and_data_with_shared_shape(
84 context.root_shape(),
85 prototype,
86 Error::with_caller_position(ErrorKind::Reference, context),
87 );
88
89 let message = args.get_or_undefined(0);
91 if !message.is_undefined() {
92 let msg = message.to_string(context)?;
94
95 o.create_non_enumerable_data_property_or_throw(js_string!("message"), msg, context);
97 }
98
99 Error::install_error_cause(&o, args.get_or_undefined(1), context)?;
101
102 Ok(o.into())
104 }
105}