• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Working with Proxy Using JSVM-API
2## Introduction
3This topic walks you through on how to use JSVM-API to create a proxy instance, check whether the given **JSVM_Value** is of the proxy type, and obtain the target object from the given proxy.
4
5## Available APIs
6
7| API                    | Description                                           |
8| ---------------------- | ----------------------------------------------- |
9| OH_JSVM_CreateProxy    | Creates a proxy. This API is equivalent to calling **new Proxy (target, handler)** in JS.|
10| OH_JSVM_IsProxy        | Checks whether the **JSVM_Value** is of the proxy type.                  |
11| OH_JSVM_ProxyGetTarget | Obtains the target object of the specified proxy.                               |
12
13## Example
14If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ code involved in proxy-related APIs.
15
16CPP code:
17```
18// Define OH_JSVM_CreateProxy.
19static JSVM_Value CreateProxy(JSVM_Env env, JSVM_CallbackInfo info) {
20    // Pass in two parameters: target and 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    // Call OH_JSVM_CreateProxy to create a proxy for the target object.
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// Define 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    // Call OH_JSVM_IsProxy to check whether JSVM_Value is a proxy.
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// Define 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    // Call OH_JSVM_ProxyGetTarget to obtain the target object in the proxy.
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// Register the proxy-related callbacks.
72static JSVM_CallbackStruct param[] = {{.data = nullptr, .callback = CreateProxy},
73                                      {.data = nullptr, .callback = IsProxy},
74                                      {.data = nullptr, .callback = GetProxyTarget}};
75static JSVM_CallbackStruct *method = param;
76// Aliases for the proxy-related callbacks to be called from JS.
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
100Expected result:
101```
102JSVM OH_JSVM_CreateProxy: success
103JSVM OH_JSVM_IsProxy: success: is a proxy
104JSVM OH_JSVM_ProxyGetTarget: success
105```
106