@@ -138,10 +138,15 @@ impl JSObject {
138
138
///
139
139
/// This can be used to create a new property, or to update an existing property.
140
140
///
141
- /// * `name `: A value that can be converted to a [`JSString`] containing
141
+ /// * `index `: A value that can be converted to a [`JSString`] containing
142
142
/// the property's name.
143
143
/// * `value`: A value containig the property's value.
144
144
///
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
+ ///
145
150
/// ```
146
151
/// # use javascriptcore::{JSContext, JSValue};
147
152
/// let ctx = JSContext::default();
@@ -175,6 +180,46 @@ impl JSObject {
175
180
Ok ( ( ) )
176
181
}
177
182
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
+
178
223
/// Call this object considering it is a valid function.
179
224
///
180
225
/// ```rust
@@ -346,6 +391,24 @@ mod tests {
346
391
Ok ( ( ) )
347
392
}
348
393
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
+
349
412
#[ test]
350
413
fn can_use_as_jsvalue_via_deref ( ) {
351
414
let ctx = JSContext :: default ( ) ;
0 commit comments