Function quickjs_runtime::quickjs_utils::arrays::get_element_q
source · pub fn get_element_q(
q_ctx: &QuickJsRealmAdapter,
array_ref: &QuickJsValueAdapter,
index: u32,
) -> Result<QuickJsValueAdapter, JsError>
Expand description
Get a single element from 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("get_element_test.es", "([1, 2, 3]);")).ok().expect("script failed");
// get a value, the 3 in this case
let val_ref = arrays::get_element_q(q_ctx, &arr_ref, 2).ok().unwrap();
let val_i32 = primitives::to_i32(&val_ref).ok().unwrap();
// get the length
assert_eq!(val_i32, 3);
});