rong_arkjs 0.3.1

ArkJS backend for RongJS
Documentation
use crate::{ArkJSValue, arkjs};
use rong_core::JSValueImpl;
use rong_core::engine::JSProxyOps;

impl JSProxyOps for ArkJSValue {
    fn new_proxy(ctx: &Self::Context, target: Self, handler: Self) -> Result<Self, Self> {
        unsafe {
            let env = ctx.to_raw();
            let mut proxy: arkjs::JSVM_Value = std::ptr::null_mut();
            let status = arkjs::OH_JSVM_CreateProxy(
                env,
                target.raw_value_for_api(),
                handler.raw_value_for_api(),
                &mut proxy,
            );
            if status == arkjs::JSVM_Status_JSVM_OK {
                Ok(ArkJSValue::from_owned_raw(env, proxy).protect())
            } else {
                let mut exception: arkjs::JSVM_Value = std::ptr::null_mut();
                arkjs::OH_JSVM_GetAndClearLastException(env, &mut exception);
                Err(ArkJSValue::from_owned_raw(env, exception)
                    .protect()
                    .with_exception())
            }
        }
    }

    fn proxy_target(&self) -> Result<Self, Self> {
        unsafe {
            let mut target: arkjs::JSVM_Value = std::ptr::null_mut();
            let status =
                arkjs::OH_JSVM_ProxyGetTarget(self.env, self.raw_value_for_api(), &mut target);
            if status == arkjs::JSVM_Status_JSVM_OK {
                Ok(ArkJSValue::from_owned_raw(self.env, target).protect())
            } else {
                let mut exception: arkjs::JSVM_Value = std::ptr::null_mut();
                arkjs::OH_JSVM_GetAndClearLastException(self.env, &mut exception);
                Err(ArkJSValue::from_owned_raw(self.env, exception)
                    .protect()
                    .with_exception())
            }
        }
    }
}