Skip to content

Commit 2b98c24

Browse files
Hywanwaywardmonkeys
authored andcommitted
feat: Implement JSObject::set_property_at_index.
This patch implements `JSObject::set_property_at_index`. Behind the scene, it uses `sys::JSOBjectSetPropertyAtIndex`.
1 parent 5eb6612 commit 2b98c24

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

Diff for: src/object.rs

+64-1
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ impl JSObject {
138138
///
139139
/// This can be used to create a new property, or to update an existing property.
140140
///
141-
/// * `name`: A value that can be converted to a [`JSString`] containing
141+
/// * `index`: A value that can be converted to a [`JSString`] containing
142142
/// the property's name.
143143
/// * `value`: A value containig the property's value.
144144
///
145+
/// Calling `get_property_at_index` is equivalent to calling
146+
/// `get_property` with a string containing `index`,
147+
/// but `get_property_at_index` provides optimized access to
148+
/// numeric properties.
149+
///
145150
/// ```
146151
/// # use javascriptcore::{JSContext, JSValue};
147152
/// let ctx = JSContext::default();
@@ -175,6 +180,46 @@ impl JSObject {
175180
Ok(())
176181
}
177182

183+
/// Set a property onto an object by using a numeric index.
184+
///
185+
/// This can be used to create a new property, or to update an existing property.
186+
///
187+
/// * `index`: An integer value that is the property's name.
188+
/// * `value`: A value containig the property's value.
189+
///
190+
/// Calling `set_property_at_index` is equivalent to calling `set_property` with
191+
/// a string containing `index`, but `set_property_at_index` provides optimized
192+
/// access to numeric properties.
193+
///
194+
/// ```
195+
/// # use javascriptcore::{JSContext, JSValue};
196+
/// let ctx = JSContext::default();
197+
/// let object = JSValue::new_from_json(&ctx, r#"[10]"#).expect("valid array").as_object().unwrap();
198+
/// object.set_property_at_index(1, JSValue::new_number(&ctx, 11.)).unwrap();
199+
///
200+
/// assert!(object.has_property("0"));
201+
/// assert!(object.has_property("1"));
202+
/// ```
203+
pub fn set_property_at_index(&self, index: u32, value: JSValue) -> Result<(), JSException> {
204+
let mut exception: sys::JSValueRef = ptr::null_mut();
205+
let context = self.value.ctx;
206+
207+
unsafe {
208+
sys::JSObjectSetPropertyAtIndex(context, self.raw, index, value.raw, &mut exception)
209+
}
210+
211+
if !exception.is_null() {
212+
return Err(JSException {
213+
value: JSValue {
214+
raw: exception,
215+
ctx: context,
216+
},
217+
});
218+
}
219+
220+
Ok(())
221+
}
222+
178223
/// Call this object considering it is a valid function.
179224
///
180225
/// ```rust
@@ -346,6 +391,24 @@ mod tests {
346391
Ok(())
347392
}
348393

394+
#[test]
395+
fn can_set_property_at_index() -> Result<(), JSException> {
396+
let ctx = JSContext::default();
397+
let object = JSValue::new_from_json(&ctx, r#"[10]"#)
398+
.unwrap()
399+
.as_object()?;
400+
401+
assert!(object.has_property("0"));
402+
assert!(!object.has_property("1"));
403+
404+
object.set_property_at_index(1, JSValue::new_number(&ctx, 11.))?;
405+
406+
assert!(object.has_property("1"));
407+
assert_eq!(object.get_property_at_index(1).as_number()?, 11.);
408+
409+
Ok(())
410+
}
411+
349412
#[test]
350413
fn can_use_as_jsvalue_via_deref() {
351414
let ctx = JSContext::default();

0 commit comments

Comments
 (0)