1 /** 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JS_REFCONVERT_RECORD_H_ 17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JS_REFCONVERT_RECORD_H_ 18 19 #include "libpandabase/macros.h" 20 #include "plugins/ets/runtime/ets_class_linker_extension.h" 21 #include "plugins/ets/runtime/interop_js/interop_context.h" 22 #include "plugins/ets/runtime/interop_js/interop_common.h" 23 #include "plugins/ets/runtime/interop_js/js_refconvert.h" 24 #include "plugins/ets/runtime/interop_js/js_convert.h" 25 #include "plugins/ets/runtime/interop_js/js_refconvert_function.h" 26 #include "plugins/ets/runtime/interop_js/code_scopes.h" 27 #include "plugins/ets/runtime/types/ets_object.h" 28 #include "plugins/ets/runtime/types/ets_type.h" 29 #include "runtime/mem/local_object_handle.h" 30 31 namespace ark::ets::interop::js { 32 33 class JSRefConvertRecord : public JSRefConvert { 34 public: JSRefConvertRecord(InteropCtx * ctx)35 explicit JSRefConvertRecord(InteropCtx *ctx) : JSRefConvert(this) 36 { 37 auto env = ctx->GetJSEnv(); 38 napi_value handlerObj; 39 NAPI_CHECK_FATAL(napi_create_object(env, &handlerObj)); 40 41 napi_value getHandleFunc; 42 napi_value setHandleFunc; 43 NAPI_CHECK_FATAL(napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, JSRefConvertRecord::RecordGetHandler, 44 nullptr, &getHandleFunc)); 45 NAPI_CHECK_FATAL(napi_create_function(env, nullptr, NAPI_AUTO_LENGTH, JSRefConvertRecord::RecordSetHandler, 46 nullptr, &setHandleFunc)); 47 NAPI_CHECK_FATAL(napi_set_named_property(env, handlerObj, "get", getHandleFunc)); 48 NAPI_CHECK_FATAL(napi_set_named_property(env, handlerObj, "set", setHandleFunc)); 49 50 NAPI_CHECK_FATAL(napi_create_reference(env, handlerObj, 1, &handleObjCache_)); 51 } 52 53 napi_value WrapImpl(InteropCtx *ctx, EtsObject *obj); 54 55 EtsObject *UnwrapImpl(InteropCtx *ctx, napi_value jsValue); 56 57 private: 58 napi_ref handleObjCache_ {}; 59 60 static napi_value RecordGetHandler(napi_env env, napi_callback_info cbinfo); 61 62 static napi_value RecordSetHandler(napi_env env, napi_callback_info cbinfo); 63 }; 64 65 } // namespace ark::ets::interop::js 66 67 #endif // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_JS_REFCONVERT_RECORD_H_ 68