Function quickjs_runtime::quickjs_utils::arrays::set_element_q

source ·
pub fn set_element_q(
    q_ctx: &QuickJsRealmAdapter,
    array_ref: &QuickJsValueAdapter,
    index: u32,
    entry_value_ref: &QuickJsValueAdapter,
) -> Result<(), JsError>
Expand description

Set a single element in an array

§Example

use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::jsutils::Script;
use quickjs_runtime::quickjs_utils::{arrays, primitives};
use quickjs_runtime::quickjs_utils;

let rt = QuickJsRuntimeBuilder::new().build();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
    let q_ctx = q_js_rt.get_main_realm();
    // get an Array from script
    let arr_ref = q_ctx.eval(Script::new("set_element_test.es", "([1, 2, 3]);")).ok().expect("script failed");
    // add some values
    arrays::set_element_q(q_ctx, &arr_ref, 3, &primitives::from_i32(12)).expect("could not set element");
    arrays::set_element_q(q_ctx, &arr_ref, 4, &primitives::from_i32(17)).expect("could not set element");
    // get the length
    let len = arrays::get_length_q(q_ctx, &arr_ref).ok().unwrap();
    assert_eq!(len, 5);
});