Function quickjs_runtime::quickjs_utils::objects::define_getter_setter_q
source · pub fn define_getter_setter_q(
q_ctx: &QuickJsRealmAdapter,
obj_ref: &QuickJsValueAdapter,
prop_name: &str,
getter_func_ref: &QuickJsValueAdapter,
setter_func_ref: &QuickJsValueAdapter,
) -> Result<(), JsError>
Expand description
define a getter/setter property
§Example
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
use quickjs_runtime::quickjs_utils::objects::{create_object_q, define_getter_setter_q, set_property_q};
use quickjs_runtime::quickjs_utils::functions::new_function_q;
use quickjs_runtime::quickjs_utils::primitives::from_i32;
use quickjs_runtime::quickjs_utils::{new_null_ref, get_global_q};
use quickjs_runtime::jsutils::Script;
use quickjs_runtime::JsError::JsError;
let rt = EsRuntimeBuilder::new().build();
rt.add_to_event_queue_sync(|q_js_rt| {
let q_ctx = q_js_rt.get_main_context();
let obj = create_object_q(q_ctx).ok().expect("create obj failed");
let getter_func = new_function_q(q_ctx, "getter", |_q_ctx, _this_ref, _args| {Ok(from_i32(13))}, 0).ok().expect("new_function_q getter failed");
let setter_func = new_function_q(q_ctx, "setter", |_q_ctx, _this_ref, args| {
log::debug!("setting someProperty to {:?}", &args[0]);
Ok(new_null_ref())
}, 1).ok().expect("new_function_q setter failed");
let res = define_getter_setter_q(q_ctx, &obj, "someProperty", &getter_func, &setter_func);
match res {
Ok(_) => {},
Err(e) => {panic!("define_getter_setter_q fail: {}", e)}}
let global = get_global_q(q_ctx);
set_property_q(q_ctx, &global, "testObj431", &obj).ok().expect("set prop on global failed");
});
rt.eval_sync(Script::new("define_getter_setter_q.es", "testObj431.someProperty = 'hello prop';")).ok().expect("script failed");