From 53c5bd463b4b7dd703b480c228b9dc4cc9ecabcb Mon Sep 17 00:00:00 2001 From: chenraozhong Date: Fri, 15 Nov 2024 14:41:19 +0800 Subject: [PATCH] Add jsvm proxy API Signed-off-by: wangyimin --- ark_runtime/jsvm/jsvm.h | 55 +++++++++++++++++++++++++++++++ ark_runtime/jsvm/jsvm_types.h | 2 ++ ark_runtime/jsvm/libjsvm.ndk.json | 12 +++++++ 3 files changed, 69 insertions(+) diff --git a/ark_runtime/jsvm/jsvm.h b/ark_runtime/jsvm/jsvm.h index 3f47366b4..eabedc882 100644 --- a/ark_runtime/jsvm/jsvm.h +++ b/ark_runtime/jsvm/jsvm.h @@ -2314,6 +2314,61 @@ JSVM_EXTERN JSVM_Status OH_JSVM_IsPromise(JSVM_Env env, JSVM_Value value, bool* isPromise); +/** + * @brief This API allocates a default JavaScript Proxy. It is the equivalent of + * doing new Proxy(target, handler) in JavaScript. + * + * @param env The environment that the API is invoked under. + * @param target A JSVM_Value representing the JavaScript Object which you want to proxy. + * @param handler A JSVM_Value representing the JavaScript Object that defines which + * operations will be intercepted and how to redefine intercepted operations. + * @param result A JSVM_Value representing a JavaScript Proxy. + * @return Returns JSVM functions result code. + * {@link JSVM_OK } if the API succeeded. \n + * {@link JSVM_INVALID_ARG } if the any of the input arguments is NULL. \n + * {@link JSVM_OBJECT_EXPECTED} if target or handler is not Javascript Object. \n + * {@link JSVM_PENDING_EXCEPTION} if an exception occurs. \n + * + * @since 16 + */ +JSVM_EXTERN JSVM_Status OH_JSVM_CreateProxy(JSVM_Env env, + JSVM_Value target, + JSVM_Value handler, + JSVM_Value* result); + +/** + * @brief This API checks if the value passed in is a Proxy. + * + * @param env The environment that the API is invoked under. + * @param value The JavaScript value to check. + * @param isProxy Whether the given value is Proxy. + * @return Returns JSVM functions result code. + * {@link JSVM_OK } if the API succeeded. \n + * {@link JSVM_INVALID_ARG } if the any of the input arguments is NULL. \n + * + * @since 16 + */ +JSVM_EXTERN JSVM_Status OH_JSVM_IsProxy(JSVM_Env env, + JSVM_Value value, + bool* isProxy); + +/** + * @brief This API gets target from proxy. + * + * @param env The environment that the API is invoked under. + * @param value JSVM_Value representing JavaScript Proxy whose target to return. + * @param result Target of the given proxy. + * @return Returns JSVM functions result code. + * {@link JSVM_OK } if the API succeeded. \n + * {@link JSVM_INVALID_ARG } if the any of the input arguments is NULL. \n + * {@link JSVM_INVALID_TYPE} if value is not a Javascript Proxy. \n + * + * @since 16 + */ +JSVM_EXTERN JSVM_Status OH_JSVM_ProxyGetTarget(JSVM_Env env, + JSVM_Value value, + JSVM_Value* result); + /** * @brief This API parses a JSON string and returns it as value if successful. * @param env: The environment that the API is invoked under. diff --git a/ark_runtime/jsvm/jsvm_types.h b/ark_runtime/jsvm/jsvm_types.h index 1141b051f..0ba016fba 100644 --- a/ark_runtime/jsvm/jsvm_types.h +++ b/ark_runtime/jsvm/jsvm_types.h @@ -327,6 +327,8 @@ typedef enum { JSVM_NO_EXTERNAL_BUFFERS_ALLOWED, /** cannot run +js status. */ JSVM_CANNOT_RUN_JS, + /** invalid input type status. */ + JSVM_INVALID_TYPE, } JSVM_Status; /** diff --git a/ark_runtime/jsvm/libjsvm.ndk.json b/ark_runtime/jsvm/libjsvm.ndk.json index e0157b772..65045fa77 100644 --- a/ark_runtime/jsvm/libjsvm.ndk.json +++ b/ark_runtime/jsvm/libjsvm.ndk.json @@ -734,5 +734,17 @@ { "first_introduced": "12", "name": "OH_JSVM_ReleaseCache" + }, + { + "first_introduced": "16", + "name": "OH_JSVM_CreateProxy" + }, + { + "first_introduced": "16", + "name": "OH_JSVM_IsProxy" + }, + { + "first_introduced": "16", + "name": "OH_JSVM_ProxyGetTarget" } ] -- Gitee