1/* 2 * Copyright (c) 2020 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 "nativeapi_common.h" 17#include "jsi/jsi_types.h" 18#include "nativeapi_config.h" 19 20namespace OHOS { 21namespace ACELite { 22void NativeapiCommon::FailCallBack(const JSIValue thisVal, const JSIValue args, int ret) 23{ 24 if (JSI::ValueIsUndefined(args)) { 25 return; 26 } 27 JSIValue errInfo; 28 if (ret == ERROR_CODE_IO) { 29 errInfo = JSI::CreateString(ERR_IO_EXCEPTION); 30 ret = (-ERROR_CODE_IO); 31 } else if ((ret == ERROR_CODE_PARAM) || (ret == ERROR_DIR_NOT_EMPTY)) { 32 errInfo = JSI::CreateString(ERR_ILLEGAL_PARAM); 33 ret = (-ERROR_CODE_PARAM); 34 } else if (ret == ERROR_FR_NO_FILE) { 35 errInfo = JSI::CreateString(ERR_NO_FILE); 36 ret = (-ERROR_CODE_NO_FILE); 37 } else if (ret == ERROR_CODE_READ_TOO_LONG) { 38 errInfo = JSI::CreateString(ERR_READ_TOO_LONG); 39 ret = (-ERROR_CODE_READ_TOO_LONG); 40 } else { 41 errInfo = JSI::CreateString(ERR_GENERAL); 42 ret = (-ERROR_CODE_GENERAL); 43 } 44 JSIValue errCode = JSI::CreateNumber(ret); 45 JSIValue fail = JSI::GetNamedProperty(args, CB_FAIL); 46 JSIValue complete = JSI::GetNamedProperty(args, CB_COMPLETE); 47 JSIValue argv[ARGC_TWO] = {errInfo, errCode}; 48 if (!JSI::ValueIsUndefined(fail)) { 49 JSI::CallFunction(fail, thisVal, argv, ARGC_TWO); 50 } 51 if (!JSI::ValueIsUndefined(complete)) { 52 JSI::CallFunction(complete, thisVal, nullptr, 0); 53 } 54 JSI::ReleaseValueList(fail, complete, errInfo, errCode, ARGS_END); 55} 56 57void NativeapiCommon::SuccessCallBack(const JSIValue thisVal, const JSIValue args, JSIValue jsiValue) 58{ 59 if (JSI::ValueIsUndefined(args)) { 60 return; 61 } 62 JSIValue success = JSI::GetNamedProperty(args, CB_SUCCESS); 63 JSIValue complete = JSI::GetNamedProperty(args, CB_COMPLETE); 64 if (!JSI::ValueIsUndefined(success)) { 65 if (JSI::ValueIsUndefined(jsiValue)) { 66 JSI::CallFunction(success, thisVal, nullptr, 0); 67 } else { 68 JSI::CallFunction(success, thisVal, &jsiValue, ARGC_ONE); 69 } 70 } 71 if (!JSI::ValueIsUndefined(complete)) { 72 JSI::CallFunction(complete, thisVal, nullptr, 0); 73 } 74 JSI::ReleaseValueList(success, complete, ARGS_END); 75} 76 77bool NativeapiCommon::IsValidJSIValue(const JSIValue* args, uint8_t argsNum) 78{ 79 if ((args == nullptr) || (argsNum == 0) || JSI::ValueIsUndefined(args[0])) { 80 return false; 81 } 82 return true; 83} 84} 85} 86