1# 使用 JSVM-API 提供的 proxy 接口 2## 简介 3JSVM-API 提供了创建 Proxy、判断 JSVM_Value 是否为 Proxy 类型和获取 Proxy 中的目标对象的接口。 4 5## 接口说明 6 7| 接口 | 功能说明 | 8| ---------------------- | ----------------------------------------------- | 9| OH_JSVM_CreateProxy | 创建 Proxy,等价于在 js 中执行 new Proxy(target, handler) | 10| OH_JSVM_IsProxy | 判断传入的 JSVM_Value 是否为 Proxy 类型 | 11| OH_JSVM_ProxyGetTarget | 获取给定 proxy 的目标对象 | 12 13## 使用示例 14JSVM-API接口开发流程参考[使用JSVM-API实现JS与C/C++语言交互开发流程](use-jsvm-process.md),本文仅对接口对应C++相关代码进行展示。 15 16cpp 部分代码 17``` 18// OH_JSVM_CreateProxy 的样例方法 19static JSVM_Value CreateProxy(JSVM_Env env, JSVM_CallbackInfo info) { 20 // 接受两个入参,第 1 个参数为 target,第 2 个参数为 handler 21 size_t argc = 2; 22 JSVM_Value args[2] = {nullptr}; 23 JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); 24 // 用 OH_JSVM_CreateProxy 为目标对象创建代理 25 JSVM_Value result = nullptr; 26 JSVM_Status status = OH_JSVM_CreateProxy(env, args[0], args[1], &result); 27 if (status != JSVM_OK) { 28 OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_CreateProxy: failed: %{public}d", status); 29 } else { 30 OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_CreateProxy: success"); 31 } 32 33 return result; 34} 35 36// OH_JSVM_IsProxy 的样例方法 37static JSVM_Value IsProxy(JSVM_Env env, JSVM_CallbackInfo info) { 38 size_t argc = 1; 39 JSVM_Value args[1] = {nullptr}; 40 JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); 41 // 调用 OH_JSVM_IsProxy 判断 JSVM_Value 是否为代理 42 bool result = false; 43 JSVM_Status status = OH_JSVM_IsProxy(env, args[0], &result); 44 if (status != JSVM_OK) { 45 OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_IsProxy: failed"); 46 } else { 47 OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_IsProxy: success: %{public}s", result ? "is a proxy" : "is not a proxy"); 48 } 49 JSVM_Value isProxy; 50 JSVM_CALL(OH_JSVM_GetBoolean(env, result, &isProxy)); 51 return isProxy; 52} 53 54// OH_JSVM_ProxyGetTarget 的样例方法 55static JSVM_Value GetProxyTarget(JSVM_Env env, JSVM_CallbackInfo info) { 56 size_t argc = 1; 57 JSVM_Value args[1] = {nullptr}; 58 JSVM_CALL(OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr)); 59 // 调用 OH_JSVM_ProxyGetTarget 获取代理中的目标对象 60 JSVM_Value result = nullptr; 61 JSVM_Status status = OH_JSVM_ProxyGetTarget(env, args[0], &result); 62 if (status != JSVM_OK) { 63 OH_LOG_ERROR(LOG_APP, "JSVM OH_JSVM_ProxyGetTarget: failed"); 64 } else { 65 OH_LOG_INFO(LOG_APP, "JSVM OH_JSVM_ProxyGetTarget: success"); 66 } 67 68 return result; 69} 70 71// Proxy 相关回调注册 72static JSVM_CallbackStruct param[] = {{.data = nullptr, .callback = CreateProxy}, 73 {.data = nullptr, .callback = IsProxy}, 74 {.data = nullptr, .callback = GetProxyTarget}}; 75static JSVM_CallbackStruct *method = param; 76// Proxy 相关回调别名 77static JSVM_PropertyDescriptor descriptor[] = { 78 {"CreateProxy", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 79 {"IsProxy", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}, 80 {"GetProxyTarget", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT}}; 81 82const char *srcCallNative = R"JS( 83 target = { 84 message1: "hello", 85 message2: "everyone", 86 }; 87 88 handler = { 89 get(target, prop, receiver) { 90 return "world"; 91 }, 92 }; 93 94 proxy = CreateProxy(target, handler) 95 isProxy = IsProxy(proxy) 96 target1 = GetProxyTarget(proxy) 97)JS"; 98``` 99 100预期的输出结果 101``` 102JSVM OH_JSVM_CreateProxy: success 103JSVM OH_JSVM_IsProxy: success: is a proxy 104JSVM OH_JSVM_ProxyGetTarget: success 105```