1 /**
2 * Copyright (c) 2024-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 #include "plugins/ets/runtime/interop_js/handshake.h"
17 #include "plugins/ets/runtime/interop_js/code_scopes.h"
18 #include "hybrid/ecma_vm_interface.h"
19
20 #include "interfaces/inner_api/napi/native_node_hybrid_api.h"
21
22 napi_status __attribute__((weak)) // CC-OFF(G.FMT.10) project code style
napi_vm_handshake(napi_env env,void * stsIface,void ** ecmaIface)23 napi_vm_handshake([[maybe_unused]] napi_env env, [[maybe_unused]] void *stsIface, [[maybe_unused]] void **ecmaIface)
24 {
25 INTEROP_LOG(FATAL) << "ETS_INTEROP_GTEST_PLUGIN: " << __func__
26 << " is implemented in later versions of OHOS, please update." << std::endl;
27 return napi_ok;
28 }
29
30 namespace ark::ets::interop::js {
31
VmHandshake(napi_env env,InteropCtx * ctx)32 void Handshake::VmHandshake(napi_env env, InteropCtx *ctx)
33 {
34 void *jsvmIface;
35 auto status = napi_vm_handshake(env, ctx->GetSTSVMInterface(), &jsvmIface);
36 if (status != napi_status::napi_ok) {
37 InteropCtx::ThrowJSError(env, "Handshake error: napi_vm_handshake failed");
38 return;
39 }
40 if (jsvmIface == nullptr) {
41 InteropCtx::ThrowJSError(env, "Handshake error: got null VMInterfaceType");
42 return;
43 }
44 auto *iface = static_cast<arkplatform::VMInterface *>(jsvmIface);
45 if (iface->GetVMType() != arkplatform::VMInterface::VMInterfaceType::ECMA_VM_IFACE) {
46 InteropCtx::ThrowJSError(env, "Handshake error: got wrong VMInterfaceType");
47 return;
48 }
49 ctx->CreateXGCVmAdaptor<XGCVmAdaptor>(env, static_cast<arkplatform::EcmaVMInterface *>(jsvmIface));
50 }
51
52 } // namespace ark::ets::interop::js
53