Skip to content

Commit

Permalink
Re-added Property Descriptor Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Redfire75369 committed Dec 23, 2023
1 parent 103a747 commit 8c620d9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use mozjs::gc::{RootedTraceableSet, Traceable};
use mozjs::gc::GCMethods;
use mozjs::jsapi::{
BigInt, Heap, JS_GetContextPrivate, JS_SetContextPrivate, JSContext, JSFunction, JSObject, JSScript, JSString,
PropertyKey, Symbol,
PropertyDescriptor, PropertyKey, Symbol,
};
use mozjs::jsval::JSVal;
use mozjs::rust::Runtime;
Expand Down Expand Up @@ -173,13 +173,13 @@ impl Context {
}
}

// TODO: (root_property_descriptor, PropertyDescriptor, property_descriptors, PropertyDescriptor),
impl_root_methods! {
(root_value, JSVal, values, Value),
(root_object, *mut JSObject, objects, Object),
(root_string, *mut JSString, strings, String),
(root_script, *mut JSScript, scripts, Script),
(root_property_key, PropertyKey, property_keys, PropertyKey),
(root_property_descriptor, PropertyDescriptor, property_descriptors, PropertyDescriptor),
(root_function, *mut JSFunction, functions, Function),
(root_bigint, *mut BigInt, big_ints, BigInt),
(root_symbol, *mut Symbol, symbols, Symbol),
Expand Down
3 changes: 1 addition & 2 deletions ion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ pub use functions::{Arguments, Function};
pub use future::PromiseFuture;
#[cfg(feature = "macros")]
pub use ion_proc::*;
pub use objects::{Array, Date, Iterator, JSIterator, Object, OwnedKey, Promise, PropertyKey, RegExp};
pub use objects::typedarray;
pub use objects::*;
pub use root::Root;
pub use stack::{Stack, StackRecord};
pub use string::{String, StringRef};
Expand Down
17 changes: 9 additions & 8 deletions ion/src/objects/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::ops::{Deref, DerefMut};
use mozjs::glue::{SetAccessorPropertyDescriptor, SetDataPropertyDescriptor};
use mozjs::jsapi::{FromPropertyDescriptor, Heap, ObjectToCompletePropertyDescriptor};
use mozjs::jsapi::PropertyDescriptor as JSPropertyDescriptor;
use mozjs::jsval::UndefinedValue;

use crate::{Context, Function, Object, Root, Value};
use crate::flags::PropertyFlags;
Expand All @@ -19,17 +20,17 @@ pub struct PropertyDescriptor {

impl PropertyDescriptor {
pub fn new(cx: &Context, value: &Value, attrs: PropertyFlags) -> PropertyDescriptor {
let mut desc = PropertyDescriptor::from(cx.root(JSPropertyDescriptor::default()));
rooted!(in(cx.as_ptr()) let mut desc = JSPropertyDescriptor::default());
unsafe { SetDataPropertyDescriptor(desc.handle_mut().into(), value.handle().into(), attrs.bits() as u32) };
desc
PropertyDescriptor::from(cx.root(desc.get()))
}

pub fn new_accessor(
cx: &Context, getter: &Function, setter: &Function, attrs: PropertyFlags,
) -> PropertyDescriptor {
let getter = getter.to_object(cx);
let setter = setter.to_object(cx);
let mut desc = PropertyDescriptor::from(cx.root(JSPropertyDescriptor::default()));
rooted!(in(cx.as_ptr()) let mut desc = JSPropertyDescriptor::default());
unsafe {
SetAccessorPropertyDescriptor(
desc.handle_mut().into(),
Expand All @@ -38,27 +39,27 @@ impl PropertyDescriptor {
attrs.bits() as u32,
)
};
desc
PropertyDescriptor::from(cx.root(desc.get()))
}

pub fn from_object(cx: &Context, object: &Object) -> Option<PropertyDescriptor> {
let mut desc = PropertyDescriptor::from(cx.root(JSPropertyDescriptor::default()));
let desc_value = Value::object(cx, object);
rooted!(in(cx.as_ptr()) let mut desc = JSPropertyDescriptor::default());
unsafe {
ObjectToCompletePropertyDescriptor(
cx.as_ptr(),
object.handle().into(),
desc_value.handle().into(),
desc.handle_mut().into(),
)
.then_some(desc)
.then(|| PropertyDescriptor::from(cx.root(desc.get())))
}
}

pub fn to_object(&self, cx: &Context) -> Option<Object> {
let mut value = Value::undefined(cx);
rooted!(in(cx.as_ptr()) let mut value = UndefinedValue());
unsafe { FromPropertyDescriptor(cx.as_ptr(), self.handle().into(), value.handle_mut().into()) }
.then(|| value.to_object(cx))
.then(|| Object::from(cx.root(value.to_object())))
}

pub fn is_configurable(&self) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions ion/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use mozjs::rust::{RealmOptions, SIMPLE_GLOBAL_CLASS};

pub use array::Array;
pub use date::Date;
// pub use descriptor::PropertyDescriptor;
pub use descriptor::PropertyDescriptor;
pub use iterator::{Iterator, JSIterator};
pub use key::{OwnedKey, PropertyKey};
pub use object::Object;
Expand All @@ -25,7 +25,7 @@ use crate::Context;

mod array;
mod date;
// mod descriptor;
mod descriptor;
mod iterator;
mod key;
mod object;
Expand Down

0 comments on commit 8c620d9

Please sign in to comment.