133eb0b6dSopenharmony_ci/* 233eb0b6dSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 333eb0b6dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 433eb0b6dSopenharmony_ci * you may not use this file except in compliance with the License. 533eb0b6dSopenharmony_ci * You may obtain a copy of the License at 633eb0b6dSopenharmony_ci * 733eb0b6dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 833eb0b6dSopenharmony_ci * 933eb0b6dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1033eb0b6dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1133eb0b6dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1233eb0b6dSopenharmony_ci * See the License for the specific language governing permissions and 1333eb0b6dSopenharmony_ci * limitations under the License. 1433eb0b6dSopenharmony_ci */ 1533eb0b6dSopenharmony_ci#include "js_napi_common.h" 1633eb0b6dSopenharmony_ci#include "napi/native_api.h" 1733eb0b6dSopenharmony_ci#include "napi/native_common.h" 1833eb0b6dSopenharmony_ci#include "napi/native_node_api.h" 1933eb0b6dSopenharmony_ci#include "utils/log.h" 2033eb0b6dSopenharmony_cinamespace ACE { 2133eb0b6dSopenharmony_cinamespace NAPI { 2233eb0b6dSopenharmony_cinamespace SYSTEM_TEST_NAPI { 2333eb0b6dSopenharmony_cistatic napi_value CreateArrayBuffer(napi_env env, napi_callback_info info) 2433eb0b6dSopenharmony_ci{ 2533eb0b6dSopenharmony_ci HILOG_INFO("%{public}s,called", __func__); 2633eb0b6dSopenharmony_ci 2733eb0b6dSopenharmony_ci void* arrayBufferPtr = nullptr; 2833eb0b6dSopenharmony_ci napi_value arrayBuffer = nullptr; 2933eb0b6dSopenharmony_ci size_t arrayBufferSize = 1024*4; 3033eb0b6dSopenharmony_ci NAPI_CALL(env, napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer)); 3133eb0b6dSopenharmony_ci 3233eb0b6dSopenharmony_ci return arrayBuffer; 3333eb0b6dSopenharmony_ci} 3433eb0b6dSopenharmony_ci 3533eb0b6dSopenharmony_cistatic napi_value DetachArrayBuffer(napi_env env, napi_callback_info info) 3633eb0b6dSopenharmony_ci{ 3733eb0b6dSopenharmony_ci HILOG_INFO("%{public}s,called", __func__); 3833eb0b6dSopenharmony_ci size_t argc = 1; 3933eb0b6dSopenharmony_ci napi_value args[1] = { nullptr }; 4033eb0b6dSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); 4133eb0b6dSopenharmony_ci NAPI_ASSERT(env, argc == 1, "Wrong number of arguments."); 4233eb0b6dSopenharmony_ci 4333eb0b6dSopenharmony_ci bool isArrayBuffer = false; 4433eb0b6dSopenharmony_ci NAPI_CALL(env, napi_is_arraybuffer(env, args[0], &isArrayBuffer)); 4533eb0b6dSopenharmony_ci HILOG_INFO("%{public}s called isArrayBuffer = %{public}s", __func__, (isArrayBuffer == true) ? "true" : "false"); 4633eb0b6dSopenharmony_ci NAPI_ASSERT(env, isArrayBuffer, "Wrong type of arguments. Expects a typedarray as first argument."); 4733eb0b6dSopenharmony_ci 4833eb0b6dSopenharmony_ci NAPI_CALL(env, napi_detach_arraybuffer(env, args[0])); 4933eb0b6dSopenharmony_ci HILOG_INFO("%{public}s called end", __func__); 5033eb0b6dSopenharmony_ci 5133eb0b6dSopenharmony_ci napi_value result = 0; 5233eb0b6dSopenharmony_ci napi_get_null(env, &result); 5333eb0b6dSopenharmony_ci return result; 5433eb0b6dSopenharmony_ci} 5533eb0b6dSopenharmony_ci 5633eb0b6dSopenharmony_cistatic napi_value IsDetachedArrayBuffer(napi_env env, napi_callback_info info) 5733eb0b6dSopenharmony_ci{ 5833eb0b6dSopenharmony_ci HILOG_INFO("%{public}s,called", __func__); 5933eb0b6dSopenharmony_ci size_t argc = 1; 6033eb0b6dSopenharmony_ci napi_value args[1] = { nullptr }; 6133eb0b6dSopenharmony_ci NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); 6233eb0b6dSopenharmony_ci NAPI_ASSERT(env, argc == 1, "Wrong number of arguments."); 6333eb0b6dSopenharmony_ci 6433eb0b6dSopenharmony_ci napi_value array_buffer = args[0]; 6533eb0b6dSopenharmony_ci bool isArrayBuffer = false; 6633eb0b6dSopenharmony_ci NAPI_CALL(env, napi_is_arraybuffer(env, array_buffer, &isArrayBuffer)); 6733eb0b6dSopenharmony_ci NAPI_ASSERT(env, isArrayBuffer, "Wrong type of arguments. Expects an array buffer as first argument."); 6833eb0b6dSopenharmony_ci 6933eb0b6dSopenharmony_ci bool isDetached = false; 7033eb0b6dSopenharmony_ci NAPI_CALL(env, napi_is_detached_arraybuffer(env, array_buffer, &isDetached)); 7133eb0b6dSopenharmony_ci 7233eb0b6dSopenharmony_ci napi_value result = nullptr; 7333eb0b6dSopenharmony_ci NAPI_CALL(env, napi_get_boolean(env, isDetached, &result)); 7433eb0b6dSopenharmony_ci 7533eb0b6dSopenharmony_ci return result; 7633eb0b6dSopenharmony_ci} 7733eb0b6dSopenharmony_ci 7833eb0b6dSopenharmony_cinapi_value ArrayDetachInit(napi_env env, napi_value exports) 7933eb0b6dSopenharmony_ci{ 8033eb0b6dSopenharmony_ci HILOG_INFO("%{public}s,called", __func__); 8133eb0b6dSopenharmony_ci 8233eb0b6dSopenharmony_ci napi_property_descriptor descriptors[] = { 8333eb0b6dSopenharmony_ci DECLARE_NAPI_FUNCTION("testCreateArrayBuffer", CreateArrayBuffer), 8433eb0b6dSopenharmony_ci DECLARE_NAPI_FUNCTION("testDetachArrayBuffer", DetachArrayBuffer), 8533eb0b6dSopenharmony_ci DECLARE_NAPI_FUNCTION("testIsDetachedArrayBuffer", IsDetachedArrayBuffer), 8633eb0b6dSopenharmony_ci }; 8733eb0b6dSopenharmony_ci 8833eb0b6dSopenharmony_ci NAPI_CALL(env, napi_define_properties(env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); 8933eb0b6dSopenharmony_ci 9033eb0b6dSopenharmony_ci return exports; 9133eb0b6dSopenharmony_ci} 9233eb0b6dSopenharmony_ci} // namespace SYSTEM_TEST_NAPI 9333eb0b6dSopenharmony_ci} // namespace NAPI 9433eb0b6dSopenharmony_ci} // namespace ACE 95