diff --git a/boa/src/context.rs b/boa/src/context.rs index d2745a7a51..c504ad219e 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -10,7 +10,7 @@ use crate::{ class::{Class, ClassBuilder}, exec::Interpreter, object::{GcObject, Object, ObjectData, PROTOTYPE}, - property::{DataDescriptor, PropertyKey}, + property::{Attribute, DataDescriptor, PropertyKey}, realm::Realm, syntax::{ ast::{ @@ -614,6 +614,34 @@ impl Context { Ok(()) } + /// Register a global property. + /// + /// # Example + /// ``` + /// use boa::{Context, property::Attribute, object::ObjectInitializer}; + /// + /// let mut context = Context::new(); + /// + /// context.register_global_property("myPrimitiveProperty", 10, Attribute::all()); + /// + /// let object = ObjectInitializer::new(&mut context) + /// .property("x", 0, Attribute::all()) + /// .property("y", 1, Attribute::all()) + /// .build(); + /// context.register_global_property("myObjectProperty", object, Attribute::all()); + /// ``` + pub fn register_global_property(&mut self, key: K, value: V, attribute: Attribute) + where + K: Into, + V: Into, + { + let property = DataDescriptor::new(value, attribute); + self.global_object() + .as_object() + .unwrap() + .insert(key, property); + } + /// Evaluates the given code. /// /// # Examples