1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use std::fmt;
use chakracore_sys::*;
use context::{Context, ContextGuard};

/// A property identifier used with objects.
#[derive(PartialEq)]
pub struct Property(JsPropertyIdRef);

impl Property {
    /// Creates a property identifier from a string.
    ///
    /// If a property identifier with this name has already been created, it will
    /// be returned instead of creating a new one.
    pub fn new(_guard: &ContextGuard, name: &str) -> Self {
        let bytes = name.as_bytes();
        let mut reference = JsPropertyIdRef::new();
        unsafe {
            jsassert!(JsCreatePropertyId(bytes.as_ptr() as _, bytes.len(), &mut reference));
            Self::from_raw(reference)
        }
    }

    /// Converts a JavaScript property to a native string.
    pub fn to_string(&self, _guard: &ContextGuard) -> String {
        ::util::to_string_impl(self.as_raw(), JsCopyPropertyId)
            .expect("converting property to string")
    }
}

impl fmt::Debug for Property {
    /// Only use for debugging, it relies on an implicit active context and
    /// panics otherwise.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Context::exec_with_current(|guard| {
            let output = self.to_string(&guard);
            write!(f, "Property('{}')", output)
        }).expect("property debug output without an active context")
    }
}

reference!(Property);

#[cfg(test)]
mod tests {
    use {test, Property};

    #[test]
    fn string_conversion() {
        test::run_with_context(|guard| {
            let property = Property::new(guard, "foo");
            assert_eq!(property.to_string(guard), "foo");
        });
    }
}