Skip to content

Commit

Permalink
Feature Context::register_global_property() (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Oct 24, 2020
1 parent 16e3d2e commit 4eb2ed4
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<K, V>(&mut self, key: K, value: V, attribute: Attribute)
where
K: Into<PropertyKey>,
V: Into<Value>,
{
let property = DataDescriptor::new(value, attribute);
self.global_object()
.as_object()
.unwrap()
.insert(key, property);
}

/// Evaluates the given code.
///
/// # Examples
Expand Down

0 comments on commit 4eb2ed4

Please sign in to comment.