From d93f78f46b4f963000066cc1b53ea978ae4f48cc Mon Sep 17 00:00:00 2001 From: tongdiaoZS <1904807608@qq.com> Date: Thu, 11 Sep 2025 22:18:23 +0800 Subject: [PATCH] taihe/test: update taihe test for v0.47 Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICXM4K Signed-off-by: tongdiaoZS <1904807608@qq.com> --- taihe/CMakeLists.txt | 319 +----------- taihe/cmake/TaiheUtils.cmake | 466 ++++++++++++++++++ taihe/cmake/TaiheWorkflows.cmake | 62 +++ taihe/test/CMakeLists.txt | 4 +- taihe/test/ani_array/CMakeLists.txt | 5 + taihe/test/ani_arraybuffer/CMakeLists.txt | 5 + taihe/test/ani_arraybuffer/user/main.ets | 2 +- taihe/test/ani_async/CMakeLists.txt | 5 + taihe/test/ani_basetype/CMakeLists.txt | 5 + taihe/test/ani_bigint/CMakeLists.txt | 5 + taihe/test/ani_bigint/idl/bigint_new.taihe | 2 +- taihe/test/ani_bundle/CMakeLists.txt | 5 + taihe/test/ani_bundlemanager/CMakeLists.txt | 5 + taihe/test/ani_callback/CMakeLists.txt | 5 + taihe/test/ani_compare/CMakeLists.txt | 5 + taihe/test/ani_enum/CMakeLists.txt | 5 + taihe/test/ani_export_default/CMakeLists.txt | 5 + taihe/test/ani_finalization/CMakeLists.txt | 5 + taihe/test/ani_fixedarray/CMakeLists.txt | 5 + taihe/test/ani_fixedarray/idl/hello.taihe | 2 +- taihe/test/ani_fixedarray/user/main.ets | 4 +- taihe/test/ani_function/CMakeLists.txt | 5 + taihe/test/ani_graphics3d/CMakeLists.txt | 5 + taihe/test/ani_iface/CMakeLists.txt | 5 + taihe/test/ani_import/CMakeLists.txt | 5 + taihe/test/ani_import/idl/mate.bar.taihe | 2 +- taihe/test/ani_import/idl/mate.foo.taihe | 2 +- taihe/test/ani_import/idl/mate.taihe | 2 +- taihe/test/ani_import/idl/pura.baz.taihe | 2 +- taihe/test/ani_import/idl/test.inner.taihe | 2 +- taihe/test/ani_inject/CMakeLists.txt | 7 + taihe/test/ani_inject/idl/moduleA.foo.taihe | 17 + taihe/test/ani_inject/user/main.ets | 12 + .../test/ani_javalike_overload/CMakeLists.txt | 32 ++ .../author/src/ani_constructor.cpp | 42 ++ .../author/src/overload.impl.cpp | 312 ++++++++++++ .../author/src/overload_test.impl.cpp | 62 +++ .../ani_javalike_overload/idl/overload.taihe | 176 +++++++ .../idl/overload_test.taihe | 27 + .../test/ani_javalike_overload/user/main.ets | 396 +++++++++++++++ taihe/test/ani_keep_name/CMakeLists.txt | 5 + .../ani_keep_name/idl/keep_name_test.taihe | 1 - taihe/test/ani_maythrow/CMakeLists.txt | 5 + taihe/test/ani_namespace/CMakeLists.txt | 5 + .../ani_namespace/author/src/inner.impl.cpp | 2 +- .../idl/ns_alltest.functiontest.taihe | 2 +- taihe/test/ani_namespace/user/main.ets | 3 +- taihe/test/ani_new_async/CMakeLists.txt | 33 ++ .../author/src/ani_constructor.cpp | 37 ++ taihe/test/ani_new_async/author/src/impl.cpp | 143 ++++++ taihe/test/ani_new_async/idl/async_test.taihe | 111 +++++ taihe/test/ani_new_async/user/main.ets | 181 +++++++ taihe/test/ani_on_off/CMakeLists.txt | 5 + taihe/test/ani_on_off/idl/on_off.taihe | 4 +- taihe/test/ani_opaque/CMakeLists.txt | 5 + taihe/test/ani_optional/CMakeLists.txt | 5 + .../test/ani_optional/author/src/opt.impl.cpp | 4 +- taihe/test/ani_optional/idl/opt.taihe | 4 +- taihe/test/ani_optional/user/main.ets | 8 +- taihe/test/ani_overload/CMakeLists.txt | 5 + .../ani_perf_hello/author/src/hello.impl.cpp | 14 +- taihe/test/ani_polymorphism/CMakeLists.txt | 5 + taihe/test/ani_primitives/CMakeLists.txt | 7 +- ...tives_test.taihe => primitives_test.ohidl} | 3 +- taihe/test/ani_primitives/user/main.ets | 12 +- taihe/test/ani_record/CMakeLists.txt | 5 + taihe/test/ani_session/CMakeLists.txt | 5 + taihe/test/ani_static/CMakeLists.txt | 5 + taihe/test/ani_static/author/src/impl.cpp | 3 +- taihe/test/ani_static/idl/staticTest.taihe | 20 +- taihe/test/ani_static/user/main.ets | 15 +- taihe/test/ani_string/CMakeLists.txt | 5 + taihe/test/ani_struct/CMakeLists.txt | 5 + .../author/src/struct_test.impl.cpp | 38 +- taihe/test/ani_struct/idl/struct_test.taihe | 36 +- taihe/test/ani_struct/user/main.ets | 37 ++ taihe/test/ani_struct_extend/CMakeLists.txt | 5 + taihe/test/ani_test/CMakeLists.txt | 5 + taihe/test/ani_test/idl/ani_test.taihe | 2 +- taihe/test/ani_test/user/main.ets | 2 +- taihe/test/ani_thread/CMakeLists.txt | 5 + taihe/test/ani_tp_cookies/CMakeLists.txt | 5 + taihe/test/ani_tp_ui/CMakeLists.txt | 5 + taihe/test/ani_typedarray/CMakeLists.txt | 5 + taihe/test/ani_typedarray/idl/bar.taihe | 2 +- taihe/test/ani_typedarray/user/main.ets | 44 +- taihe/test/ani_union/CMakeLists.txt | 5 + taihe/test/ani_union/idl/union_ani.taihe | 10 +- 88 files changed, 2518 insertions(+), 399 deletions(-) create mode 100644 taihe/cmake/TaiheUtils.cmake create mode 100644 taihe/cmake/TaiheWorkflows.cmake create mode 100644 taihe/test/ani_inject/idl/moduleA.foo.taihe create mode 100644 taihe/test/ani_javalike_overload/CMakeLists.txt create mode 100644 taihe/test/ani_javalike_overload/author/src/ani_constructor.cpp create mode 100644 taihe/test/ani_javalike_overload/author/src/overload.impl.cpp create mode 100644 taihe/test/ani_javalike_overload/author/src/overload_test.impl.cpp create mode 100644 taihe/test/ani_javalike_overload/idl/overload.taihe create mode 100644 taihe/test/ani_javalike_overload/idl/overload_test.taihe create mode 100644 taihe/test/ani_javalike_overload/user/main.ets create mode 100644 taihe/test/ani_new_async/CMakeLists.txt create mode 100644 taihe/test/ani_new_async/author/src/ani_constructor.cpp create mode 100644 taihe/test/ani_new_async/author/src/impl.cpp create mode 100644 taihe/test/ani_new_async/idl/async_test.taihe create mode 100644 taihe/test/ani_new_async/user/main.ets rename taihe/test/ani_primitives/idl/{primitives_test.taihe => primitives_test.ohidl} (99%) diff --git a/taihe/CMakeLists.txt b/taihe/CMakeLists.txt index ca3f7b98bd..7444793568 100644 --- a/taihe/CMakeLists.txt +++ b/taihe/CMakeLists.txt @@ -11,323 +11,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(cmake/TaiheUtils.cmake) +setup_taihe_cmake_test_env() cmake_minimum_required(VERSION 3.18) -project(TaiheCompiler) - -# 设置 C 编译器为 clang -set(CMAKE_C_COMPILER "clang") - -# 设置 C++ 编译器为 clang++ -set(CMAKE_CXX_COMPILER "clang++") - -# 设置 C++ 标准 -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED True) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -# 启用 ASan -# if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") -# # 添加编译和链接时的 ASan 选项 -# add_compile_options(-fsanitize=address -fno-omit-frame-pointer) -# add_link_options(-fsanitize=address) -# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address ") -# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address ") -# endif() - -# 启用调试信息 -set(CMAKE_BUILD_TYPE Debug) - -# 为编译器设置 -fPIC -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") - -function(execute_and_set_variable OUTPUT_VAR_NAME) - execute_process( - COMMAND taihec ${ARGN} - OUTPUT_VARIABLE _output - OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE _result - ) - - if(_result EQUAL 0) - set(${OUTPUT_VAR_NAME} "${_output}" PARENT_SCOPE) - message(STATUS "${OUTPUT_VAR_NAME} set to: ${_output}") - else() - message(FATAL_ERROR "Failed to execute 'taihec ${COMMAND_ARGS}'. Error code: ${_result}") - endif() -endfunction() - -if(ENABLE_COVERAGE) - set(TAIHEC coverage run --parallel-mode -m taihe.cli.compiler) -else() - set(TAIHEC taihec) -endif() - -execute_and_set_variable(TAIHE_RUNTIME_INCLUDE_DIR "--print-runtime-header-path") -execute_and_set_variable(TAIHE_RUNTIME_SRC_DIR "--print-runtime-source-path") -set(TAIHE_RUNTIME_BUILD_DIR "${CMAKE_BINARY_DIR}/runtime") - -# 设置 Python -set(Python3_EXECUTABLE "/usr/bin/python3") -find_package(Python3 REQUIRED COMPONENTS Interpreter) - -# 启用或禁用 coverage -option(ENABLE_COVERAGE "Enable coverage run for the Python command" OFF) - -# 编译 taihe runtime 静态库 -set(TAIHE_RUNTIME_SOURCES - "${TAIHE_RUNTIME_SRC_DIR}/string.cpp" - "${TAIHE_RUNTIME_SRC_DIR}/object.cpp" - "${TAIHE_RUNTIME_SRC_DIR}/runtime.cpp" -) - -add_library(taihe_runtime STATIC ${TAIHE_RUNTIME_SOURCES}) - -set_target_properties(taihe_runtime PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY ${TAIHE_RUNTIME_BUILD_DIR} -) - -target_include_directories(taihe_runtime PUBLIC ${TAIHE_RUNTIME_INCLUDE_DIR}) - -execute_and_set_variable(TAIHE_STDLIB_DIR "--print-stdlib-path") -set(TAIHE_STDLIB_GEN_DIR "${CMAKE_BINARY_DIR}/stdlib/generated") -set(TAIHE_STDLIB_GEN_INCLUDE_DIR "${TAIHE_STDLIB_GEN_DIR}/include") -set(TAIHE_STDLIB_GEN_SRC_DIR "${TAIHE_STDLIB_GEN_DIR}/src") -set(TAIHE_STDLIB_BUILD_DIR "${CMAKE_BINARY_DIR}/stdlib/build") - -set(TAIHE_STDLIB_IDL_FILES - "${TAIHE_STDLIB_DIR}/taihe.platform.ani.taihe" -) - -set(TAIHE_STDLIB_GEN_SOURCES - "${TAIHE_STDLIB_GEN_SRC_DIR}/taihe.platform.ani.abi.c" -) - -add_custom_command( - OUTPUT ${TAIHE_STDLIB_GEN_SOURCES} ${TAIHE_STDLIB_GEN_INCLUDE_DIR} - COMMAND ${TAIHEC} - ${TAIHE_STDLIB_IDL_FILES} - -O${TAIHE_STDLIB_GEN_DIR} - -Gcpp-common -Gabi-source - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - DEPENDS ${TAIHE_STDLIB_IDL_FILES} ${CMAKE_SOURCE_DIR}/compiler/taihe/parse/antlr/TaiheAST.py - COMMENT "Generating Taihe standard library C++ header and source files..." - VERBATIM -) - -add_library(taihe_stdlib STATIC ${TAIHE_STDLIB_GEN_SOURCES}) - -set_target_properties(taihe_stdlib PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY ${TAIHE_STDLIB_BUILD_DIR} -) - -target_include_directories(taihe_stdlib PUBLIC ${TAIHE_STDLIB_GEN_INCLUDE_DIR} ${TAIHE_RUNTIME_INCLUDE_DIR}) - -function(generate_code_from_idl demo_name idl_files gen_ets_names taihe_configs gen_include_dir gen_abi_c_files gen_ani_cpp_files gen_ets_files) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") - - set(GEN_INCLUDE_DIR "${GEN_DIR}/include") - - set(GEN_ABI_C_FILES) - set(GEN_ANI_CPP_FILES) - foreach(TAIHE_FILE ${idl_files}) - # 替换扩展名 - get_filename_component(TAIHE_FILE_NAME ${TAIHE_FILE} NAME) - # 将修改后的文件名添加到新的列表中 - string(REGEX REPLACE "\\.taihe$" ".abi.c" GEN_ABI_C_FILE ${TAIHE_FILE_NAME}) - list(APPEND GEN_ABI_C_FILES "${GEN_DIR}/src/${GEN_ABI_C_FILE}") - string(REGEX REPLACE "\\.taihe$" ".ani.cpp" GEN_ANI_CPP_FILE ${TAIHE_FILE_NAME}) - list(APPEND GEN_ANI_CPP_FILES "${GEN_DIR}/src/${GEN_ANI_CPP_FILE}") - endforeach() - - set(GEN_ETS_FILES) - foreach(ETS_NAME ${gen_ets_names}) - set(ETS_FILE "${GEN_DIR}/${ETS_NAME}") - list(APPEND GEN_ETS_FILES "${ETS_FILE}") - endforeach() - - add_custom_command( - OUTPUT ${GEN_INCLUDE_DIR} ${GEN_ABI_C_FILES} ${GEN_ANI_CPP_FILES} ${GEN_ETS_FILES} - COMMAND ${TAIHEC} - ${idl_files} - -O${GEN_DIR} - -Gani-bridge -Gcpp-author -Gpretty-print - ${taihe_configs} - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - DEPENDS ${idl_files} ${CMAKE_SOURCE_DIR}/compiler/taihe/parse/antlr/TaiheAST.py - COMMENT "Generating Taihe C++ header and source files... ${GEN_DIR}" - VERBATIM - ) - - set(${gen_ets_files} ${GEN_ETS_FILES} PARENT_SCOPE) - set(${gen_abi_c_files} ${GEN_ABI_C_FILES} PARENT_SCOPE) - set(${gen_ani_cpp_files} ${GEN_ANI_CPP_FILES} PARENT_SCOPE) - set(${gen_include_dir} ${GEN_INCLUDE_DIR} PARENT_SCOPE) -endfunction() - -# 准备 panda sdk -function(setup_panda_sdk) - # 用户显式提供了 PANDA_HOME,不执行下载逻辑 - if(DEFINED PANDA_HOME AND IS_DIRECTORY "${PANDA_HOME}") - message(STATUS "Using user-provided PANDA_HOME: ${PANDA_HOME}") - set(ENV{PANDA_HOME} "${PANDA_HOME}") - # 自动下载模式 - else() - execute_and_set_variable(PANDA_EXTRACT_DIR "--print-panda-vm-path") - set(PANDA_HOME "${PANDA_EXTRACT_DIR}/linux_host_tools") - set(ENV{PANDA_HOME} "${PANDA_HOME}") - set(PANDA_HOME "${PANDA_HOME}" PARENT_SCOPE) - message(STATUS "PANDA_HOME set to: ${PANDA_HOME}") - endif() - - # Set ETS compiler path - list(APPEND CMAKE_PROGRAM_PATH "$ENV{PANDA_HOME}/bin") - - find_program(ETS_COMPILER es2panda) - find_program(ETS_RUNTIME ark) - find_program(ETS_DISASM ark_disasm) - find_program(ETS_LINK ark_link) - - if(NOT ETS_COMPILER) - message(FATAL_ERROR "ets_compiler not found! Please set ETS_COMPILER_PATH or ensure ets_compiler is in your PATH.") - else() - message(STATUS "Found ets_compiler: ${ETS_COMPILER}") - endif() -endfunction() - - -# 编译动态库 -function(compile_dylib demo_name user_include_dir user_cpp_files gen_include_dir gen_abi_c_files gen_ani_cpp_files) - add_library(${demo_name} SHARED ${gen_abi_c_files} ${gen_ani_cpp_files} ${user_cpp_files}) - - target_compile_options(${demo_name} PRIVATE "-Wno-attributes") - set_target_properties(${demo_name} PROPERTIES LINKER_LANGUAGE CXX) - target_link_libraries(${demo_name} PRIVATE taihe_runtime taihe_stdlib) - target_link_options(${demo_name} PRIVATE "-Wl,--no-undefined") - target_include_directories(${demo_name} PUBLIC ${user_include_dir} ${gen_include_dir} ${TAIHE_RUNTIME_INCLUDE_DIR} ${TAIHE_STDLIB_GEN_INCLUDE_DIR}) -endfunction() - - -# 生成 arktsconfig.json -function(write_arkts_config arktsconfig ets_files) - foreach(file IN LISTS ets_files) - get_filename_component(file_name ${file} NAME) - string(REGEX REPLACE "\\.ets$" "" stripped_file "${file_name}") - set(entry " \"${stripped_file}\": [\"${file}\"]") - list(APPEND ETS_PATAIHE_ENTRIES "${entry}") - endforeach() - - string(REPLACE ";" ",\n" ETS_PATAIHE_ENTRIES_JOINED "${ETS_PATAIHE_ENTRIES}") - - file(WRITE "${arktsconfig}" - "{\n" - " \"compilerOptions\": {\n" - " \"baseUrl\": \"${PANDA_HOME}\",\n" - " \"paths\": {\n" - " \"std\": [\"${PANDA_HOME}/../ets/stdlib/std\"],\n" - " \"escompat\": [\"${PANDA_HOME}/../ets/stdlib/escompat\"],\n" - " \"@ohos\": [\"${PANDA_HOME}/../ets/sdk/sdk/api/@ohos\"],\n" - "${ETS_PATAIHE_ENTRIES_JOINED}\n" - " }\n" - " }\n" - "}\n" - ) -endfunction() - - -# 自定义 ETS 构建规则 -function(build_ets_target ets_file output_dir abc_file dump_file ets_files arktsconfig) - # 创建输出目录(如果不存在) - file(MAKE_DIRECTORY ${output_dir}) - - add_custom_command( - OUTPUT ${abc_file} # 输出文件 - COMMAND ${ETS_COMPILER} ${ets_file} - --output ${abc_file} - --extension ets - --arktsconfig ${arktsconfig} # 生成命令 - && ${ETS_DISASM} ${abc_file} ${dump_file} - DEPENDS ${ets_file} ${ets_files} ${arktsconfig} # 输入文件依赖 - COMMENT "Building ${ets_file} ETS target" # 注释 - ) -endfunction() - - -function(abc_link demo_name main_abc ets_files) - set(ABC_FILES) - set(BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/build") - set(ARKTSCONFIG "${CMAKE_CURRENT_BINARY_DIR}/arktsconfig.json") - - write_arkts_config(${ARKTSCONFIG} "${ets_files}") - - # 为每个 ETS 文件创建编译目标 - foreach(ETS_FILE ${ets_files}) - get_filename_component(ETS_NAME_P ${ETS_FILE} NAME) - string(REGEX REPLACE "\\.[^.]*$" "" ETS_NAME "${ETS_NAME_P}") - - set(ABC_FILE "${BUILD_DIR}/${ETS_NAME}.ets.abc") - set(DUMP_FILE "${BUILD_DIR}/${ETS_NAME}.ets.abc.dump") - - build_ets_target(${ETS_FILE} ${BUILD_DIR} ${ABC_FILE} ${DUMP_FILE} "${ets_files}" ${ARKTSCONFIG}) - list(APPEND ABC_FILES "${ABC_FILE}") - endforeach() - - # 创建链接命令 - add_custom_command( - OUTPUT ${main_abc} - COMMAND ${ETS_LINK} --output ${main_abc} -- ${ABC_FILES} - DEPENDS ${ABC_FILES} - COMMENT "Linking all ABC files to main.abc" - ) -endfunction() - - -function(run_abc demo_name main_abc) - # 设置 - get_filename_component(demo_name ${demo_name} NAME_WE) - - # 创建运行目标,并明确依赖于链接目标 - add_custom_target(${demo_name}_run ALL - COMMAND # LD_PRELOAD=/usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.asan-x86_64.so - LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR} ${ETS_RUNTIME} - --boot-panda-files=$ENV{PANDA_HOME}/../ets/etsstdlib.abc - --boot-panda-files=$ENV{PANDA_HOME}/../ets/etssdk.abc - --load-runtimes=ets ${main_abc} main.ETSGLOBAL::main - && echo "Run successful" || (echo "Run failed" && exit 1) - COMMENT "Running ${demo_name}" - DEPENDS ${main_abc} ${demo_name} - ) -endfunction() - - -function(add_ani_demo demo_name idl_files taihe_configs gen_ets_names user_ets_files user_include_dir user_cpp_files) - set(MAIN_ABC "${CMAKE_CURRENT_BINARY_DIR}/main.abc") - - # 生成代码 - generate_code_from_idl(${demo_name} "${idl_files}" "${gen_ets_names}" "${taihe_configs}" GEN_INCLUDE_DIR GEN_ABI_C_FILES GEN_ANI_CPP_FILES GEN_ETS_FILES) - - # 动态库编译 - compile_dylib(${demo_name} "${user_include_dir}" "${user_cpp_files}" "${GEN_INCLUDE_DIR}" "${GEN_ABI_C_FILES}" "${GEN_ANI_CPP_FILES}") - - # 链接为 main.abc - set(ALL_ETS_FILES ${user_ets_files} ${GEN_ETS_FILES}) - abc_link(${demo_name} ${MAIN_ABC} "${ALL_ETS_FILES}") - - # 运行 - run_abc(${demo_name} ${MAIN_ABC}) -endfunction() - - -setup_panda_sdk() - -# INCLUDE_ANI_HEADER -set(ANI_HEADER $ENV{PANDA_HOME}/../ohos_arm64/include/plugins/ets/runtime/ani) -if(EXISTS "${ANI_HEADER}") - message(STATUS "Adding Panda include directory: ${ANI_HEADER}") - include_directories("${ANI_HEADER}") -else() - message(FATAL_ERROR "Cannot find the path: ${ANI_HEADER}") -endif() +project(TaiheCompilerTest) add_subdirectory(test) #add_subdirectory(cookbook) diff --git a/taihe/cmake/TaiheUtils.cmake b/taihe/cmake/TaiheUtils.cmake new file mode 100644 index 0000000000..a416d1f385 --- /dev/null +++ b/taihe/cmake/TaiheUtils.cmake @@ -0,0 +1,466 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +function(setup_taihe_cmake_test_env) + if(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) + return() # 不是最顶层项目,跳过设置 + endif() + # 编译器设置 + set(CMAKE_C_COMPILER "clang" CACHE STRING "C compiler" FORCE) + set(CMAKE_CXX_COMPILER "clang++" CACHE STRING "C++ compiler" FORCE) + + # 编译标准 + set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard" FORCE) + set(CMAKE_CXX_STANDARD_REQUIRED ON CACHE BOOL "" FORCE) + + # PIC 与调试构建 + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC" CACHE STRING "C flags" FORCE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" CACHE STRING "C++ flags" FORCE) + set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE) + + set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Generate compile_commands.json" FORCE) + + # 用于解决多次配置 python 导致的列表空字符串问题 + if(POLICY CMP0007) + cmake_policy(SET CMP0007 NEW) + endif() + # Python 查找 + if(NOT DEFINED Python3_EXECUTABLE) + set(Python3_EXECUTABLE "/usr/bin/python3" CACHE FILEPATH "Python3 executable") + endif() + find_package(Python3 REQUIRED COMPONENTS Interpreter) + + # Coverage 开关 + option(ENABLE_COVERAGE "Enable coverage run for the Python command" OFF) + + # ASan 可选 + option(ENABLE_ASAN "Enable AddressSanitizer" OFF) + if(ENABLE_ASAN AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") + add_compile_options(-fsanitize=address -fno-omit-frame-pointer) + add_link_options(-fsanitize=address) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address" CACHE STRING "" FORCE) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address" CACHE STRING "" FORCE) + endif() +endfunction() + +# taihec 获取配置路径 +function(execute_and_set_variable OUTPUT_VAR_NAME) + set(options "") + set(oneValueArgs "TAIHEC_PATH") + set(multiValueArgs "") + cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(ARGS_TAIHEC_PATH) + set(TAIHEC_PATH "${ARGS_TAIHEC_PATH}") + elseif(DEFINED IDE_TAIHEC_PATH) + set(TAIHEC_PATH "${IDE_TAIHEC_PATH}") + else() + set(TAIHEC_PATH "taihec") + endif() + + execute_process( + COMMAND ${TAIHEC_PATH} ${ARGN} + OUTPUT_VARIABLE _output + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE _result + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../.. + ) + + if(_result EQUAL 0) + string(REPLACE "\\" "/" _output "${_output}") + set(${OUTPUT_VAR_NAME} "${_output}" CACHE STRING "Set ${OUTPUT_VAR_NAME} from taihec") + else() + message(FATAL_ERROR "Failed to execute 'taihec ${COMMAND_ARGS}'. Error code: ${_result}") + endif() +endfunction() + +# taihe 代码生成 +function(generate_code_from_idl demo_name idl_files gen_ets_names author_bridge user_bridge taihe_configs gen_include_dir gen_abi_c_files gen_bridge_cpp_files gen_ets_files) + set(options "") + set(oneValueArgs "TAIHEC_PATH") + set(multiValueArgs "") + cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(ARGS_TAIHEC_PATH) + set(TAIHEC_PATH "${ARGS_TAIHEC_PATH}") + elseif(DEFINED IDE_TAIHEC_PATH) + set(TAIHEC_PATH "${IDE_TAIHEC_PATH}") + else() + set(TAIHEC_PATH "taihec") + endif() + + set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") + + set(GEN_INCLUDE_DIR "${GEN_DIR}/include") + + set(GEN_ABI_C_FILES) + set(GEN_AUTHOR_CPP_FILES) + set(GEN_USER_CPP_FILES) + + # config 为字符串时需要拆分为字符串列表 + string(REGEX MATCH " " HAS_SPACE "${taihe_configs}") + + if(HAS_SPACE) + separate_arguments(TAIHE_CONFIGS_LIST UNIX_COMMAND "${taihe_configs}") + else() + set(TAIHE_CONFIGS_LIST ${taihe_configs}) + endif() + + set(IDL_EXTENSION_REGEX "\\.(taihe|ohidl|hmidl)$") + + foreach(TAIHE_FILE ${idl_files}) + # 替换扩展名 + get_filename_component(TAIHE_FILE_NAME ${TAIHE_FILE} NAME) + # 将修改后的文件名添加到新的列表中 + string(REGEX REPLACE "${IDL_EXTENSION_REGEX}" ".abi.c" GEN_ABI_C_FILE ${TAIHE_FILE_NAME}) + list(APPEND GEN_ABI_C_FILES "${GEN_DIR}/src/${GEN_ABI_C_FILE}") + + # author bridge + if(author_bridge STREQUAL "kn-author") + string(REGEX REPLACE "${IDL_EXTENSION_REGEX}" ".knapi.cpp" GEN_AUTHOR_FILE ${TAIHE_FILE_NAME}) + list(APPEND GEN_AUTHOR_CPP_FILES "${GEN_DIR}/src/${GEN_AUTHOR_FILE}") + # TODO: kn_author config + # list(APPEND TAIHE_CONFIGS_LIST "-Gkn_author") + elseif(author_bridge STREQUAL "cpp-author") + list(APPEND TAIHE_CONFIGS_LIST "-Gcpp-author") + endif() + + # user bridge + if(user_bridge STREQUAL "cpp-user") + list(APPEND TAIHE_CONFIGS_LIST "-Gcpp-user") + elseif(user_bridge STREQUAL "napi-bridge") + string(REGEX REPLACE "${IDL_EXTENSION_REGEX}" ".napi.cpp" GEN_USER_FILE ${TAIHE_FILE_NAME}) + list(APPEND GEN_USER_CPP_FILES "${GEN_DIR}/src/${GEN_USER_FILE}") + list(APPEND TAIHE_CONFIGS_LIST "-Gnapi-bridge") + # TODO: napi_bridge config + # list(APPEND TAIHE_CONFIGS_LIST "-Gnapi-bridge") + elseif(user_bridge STREQUAL "ani-bridge") + string(REGEX REPLACE "${IDL_EXTENSION_REGEX}" ".ani.cpp" GEN_USER_FILE ${TAIHE_FILE_NAME}) + list(APPEND GEN_USER_CPP_FILES "${GEN_DIR}/src/${GEN_USER_FILE}") + list(APPEND TAIHE_CONFIGS_LIST "-Gani-bridge") + endif() + endforeach() + + set(GEN_BRIDGE_CPP_FILES ${GEN_AUTHOR_CPP_FILES} ${GEN_USER_CPP_FILES}) + + if(user_bridge STREQUAL "ani-bridge") + list(APPEND GEN_ABI_C_FILES "${GEN_DIR}/src/taihe.platform.ani.abi.c") + endif() + + set_source_files_properties( + ${GEN_BRIDGE_CPP_FILES} + PROPERTIES + LANGUAGE CXX + COMPILE_FLAGS "-std=c++17" + ) + + set(GEN_ETS_FILES) + foreach(ETS_NAME ${gen_ets_names}) + set(ETS_FILE "${GEN_DIR}/${ETS_NAME}") + list(APPEND GEN_ETS_FILES "${ETS_FILE}") + endforeach() + + if(ENABLE_COVERAGE) + list(APPEND COMMAND_TO_RUN "coverage" "run" "--parallel-mode" "-m" "taihe.cli.compiler") + else() + list(APPEND COMMAND_TO_RUN "${TAIHEC_PATH}") + endif() + + add_custom_command( + OUTPUT ${GEN_INCLUDE_DIR} ${GEN_ABI_C_FILES} ${GEN_BRIDGE_CPP_FILES} ${GEN_ETS_FILES} + COMMAND ${COMMAND_TO_RUN} + ${idl_files} + -O${GEN_DIR} + ${TAIHE_CONFIGS_LIST} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../.. + DEPENDS ${idl_files} + COMMENT "Generating Taihe C++ header and source files... ${GEN_DIR}" + VERBATIM + ) + + set(${gen_ets_files} ${GEN_ETS_FILES} PARENT_SCOPE) + set(${gen_abi_c_files} ${GEN_ABI_C_FILES} PARENT_SCOPE) + set(${gen_bridge_cpp_files} ${GEN_BRIDGE_CPP_FILES} PARENT_SCOPE) + set(${gen_include_dir} ${GEN_INCLUDE_DIR} PARENT_SCOPE) +endfunction() + +# 编译 taihe runtime 为 静态库 +function(add_taihe_runtime) + if (NOT TARGET taihe_runtime) + execute_and_set_variable(TAIHE_RUNTIME_SOURCE_DIR "--print-runtime-source-path") + execute_and_set_variable(TAIHE_RUNTIME_HEADER_DIR "--print-runtime-header-path") + + set(TAIHE_RUNTIME_SOURCES + "${TAIHE_RUNTIME_SOURCE_DIR}/string.cpp" + "${TAIHE_RUNTIME_SOURCE_DIR}/object.cpp" + "${TAIHE_RUNTIME_SOURCE_DIR}/runtime.cpp" + ) + + add_library(taihe_runtime STATIC + ${TAIHE_RUNTIME_SOURCES} + ) + + set_target_properties(taihe_runtime PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + ) + + target_include_directories(taihe_runtime PUBLIC ${TAIHE_RUNTIME_HEADER_DIR}) + endif() +endfunction() + +# 配置 panda sdk +function(setup_panda_sdk) + # 用户显式提供了 PANDA_HOME, 不执行下载逻辑 + if(DEFINED PANDA_HOME AND IS_DIRECTORY "${PANDA_HOME}") + message(STATUS "Using user-provided PANDA_HOME: ${PANDA_HOME}") + set(ENV{PANDA_HOME} "${PANDA_HOME}") + # 自动下载模式 + else() + if(NOT DEFINED PANDA_EXTRACT_DIR) + execute_and_set_variable(PANDA_EXTRACT_DIR "--print-panda-vm-path") + message(STATUS "PANDA_HOME set to: ${PANDA_EXTRACT_DIR}/linux_host_tools") + endif() + set(PANDA_HOME "${PANDA_EXTRACT_DIR}/linux_host_tools") + set(ENV{PANDA_HOME} "${PANDA_HOME}") + set(PANDA_HOME "${PANDA_HOME}" PARENT_SCOPE) + endif() + + # Set ETS compiler path + list(APPEND CMAKE_PROGRAM_PATH "$ENV{PANDA_HOME}/bin") + + find_program(ETS_COMPILER es2panda) + find_program(ETS_RUNTIME ark) + find_program(ETS_DISASM ark_disasm) + find_program(ETS_LINK ark_link) + + if(NOT ETS_COMPILER) + message(FATAL_ERROR "ets_compiler not found! Please set ETS_COMPILER_PATH or ensure ets_compiler is in your PATH.") + endif() +endfunction() + +# 生成 arktsconfig.json +function(write_arkts_config arktsconfig ets_files) + foreach(file IN LISTS ets_files) + get_filename_component(file_name ${file} NAME) + string(REGEX REPLACE "\\.ets$" "" stripped_file "${file_name}") + set(entry " \"${stripped_file}\": [\"${file}\"]") + list(APPEND ETS_PATAIHE_ENTRIES "${entry}") + endforeach() + + string(REPLACE ";" ",\n" ETS_PATAIHE_ENTRIES_JOINED "${ETS_PATAIHE_ENTRIES}") + + file(WRITE "${arktsconfig}" + "{\n" + " \"compilerOptions\": {\n" + " \"baseUrl\": \"${PANDA_HOME}\",\n" + " \"paths\": {\n" + " \"std\": [\"${PANDA_HOME}/../ets/stdlib/std\"],\n" + " \"escompat\": [\"${PANDA_HOME}/../ets/stdlib/escompat\"],\n" + " \"@ohos\": [\"${PANDA_HOME}/../ets/sdk/sdk/api/@ohos\"],\n" + "${ETS_PATAIHE_ENTRIES_JOINED}\n" + " }\n" + " }\n" + "}\n" + ) +endfunction() + +# 自定义 ETS 构建规则 +function(build_ets_target ets_file output_dir abc_file dump_file ets_files arktsconfig) + # 创建输出目录(如果不存在) + file(MAKE_DIRECTORY ${output_dir}) + + add_custom_command( + OUTPUT ${abc_file} # 输出文件 + COMMAND ${ETS_COMPILER} ${ets_file} + --output ${abc_file} + --extension ets + --arktsconfig ${arktsconfig} # 生成命令 + && ${ETS_DISASM} ${abc_file} ${dump_file} + DEPENDS ${ets_file} ${ets_files} ${arktsconfig} # 输入文件依赖 + COMMENT "Building ${ets_file} ETS target" # 注释 + ) +endfunction() + +function(abc_link demo_name main_abc ets_files) + set(ABC_FILES) + set(BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/build") + set(ARKTSCONFIG "${CMAKE_CURRENT_BINARY_DIR}/arktsconfig.json") + + write_arkts_config(${ARKTSCONFIG} "${ets_files}") + + # 为每个 ETS 文件创建编译目标 + foreach(ETS_FILE ${ets_files}) + get_filename_component(ETS_NAME_P ${ETS_FILE} NAME) + string(REGEX REPLACE "\\.[^.]*$" "" ETS_NAME "${ETS_NAME_P}") + + set(ABC_FILE "${BUILD_DIR}/${ETS_NAME}.ets.abc") + set(DUMP_FILE "${BUILD_DIR}/${ETS_NAME}.ets.abc.dump") + + build_ets_target(${ETS_FILE} ${BUILD_DIR} ${ABC_FILE} ${DUMP_FILE} "${ets_files}" ${ARKTSCONFIG}) + list(APPEND ABC_FILES "${ABC_FILE}") + endforeach() + + # 创建链接命令 + add_custom_command( + OUTPUT ${main_abc} + COMMAND ${ETS_LINK} --output ${main_abc} -- ${ABC_FILES} + DEPENDS ${ABC_FILES} + COMMENT "Linking all ABC files to main.abc" + ) +endfunction() + +function(run_abc demo_name main_abc) + # 设置 + get_filename_component(demo_name ${demo_name} NAME_WE) + + # 创建运行目标,并明确依赖于链接目标 + add_custom_target(${demo_name}_run ALL + COMMAND # LD_PRELOAD=/usr/lib/llvm-14/lib/clang/14.0.0/lib/linux/libclang_rt.asan-x86_64.so + LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR} ${ETS_RUNTIME} + --boot-panda-files=$ENV{PANDA_HOME}/../ets/etsstdlib.abc + --boot-panda-files=$ENV{PANDA_HOME}/../ets/etssdk.abc + --load-runtimes=ets ${main_abc} main.ETSGLOBAL::main + && echo "Run successful" || (echo "Run failed" && exit 1) + COMMENT "Running ${demo_name}" + DEPENDS ${main_abc} ${demo_name} + ) +endfunction() + +# 配置 ani 头文件 +function(setup_ani_header) + if(NOT DEFINED ANI_HEADER) + set(ANI_HEADER $ENV{PANDA_HOME}/../ohos_arm64/include/plugins/ets/runtime/ani CACHE PATH "ANI include path") + message(STATUS "Panda include directory: ${ANI_HEADER}") + endif() + + if(EXISTS "${ANI_HEADER}") + include_directories("${ANI_HEADER}") + else() + message(FATAL_ERROR "Cannot find the path: ${ANI_HEADER}") + endif() +endfunction() + +# 将生成文件编译为静态库 +function(compile_gen_lib gen_demo_name gen_include_dir gen_abi_c_files gen_ani_cpp_files) + add_library(${gen_demo_name} STATIC ${gen_abi_c_files} ${gen_ani_cpp_files}) + + target_compile_options(${gen_demo_name} PRIVATE "-Wno-attributes") + set_target_properties(${gen_demo_name} PROPERTIES LINKER_LANGUAGE CXX) + target_link_libraries(${gen_demo_name} PRIVATE taihe_runtime) + target_link_options(${gen_demo_name} PRIVATE "-Wl,--no-undefined") + target_include_directories(${gen_demo_name} PUBLIC ${gen_include_dir} ${TAIHE_RUNTIME_HEADER_DIR}) +endfunction() + +# 将用户文件编译为动态库 +function(compile_dylib demo_name user_include_dir user_cpp_files gen_include_dir link_gen_lib) + if (user_cpp_files) + add_library(${demo_name} SHARED ${user_cpp_files}) + else() + set(dummy_cpp "${CMAKE_CURRENT_BINARY_DIR}/${demo_name}_dummy.cpp") + file(WRITE "${dummy_cpp}" "extern \"C\" void __taihe_dummy_symbol__() {}\n") + add_library(${demo_name} SHARED "${dummy_cpp}") + endif() + target_compile_options(${demo_name} PRIVATE "-Wno-attributes") + set_target_properties(${demo_name} PROPERTIES LINKER_LANGUAGE CXX) + target_link_libraries(${demo_name} PRIVATE taihe_runtime ${link_gen_lib}) + target_link_options(${demo_name} PRIVATE "-Wl,--no-undefined") + target_include_directories(${demo_name} PUBLIC ${user_include_dir} ${gen_include_dir} ${TAIHE_RUNTIME_HEADER_DIR}) +endfunction() + +function(add_taihe_library target_name idl_files) + set(options "") + set(oneValueArgs "AUTHOR_BRIDGE" "USER_BRIDGE" "TAIHE_CONFIGS" "TAIHEC_PATH") + set(multiValueArgs "") + cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + if(NOT ARGS_AUTHOR_BRIDGE) + set(ARGS_AUTHOR_BRIDGE "cpp-author") + endif() + if(NOT ARGS_USER_BRIDGE) + set(ARGS_USER_BRIDGE "ani-bridge") + endif() + if(NOT ARGS_TAIHE_CONFIGS) + set(ARGS_TAIHE_CONFIGS "") + endif() + + if(ARGS_TAIHEC_PATH) + set(TAIHEC_PATH "${ARGS_TAIHEC_PATH}") + elseif(DEFINED IDE_TAIHEC_PATH) + set(TAIHEC_PATH "${IDE_TAIHEC_PATH}") + else() + set(TAIHEC_PATH "taihec") + endif() + + execute_and_set_variable(TAIHE_RUNTIME_SOURCE_DIR "--print-runtime-source-path" TAIHEC_PATH ${TAIHEC_PATH}) + execute_and_set_variable(TAIHE_RUNTIME_HEADER_DIR "--print-runtime-header-path" TAIHEC_PATH ${TAIHEC_PATH}) + set(TAIHE_RUNTIME_SOURCES + "${TAIHE_RUNTIME_SOURCE_DIR}/string.cpp" + "${TAIHE_RUNTIME_SOURCE_DIR}/object.cpp" + "${TAIHE_RUNTIME_SOURCE_DIR}/runtime.cpp" + ) + set_source_files_properties( + ${TAIHE_RUNTIME_SOURCES} + PROPERTIES + LANGUAGE CXX + COMPILE_FLAGS "-std=c++17" + ) + + generate_code_from_idl( + ${target_name} + "${idl_files}" + "" + "${ARGS_AUTHOR_BRIDGE}" + "${ARGS_USER_BRIDGE}" + "${ARGS_TAIHE_CONFIGS}" + GEN_INCLUDE_DIR + GEN_ABI_C_FILES + GEN_BRIDGE_CPP_FILES + GEN_ETS_FILES + TAIHEC_PATH ${TAIHEC_PATH} + ) + + # compile static library + add_library(${target_name} STATIC ${TAIHE_RUNTIME_SOURCES} ${GEN_ABI_C_FILES} ${GEN_BRIDGE_CPP_FILES} ${GEN_STDLIB_ABI_C_FILES}) + target_compile_options(${target_name} PRIVATE "-Wno-attributes") + set_target_properties(${target_name} PROPERTIES LINKER_LANGUAGE CXX) + target_link_options(${target_name} PRIVATE "-Wl,--no-undefined") + target_include_directories(${target_name} PUBLIC ${GEN_INCLUDE_DIR} ${TAIHE_RUNTIME_HEADER_DIR}) +endfunction() + +function(add_ani_demo demo_name idl_files taihe_configs gen_ets_names user_ets_files user_include_dir user_cpp_files) + if (NOT DEFINED TAIHE_STDLIB_DIR) + execute_and_set_variable(TAIHE_STDLIB_DIR "--print-stdlib-path") + endif() + + set(MAIN_ABC "${CMAKE_CURRENT_BINARY_DIR}/main.abc") + + # panda sdk 环境配置 + setup_panda_sdk() + # ani 头文件 + setup_ani_header() + # 编译 taihe 运行时 + add_taihe_runtime() + # ani 代码生成相关配置 + set(taihe_configs "-Gpretty-print ${taihe_configs}") + # 生成代码 + generate_code_from_idl(${demo_name} "${idl_files}" "${gen_ets_names}" "cpp-author" "ani-bridge" "${taihe_configs}" GEN_INCLUDE_DIR GEN_ABI_C_FILES GEN_ANI_CPP_FILES GEN_ETS_FILES) + # 生成代码静态库编译 + set(ALL_GEN_ABI_C_FILES ${GEN_STDLIB_ABI_C_FILES} ${GEN_ABI_C_FILES}) + compile_gen_lib("taihe_gen_${demo_name}" "${GEN_INCLUDE_DIR}" "${ALL_GEN_ABI_C_FILES}" "${GEN_ANI_CPP_FILES}") + # 动态库编译 + compile_dylib(${demo_name} "${user_include_dir}" "${user_cpp_files}" "${GEN_INCLUDE_DIR}" "taihe_gen_${demo_name}") + # 链接为 main.abc + set(ALL_ETS_FILES ${user_ets_files} ${GEN_ETS_FILES}) + abc_link(${demo_name} ${MAIN_ABC} "${ALL_ETS_FILES}") + # 运行 + run_abc(${demo_name} ${MAIN_ABC}) +endfunction() diff --git a/taihe/cmake/TaiheWorkflows.cmake b/taihe/cmake/TaiheWorkflows.cmake new file mode 100644 index 0000000000..9a13f9e8f3 --- /dev/null +++ b/taihe/cmake/TaiheWorkflows.cmake @@ -0,0 +1,62 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include(${CMAKE_CURRENT_LIST_DIR}/TaiheUtils.cmake) + +function(add_taihe_ani_library target_name idl_files) + set(options "") + set(oneValueArgs "TAIHE_CONFIGS" "TAIHEC_PATH") + set(multiValueArgs "") + cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + if(NOT ARGS_TAIHE_CONFIGS) + set(ARGS_TAIHE_CONFIGS "") + endif() + if(ARGS_TAIHEC_PATH) + set(TAIHEC_PATH "${ARGS_TAIHEC_PATH}") + elseif(DEFINED IDE_TAIHEC_PATH) + set(TAIHEC_PATH "${IDE_TAIHEC_PATH}") + else() + set(TAIHEC_PATH "taihec") + endif() + + add_taihe_library(${target_name} "${idl_files}" + AUTHOR_BRIDGE "cpp-author" + USER_BRIDGE "ani-bridge" + TAIHE_CONFIGS "${ARGS_TAIHE_CONFIGS}" + TAIHEC_PATH ${TAIHEC_PATH} + ) +endfunction() + +function(add_taihe_cpp_library target_name idl_files) + set(options "") + set(oneValueArgs "TAIHE_CONFIGS" "TAIHEC_PATH") + set(multiValueArgs "") + cmake_parse_arguments(ARGS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + if(NOT ARGS_TAIHE_CONFIGS) + set(ARGS_TAIHE_CONFIGS "") + endif() + if(ARGS_TAIHEC_PATH) + set(TAIHEC_PATH "${ARGS_TAIHEC_PATH}") + elseif(DEFINED IDE_TAIHEC_PATH) + set(TAIHEC_PATH "${IDE_TAIHEC_PATH}") + else() + set(TAIHEC_PATH "taihec") + endif() + + add_taihe_library(${target_name} "${idl_files}" + AUTHOR_BRIDGE "cpp-author" + USER_BRIDGE "cpp-user" + TAIHE_CONFIGS "${ARGS_TAIHE_CONFIGS}" + TAIHEC_PATH ${TAIHEC_PATH} + ) +endfunction() diff --git a/taihe/test/CMakeLists.txt b/taihe/test/CMakeLists.txt index 6fa9061b3d..aaffd61947 100644 --- a/taihe/test/CMakeLists.txt +++ b/taihe/test/CMakeLists.txt @@ -17,7 +17,7 @@ add_subdirectory(ani_async) add_subdirectory(ani_basetype) add_subdirectory(ani_bigint) add_subdirectory(ani_callback) -# add_subdirectory(ani_enum) +add_subdirectory(ani_enum) add_subdirectory(ani_finalization) add_subdirectory(ani_function) add_subdirectory(ani_iface) @@ -50,5 +50,7 @@ add_subdirectory(ani_tp_cookies) add_subdirectory(ani_tp_ui) add_subdirectory(ani_compare) add_subdirectory(ani_export_default) +add_subdirectory(ani_javalike_overload) +add_subdirectory(ani_new_async) # add_subdirectory(ani_huks) # add_subdirectory(ani_image) diff --git a/taihe/test/ani_array/CMakeLists.txt b/taihe/test/ani_array/CMakeLists.txt index 1b43a3ca22..914edb7c33 100644 --- a/taihe/test/ani_array/CMakeLists.txt +++ b/taihe/test/ani_array/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_array) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/array_test.taihe" ) diff --git a/taihe/test/ani_arraybuffer/CMakeLists.txt b/taihe/test/ani_arraybuffer/CMakeLists.txt index 284f046f26..d44ac7613f 100644 --- a/taihe/test/ani_arraybuffer/CMakeLists.txt +++ b/taihe/test/ani_arraybuffer/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_arraybuffer) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/arraybuffer.taihe" ) diff --git a/taihe/test/ani_arraybuffer/user/main.ets b/taihe/test/ani_arraybuffer/user/main.ets index 20c72cada5..08a9d2cf51 100644 --- a/taihe/test/ani_arraybuffer/user/main.ets +++ b/taihe/test/ani_arraybuffer/user/main.ets @@ -83,7 +83,7 @@ function arraybuffer_uint8toarraybuffer() { } function arraybuffer_dataview() { - // case 6 : test Uint8Array to ArrayBuffer conversion, use DataView + // case 6 : test Uint8Array to ArrayBuffer conversion, use DataView let uint8ArrayData: Uint8Array = new Uint8Array([8, 9, 10]); let arrayBufferFromUint8Array: ArrayBuffer = new ArrayBuffer(uint8ArrayData.length); diff --git a/taihe/test/ani_async/CMakeLists.txt b/taihe/test/ani_async/CMakeLists.txt index f2d27b9a0f..d5f2856553 100644 --- a/taihe/test/ani_async/CMakeLists.txt +++ b/taihe/test/ani_async/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_async) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/async_test.taihe" ) diff --git a/taihe/test/ani_basetype/CMakeLists.txt b/taihe/test/ani_basetype/CMakeLists.txt index 33f643e40d..f09f3d1f85 100644 --- a/taihe/test/ani_basetype/CMakeLists.txt +++ b/taihe/test/ani_basetype/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_basetype) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/base_test.taihe" ) diff --git a/taihe/test/ani_bigint/CMakeLists.txt b/taihe/test/ani_bigint/CMakeLists.txt index 5563d03c27..772011ab57 100644 --- a/taihe/test/ani_bigint/CMakeLists.txt +++ b/taihe/test/ani_bigint/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_bigint) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/bigint_test.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/bigint_new.taihe" diff --git a/taihe/test/ani_bigint/idl/bigint_new.taihe b/taihe/test/ani_bigint/idl/bigint_new.taihe index a4ca1b769e..c377e17a38 100644 --- a/taihe/test/ani_bigint/idl/bigint_new.taihe +++ b/taihe/test/ani_bigint/idl/bigint_new.taihe @@ -36,7 +36,7 @@ function MapBigInt(a: @record Map>): @record Map; - @null empty; + empty: unit; stringValue: String; } diff --git a/taihe/test/ani_bundle/CMakeLists.txt b/taihe/test/ani_bundle/CMakeLists.txt index f2c6875a6e..73dc5972d0 100644 --- a/taihe/test/ani_bundle/CMakeLists.txt +++ b/taihe/test/ani_bundle/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_bundle) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/abilityInfo.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/applicationInfo.taihe" diff --git a/taihe/test/ani_bundlemanager/CMakeLists.txt b/taihe/test/ani_bundlemanager/CMakeLists.txt index 7832eeccc3..e4cf347f4e 100644 --- a/taihe/test/ani_bundlemanager/CMakeLists.txt +++ b/taihe/test/ani_bundlemanager/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_bundlemanager) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/abilityInfo.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/applicationInfo.taihe" diff --git a/taihe/test/ani_callback/CMakeLists.txt b/taihe/test/ani_callback/CMakeLists.txt index eb4afac170..d8497b8df1 100644 --- a/taihe/test/ani_callback/CMakeLists.txt +++ b/taihe/test/ani_callback/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_callback) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/callbackTest.taihe" ) diff --git a/taihe/test/ani_compare/CMakeLists.txt b/taihe/test/ani_compare/CMakeLists.txt index 569bc0c3db..a338fc3bc9 100644 --- a/taihe/test/ani_compare/CMakeLists.txt +++ b/taihe/test/ani_compare/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_compare) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/test.taihe" ) diff --git a/taihe/test/ani_enum/CMakeLists.txt b/taihe/test/ani_enum/CMakeLists.txt index b61b5be30d..2b1e19d490 100644 --- a/taihe/test/ani_enum/CMakeLists.txt +++ b/taihe/test/ani_enum/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_enum) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/enum_test.taihe" ) diff --git a/taihe/test/ani_export_default/CMakeLists.txt b/taihe/test/ani_export_default/CMakeLists.txt index d58db2ce14..996c12333a 100644 --- a/taihe/test/ani_export_default/CMakeLists.txt +++ b/taihe/test/ani_export_default/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_export_default) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/export_enum.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/export_iface.taihe" diff --git a/taihe/test/ani_finalization/CMakeLists.txt b/taihe/test/ani_finalization/CMakeLists.txt index f3b92b1b5b..be26039a0d 100644 --- a/taihe/test/ani_finalization/CMakeLists.txt +++ b/taihe/test/ani_finalization/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_finalization) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/finalization_test.taihe" ) diff --git a/taihe/test/ani_fixedarray/CMakeLists.txt b/taihe/test/ani_fixedarray/CMakeLists.txt index c9ded31615..00f4af5f5b 100644 --- a/taihe/test/ani_fixedarray/CMakeLists.txt +++ b/taihe/test/ani_fixedarray/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_fixedarray) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/hello.taihe" ) diff --git a/taihe/test/ani_fixedarray/idl/hello.taihe b/taihe/test/ani_fixedarray/idl/hello.taihe index f25d0fbe64..61d0f53efe 100644 --- a/taihe/test/ani_fixedarray/idl/hello.taihe +++ b/taihe/test/ani_fixedarray/idl/hello.taihe @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -struct Data {} +struct Data { v: i32; } @class interface TestInterface { diff --git a/taihe/test/ani_fixedarray/user/main.ets b/taihe/test/ani_fixedarray/user/main.ets index cbe3d40e49..075e9d1d9e 100644 --- a/taihe/test/ani_fixedarray/user/main.ets +++ b/taihe/test/ani_fixedarray/user/main.ets @@ -80,10 +80,10 @@ function main() { testFixedArrayString(test, ["hello", "world", "test"]); }); suite.addTest("FixedArrayData", () => { - testFixedArrayData(test, [{}, {}, {}]); + testFixedArrayData(test, [{v: 0}, {v: 0}, {v: 0}]); }); suite.addTest("OptionalFixedArrayData", () => { - testOptionalFixedArrayData(test, [{}, {}, {}]); + testOptionalFixedArrayData(test, [{v: 0}, {v: 0}, {v: 0}]); }); suite.addTest("OptionalFixedArrayData", () => { testOptionalFixedArrayData(test, undefined); diff --git a/taihe/test/ani_function/CMakeLists.txt b/taihe/test/ani_function/CMakeLists.txt index 7e261846d9..0900ac55df 100644 --- a/taihe/test/ani_function/CMakeLists.txt +++ b/taihe/test/ani_function/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_function) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/function_test.taihe" ) diff --git a/taihe/test/ani_graphics3d/CMakeLists.txt b/taihe/test/ani_graphics3d/CMakeLists.txt index 386cb3ea21..4bcfe9cde9 100644 --- a/taihe/test/ani_graphics3d/CMakeLists.txt +++ b/taihe/test/ani_graphics3d/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_graphics3d) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/scene.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/sceneNodeParameters.taihe" diff --git a/taihe/test/ani_iface/CMakeLists.txt b/taihe/test/ani_iface/CMakeLists.txt index 743918f74d..aaf5a5f55a 100644 --- a/taihe/test/ani_iface/CMakeLists.txt +++ b/taihe/test/ani_iface/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_iface) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/iface_readonly_test.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/iface_test.taihe" diff --git a/taihe/test/ani_import/CMakeLists.txt b/taihe/test/ani_import/CMakeLists.txt index f05d64eef2..3f5c0e1ad2 100644 --- a/taihe/test/ani_import/CMakeLists.txt +++ b/taihe/test/ani_import/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_import) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/mate.bar.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/mate.foo.taihe" diff --git a/taihe/test/ani_import/idl/mate.bar.taihe b/taihe/test/ani_import/idl/mate.bar.taihe index fbc85a435a..f5135158e5 100644 --- a/taihe/test/ani_import/idl/mate.bar.taihe +++ b/taihe/test/ani_import/idl/mate.bar.taihe @@ -20,6 +20,6 @@ from mate.foo use FooType; -struct BarType {} +struct BarType { v: i32; } function testFoo(foo: FooType): void; diff --git a/taihe/test/ani_import/idl/mate.foo.taihe b/taihe/test/ani_import/idl/mate.foo.taihe index fde4d76e60..981152f80a 100644 --- a/taihe/test/ani_import/idl/mate.foo.taihe +++ b/taihe/test/ani_import/idl/mate.foo.taihe @@ -18,7 +18,7 @@ from mate use MateType; from nova use NovaType; from pura use PuraType; -struct FooType {} +struct FooType { v: i32; } function testMate(mate: MateType): void; function testNova(nova: NovaType): void; diff --git a/taihe/test/ani_import/idl/mate.taihe b/taihe/test/ani_import/idl/mate.taihe index 3ac634cb6f..a98d31bc86 100644 --- a/taihe/test/ani_import/idl/mate.taihe +++ b/taihe/test/ani_import/idl/mate.taihe @@ -17,7 +17,7 @@ from mate.bar use BarType as MyBar; union MateType { sValue: String; bValue: bool; - @undefined xValue; + xValue: @undefined unit; } function testBar(bar: MyBar): void; diff --git a/taihe/test/ani_import/idl/pura.baz.taihe b/taihe/test/ani_import/idl/pura.baz.taihe index 6538898b76..eb695017ad 100644 --- a/taihe/test/ani_import/idl/pura.baz.taihe +++ b/taihe/test/ani_import/idl/pura.baz.taihe @@ -16,7 +16,7 @@ from mate.foo use FooType; -struct BazType {} +struct BazType { v: i32; } function testFoo(foo: FooType): void; function testBaz(baz: BazType): void; diff --git a/taihe/test/ani_import/idl/test.inner.taihe b/taihe/test/ani_import/idl/test.inner.taihe index 17e7c55a56..ffc1464f96 100644 --- a/taihe/test/ani_import/idl/test.inner.taihe +++ b/taihe/test/ani_import/idl/test.inner.taihe @@ -14,4 +14,4 @@ */ @!namespace("@ohos.test", "inner") -struct MyStruct {} +struct MyStruct { v: i32; } diff --git a/taihe/test/ani_inject/CMakeLists.txt b/taihe/test/ani_inject/CMakeLists.txt index 91131a02cc..16ec21b00b 100644 --- a/taihe/test/ani_inject/CMakeLists.txt +++ b/taihe/test/ani_inject/CMakeLists.txt @@ -11,11 +11,18 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_inject) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/inject_test.taihe" + "${CMAKE_CURRENT_LIST_DIR}/idl/moduleA.foo.taihe" ) set(GEN_ETS_FILES + "moduleA.ets" "inject_test.ets" ) diff --git a/taihe/test/ani_inject/idl/moduleA.foo.taihe b/taihe/test/ani_inject/idl/moduleA.foo.taihe new file mode 100644 index 0000000000..1e66ee580d --- /dev/null +++ b/taihe/test/ani_inject/idl/moduleA.foo.taihe @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@!sts_inject("""export function foo(a: int): int { return a; }""") +@!sts_inject_into_module("""export function toplevelFunc(a: String): String { return a; }""") +@!namespace("moduleA", "foo") \ No newline at end of file diff --git a/taihe/test/ani_inject/user/main.ets b/taihe/test/ani_inject/user/main.ets index 6b8e8af8ff..652efcb931 100644 --- a/taihe/test/ani_inject/user/main.ets +++ b/taihe/test/ani_inject/user/main.ets @@ -14,11 +14,23 @@ */ import {BusinessError} from "@ohos.base"; import * as test from "inject_test"; +import * as moduleA from "moduleA"; loadLibrary("ani_inject"); function main() { + let res1: String = moduleA.toplevelFunc("hello"); + console.log("toplevelFunc: " + res1); + let res2: int = moduleA.foo.foo(100); + console.log("foo.foo: " + res2); let foo = new test.Foo(); foo.callWithThis(); foo.callWithThis(); + let structObj: test.RGB = { + color: "red", + r: 255, + g: 0, + b: 0, + }; + console.log(structObj.color + " " + structObj.r); } diff --git a/taihe/test/ani_javalike_overload/CMakeLists.txt b/taihe/test/ani_javalike_overload/CMakeLists.txt new file mode 100644 index 0000000000..b97bca3683 --- /dev/null +++ b/taihe/test/ani_javalike_overload/CMakeLists.txt @@ -0,0 +1,32 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_javalike_overload) + +file(GLOB IDL_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/idl/*.taihe") + +set(GEN_ETS_FILES + "overload_test.ets" + "overload.ets" +) + +set(USER_ETS_FILES + "${CMAKE_CURRENT_LIST_DIR}/user/main.ets" +) + +file(GLOB AUTHOR_CPP_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/author/src/*.cpp") + +add_ani_demo(ani_javalike_overload "${IDL_FILES}" "-Csts:keep-name" "${GEN_ETS_FILES}" "${USER_ETS_FILES}" "" "${AUTHOR_CPP_FILES}") diff --git a/taihe/test/ani_javalike_overload/author/src/ani_constructor.cpp b/taihe/test/ani_javalike_overload/author/src/ani_constructor.cpp new file mode 100644 index 0000000000..8870464f07 --- /dev/null +++ b/taihe/test/ani_javalike_overload/author/src/ani_constructor.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "overload.ani.hpp" +#include "overload_test.ani.hpp" + +#if __has_include() +#include +#elif __has_include() +#include +#else +#error "ani.h not found. Please ensure the Ani SDK is correctly installed." +#endif + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != overload_test::ANIRegister(env)) { + std::cerr << "Error from overload_test::ANIRegister" << std::endl; + return ANI_ERROR; + } + if (ANI_OK != overload::ANIRegister(env)) { + std::cerr << "Error from overload::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/taihe/test/ani_javalike_overload/author/src/overload.impl.cpp b/taihe/test/ani_javalike_overload/author/src/overload.impl.cpp new file mode 100644 index 0000000000..5d6146c7ea --- /dev/null +++ b/taihe/test/ani_javalike_overload/author/src/overload.impl.cpp @@ -0,0 +1,312 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "overload.impl.hpp" +#include +#include "overload.Color.proj.1.hpp" +#include "overload.Mystruct.proj.1.hpp" +#include "overload.OverloadInterface.proj.2.hpp" +#include "taihe/array.hpp" +#include "taihe/map.hpp" +#include "taihe/string.hpp" + +using namespace taihe; + +// NOLINTBEGIN +namespace { +class OverloadInterface { +public: + int8_t OverloadFuncI8(int8_t a, int8_t b) + { + std::cout << "OverloadFuncI8: a = " << (int)(a) << ", b = " << (int)(b) << std::endl; + return a; + } + + int16_t OverloadFuncI16(int16_t a, int16_t b) + { + std::cout << "OverloadFuncI16: a = " << a << ", b = " << b << std::endl; + return a; + } + + int32_t OverloadFuncI32(int32_t a, int32_t b) + { + std::cout << "OverloadFuncI32: a = " << a << ", b = " << b << std::endl; + return a; + } + + float OverloadFuncF32(float a, float b) + { + std::cout << "OverloadFuncF32: a = " << a << ", b = " << b << std::endl; + return a; + } + + double OverloadFuncF64(double a, double b) + { + std::cout << "OverloadFuncF64: a = " << a << ", b = " << b << std::endl; + return a; + } + + bool OverloadFuncBool(bool a, bool b) + { + std::cout << "OverloadFuncBool: a = " << a << ", b = " << b << std::endl; + return a; + } + + string OverloadFuncString(string_view a, string_view b) + { + std::cout << "OverloadFuncString: a = " << a << ", b = " << b << std::endl; + return string(a); + } + + int8_t OverloadFuncI8I16(int8_t a, int16_t b) + { + std::cout << "OverloadFuncI8I16: a = " << (int)(a) << ", b = " << b << std::endl; + return a; + } + + int8_t OverloadFuncI8F32(int8_t a, float b) + { + std::cout << "OverloadFuncI8F32: a = " << (int)(a) << ", b = " << b << std::endl; + return a; + } + + int8_t OverloadFuncI8String(int8_t a, string_view b) + { + std::cout << "OverloadFuncI8String: a = " << (int)(a) << ", b = " << b << std::endl; + return a; + } + + int32_t OverloadFuncEnum(::overload::Color const &p0) + { + std::cout << "OverloadFuncEnum: color = " << p0 << std::endl; + return static_cast(p0); + } + + string OverloadFuncMystruct(::overload::Mystruct const &p0) + { + std::cout << "OverloadFuncMystruct: testNum = " << p0.testNum << ", testStr = " << p0.testStr << std::endl; + return p0.testStr; + } + + void OverloadFunc5param1(int8_t p0, int16_t p1, int32_t p2, float p3, double p4) + { + std::cout << "OverloadFunc5param1: p0 = " << (int)p0 << ", p1 = " << p1 << ", p2 = " << p2 << ", p3 = " << p3 + << ", p4 = " << p4 << std::endl; + } + + bool OverloadFunc5paramPrimitive2(bool p0, string_view p1, int8_t p2, int16_t p3, int32_t p4) + { + std::cout << "OverloadFunc5paramPrimitive2: p0 = " << p0 << ", p1 = " << p1 << ", p2 = " << (int)p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return true; + } + + float OverloadFunc5paramPrimitive3(float p0, double p1, bool p2, string_view p3, int8_t p4) + { + std::cout << "OverloadFunc5paramPrimitive3: p0 = " << p0 << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << (int)p4 << std::endl; + return p0; + } + + string OverloadFunc5paramPrimitive4(string_view p0, int16_t p1, int32_t p2, float p3, bool p4) + { + std::cout << "OverloadFunc5paramPrimitive4: p0 = " << p0 << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return string(p0); + } + + string OverloadFunc5paramPrimitive5(string_view p0, string_view p1, string_view p2, bool p3, bool p4) + { + std::cout << "OverloadFunc5paramPrimitive5: p0 = " << p0 << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return string(p0); + } + + int16_t OverloadFunc5paramPrimitive6(int16_t p0, int32_t p1, float p2, double p3, bool p4) + { + std::cout << "OverloadFunc5paramPrimitive6: p0 = " << p0 << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return p0; + } + + string OverloadFunc5paramPrimitive7(string_view p0, int8_t p1, int16_t p2, float p3, bool p4) + { + std::cout << "OverloadFunc5paramPrimitive7: p0 = " << p0 << ", p1 = " << (int)p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return string(p0); + } + + bool OverloadFunc5paramPrimitive8(bool p0, int8_t p1, int32_t p2, double p3, string_view p4) + { + std::cout << "OverloadFunc5paramPrimitive8: p0 = " << p0 << ", p1 = " << (int)p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return true; + } + + double OverloadFunc5paramPrimitive9(double p0, bool p1, string_view p2, int16_t p3, int32_t p4) + { + std::cout << "OverloadFunc5paramPrimitive9: p0 = " << p0 << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return p0; + } + + int8_t OverloadFunc5paramPrimitive10(int8_t p0, float p1, bool p2, string_view p3, int32_t p4) + { + std::cout << "OverloadFunc5paramPrimitive10: p0 = " << (int)p0 << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << std::endl; + return p0; + } + + // These functions are testcases for overload, which use many input parameters + // NOLINTBEGIN(readability-function-size) + void OverloadFunc10param(int8_t p0, int16_t p1, int32_t p2, float p3, double p4, bool p5, string_view p6, + array_view p7, array_view p8, array_view p9) + { + std::cout << "OverloadFunc10param: p0 = " << static_cast(p0) << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << ", p5 = " << p5 << ", p6 = " << p6 << ", p7 = ["; + + for (size_t i = 0; i < p7.size(); ++i) { + std::cout << static_cast(p7.data()[i]); + if (i < p7.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "], p8 = ["; + for (size_t i = 0; i < p8.size(); ++i) { + std::cout << p8.data()[i]; + if (i < p8.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "], p9 = ["; + for (size_t i = 0; i < p9.size(); ++i) { + std::cout << p9.data()[i]; + if (i < p9.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "]" << std::endl; + } + + void OverloadFunc10param1(int8_t p0, int16_t p1, int32_t p2, float p3, double p4, bool p5, string_view p6, + array_view p7, ::overload::Mystruct const &p8, ::overload::Color const &p9) + { + std::cout << "OverloadFunc10param1: p0 = " << static_cast(p0) << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << ", p5 = " << p5 << ", p6 = " << p6 << ", p7 = ["; + + for (size_t i = 0; i < p7.size(); ++i) { + std::cout << static_cast(p7.data()[i]); + if (i < p7.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "], p8 = {testNum = " << p8.testNum << ", testStr = " << p8.testStr << "}" + << ", p9 = " << p9 << std::endl; + } + + void OverloadFunc10param3(int8_t p0, int16_t p1, int32_t p2, float p3, double p4, bool p5, string_view p6, + array_view p7, ::overload::Mystruct const &p8, ::overload::Color const &p9) + { + std::cout << "OverloadFunc10param3: p0 = " << static_cast(p0) << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << ", p5 = " << p5 << ", p6 = " << p6 << ", p7 = ["; + + for (size_t i = 0; i < p7.size(); ++i) { + std::cout << static_cast(p7.data()[i]); + if (i < p7.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "], p8 = {testNum = " << p8.testNum << ", testStr = " << p8.testStr << "}" + << ", p9 = " << p9 << std::endl; + } + + void OverloadFunc10param4(int8_t p0, int16_t p1, int32_t p2, float p3, double p4, bool p5, string_view p6, + array_view p7, array_view p8, ::overload::Color const &p9) + { + std::cout << "OverloadFunc10param4: p0 = " << static_cast(p0) << ", p1 = " << p1 << ", p2 = " << p2 + << ", p3 = " << p3 << ", p4 = " << p4 << ", p5 = " << p5 << ", p6 = " << p6 << ", p7 = ["; + + for (size_t i = 0; i < p7.size(); ++i) { + std::cout << static_cast(p7.data()[i]); + if (i < p7.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "], p8 = ["; + for (size_t i = 0; i < p8.size(); ++i) { + std::cout << static_cast(p8.data()[i]); + if (i < p8.size() - 1) { + std::cout << ", "; + } + } + + std::cout << "], p9 = " << p9 << std::endl; + } + + // NOLINTEND(readability-function-size) + + int32_t OverloadFuncPoint(array_view a) + { + std::cout << "OverloadFuncPoint: a = ["; + for (size_t i = 0; i < a.size(); ++i) { + std::cout << a.data()[i]; + if (i < a.size() - 1) { + std::cout << ", "; + } + } + std::cout << "]" << std::endl; + return a.data()[0]; + } + + uint8_t OverloadFuncArrayBuffer(array_view a) + { + std::cout << "OverloadFuncArrayBuffer: a = ["; + for (size_t i = 0; i < a.size(); ++i) { + std::cout << static_cast(a.data()[i]); + if (i < a.size() - 1) { + std::cout << ", "; + } + } + std::cout << "]" << std::endl; + return std::accumulate(a.begin(), a.end(), 0); + } + + void OverloadFuncEnumRecord(::overload::Color const &p1, map_view p2) + { + // Function body + } + + void OverloadFuncArrayRecord(array_view p1, map_view p2) + { + // Function body + } +}; + +::overload::OverloadInterface get_interface() +{ + return make_holder(); +} + +} // namespace + +// because these macros are auto-generate, lint will cause false positive. + +TH_EXPORT_CPP_API_get_interface(get_interface); +// NOLINTEND \ No newline at end of file diff --git a/taihe/test/ani_javalike_overload/author/src/overload_test.impl.cpp b/taihe/test/ani_javalike_overload/author/src/overload_test.impl.cpp new file mode 100644 index 0000000000..eebe72b341 --- /dev/null +++ b/taihe/test/ani_javalike_overload/author/src/overload_test.impl.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "overload_test.impl.hpp" + +#include + +#include "stdexcept" +#include "taihe/string.hpp" +// Please delete include when you implement +using namespace taihe; + +// NOLINTBEGIN +namespace { + +class Foo { +public: + int32_t bar_int(int32_t a) + { + return a; + } + + string bar_str(string_view a) + { + return a; + } +}; + +int32_t add_int(int32_t a, int32_t b) +{ + return a + b; +} + +string add_str(string_view a, string_view b) +{ + return a + b; +} + +::overload_test::Foo makeFoo() +{ + return make_holder(); +} + +} // namespace + +// because these macros are auto-generate, lint will cause false positive. + +TH_EXPORT_CPP_API_add_int(add_int); +TH_EXPORT_CPP_API_add_str(add_str); +TH_EXPORT_CPP_API_makeFoo(makeFoo); +// NOLINTEND \ No newline at end of file diff --git a/taihe/test/ani_javalike_overload/idl/overload.taihe b/taihe/test/ani_javalike_overload/idl/overload.taihe new file mode 100644 index 0000000000..56f4ea2922 --- /dev/null +++ b/taihe/test/ani_javalike_overload/idl/overload.taihe @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +enum Color: i32 { + red = 1, + yellow = 2, + blue = 3, +} + +struct Mystruct { + testNum: i32; + testStr: String; +} + +interface OverloadInterface { + @static_overload("overloadFunc") + OverloadFuncI8(a: i8, b: i8): i8; + + @static_overload("overloadFunc") + OverloadFuncI16(a: i16, b: i16): i16; + + @static_overload("overloadFunc") + OverloadFuncI32(a: i32, b: i32): i32; + + @static_overload("overloadFunc") + OverloadFuncF32(a: f32, b: f32): f32; + + @static_overload("overloadFunc") + OverloadFuncF64(a: f64, b: f64): f64; + + @static_overload("overloadFunc") + OverloadFuncBool(a: bool, b: bool): bool; + + @static_overload("overloadFunc") + OverloadFuncString(a: String, b: String): String; + + @static_overload("overloadFunc") + OverloadFuncI8I16(a: i8, b: i16): i8; + + @static_overload("overloadFunc") + OverloadFuncI8F32(a: i8, b: f32): i8; + + @static_overload("overloadFunc") + OverloadFuncI8String(a: i8, b: String): i8; + + @static_overload("overloadFunc") + OverloadFuncEnum(p0: Color): i32; + + @static_overload("overloadFunc") + OverloadFuncMystruct(p0: Mystruct): String; + + @static_overload("overloadFunc") + OverloadFuncPoint(a: Array): i32; + + @static_overload("overloadFunc") + OverloadFuncArrayBuffer(a: @arraybuffer Array): u8; + + @static_overload("overloadFunc") + OverloadFunc5param1(p0: i8, p1: i16, p2: i32, p3: f32, p4: f64): void; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive2( + p0: bool, + p1: String, + p2: i8, + p3: i16, + p4: i32 + ): bool; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive3( + p0: f32, + p1: f64, + p2: bool, + p3: String, + p4: i8 + ): f32; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive4( + p0: String, + p1: i16, + p2: i32, + p3: f32, + p4: bool + ): String; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive5( + p0: String, + p1: String, + p2: String, + p3: bool, + p4: bool + ): String; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive6( + p0: i16, + p1: i32, + p2: f32, + p3: f64, + p4: bool + ): i16; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive7( + p0: String, + p1: i8, + p2: i16, + p3: f32, + p4: bool + ): String; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive8( + p0: bool, + p1: i8, + p2: i32, + p3: f64, + p4: String + ): bool; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive9( + p0: f64, + p1: bool, + p2: String, + p3: i16, + p4: i32 + ): f64; + + @static_overload("overloadFunc") + OverloadFunc5paramPrimitive10( + p0: i8, + p1: f32, + p2: bool, + p3: String, + p4: i32 + ): i8; + + @static_overload("overloadFunc") + OverloadFunc10param(p0: i8, p1: i16, p2: i32, p3: f32, p4: f64, p5: bool, p6: String, p7: Array, p8:Array, p9:Array): void; + + @static_overload("overloadFunc") + OverloadFunc10param1(p0: i8, p1: i16, p2: i32, p3: f32, p4: f64, p5: bool, p6: String, p7: Array, p8:Mystruct, p9:Color): void; + +// @static_overload("overloadFunc") +// OverloadFunc10param2(p0: i8, p1: Mystruct, p2: Color, p3: Array, p4: Array, p5: Array, p6: Array, p7: Array, p8:Array, p9:Array): void; + + @static_overload("overloadFunc") + OverloadFunc10param3(p0: i8, p1: i16, p2: i32, p3: f32, p4: f64, p5: bool, p6: String, p7: @arraybuffer Array, p8:Mystruct, p9:Color): void; + + @static_overload("overloadFunc") + OverloadFunc10param4(p0: i8, p1: i16, p2: i32, p3: f32, p4: f64, p5: bool, p6: String, p7: Array, p8:@arraybuffer Array, p9:Color): void; + + @static_overload("overloadFunc") + OverloadFuncEnumRecord(p1:Color, p2:@record Map): void; + + @static_overload("overloadFunc") + OverloadFuncArrayRecord(p1:Array, p2:@record Map): void; +} + + +function get_interface(): OverloadInterface; diff --git a/taihe/test/ani_javalike_overload/idl/overload_test.taihe b/taihe/test/ani_javalike_overload/idl/overload_test.taihe new file mode 100644 index 0000000000..fb95cc9a29 --- /dev/null +++ b/taihe/test/ani_javalike_overload/idl/overload_test.taihe @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@static_overload("add") +function add_int(a: i32, b: i32): i32; +@static_overload("add") +function add_str(a: String, b: String): String; + +interface Foo { + @static_overload("bar") + bar_int(a: i32): i32; + @static_overload("bar") + bar_str(a: String): String; +} + +function makeFoo(): Foo; diff --git a/taihe/test/ani_javalike_overload/user/main.ets b/taihe/test/ani_javalike_overload/user/main.ets new file mode 100644 index 0000000000..80a64b4a2b --- /dev/null +++ b/taihe/test/ani_javalike_overload/user/main.ets @@ -0,0 +1,396 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {BusinessError} from "@ohos.base"; +import * as test_overload from "overload"; +import * as OverloadTest from "overload_test"; + +loadLibrary("ani_javalike_overload"); + + +function test_add_numbers() { + const result = OverloadTest.add(1, 2); + arktest.assertEQ(result, 3); +} + +function test_add_strings() { + const result = OverloadTest.add("1", "2"); + arktest.assertEQ(result, "12"); +} + +function test_Foo_bar_number(foo: OverloadTest.Foo) { + const result = foo.bar(123); + arktest.assertEQ(result, 123); +} + +function test_Foo_bar_string(foo: OverloadTest.Foo) { + const result = foo.bar("Hello"); + arktest.assertEQ(result, "Hello"); +} +// 测试 byte (i8) +function testByte() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((5).toByte(), (5).toByte()); + arktest.assertEQ(res, 5); +} + +// 测试 short (i16) +function testShort() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((42).toShort(), (42).toShort()); + arktest.assertEQ(res, 42); +} + +// 测试 int (i32) +function testInt() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((1000).toInt(), (1000).toInt()); + arktest.assertEQ(res, 1000); +} + +// 测试 long (i64) +function testLong() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((1000000).toLong(), (1000000).toLong()); + arktest.assertEQ(res, 1000000); +} + +// 测试 float (f32) +function testFloat() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc(3.14f, 3.14f); + arktest.assertEQ(res, 3.14f); +} + +// 测试 double (f64) +function testDouble() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((2.71828).toDouble(), (2.71828).toDouble()); + arktest.assertEQ(res, 2.71828); +} + +// 测试 String +function testString() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc("123", "123"); + arktest.assertEQ(res, "123"); +} + +// 测试 i8 与 i16 +function testByte_Short() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((5).toByte(), (42).toShort()); + arktest.assertEQ(res, 5); +} +// 测试 i8 与 f32 +function testByte_Float() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((5).toByte(), 3.14f); + arktest.assertEQ(res, 5); +} +// 测试 i8 与 String +function testByte_String() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc((5).toByte(), "123"); + arktest.assertEQ(res, 5); +} + +// 测试传入 enum 类型 +function test_enum() { + let color = test_overload.Color.red; + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc(color); + arktest.assertEQ(res, (1).toInt()); +} + +// 测试自定义的 struct 类型 +function test_struct() { + let mystruct: test_overload.Mystruct = { + testNum: 2147483647, + testStr: "path/to/source" + }; + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let res = instance.overloadFunc(mystruct) + arktest.assertEQ(res, "path/to/source") +} + +// 多参数组合 + +// 测试 5 param +function test_5param() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc( + (1).toByte(), (1).toShort(), (1).toInt(), 1.1f, (1.123).toDouble()); +} +function test_5param2() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc( + true, "hello", (1).toByte(), (1).toShort(), (1).toInt()); +} +function test_5param3() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc(1.0f, (1).toDouble(), true, "hello", (1).toByte()); +} +function test_5param4() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc("1 as byte", (1).toShort(), (1).toInt(), 1.1f, true); +} +function test_5param5() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc("1 as byte", "1 as short", "1 as int", true, false); +} + +function test_5param6() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc( + (10).toShort(), (20).toInt(), 3.0f, (4.0).toDouble(), true); +} + +function test_5param7() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc("hello", (1).toByte(), (2).toShort(), 3.0f, false); +} + +function test_5param8() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc( + true, (5).toByte(), (10).toInt(), (2.5).toDouble(), "world"); +} + +function test_5param9() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc( + (3.14).toDouble(), false, "test", (100).toShort(), (200).toInt()); +} + +function test_5param10() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + instance.overloadFunc((42).toByte(), 1.5f, true, "example", (1000).toInt()); +} + + +// 测试 10 param +function test_10param() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let arri8: byte[] = [1, 2, 3]; + let arri16: short[] = [1, 2, 3, 4]; + let arri32: int[] = [1, 2, 3, 4, 5]; + instance.overloadFunc( + (1).toByte(), (1).toShort(), (1).toInt(), 1.1f, (1.123).toDouble(), + true, "123", arri8, arri16, arri32); +} + +// 测试 10 param1 +function test_10param1() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let arri8: byte[] = [1, 2, 3]; + let mystruct: test_overload.Mystruct = { + testNum: 2147483647, + testStr: "path/to/source" + }; + let color = test_overload.Color.red; + instance.overloadFunc( + (1).toByte(), (1).toShort(), (1).toInt(), 1.1f, (1.123).toDouble(), + true, "123", arri8, mystruct, color); +} + +//// 测试 10 param2 +//function test_10param2() { +// let instance: test_overload.OverloadInterface = +// test_overload.get_interface(); +// let arri8: byte[] = [1, 2, 3]; +// let arri16: short[] = [1, 2, 3, 4]; +// let arri32: int[] = [1, 2, 3, 4, 5]; +// let arrbool: boolean[] = [true, false]; +// let arrf32: float[] = [1.1f, 2.2f]; +// let arrf64: double[] = [1.11111, 2.22222]; +// let arrstring: String[] = ["hello", "world"]; +// +// let mystruct: test_overload.Mystruct = { +// testNum: 2147483647, +// testStr: "path/to/source" +// }; +// let color = test_overload.Color.red; +// instance.overloadFunc( +// (1).toByte(), mystruct, color, arrf32, arrf64, arrbool, arrstring, +// arri8, arri16, arri32); +//} + +// 测试 10 param3 +function test_10param3() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let arri8: byte[] = [1, 2, 3]; + let mystruct: test_overload.Mystruct = { + testNum: 2147483647, + testStr: "path/to/source" + }; + let color = test_overload.Color.red; + + let numbersU8: byte[] = [1, 2, 3, 4, 5]; + let arrbuf1: ArrayBuffer = new ArrayBuffer(numbersU8.length); + for (let i = 0; i < numbersU8.length; i++) { + arrbuf1.set(i, numbersU8[i]); + } + + instance.overloadFunc( + (1).toByte(), (1).toShort(), (1).toInt(), 1.1f, (1.123).toDouble(), + true, "123", arrbuf1, mystruct, color); +} +// 测试 10 param4 +function test_10param4() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface(); + let arri8: byte[] = [1, 2, 3]; + let mystruct: test_overload.Mystruct = { + testNum: 2147483647, + testStr: "path/to/source" + }; + let color = test_overload.Color.red; + + let numbersU8: byte[] = [1, 2, 3, 4, 5]; + let arrbuf1: ArrayBuffer = new ArrayBuffer(numbersU8.length); + for (let i = 0; i < numbersU8.length; i++) { + arrbuf1.set(i, numbersU8[i]); + } + + instance.overloadFunc( + (1).toByte(), (1).toShort(), (1).toInt(), 1.1f, (1.123).toDouble(), + true, "123", arri8, arrbuf1, color); +} + + + +// 测试重载函数传入数组 +function test_Array() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface() + + let numbers: int[] = [1, 2, 3, 4, 5]; + let ocp = instance.overloadFunc(numbers) + arktest.assertEQ(ocp, (1).toInt()); +} + +function test_buffer() { + let instance: test_overload.OverloadInterface = + test_overload.get_interface() + let numbersU8: byte[] = [1, 2, 3, 4, 5]; + let arrbuf1: ArrayBuffer = new ArrayBuffer(numbersU8.length); + for (let i = 0; i < numbersU8.length; i++) { + arrbuf1.set(i, numbersU8[i]); + } + let res = instance.overloadFunc(arrbuf1) + arktest.assertEQ(res, 15); +} + +function test_enum_record() { + const studentScores: Record = { + "math": (10).toShort(), + "english": (85).toShort(), + "science": (95).toShort(), + }; + let instance: test_overload.OverloadInterface = + test_overload.get_interface() + let color = test_overload.Color.red; + // instance.overloadFunc((test_overload.Color)color , studentScores) +} + +function test_array_record() { + const studentScores: Record = { + "math": (10).toShort(), + "english": (85).toShort(), + "science": (95).toShort(), + }; + let instance: test_overload.OverloadInterface = + test_overload.get_interface() + let numbers: int[] = [1, 2, 3, 4, 5]; + instance.overloadFunc(numbers, studentScores) +} + + +function main() { + console.log("##############start#############"); + const suite = new arktest.ArkTestsuite("Overload Test"); + + suite.addTest("test add numbers", test_add_numbers); + suite.addTest("test add strings", test_add_strings); + + let foo = OverloadTest.makeFoo(); + + suite.addTest("test Foo bar with number", () => test_Foo_bar_number(foo)); + suite.addTest("test Foo bar with string", () => test_Foo_bar_string(foo)); + + // primitive + suite.addTest("testByte", testByte); + suite.addTest("testShort", testShort); + suite.addTest("testInt", testInt); + suite.addTest("testLong", testLong); + suite.addTest("testFloat", testFloat); + suite.addTest("testDouble", testDouble); + suite.addTest("testString", testString); + suite.addTest("testByte_Short", testByte_Short); + suite.addTest("testByte_Float", testByte_Float); + suite.addTest("testByte_String", testByte_String); + + // + suite.addTest("test_enum", test_enum); + suite.addTest("test_struct", test_struct); + suite.addTest("test_Array", test_Array); + suite.addTest("test_buffer", test_buffer); + suite.addTest("test_enum_record", test_enum_record); + suite.addTest("test_array_record", test_array_record); + + // 多参数组合 + suite.addTest("testByte_5param", test_5param); + suite.addTest("testByte_5param2", test_5param2); + suite.addTest("testByte_5param3", test_5param3); + suite.addTest("testByte_5param4", test_5param4); + suite.addTest("testByte_5param5", test_5param5); + suite.addTest("testByte_5param6", test_5param6); + suite.addTest("testByte_5param7", test_5param7); + suite.addTest("testByte_5param8", test_5param8); + suite.addTest("testByte_5param9", test_5param9); + suite.addTest("testByte_5param10", test_5param10); + + suite.addTest("testByte_10param", test_10param); + suite.addTest("testByte_10param1", test_10param1); + // suite.addTest("testByte_10param2", test_10param2); + suite.addTest("testByte_10param3", test_10param3); + suite.addTest("testByte_10param4", test_10param4); + exit(suite.run()); +} diff --git a/taihe/test/ani_keep_name/CMakeLists.txt b/taihe/test/ani_keep_name/CMakeLists.txt index b3087e39d5..db8ce4895f 100644 --- a/taihe/test/ani_keep_name/CMakeLists.txt +++ b/taihe/test/ani_keep_name/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_keep_name) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/keep_name_test.taihe" ) diff --git a/taihe/test/ani_keep_name/idl/keep_name_test.taihe b/taihe/test/ani_keep_name/idl/keep_name_test.taihe index 853908e9b2..b29c010833 100644 --- a/taihe/test/ani_keep_name/idl/keep_name_test.taihe +++ b/taihe/test/ani_keep_name/idl/keep_name_test.taihe @@ -30,4 +30,3 @@ function GetIBase(a: String, b: String): IBase; function GetFooIface(): Foo; function PrintFooName(foo: Foo): String; -@!sts_keep_name diff --git a/taihe/test/ani_maythrow/CMakeLists.txt b/taihe/test/ani_maythrow/CMakeLists.txt index b5a41a51e1..eebd1cbedd 100644 --- a/taihe/test/ani_maythrow/CMakeLists.txt +++ b/taihe/test/ani_maythrow/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_maythrow) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/maythrow.taihe" ) diff --git a/taihe/test/ani_namespace/CMakeLists.txt b/taihe/test/ani_namespace/CMakeLists.txt index acfb6ebe57..c731eccadc 100644 --- a/taihe/test/ani_namespace/CMakeLists.txt +++ b/taihe/test/ani_namespace/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_namespace) + file(GLOB IDL_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/idl/*.taihe") set(GEN_ETS_FILES diff --git a/taihe/test/ani_namespace/author/src/inner.impl.cpp b/taihe/test/ani_namespace/author/src/inner.impl.cpp index 849616624f..787965659b 100644 --- a/taihe/test/ani_namespace/author/src/inner.impl.cpp +++ b/taihe/test/ani_namespace/author/src/inner.impl.cpp @@ -116,7 +116,7 @@ public: int8_t geti8() { - std::cout << __func__ << " " << i8 << std::endl; + std::cout << __func__ << " " << (int)i8 << std::endl; return i8; } diff --git a/taihe/test/ani_namespace/idl/ns_alltest.functiontest.taihe b/taihe/test/ani_namespace/idl/ns_alltest.functiontest.taihe index 82e5eda686..23f68e4904 100644 --- a/taihe/test/ani_namespace/idl/ns_alltest.functiontest.taihe +++ b/taihe/test/ani_namespace/idl/ns_alltest.functiontest.taihe @@ -230,7 +230,7 @@ union TestUnionName { value1:i16; value2:String; value3:bool; - @null value4; + value4:unit; } diff --git a/taihe/test/ani_namespace/user/main.ets b/taihe/test/ani_namespace/user/main.ets index 7c216a4aed..dfc9098415 100644 --- a/taihe/test/ani_namespace/user/main.ets +++ b/taihe/test/ani_namespace/user/main.ets @@ -715,7 +715,6 @@ function testCasebaseFunctionTest64() { arktest.assertEQ(res, true) } -// Frontend Bug // function testCasebaseFunctionTest64_1() { // let record: Record = { // [-100]: false, @@ -727,6 +726,7 @@ function testCasebaseFunctionTest64() { // arktest.assertEQ(res, false) // } + function testCasebaseFunctionTest64_2() { let record: Record = { 50: false, @@ -1535,7 +1535,6 @@ function main() { "check testCasebaseFunctionTest63", testCasebaseFunctionTest63); suite.addTest( "check testCasebaseFunctionTest64", testCasebaseFunctionTest64); - // Frontend Bug // suite.addTest( // "check testCasebaseFunctionTest64_1", testCasebaseFunctionTest64_1); suite.addTest( diff --git a/taihe/test/ani_new_async/CMakeLists.txt b/taihe/test/ani_new_async/CMakeLists.txt new file mode 100644 index 0000000000..bf18bad78a --- /dev/null +++ b/taihe/test/ani_new_async/CMakeLists.txt @@ -0,0 +1,33 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_new_async) + +set(IDL_FILES + "${CMAKE_CURRENT_LIST_DIR}/idl/async_test.taihe" +) + +set(GEN_ETS_FILES + "async_test.ets" +) + +set(USER_ETS_FILES + "${CMAKE_CURRENT_LIST_DIR}/user/main.ets" +) + +file(GLOB AUTHOR_CPP_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/author/src/*.cpp") + +add_ani_demo(ani_new_async "${IDL_FILES}" "" "${GEN_ETS_FILES}" "${USER_ETS_FILES}" "" "${AUTHOR_CPP_FILES}") diff --git a/taihe/test/ani_new_async/author/src/ani_constructor.cpp b/taihe/test/ani_new_async/author/src/ani_constructor.cpp new file mode 100644 index 0000000000..8238600e59 --- /dev/null +++ b/taihe/test/ani_new_async/author/src/ani_constructor.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "async_test.ani.hpp" + +#if __has_include() +#include +#elif __has_include() +#include +#else +#error "ani.h not found. Please ensure the Ani SDK is correctly installed." +#endif + +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + return ANI_ERROR; + } + if (ANI_OK != async_test::ANIRegister(env)) { + std::cerr << "Error from async_test::ANIRegister" << std::endl; + return ANI_ERROR; + } + *result = ANI_VERSION_1; + return ANI_OK; +} \ No newline at end of file diff --git a/taihe/test/ani_new_async/author/src/impl.cpp b/taihe/test/ani_new_async/author/src/impl.cpp new file mode 100644 index 0000000000..17bf2e66b9 --- /dev/null +++ b/taihe/test/ani_new_async/author/src/impl.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include + +#include "taihe/runtime.hpp" + +namespace { +int32_t AddImpl(int32_t a, int32_t b) +{ + if (a == 0) { + taihe::set_business_error(1, "some error happen in add impl"); + return b; + } else { + std::cout << "add impl " << a + b << std::endl; + return a + b; + } +} + +::async_test::IBase GetIBaseImpl() +{ + struct AuthorIBase { + taihe::string name; + + AuthorIBase() : name("My IBase") {} + + ~AuthorIBase() {} + + taihe::string Get() + { + return name; + } + + taihe::string GetWithCallback() + { + return name; + } + + taihe::string GetReturnsPromise() + { + return name; + } + + void Set(taihe::string_view a) + { + this->name = a; + return; + } + + void SetWithCallback(taihe::string_view a) + { + this->name = a; + return; + } + + void SetReturnsPromise(taihe::string_view a) + { + this->name = a; + return; + } + + void MakeSync() + { + TH_THROW(std::runtime_error, "makeSync not implemented"); + } + + void MakeWithCallback() + { + TH_THROW(std::runtime_error, "makeSync not implemented"); + } + + void MakeReturnsPromise() + { + TH_THROW(std::runtime_error, "makeSync not implemented"); + } + }; + + return taihe::make_holder(); +} + +void FromStructSyncImpl(::async_test::Data const &data) +{ + std::cout << data.a.c_str() << " " << data.b.c_str() << " " << data.c << std::endl; + if (data.c == 0) { + taihe::set_business_error(1, "some error happen in fromStructSyncImpl"); + } + return; +} + +::async_test::Data ToStructSyncImpl(taihe::string_view a, taihe::string_view b, int32_t c) +{ + if (c == 0) { + taihe::set_business_error(1, "some error happen in toStructSyncImpl"); + return {a, b, c}; + } + return {a, b, c}; +} + +void PrintSync() +{ + std::cout << "print Sync" << std::endl; +} + +void MakeGlobalSync() +{ + std::cout << "makeGlobal" << std::endl; +} +} // namespace + +// because these macros are auto-generate, lint will cause false positive. +// NOLINTBEGIN +TH_EXPORT_CPP_API_addSync(AddImpl); +TH_EXPORT_CPP_API_addWithAsync(AddImpl); +TH_EXPORT_CPP_API_addReturnsPromise(AddImpl); +TH_EXPORT_CPP_API_getIBase(GetIBaseImpl); +TH_EXPORT_CPP_API_getIBaseWithCallback(GetIBaseImpl); +TH_EXPORT_CPP_API_getIBaseReturnsPromise(GetIBaseImpl); +TH_EXPORT_CPP_API_fromStructSync(FromStructSyncImpl); +TH_EXPORT_CPP_API_fromStructWithCallback(FromStructSyncImpl); +TH_EXPORT_CPP_API_fromStructReturnsPromise(FromStructSyncImpl); +TH_EXPORT_CPP_API_toStructSync(ToStructSyncImpl); +TH_EXPORT_CPP_API_toStructWithCallback(ToStructSyncImpl); +TH_EXPORT_CPP_API_toStructReturnsPromise(ToStructSyncImpl); +TH_EXPORT_CPP_API_printSync(PrintSync); +TH_EXPORT_CPP_API_printWithCallback(PrintSync); +TH_EXPORT_CPP_API_printReturnsPromise(PrintSync); +TH_EXPORT_CPP_API_makeGlobalSync(MakeGlobalSync); +TH_EXPORT_CPP_API_makeGlobalWithCallback(MakeGlobalSync); +TH_EXPORT_CPP_API_makeGlobalReturnsPromise(MakeGlobalSync); +// NOLINTEND \ No newline at end of file diff --git a/taihe/test/ani_new_async/idl/async_test.taihe b/taihe/test/ani_new_async/idl/async_test.taihe new file mode 100644 index 0000000000..32497d826a --- /dev/null +++ b/taihe/test/ani_new_async/idl/async_test.taihe @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@static_overload("add") +@async function addWithAsync(a: i32, b: i32): i32; + +@static_overload("add") +@promise function addReturnsPromise(a: i32, b: i32): i32; + +function addSync(a: i32, b: i32): i32; + +@class +interface IBase { + @rename("getAsync") + @async GetWithCallback(): String; + + @rename("getPromise") + @promise GetReturnsPromise(): String; + + Get(): String; + + @rename("setAsync") + @async SetWithCallback(a: String): void; + + @rename("setPromise") + @promise SetReturnsPromise(a: String): void; + + Set(a: String): void; + + @static_overload("make") + MakeWithCallback(); + + @static_overload("make") + MakeReturnsPromise(); + + MakeSync(); +} + +@rename("makeGlobal") +@async function makeGlobalWithCallback(); + +@rename("makeGlobal") +@promise function makeGlobalReturnsPromise(); + +function makeGlobalSync(); + +@rename("getIBaseAsync") +@async function getIBaseWithCallback(): IBase; + +@rename("getIBasePromise") +@promise function getIBaseReturnsPromise(): IBase; + +function getIBase(): IBase; + +@static("IBase") +@rename("printAsync") +@async function printWithCallback(): void; + +@static("IBase") +@rename("printPromise") +@promise function printReturnsPromise(): void; + +@static("IBase") +function printSync(): void; + +struct Data { + a: String; + b: String; + c: i32; +} + +@rename("fromStruct") +@async function fromStructWithCallback(data: Data): void; + +@rename("fromStruct") +@promise function fromStructReturnsPromise(data: Data): void; + +function fromStructSync(data: Data): void; + +@rename("toStruct") +@async function toStructWithCallback(a: String, b: String, c: i32): Data; + +@rename("toStruct") +@promise function toStructReturnsPromise(a: String, b: String, c: i32): Data; + +function toStructSync(a: String, b: String, c: i32): Data; diff --git a/taihe/test/ani_new_async/user/main.ets b/taihe/test/ani_new_async/user/main.ets new file mode 100644 index 0000000000..c02b9ca37e --- /dev/null +++ b/taihe/test/ani_new_async/user/main.ets @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {BusinessError} from "@ohos.base"; +import * as async_test from "async_test"; + +loadLibrary("ani_new_async"); + +function testSyncAdd() { + { + let res: int|undefined; + let err: BusinessError|undefined; + try { + res = async_test.addSync(1, 2); + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(res, 3, "addSync should correctly add 1 + 2"); + } + { + let res: int|undefined; + let err: BusinessError|undefined; + try { + res = async_test.addSync(0, 2); + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(err?.code, 1, "addSync should throw an error"); + arktest.assertEQ( + err?.message, "some error happen in add impl", + "addSync should throw an error when first parameter is 0"); + } + console.log("testSyncAdd end"); +} + +function testAsyncAdd() { + { + let promise = new Promise((resolve, reject) => { + async_test.add(10, 20, (err: BusinessError|null, data?: int) => { + if (err !== null && err.code !== 0) { + reject(err); + } else { + resolve(data!); + } + }); + }); + let res: int|undefined; + let err: BusinessError|undefined; + try { + res = await promise; + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(res, 30, "add callback should receive 10 + 20 = 30"); + } + { + let promise = new Promise((resolve, reject) => { + async_test.add(0, 2, (err: BusinessError|null, data?: int) => { + if (err !== null && err.code !== 0) { + reject(err); + } else { + resolve(data!); + } + }); + }); + let res: int|undefined; + let err: BusinessError|undefined; + try { + res = await promise; + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(err?.code, 1, "add callback should receive an err"); + arktest.assertEQ( + err?.message, "some error happen in add impl", + "add callback should receive an err when first parameter is 0"); + } + console.log("testAsyncAdd end"); +} + +function testPromiseAdd() { + { + let res: int|undefined; + let err: BusinessError|undefined; + try { + res = await async_test.add(1, 2); + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(res, 3, "add promise should correctly add 1 + 2"); + } + { + let res: int|undefined; + let err: BusinessError|undefined; + try { + res = await async_test.add(0, 2); + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(err?.code, 1, "add promise should throw an error"); + arktest.assertEQ( + err?.message, "some error happen in add impl", + "add promise should throw an error when first parameter is 0"); + } + console.log("testPromiseAdd end"); +} + +function testIBase() { + const ibase = async_test.getIBase(); + arktest.assertEQ( + ibase.get(), "My IBase", "IBase.get() should return 'My IBase'"); + + await ibase.setPromise("interface async set"); + arktest.assertEQ( + await ibase.getPromise(), "interface async set", + "getPromise should resolve with the value set in setAsync"); +} + +function testStaticAsyncMethods() { + await async_test.IBase.printPromise(); +} + +function testStructMethods() { + await async_test.fromStruct( + {a: "abc", b: "efg", c: 3}); // should not throw an error + + { + let err: BusinessError|undefined; + try { + await async_test.fromStruct({a: "abc", b: "efg", c: 0}); + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(err?.code, 1, "fromStruct should throw an error"); + arktest.assertEQ( + err?.message, "some error happen in fromStructSyncImpl", + "fromStruct should throw an error when c is 0"); + } + + let data: async_test.Data = await async_test.toStruct("aaa", "bbb", 1); + arktest.assertEQ(data.a, "aaa", "Data.a should be 'aaa'"); + arktest.assertEQ(data.b, "bbb", "Data.b should be 'bbb'"); + arktest.assertEQ(data.c, 1, "Data.c should be 1"); + + { + let err: BusinessError|undefined; + try { + data = await async_test.toStruct("aaa", "bbb", 0); + } catch (e) { + err = e as BusinessError; + } + arktest.assertEQ(err?.code, 1, "toStruct should throw an error"); + arktest.assertEQ( + err?.message, "some error happen in toStructSyncImpl", + "toStruct should throw an error when c is 0"); + } +} + +function main() { + const suite = new arktest.ArkTestsuite("Async Test Suite"); + + suite.addTest("Sync Add Tests", testSyncAdd); + suite.addTest("Async Add Tests", testAsyncAdd); + suite.addTest("Promise Add Tests", testPromiseAdd); + suite.addTest("IBase Tests", testIBase); + suite.addTest("Static Async Methods Tests", testStaticAsyncMethods); + suite.addTest("Struct Methods Tests", testStructMethods); + + exit(suite.run()); +} diff --git a/taihe/test/ani_on_off/CMakeLists.txt b/taihe/test/ani_on_off/CMakeLists.txt index 45e3a753d8..224522d50c 100644 --- a/taihe/test/ani_on_off/CMakeLists.txt +++ b/taihe/test/ani_on_off/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_on_off) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/on_off.taihe" ) diff --git a/taihe/test/ani_on_off/idl/on_off.taihe b/taihe/test/ani_on_off/idl/on_off.taihe index bd1fdf9c28..d23325ad3f 100644 --- a/taihe/test/ani_on_off/idl/on_off.taihe +++ b/taihe/test/ani_on_off/idl/on_off.taihe @@ -19,14 +19,14 @@ interface IBase { @on_off offSet(a: () => void): void; - @on_off(overload = "mytest") + @on_off(name = "mytest") mytestNew(); @on_off("type_name") onTest(): i32; } -@on_off(overload = "mytest") +@on_off(name = "mytest") function mytestGlobalnew(): i32; @class diff --git a/taihe/test/ani_opaque/CMakeLists.txt b/taihe/test/ani_opaque/CMakeLists.txt index ddc1cb922a..aca923e0ca 100644 --- a/taihe/test/ani_opaque/CMakeLists.txt +++ b/taihe/test/ani_opaque/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_opaque) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/opaque_test.taihe" ) diff --git a/taihe/test/ani_optional/CMakeLists.txt b/taihe/test/ani_optional/CMakeLists.txt index 0e5d54874d..a6ebbd4a00 100644 --- a/taihe/test/ani_optional/CMakeLists.txt +++ b/taihe/test/ani_optional/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_optional) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/opt.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/optional.taihe" diff --git a/taihe/test/ani_optional/author/src/opt.impl.cpp b/taihe/test/ani_optional/author/src/opt.impl.cpp index a7741df45c..c9328bc05a 100644 --- a/taihe/test/ani_optional/author/src/opt.impl.cpp +++ b/taihe/test/ani_optional/author/src/opt.impl.cpp @@ -30,12 +30,12 @@ public: TestImpl() {} - void Setstring(::taihe::optional_view<::taihe::string> a) + void SetMemberStr(::taihe::optional_view<::taihe::string> a) { this->a_ = a; } - ::taihe::optional<::taihe::string> Getstring() + ::taihe::optional<::taihe::string> GetMemberStr() { return a_; } diff --git a/taihe/test/ani_optional/idl/opt.taihe b/taihe/test/ani_optional/idl/opt.taihe index dfe5417d0b..99f1181276 100644 --- a/taihe/test/ani_optional/idl/opt.taihe +++ b/taihe/test/ani_optional/idl/opt.taihe @@ -29,8 +29,8 @@ struct MyStruct { } interface Test { - @set("string") Setstring(a: Optional): void; - @get("string") Getstring(): Optional; + @set("memberStr") SetMemberStr(a: Optional): void; + @get("memberStr") GetMemberStr(): Optional; SetIntData(a: String): void; ShowOptionalString(a: Optional): Optional; diff --git a/taihe/test/ani_optional/user/main.ets b/taihe/test/ani_optional/user/main.ets index aa1c5d0460..7e6b9e1d7f 100644 --- a/taihe/test/ani_optional/user/main.ets +++ b/taihe/test/ani_optional/user/main.ets @@ -205,15 +205,15 @@ function func_getTest_setIntData() { function func_getTest_property() { let instance: OptTest.Test = OptTest.getTest(); - instance.string = "this is string value"; - let res = instance.string; + instance.memberStr = "this is string value"; + let res = instance.memberStr; arktest.assertEQ(res, "this is string value"); } function func_getTest_property_1() { let instance: OptTest.Test = OptTest.getTest(); - instance.string = undefined; - let res = instance.string; + instance.memberStr = undefined; + let res = instance.memberStr; arktest.assertEQ(res, undefined); } diff --git a/taihe/test/ani_overload/CMakeLists.txt b/taihe/test/ani_overload/CMakeLists.txt index 26351fcf81..cba6a21afd 100644 --- a/taihe/test/ani_overload/CMakeLists.txt +++ b/taihe/test/ani_overload/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_overload) + file(GLOB IDL_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/idl/*.taihe") set(GEN_ETS_FILES diff --git a/taihe/test/ani_perf_hello/author/src/hello.impl.cpp b/taihe/test/ani_perf_hello/author/src/hello.impl.cpp index e2e1cec801..300d2f841a 100644 --- a/taihe/test/ani_perf_hello/author/src/hello.impl.cpp +++ b/taihe/test/ani_perf_hello/author/src/hello.impl.cpp @@ -27,13 +27,13 @@ void setData(::hello::Data const &data) {} ::hello::Data getData() { return { - .a = {std::in_place, "name"}, - .b = {std::in_place, 2.5}, - .c = {std::in_place, 3}, - .d = {std::in_place, true}, - .e = {std::in_place, true}, - .f = {std::in_place, false}, - .g = {std::in_place, false}, + .a = taihe::optional {std::in_place, "name"}, + .b = taihe::optional {std::in_place, 2.5}, + .c = taihe::optional {std::in_place, 3}, + .d = taihe::optional {std::in_place, true}, + .e = taihe::optional {std::in_place, true}, + .f = taihe::optional {std::in_place, false}, + .g = taihe::optional {std::in_place, false}, }; } diff --git a/taihe/test/ani_polymorphism/CMakeLists.txt b/taihe/test/ani_polymorphism/CMakeLists.txt index b9212f3038..0e4dc7e775 100644 --- a/taihe/test/ani_polymorphism/CMakeLists.txt +++ b/taihe/test/ani_polymorphism/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_polymorphism) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/poly.taihe" ) diff --git a/taihe/test/ani_primitives/CMakeLists.txt b/taihe/test/ani_primitives/CMakeLists.txt index 4b0f6820ab..23f83dd372 100644 --- a/taihe/test/ani_primitives/CMakeLists.txt +++ b/taihe/test/ani_primitives/CMakeLists.txt @@ -11,8 +11,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_primitives) + set(IDL_FILES - "${CMAKE_CURRENT_LIST_DIR}/idl/primitives_test.taihe" + "${CMAKE_CURRENT_LIST_DIR}/idl/primitives_test.ohidl" "${CMAKE_CURRENT_LIST_DIR}/idl/void_func.mytest.taihe" ) diff --git a/taihe/test/ani_primitives/idl/primitives_test.taihe b/taihe/test/ani_primitives/idl/primitives_test.ohidl similarity index 99% rename from taihe/test/ani_primitives/idl/primitives_test.taihe rename to taihe/test/ani_primitives/idl/primitives_test.ohidl index eaed8f8706..5ba4fec664 100644 --- a/taihe/test/ani_primitives/idl/primitives_test.taihe +++ b/taihe/test/ani_primitives/idl/primitives_test.ohidl @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + function Multiply(a: i32, b: i32): i32; function BaseCFunc(testBoolean : i32): bool; function BaseAFunc(testBoolean : bool): void; @@ -165,4 +166,4 @@ export const CONSTANT_BOOL_FALSE = false; enum TestConst:i32 { CONSTANT_I32_001 = 10000, CONSTANT_I32_002 = 10001, -} \ No newline at end of file +} diff --git a/taihe/test/ani_primitives/user/main.ets b/taihe/test/ani_primitives/user/main.ets index 4e49cd0d7e..d99dbe53a0 100644 --- a/taihe/test/ani_primitives/user/main.ets +++ b/taihe/test/ani_primitives/user/main.ets @@ -32,12 +32,14 @@ function testMultiply() { function testFunC1() { - let res = test_primitives.BaseCFunc(10) + let res = test_primitives.BaseCFunc(10); + console.log("BaseCFunc pass:", res); arktest.assertEQ(res, true) } function testFunC2() { - let res = test_primitives.BaseCFunc(5) + let res = test_primitives.BaseCFunc(5); + console.log("BaseCFunc fail", res); arktest.assertEQ(res, false) } @@ -60,12 +62,14 @@ function testFunB2() { } function testFunD1() { - let res = test_primitives.BaseDFunc("test123") + let res = test_primitives.BaseDFunc("test123"); + console.log("BaseDFunc pass:", res); arktest.assertEQ(res, true) } function testFunD2() { - let res = test_primitives.BaseDFunc("test1231") + let res = test_primitives.BaseDFunc("test1231"); + console.log("BaseDFunc fail:", res); arktest.assertEQ(res, false) } diff --git a/taihe/test/ani_record/CMakeLists.txt b/taihe/test/ani_record/CMakeLists.txt index 66f6b7e758..fbf3f3ef2a 100644 --- a/taihe/test/ani_record/CMakeLists.txt +++ b/taihe/test/ani_record/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_record) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/record_test.taihe" ) diff --git a/taihe/test/ani_session/CMakeLists.txt b/taihe/test/ani_session/CMakeLists.txt index 8671cafaf4..6675d49cb3 100644 --- a/taihe/test/ani_session/CMakeLists.txt +++ b/taihe/test/ani_session/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_session) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/session_test.taihe" ) diff --git a/taihe/test/ani_static/CMakeLists.txt b/taihe/test/ani_static/CMakeLists.txt index 97f2f8af39..9ed3ce93cd 100644 --- a/taihe/test/ani_static/CMakeLists.txt +++ b/taihe/test/ani_static/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_static) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/staticTest.taihe" ) diff --git a/taihe/test/ani_static/author/src/impl.cpp b/taihe/test/ani_static/author/src/impl.cpp index 1d4311dac2..8f739b4b82 100644 --- a/taihe/test/ani_static/author/src/impl.cpp +++ b/taihe/test/ani_static/author/src/impl.cpp @@ -110,9 +110,10 @@ void setName(::taihe::string_view a) // because these macros are auto-generate, lint will cause false positive. // NOLINTBEGIN TH_EXPORT_CPP_API_addSync(add_impl); +TH_EXPORT_CPP_API_addWithCallback(add_impl); +TH_EXPORT_CPP_API_addReturnsPromise(add_impl); TH_EXPORT_CPP_API_sumSync(sum_impl); TH_EXPORT_CPP_API_getIBase(getIBase_impl); -TH_EXPORT_CPP_API_getIBase_test(getIBase_test_impl); TH_EXPORT_CPP_API_static_func(static_func); TH_EXPORT_CPP_API_ctor_func(ctor_func); TH_EXPORT_CPP_API_getName(getName); diff --git a/taihe/test/ani_static/idl/staticTest.taihe b/taihe/test/ani_static/idl/staticTest.taihe index 9747775240..bd8703334c 100644 --- a/taihe/test/ani_static/idl/staticTest.taihe +++ b/taihe/test/ani_static/idl/staticTest.taihe @@ -22,15 +22,23 @@ interface IBase { function sumSync(a: i32, b: i32): i32; @static("IBase") -@gen_async("add") -@gen_promise("add") +@static_overload("add") +@async function addWithCallback(a: i32, b: i32): i32; + +@static("IBase") +@static_overload("add") +@promise function addReturnsPromise(a: i32, b: i32): i32; + +@static("IBase") function addSync(a: i32, b: i32): i32; -@ctor("IBase") +@constructor("IBase") +@rename() function getIBase(name: String): IBase; -@ctor("IBase") -function getIBase_test(name: String, test: String): IBase; +// @constructor("IBase") +// @static_overload("createIBase") +// function getIBase_test(name: String, test: String): IBase; interface ITest { get(): String; @@ -38,7 +46,7 @@ interface ITest { } @static("ITest") -@overload("static_test") +@rename("static_test") function static_func(a: i32, b: i32): i32; @static("ITest") diff --git a/taihe/test/ani_static/user/main.ets b/taihe/test/ani_static/user/main.ets index 707581f249..6b3d06ba44 100644 --- a/taihe/test/ani_static/user/main.ets +++ b/taihe/test/ani_static/user/main.ets @@ -19,6 +19,7 @@ loadLibrary("ani_static"); function test_sumSync() { const result = lib.IBase.sumSync(10, 20); + console.log("sumSync result:", result); arktest.assertEQ(result, 200); } @@ -29,13 +30,15 @@ function test_addSync() { function test_new_class() { let test = new lib.IBase("hello"); + console.log("new IBase get:", test.get()); arktest.assertEQ(test.get(), "hello"); } -function test_new_class_with_two_args() { - let test_1 = new lib.IBase("hello", "test"); - arktest.assertEQ(test_1.get(), "test"); -} +// Frontend javalike constructor overloading bug +// function test_new_class_with_two_args() { +// let test_1 = new lib.IBase("hello", "test"); +// arktest.assertEQ(test_1.get(), "test"); +// } function main() { console.log("##############start#############"); @@ -44,7 +47,9 @@ function main() { suite.addTest("test sumSync", test_sumSync); suite.addTest("test addSync", test_addSync); suite.addTest("test new class", test_new_class); - suite.addTest("test new class with two args", test_new_class_with_two_args); + // Frontend javalike constructor overloading bug + // suite.addTest("test new class with two args", + // test_new_class_with_two_args); exit(suite.run()); } diff --git a/taihe/test/ani_string/CMakeLists.txt b/taihe/test/ani_string/CMakeLists.txt index 5c8b709180..38c2470e4c 100644 --- a/taihe/test/ani_string/CMakeLists.txt +++ b/taihe/test/ani_string/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_string) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/string_io.taihe" "${CMAKE_CURRENT_LIST_DIR}/idl/string_op.taihe" diff --git a/taihe/test/ani_struct/CMakeLists.txt b/taihe/test/ani_struct/CMakeLists.txt index 543a4838ab..5cc966f4e6 100644 --- a/taihe/test/ani_struct/CMakeLists.txt +++ b/taihe/test/ani_struct/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_struct) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/struct_test.taihe" ) diff --git a/taihe/test/ani_struct/author/src/struct_test.impl.cpp b/taihe/test/ani_struct/author/src/struct_test.impl.cpp index 10900c99e5..b29fb1e310 100644 --- a/taihe/test/ani_struct/author/src/struct_test.impl.cpp +++ b/taihe/test/ani_struct/author/src/struct_test.impl.cpp @@ -169,6 +169,36 @@ public: return {{255, 0, 255}, drawName}; ; } + +::struct_test::Student create_student() +{ + return ::struct_test::Student {"Mary", 15}; +} + +::struct_test::Student process_student(::struct_test::Student const &a) +{ + return {a.name + " student", a.age + 10}; +} + +::struct_test::Teacher create_teacher() +{ + return ::struct_test::Teacher {"Rose", 25}; +} + +::struct_test::Teacher process_teacher(::struct_test::Teacher const &a) +{ + return {a.name + " teacher", a.age + 15}; +} + +::struct_test::G process_g(::struct_test::G const &a) +{ + return {{a.f.f + 1}, a.g + 2}; +} + +::struct_test::H process_h(::struct_test::H const &a) +{ + return {{{a.g.f.f + 1}, a.g.g + 2}, a.h + 3}; +} } // namespace TH_EXPORT_CPP_API_GetStructD(GetStructD); @@ -178,4 +208,10 @@ TH_EXPORT_CPP_API_GetStructG(GetStructG); TH_EXPORT_CPP_API_addNewCanvas(addNewCanvas); TH_EXPORT_CPP_API_GetSDB5Readonly(GetSDB5Readonly); TH_EXPORT_CPP_API_GetSEBReadonly(GetSEBReadonly); -TH_EXPORT_CPP_API_AddNewDraw(AddNewDraw); \ No newline at end of file +TH_EXPORT_CPP_API_AddNewDraw(AddNewDraw); +TH_EXPORT_CPP_API_create_student(create_student); +TH_EXPORT_CPP_API_process_student(process_student); +TH_EXPORT_CPP_API_create_teacher(create_teacher); +TH_EXPORT_CPP_API_process_teacher(process_teacher); +TH_EXPORT_CPP_API_process_g(process_g); +TH_EXPORT_CPP_API_process_h(process_h); \ No newline at end of file diff --git a/taihe/test/ani_struct/idl/struct_test.taihe b/taihe/test/ani_struct/idl/struct_test.taihe index 3ebf60c3dc..55a334153d 100644 --- a/taihe/test/ani_struct/idl/struct_test.taihe +++ b/taihe/test/ani_struct/idl/struct_test.taihe @@ -143,4 +143,38 @@ struct Draw { @extends brush: Brush; name: String; } -function AddNewDraw(drawName: String): Draw; \ No newline at end of file +function AddNewDraw(drawName: String): Draw; + +struct Student { + @readonly name: String; + age: i32; +} +function create_student(): Student; +function process_student(a: Student): Student; + +@class +struct Teacher { + @readonly name: String; + age: i32; +} + +function create_teacher(): Teacher; +function process_teacher(a: Teacher): Teacher; + +struct F { + f: i32; +} + +struct G { + @extends f: F; + g: i32; +} + +@class +struct H { + @extends g: G; + h: i32; +} + +function process_g(a: G): G; +function process_h(a: H): H; diff --git a/taihe/test/ani_struct/user/main.ets b/taihe/test/ani_struct/user/main.ets index 83d280f028..87d2845ba9 100644 --- a/taihe/test/ani_struct/user/main.ets +++ b/taihe/test/ani_struct/user/main.ets @@ -120,6 +120,39 @@ function test_struct_addNewDraw() { console.log("draw's color is " + draw.r + "," + draw.g + "," + draw.b); } +function test_struct_process_student() { + let student: StructTest.Student = {name: "Jack", age: 10}; + let pro_student = StructTest.process_student(student); + console.log("process student: ", pro_student.name, pro_student.age); + arktest.assertEQ(pro_student.name, "Jack student"); + arktest.assertEQ(pro_student.age, 20); +} + +function test_struct_process_teacher() { + let teacher: StructTest.Teacher = new StructTest.Teacher("Jony", 30); + let pro_teacher = StructTest.process_teacher(teacher); + console.log("process teacher:", pro_teacher.name, pro_teacher.age); + arktest.assertEQ(pro_teacher.name, "Jony teacher"); + arktest.assertEQ(pro_teacher.age, 45); +} + +function test_struct_process_g() { + let g: StructTest.G = {f: 0, g: 0}; + let new_g = StructTest.process_g(g); + console.log("process g:", new_g.f, new_g.g); + arktest.assertEQ(new_g.f, 1); + arktest.assertEQ(new_g.g, 2); +} + +function test_struct_process_h() { + let h = new StructTest.H(0, 0, 0); + let new_h = StructTest.process_h(h); + console.log("process h:", new_h.f, new_h.g, new_h.h); + arktest.assertEQ(new_h.f, 1); + arktest.assertEQ(new_h.g, 2); + arktest.assertEQ(new_h.h, 3); +} + function main() { let a: StructTest.A = { param1: 1, @@ -145,6 +178,10 @@ function main() { suite.addTest("test_struct_dbase5readonly", test_struct_dbase5readonly); suite.addTest("test_struct_sebreadonly", test_struct_sebreadonly); suite.addTest("test_struct_addNewDraw", test_struct_addNewDraw); + suite.addTest("test_struct_process_student", test_struct_process_student); + suite.addTest("test_struct_process_teacher", test_struct_process_teacher); + suite.addTest("test_struct_process_g", test_struct_process_g); + suite.addTest("test_struct_process_h", test_struct_process_h); exit(suite.run()); } \ No newline at end of file diff --git a/taihe/test/ani_struct_extend/CMakeLists.txt b/taihe/test/ani_struct_extend/CMakeLists.txt index 2ac665b038..96939ff55c 100644 --- a/taihe/test/ani_struct_extend/CMakeLists.txt +++ b/taihe/test/ani_struct_extend/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_struct_extend) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/struct_extend.taihe" ) diff --git a/taihe/test/ani_test/CMakeLists.txt b/taihe/test/ani_test/CMakeLists.txt index 3a356790ea..0e268b6026 100644 --- a/taihe/test/ani_test/CMakeLists.txt +++ b/taihe/test/ani_test/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_test) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/ani_test.taihe" ) diff --git a/taihe/test/ani_test/idl/ani_test.taihe b/taihe/test/ani_test/idl/ani_test.taihe index 1a23c5acfe..8511d5a14a 100644 --- a/taihe/test/ani_test/idl/ani_test.taihe +++ b/taihe/test/ani_test/idl/ani_test.taihe @@ -22,7 +22,7 @@ function makeData(): Data; function showData(s: Data): void; union Union { - @null empty; + empty: unit; iValue: i32; fValue: f32; sValue: String; diff --git a/taihe/test/ani_test/user/main.ets b/taihe/test/ani_test/user/main.ets index d48c2823fc..f27efafac8 100644 --- a/taihe/test/ani_test/user/main.ets +++ b/taihe/test/ani_test/user/main.ets @@ -39,7 +39,7 @@ function test_union() { // Test showUnion with different types ani_test.showUnion(null); ani_test.showUnion((1).toInt()); - ani_test.showUnion(2f); + ani_test.showUnion(2.0f); ani_test.showUnion("Hello from STS!"); // Test makeUnion returns expected values diff --git a/taihe/test/ani_thread/CMakeLists.txt b/taihe/test/ani_thread/CMakeLists.txt index d8e8729491..df06a4eb97 100644 --- a/taihe/test/ani_thread/CMakeLists.txt +++ b/taihe/test/ani_thread/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_thread) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/thread_test.taihe" ) diff --git a/taihe/test/ani_tp_cookies/CMakeLists.txt b/taihe/test/ani_tp_cookies/CMakeLists.txt index e8d09bfa3e..4b3ba0534d 100644 --- a/taihe/test/ani_tp_cookies/CMakeLists.txt +++ b/taihe/test/ani_tp_cookies/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_tp_cookies) + file(GLOB IDL_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/idl/*.taihe") set(GEN_ETS_FILES diff --git a/taihe/test/ani_tp_ui/CMakeLists.txt b/taihe/test/ani_tp_ui/CMakeLists.txt index 75f5ec0c90..69ab9a0da7 100644 --- a/taihe/test/ani_tp_ui/CMakeLists.txt +++ b/taihe/test/ani_tp_ui/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_tp_ui) + file(GLOB IDL_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/idl/*.taihe") set(GEN_ETS_FILES diff --git a/taihe/test/ani_typedarray/CMakeLists.txt b/taihe/test/ani_typedarray/CMakeLists.txt index eb99fa382e..1a60ca35fb 100644 --- a/taihe/test/ani_typedarray/CMakeLists.txt +++ b/taihe/test/ani_typedarray/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_typedarray) + set(IDL_FILES "${CMAKE_CURRENT_LIST_DIR}/idl/bar.taihe" ) diff --git a/taihe/test/ani_typedarray/idl/bar.taihe b/taihe/test/ani_typedarray/idl/bar.taihe index e7361a3010..651636cc0e 100644 --- a/taihe/test/ani_typedarray/idl/bar.taihe +++ b/taihe/test/ani_typedarray/idl/bar.taihe @@ -145,7 +145,7 @@ union MyUnion { i64Value: @typedarray Array; f32Value: @typedarray Array; f64Value: @typedarray Array; - @null empty; + empty: unit; } function MakeMyUnion(n: i32): MyUnion; function ShowMyUnion(u: MyUnion): void; \ No newline at end of file diff --git a/taihe/test/ani_typedarray/user/main.ets b/taihe/test/ani_typedarray/user/main.ets index 99c3b27be2..3355adad85 100644 --- a/taihe/test/ani_typedarray/user/main.ets +++ b/taihe/test/ani_typedarray/user/main.ets @@ -108,7 +108,7 @@ function test_bar_with_createAndPrintUint8Array() { typedArrInfo.createUint8Array(arr); let res = typedArrInfo.printUint8Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -117,7 +117,7 @@ function test_bar_with_setAndgetUint8Array() { let res = typedArrInfo.Uint8Array; let arr = new Uint8Array([1, 2, 3, 4, 5]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -126,7 +126,7 @@ function test_bar_with_createAndPrintInt8Array() { typedArrInfo.createInt8Array(arr); let res = typedArrInfo.printInt8Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -135,7 +135,7 @@ function test_bar_with_setAndgetInt8Array() { let res = typedArrInfo.Int8Array; let arr = new Int8Array([1, -2, 3, -4, 5]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -144,7 +144,7 @@ function test_bar_with_createAndPrintUint16Array() { typedArrInfo.createUint16Array(arr); let res = typedArrInfo.printUint16Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -153,7 +153,7 @@ function test_bar_with_setAndgetUint16Array() { let res = typedArrInfo.Uint16Array; let arr = new Uint16Array([10, 20, 30, 40, 50]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -162,7 +162,7 @@ function test_bar_with_createAndPrintInt16Array() { typedArrInfo.createInt16Array(arr); let res = typedArrInfo.printInt16Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -171,7 +171,7 @@ function test_bar_with_setAndgetInt16Array() { let res = typedArrInfo.Int16Array; let arr = new Int16Array([10, -20, 30, -40, 50]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -180,7 +180,7 @@ function test_bar_with_createAndPrintUint32Array() { typedArrInfo.createUint32Array(arr); let res = typedArrInfo.printUint32Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -189,7 +189,7 @@ function test_bar_with_setAndgetUint32Array() { let res = typedArrInfo.Uint32Array; let arr = new Uint32Array([100, 200, 300, 400, 500]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -198,7 +198,7 @@ function test_bar_with_createAndPrintInt32Array() { typedArrInfo.createInt32Array(arr); let res = typedArrInfo.printInt32Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -207,7 +207,7 @@ function test_bar_with_setAndgetInt32Array() { let res = typedArrInfo.Int32Array; let arr = new Int32Array([100, -200, 300, -400, 500]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -217,7 +217,7 @@ function test_bar_with_createAndPrintUint64Array() { typedArrInfo.createUint64Array(arr); let res = typedArrInfo.printUint64Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -227,7 +227,7 @@ function test_bar_with_setAndgetUint64Array() { let res = typedArrInfo.Uint64Array; let arr = new BigUint64Array([1000n, 2000n, 3000n, 4000n, 5000n]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -237,7 +237,7 @@ function test_bar_with_createAndPrintInt64Array() { typedArrInfo.createInt64Array(arr); let res = typedArrInfo.printInt64Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -247,7 +247,7 @@ function test_bar_with_setAndgetInt64Array() { let res = typedArrInfo.Int64Array; let arr = new BigInt64Array([1000n, -2000n, 3000n, -4000n, 5000n]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => value === res[index]); + arr.every((value, index) => value === res[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -256,7 +256,7 @@ function test_bar_with_createAndPrintFloat32Array() { typedArrInfo.createFloat32Array(arr); let res = typedArrInfo.printFloat32Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => Math.abs(value - res[index]) < 1e-6); + arr.every((value, index) => Math.abs(value - res[index.toInt()]) < 1e-6); arktest.assertEQ(arraysAreEqual, true); } @@ -265,7 +265,7 @@ function test_bar_with_setAndgetFloat32Array() { let res = typedArrInfo.Float32Array; let arr = new Float32Array([1.0, 2.0, 3.0, 4.0, 5.0]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => Math.abs(value - res[index]) < 1e-6); + arr.every((value, index) => Math.abs(value - res[index.toInt()]) < 1e-6); arktest.assertEQ(arraysAreEqual, true); } @@ -274,7 +274,7 @@ function test_bar_with_createAndPrintFloat64Array() { typedArrInfo.createFloat64Array(arr); let res = typedArrInfo.printFloat64Array(arr); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => Math.abs(value - res[index]) < 1e-15); + arr.every((value, index) => Math.abs(value - res[index.toInt()]) < 1e-15); arktest.assertEQ(arraysAreEqual, true); } @@ -283,7 +283,7 @@ function test_bar_with_setAndgetFloat64Array() { let res = typedArrInfo.Float64Array; let arr = new Float64Array([1.0, 2.0, 3.0, 4.0, 5.0]); let arraysAreEqual = arr.length === res.length && - arr.every((value, index) => Math.abs(value - res[index]) < 1e-15); + arr.every((value, index) => Math.abs(value - res[index.toInt()]) < 1e-15); arktest.assertEQ(arraysAreEqual, true); } @@ -299,7 +299,7 @@ function test_bar_with_convertToInt64Array() { let exceptArr: BigInt64Array = new BigInt64Array([1112n, 2220n, 3336n, 4440n, 5560n]); let arraysAreEqual = res.length === exceptArr.length && - res.every((value, index) => value === exceptArr[index]); + res.every((value, index) => value === exceptArr[index.toInt()]); arktest.assertEQ(arraysAreEqual, true); } @@ -315,7 +315,7 @@ function test_bar_with_convertToFloat64Array() { let exceptArr: Float64Array = new Float64Array([1112.0, -2216.0, 3336.0, -4432.0, 5560.0]); let arraysAreEqual = res.length === exceptArr.length && - res.every((value, index) => Math.abs(value - exceptArr[index]) < 1e-6); + res.every((value, index) => Math.abs(value - exceptArr[index.toInt()]) < 1e-6); arktest.assertEQ(arraysAreEqual, true); } diff --git a/taihe/test/ani_union/CMakeLists.txt b/taihe/test/ani_union/CMakeLists.txt index 7e474b3c00..0b789acbf0 100644 --- a/taihe/test/ani_union/CMakeLists.txt +++ b/taihe/test/ani_union/CMakeLists.txt @@ -11,6 +11,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +include(../../cmake/TaiheWorkflows.cmake) +setup_taihe_cmake_test_env() +cmake_minimum_required(VERSION 3.18) +project(ani_union) + file(GLOB IDL_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/idl/*.taihe") set(GEN_ETS_FILES diff --git a/taihe/test/ani_union/idl/union_ani.taihe b/taihe/test/ani_union/idl/union_ani.taihe index fc4b891414..9843d024b2 100644 --- a/taihe/test/ani_union/idl/union_ani.taihe +++ b/taihe/test/ani_union/idl/union_ani.taihe @@ -18,9 +18,9 @@ struct Pair { } union InnerUnion { + undefinedValue: @undefined unit; stringValue: String; pairValue: Pair; - @undefined undefinedValue; } union MyUnion { @@ -40,7 +40,7 @@ union BasicUnion { float64Value: f64; stringValue: String; boolValue: bool; - @null empty; + empty: unit; } function MakeAndShowBasicUnion(data: BasicUnion): BasicUnion; @@ -61,7 +61,7 @@ union TypedArrayUnion { f32Value: @typedarray Array; f64Value: @typedarray Array; - @null empty; + empty: unit; } function MakeAndShowTypedArrayUnion(data: TypedArrayUnion): TypedArrayUnion; @@ -73,7 +73,7 @@ struct ArrayStruct { union ArrayUnion { floatValue: ArrayStruct; - @null empty; + empty: unit; } function MakeAndShowArrayUnion(data: ArrayUnion): ArrayUnion; @@ -92,7 +92,7 @@ struct IntStruct { union StructUnion { strValue: StrStruct; intValue: IntStruct; - @null empty; + empty: unit; } function MakeAndShowStructUnion(data: StructUnion): StructUnion; -- Gitee