Browse Source

Feature `Context::register_global_property()` (#912)

pull/914/head
Halid Odat 4 years ago committed by GitHub
parent
commit
4eb2ed4c9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      boa/src/context.rs

30
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<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

Loading…
Cancel
Save