Function quickjs_runtime::quickjs_utils::arrays::create_array_q
source · pub fn create_array_q(
q_ctx: &QuickJsRealmAdapter,
) -> Result<QuickJsValueAdapter, JsError>
Expand description
Create a new Array
§Example
use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::jsutils::Script;
use quickjs_runtime::quickjs_utils::{arrays, primitives, functions};
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();
// create a method to pass our new array to
q_ctx.eval(Script::new("create_array_test.es", "this.create_array_func = function(arr){return arr.length;};")).ok().expect("script failed");
// create a new array
let arr_ref = arrays::create_array_q(q_ctx).ok().expect("could not create array");
// add some values
let val0 = primitives::from_i32(12);
let val1 = primitives::from_i32(17);
arrays::set_element_q(q_ctx, &arr_ref, 0, &val0).expect("could not set element");
arrays::set_element_q(q_ctx, &arr_ref, 1, &val1).expect("could not set element");
// call the function
let result_ref = functions::invoke_member_function_q(q_ctx, &quickjs_utils::get_global_q(q_ctx), "create_array_func", &[arr_ref]).ok().expect("could not invoke function");
let len = primitives::to_i32(&result_ref).ok().unwrap();
assert_eq!(len, 2);
});