diff --git a/.github/workflows/sync_to_tencent_code.yml b/.github/workflows/sync_to_tencent_code.yml index 3574dda2..45a60ff9 100644 --- a/.github/workflows/sync_to_tencent_code.yml +++ b/.github/workflows/sync_to_tencent_code.yml @@ -9,6 +9,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: mirror-repository uses: spyoungtech/mirror-action@v0.4.3 with: diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index 440341ae..ec718352 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -5,17 +5,18 @@ on: push: branches: - main - - 'feature/*' - - 'bugfix/*' jobs: - mac-build: + mac-clang-run: runs-on: macos-latest strategy: fail-fast: false matrix: backends: [ V8, JavaScriptCore, Lua, Empty ] - build_type: [ Debug, Release ] + build_type: + - Debug + # mac runner seems to be slow and less + # - Release steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 @@ -43,13 +44,15 @@ jobs: cd build ./UnitTests - windows-build: + windows-msvc-run: runs-on: windows-latest strategy: fail-fast: false matrix: backends: [ V8, JavaScriptCore, Lua, Empty ] - build_type: [ Debug, Release ] + build_type: + - Debug + - Release steps: - uses: actions/checkout@v2 - uses: actions/cache@v2 @@ -68,7 +71,6 @@ jobs: run: | mkdir -Force build cd build - ls dependencies cmake ../test -A X64 -DSCRIPTX_BACKEND=${{ matrix.backends }} - name: Configure cmake X86 if: matrix.backends == 'JavaScriptCore' @@ -91,8 +93,76 @@ jobs: cd build ${{ matrix.build_type }}/UnitTests + ubuntu-gcc-build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + backends: [ V8, JavaScriptCore, Lua, Empty ] + build_type: + - Debug + #- Release + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v2 + with: + key: build-dependencies-macos + path: | + build/ScriptXTestLibs + build/googletest-src + - name: Configure cmake + env: + SCRIPTX_TEST_FORCE_UPDATE_DEPS: ON + SCRIPTX_TEST_BUILD_ONLY: ON + run: | + mkdir -p build && cd build + cmake ../test \ + -DSCRIPTX_BACKEND=${{ matrix.backends }} \ + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + - name: Compile UnitTests + run: | + cd build + cmake --build . -j $(nproc) --target UnitTests + + android-clang-build: + # disable for now + # 1. we don't have android libraries + # 2. -undefined dynamic_lookup not work with NDK + if: ${{ false }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + backends: [ V8, JavaScriptCore, Lua, Empty ] + build_type: + - Debug + #- Release + steps: + - uses: actions/checkout@v2 + - uses: actions/cache@v2 + with: + key: build-dependencies-macos + path: | + build/ScriptXTestLibs + build/googletest-src + - name: Configure cmake + env: + SCRIPTX_TEST_FORCE_UPDATE_DEPS: ON + SCRIPTX_TEST_BUILD_ONLY: ON + run: | + mkdir -p build && cd build + cmake ../test \ + -DANDROID_ABI=arm64-v8a \ + -DANDROID_PLATFORM=android-21 \ + -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \ + -DSCRIPTX_BACKEND=${{ matrix.backends }} \ + -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + - name: Compile UnitTests + run: | + cd build + cmake --build . -j $(nproc) --target UnitTests - node-js: + node-gcc-run: runs-on: ubuntu-latest strategy: fail-fast: false @@ -115,7 +185,7 @@ jobs: cd test/node_addon npm run test - Webassembly: + webassembly-emscripten-run: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -151,6 +221,3 @@ jobs: cd build # exclude failed tests node UnitTests.js '--gtest_filter=-ThreadPool.*:EngineScopeTest.ExitEngine:EngineScopeTest.TwoThreads:EngineScopeTest.ThreadLocal:MessageQueue.Interrupt:MessageQueue.Shutdown:MessageQueue.ShutdownNow:MessageQueue.FullAndPostInsideLoopQueue:ReferenceTest.WeakGc:ReferenceTest.WeakGc:ReferenceTest.GlobalNotClear:ReferenceTest.GlobalOnEngineDestroy:ReferenceTest.WeakOnEngineDestroy:ReferenceTest.WeakNotClrear:ManagedObjectTest.EngineDispose:ManagedObjectTest.FunctionCallback:PressureTest.All' - -# todo: add NDK build? -# todo: add linux build? diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 79733fb6..506b960c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,7 +87,7 @@ They have all licensed their contributions to this project under the licensing terms detailed in LICENSE, while still keep copyright of their contributions. People who commit code to this project are encouraged to append their names here. -| name | email | copyright | -| :--: | :---: | :---: | -| Yang Chao | | THL A29 Limited, a Tencent company. | -| Yang Chao | | | +| name | email | copyright | note | +| :--: | :---: | :---: | | +| Yang Chao | | THL A29 Limited, a Tencent company. | | +| Yang Chao | | | | diff --git a/backend/JavaScriptCore/JscEngine.cc b/backend/JavaScriptCore/JscEngine.cc index 9a487803..976ad854 100644 --- a/backend/JavaScriptCore/JscEngine.cc +++ b/backend/JavaScriptCore/JscEngine.cc @@ -28,7 +28,8 @@ JSClassRef JscEngine::externalClass_{}; // When we link against high-version library // but run with a low-version one, macOS/iOS linker just set the undefined symbols to nullptr. -bool JscEngine::hasByteBufferAPI_ = &JSValueGetTypedArrayType != nullptr; +// cast to void* to suppress g++ -Werror=address +bool JscEngine::hasByteBufferAPI_ = reinterpret_cast(&JSValueGetTypedArrayType) != nullptr; JscEngine::JscEngine(std::shared_ptr mq) : messageQueue_(mq ? std::move(mq) : std::make_shared()) { diff --git a/backend/JavaScriptCore/JscNative.cc b/backend/JavaScriptCore/JscNative.cc index e04bd6db..006936c4 100644 --- a/backend/JavaScriptCore/JscNative.cc +++ b/backend/JavaScriptCore/JscNative.cc @@ -35,7 +35,7 @@ bool Arguments::hasThiz() const { return callbackInfo_.thisObject != nullptr; } size_t Arguments::size() const { return callbackInfo_.size; } Local Arguments::operator[](size_t i) const { - if (i < 0 || i >= size()) { + if (i >= size()) { return {}; } // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) diff --git a/backend/Lua/LuaByteBufferImpl.cc b/backend/Lua/LuaByteBufferImpl.cc index 0b29ed8a..959a258f 100644 --- a/backend/Lua/LuaByteBufferImpl.cc +++ b/backend/Lua/LuaByteBufferImpl.cc @@ -52,8 +52,8 @@ class LuaByteBuffer : public ScriptClass { return; } } - throw Exception( - (std::ostringstream() << "ByteBuffer should be created like ByteBuffer(n) n >= 0").str()); + + throw Exception(std::string("ByteBuffer should be created like ByteBuffer(n) n >= 0")); } const std::shared_ptr &getNativeBuffer() const { return nativeBuffer_; } @@ -62,22 +62,22 @@ class LuaByteBuffer : public ScriptClass { using ScriptClass::getScriptObject; template - Local write(uint32_t pos, T data) { + Local write(int32_t pos, T data) { writePtr(pos) = data; return getScriptObject(); } template - T read(uint32_t pos) { + T read(int32_t pos) { return readPtr(pos); } private: template - T &writePtr(uint32_t pos) { + T &writePtr(int32_t pos) { // lua use 1-based index pos--; - if (pos < 0 || pos + sizeof(T) >= size_) { + if (pos < 0 || pos + sizeof(T) > size_) { throwIndexOutOfRange(pos + 1); } if (pos % sizeof(T) != 0) { @@ -87,10 +87,10 @@ class LuaByteBuffer : public ScriptClass { } template - T readPtr(uint32_t pos) { + T readPtr(int32_t pos) { // lua use 1-based index pos--; - if (pos < 0 || pos >= size_) { + if (pos < 0 || pos + sizeof(T) > size_) { throwIndexOutOfRange(pos + 1); } if (pos % sizeof(T) != 0) { @@ -100,15 +100,15 @@ class LuaByteBuffer : public ScriptClass { } void throwIndexOutOfRange(uint32_t pos) const { - throw Exception((std::ostringstream() - << "ByteBuffer index out of range size:" << size_ << " position:" << pos) - .str()); + std::ostringstream msg; + msg << "ByteBuffer index out of range size:" << size_ << " position:" << pos; + throw Exception(msg.str()); } void throwNotAlignedMemoryAccess(uint32_t pos, uint32_t size) const { - throw Exception((std::ostringstream() << "ByteBuffer access memory at: [" << pos - << "] is not aligned with: [" << size << "]") - .str()); + std::ostringstream msg; + msg << "ByteBuffer access memory at: [" << pos << "] is not aligned with: [" << size << "]"; + throw Exception(msg.str()); } }; diff --git a/backend/Lua/LuaNative.cc b/backend/Lua/LuaNative.cc index 55c77aff..8c66d05d 100644 --- a/backend/Lua/LuaNative.cc +++ b/backend/Lua/LuaNative.cc @@ -50,7 +50,7 @@ bool Arguments::hasThiz() const { size_t Arguments::size() const { return callbackInfo_.size; } Local Arguments::operator[](size_t i) const { - if (i >= 0 && i < size()) { + if (i < size()) { return lua_backend::LuaEngine::make>( static_cast(callbackInfo_.stackBase + i)); } diff --git a/src/types.h b/src/types.h index 9284b0cc..f4307a7e 100644 --- a/src/types.h +++ b/src/types.h @@ -136,7 +136,7 @@ class Tracer; // using SFINA technology. // android ndk can't compile follow declare until NDK r21 -// #define StringLikeConcept(StringLike) \ +// #define StringLikeConcept(StringLike) // typename = std::void_t()))> #ifdef __cpp_lib_char8_t diff --git a/src/utils/MessageQueue.cc b/src/utils/MessageQueue.cc index 05014e9c..0e7f1e19 100644 --- a/src/utils/MessageQueue.cc +++ b/src/utils/MessageQueue.cc @@ -115,7 +115,7 @@ std::unique_ptr MessageQueue::obtainInplaceMessage( auto msg = messagePool_.obtain(); // InplaceMessage is essentially a Message with some extra non-virtual method. // so it's safe to cast - msg->handlerProc = reinterpret_cast(handlerProc); + msg->handlerProc = reinterpret_cast(reinterpret_cast(handlerProc)); return std::unique_ptr(reinterpret_cast(msg)); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5839dd59..0ca2cc43 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -96,6 +96,16 @@ target_link_libraries(UnitTests gtest ScriptX) if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # using clang or gcc target_compile_options(ScriptX PRIVATE -Werror -Wall -Wextra -Wno-unused-parameter) + + if (SCRIPTX_TEST_BUILD_ONLY) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + target_link_options(UnitTests PRIVATE -Wl,-undefined,dynamic_lookup) + else () + target_link_options(UnitTests PRIVATE -Wl,--unresolved-symbols=ignore-in-object-files) + endif () + message(WARNING "SCRIPTX_TEST_BUILD_ONLY is ON, the compiled UnitTests won't run properly. " + "Compiler is ${CMAKE_CXX_COMPILER_ID}") + endif () elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC") # using Visual Studio C++ target_compile_options(ScriptX PRIVATE /W4 /WX /Zc:__cplusplus /utf-8 /MP diff --git a/test/cmake/TestEnv.cmake b/test/cmake/TestEnv.cmake index 3ced4b13..60a4be75 100644 --- a/test/cmake/TestEnv.cmake +++ b/test/cmake/TestEnv.cmake @@ -2,19 +2,27 @@ # default location is next to ScriptX root dir # ScriptXLibs download by cmake script -if ("${SCRIPTX_LIBS}" STREQUAL "") - if (NOT "$ENV{SCRIPTX_LIBS}" STREQUAL "") - set(SCRIPTX_LIBS "$ENV{SCRIPTX_LIBS}") +if ("${SCRIPTX_TEST_LIBS}" STREQUAL "") + if (NOT "$ENV{SCRIPTX_TEST_LIBS}" STREQUAL "") + set(SCRIPTX_TEST_LIBS "$ENV{SCRIPTX_TEST_LIBS}") else () - set(SCRIPTX_LIBS ${CMAKE_CURRENT_BINARY_DIR}/ScriptXTestLibs) + set(SCRIPTX_TEST_LIBS ${CMAKE_CURRENT_BINARY_DIR}/ScriptXTestLibs) endif () endif () -if ("${SCRIPTX_GOOGLE_TEST}" STREQUAL "") - if (NOT "$ENV{SCRIPTX_GOOGLE_TEST}" STREQUAL "") - set(SCRIPTX_GOOGLE_TEST "$ENV{SCRIPTX_GOOGLE_TEST}") +if ("${SCRIPTX_TEST_GOOGLE_TEST}" STREQUAL "") + if (NOT "$ENV{SCRIPTX_TEST_GOOGLE_TEST}" STREQUAL "") + set(SCRIPTX_TEST_GOOGLE_TEST "$ENV{SCRIPTX_TEST_GOOGLE_TEST}") else () - set(SCRIPTX_GOOGLE_TEST ${CMAKE_CURRENT_BINARY_DIR}/googletest-src) + set(SCRIPTX_TEST_GOOGLE_TEST ${CMAKE_CURRENT_BINARY_DIR}/googletest-src) + endif () +endif () + +if ("${SCRIPTX_TEST_BUILD_ONLY}" STREQUAL "") + if (NOT "$ENV{SCRIPTX_TEST_BUILD_ONLY}" STREQUAL "") + set(SCRIPTX_TEST_BUILD_ONLY "$ENV{SCRIPTX_TEST_BUILD_ONLY}") + else () + set(SCRIPTX_TEST_BUILD_ONLY OFF) endif () endif () @@ -42,33 +50,41 @@ endif () include(${CMAKE_CURRENT_LIST_DIR}/test_libs/CMakeLists.txt) if (${SCRIPTX_BACKEND} STREQUAL V8) - if (APPLE) + if (SCRIPTX_TEST_BUILD_ONLY) + set(DEVOPS_LIBS_INCLUDE + "${SCRIPTX_TEST_LIBS}/mac/v8/include" + CACHE STRING "" FORCE) + elseif (APPLE) set(DEVOPS_LIBS_INCLUDE - "${SCRIPTX_LIBS}/mac/v8/include" + "${SCRIPTX_TEST_LIBS}/mac/v8/include" CACHE STRING "" FORCE) set(DEVOPS_LIBS_LIBPATH - "${SCRIPTX_LIBS}/mac/v8/libv8_monolith.a" + "${SCRIPTX_TEST_LIBS}/mac/v8/libv8_monolith.a" CACHE STRING "" FORCE) elseif (WIN32) set(DEVOPS_LIBS_INCLUDE - "${SCRIPTX_LIBS}/win64/v8/include" + "${SCRIPTX_TEST_LIBS}/win64/v8/include" CACHE STRING "" FORCE) set(DEVOPS_LIBS_LIBPATH - "${SCRIPTX_LIBS}/win64/v8/v8_libbase.dll.lib" - "${SCRIPTX_LIBS}/win64/v8/v8_libplatform.dll.lib" - "${SCRIPTX_LIBS}/win64/v8/v8.dll.lib" + "${SCRIPTX_TEST_LIBS}/win64/v8/v8_libbase.dll.lib" + "${SCRIPTX_TEST_LIBS}/win64/v8/v8_libplatform.dll.lib" + "${SCRIPTX_TEST_LIBS}/win64/v8/v8.dll.lib" CACHE STRING "" FORCE) add_custom_command(TARGET UnitTests POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory - "${SCRIPTX_LIBS}/win64/v8/dll" $ + "${SCRIPTX_TEST_LIBS}/win64/v8/dll" $ ) endif () elseif (${SCRIPTX_BACKEND} STREQUAL JavaScriptCore) - if (APPLE) + if (SCRIPTX_TEST_BUILD_ONLY) + set(DEVOPS_LIBS_INCLUDE + "${SCRIPTX_TEST_LIBS}/win64/jsc/include" + CACHE STRING "" FORCE) + elseif (APPLE) set(DEVOPS_LIBS_INCLUDE "" CACHE STRING "" FORCE) @@ -78,35 +94,38 @@ elseif (${SCRIPTX_BACKEND} STREQUAL JavaScriptCore) CACHE STRING "" FORCE) elseif (WIN32) set(DEVOPS_LIBS_INCLUDE - "${SCRIPTX_LIBS}/win64/jsc/include" + "${SCRIPTX_TEST_LIBS}/win64/jsc/include" CACHE STRING "" FORCE) set(DEVOPS_LIBS_LIBPATH - "${SCRIPTX_LIBS}/win64/jsc/JavaScriptCore.lib" + "${SCRIPTX_TEST_LIBS}/win64/jsc/JavaScriptCore.lib" CACHE STRING "" FORCE) add_custom_command(TARGET UnitTests POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory - "${SCRIPTX_LIBS}/win64/jsc/dll" $ + "${SCRIPTX_TEST_LIBS}/win64/jsc/dll" $ ) - endif () elseif (${SCRIPTX_BACKEND} STREQUAL Lua) - if (APPLE) + if (SCRIPTX_TEST_BUILD_ONLY) + set(DEVOPS_LIBS_INCLUDE + "${SCRIPTX_TEST_LIBS}/mac/lua/include" + CACHE STRING "" FORCE) + elseif (APPLE) set(DEVOPS_LIBS_INCLUDE - "${SCRIPTX_LIBS}/mac/lua/include" + "${SCRIPTX_TEST_LIBS}/mac/lua/include" CACHE STRING "" FORCE) set(DEVOPS_LIBS_LIBPATH - "${SCRIPTX_LIBS}/mac/lua/liblua.a" + "${SCRIPTX_TEST_LIBS}/mac/lua/liblua.a" CACHE STRING "" FORCE) elseif (WIN32) set(DEVOPS_LIBS_INCLUDE - "${SCRIPTX_LIBS}/win64/lua/include" + "${SCRIPTX_TEST_LIBS}/win64/lua/include" CACHE STRING "" FORCE) set(DEVOPS_LIBS_LIBPATH - "${SCRIPTX_LIBS}/win64/lua/Lua54.lib" + "${SCRIPTX_TEST_LIBS}/win64/lua/Lua54.lib" CACHE STRING "" FORCE) endif () diff --git a/test/cmake/gtest/CMakeLists.txt b/test/cmake/gtest/CMakeLists.txt index 40fd69eb..604e8aa7 100644 --- a/test/cmake/gtest/CMakeLists.txt +++ b/test/cmake/gtest/CMakeLists.txt @@ -1,6 +1,6 @@ # https://github.com/google/googletest/blob/master/googletest/README.md -if (NOT EXISTS ${SCRIPTX_GOOGLE_TEST}) +if (NOT EXISTS ${SCRIPTX_TEST_GOOGLE_TEST}) # Download and unpack googletest at configure time configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in googletest-download/CMakeLists.txt) execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . @@ -17,8 +17,8 @@ if (NOT EXISTS ${SCRIPTX_GOOGLE_TEST}) message(FATAL_ERROR "Build step for googletest failed: ${result}") endif () elseif (("${SCRIPTX_TEST_FORCE_UPDATE_DEPS}" STREQUAL ON) OR ("$ENV{SCRIPTX_TEST_FORCE_UPDATE_DEPS}" STREQUAL ON)) - message(STATUS "update SCRIPTX_GOOGLE_TEST ${SCRIPTX_GOOGLE_TEST}") - execute_process(COMMAND git pull --force --rebase WORKING_DIRECTORY ${SCRIPTX_GOOGLE_TEST}) + message(STATUS "update SCRIPTX_TEST_GOOGLE_TEST ${SCRIPTX_TEST_GOOGLE_TEST}") + execute_process(COMMAND git pull --force --rebase WORKING_DIRECTORY ${SCRIPTX_TEST_GOOGLE_TEST}) endif () # Prevent overriding the parent project's compiler/linker @@ -27,7 +27,7 @@ set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # Add googletest directly to our build. This defines # the gtest and gtest_main targets. -add_subdirectory(${SCRIPTX_GOOGLE_TEST} +add_subdirectory(${SCRIPTX_TEST_GOOGLE_TEST} ${CMAKE_CURRENT_BINARY_DIR}/googletest-build EXCLUDE_FROM_ALL) diff --git a/test/cmake/test_libs/CMakeLists.txt b/test/cmake/test_libs/CMakeLists.txt index 45537dd7..e6533624 100644 --- a/test/cmake/test_libs/CMakeLists.txt +++ b/test/cmake/test_libs/CMakeLists.txt @@ -1,6 +1,6 @@ -if (NOT EXISTS "${SCRIPTX_LIBS}") +if (NOT EXISTS "${SCRIPTX_TEST_LIBS}") - message(STATUS "SCRIPTX_LIBS ${SCRIPTX_LIBS} is empty. " + message(STATUS "SCRIPTX_TEST_LIBS ${SCRIPTX_TEST_LIBS} is empty. " "Downloading test libs including V8, JavaScriptCore, Lua, etc.") configure_file(${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt.in ScriptXLibsDownload/CMakeLists.txt) @@ -21,6 +21,6 @@ if (NOT EXISTS "${SCRIPTX_LIBS}") message(FATAL_ERROR "Build step for download ScriptXLibs failed: ${result}") endif () elseif (("${SCRIPTX_TEST_FORCE_UPDATE_DEPS}" STREQUAL ON) OR ("$ENV{SCRIPTX_TEST_FORCE_UPDATE_DEPS}" STREQUAL ON)) - message(STATUS "update SCRIPTX_LIBS ${SCRIPTX_LIBS}") - execute_process(COMMAND git pull --force --rebase WORKING_DIRECTORY ${SCRIPTX_LIBS}) + message(STATUS "update SCRIPTX_TEST_LIBS ${SCRIPTX_TEST_LIBS}") + execute_process(COMMAND git pull --force --rebase WORKING_DIRECTORY ${SCRIPTX_TEST_LIBS}) endif () diff --git a/test/cmake/test_libs/CMakeLists.txt.in b/test/cmake/test_libs/CMakeLists.txt.in index 8d5c8b91..99152164 100644 --- a/test/cmake/test_libs/CMakeLists.txt.in +++ b/test/cmake/test_libs/CMakeLists.txt.in @@ -7,7 +7,7 @@ ExternalProject_Add(TestLibs GIT_REPOSITORY https://github.com/LanderlYoung/ScriptXTestLibs.git GIT_TAG main GIT_SHALLOW 1 - SOURCE_DIR "${SCRIPTX_LIBS}" + SOURCE_DIR "${SCRIPTX_TEST_LIBS}" BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/ScriptXLibsDownload/build" CONFIGURE_COMMAND "" BUILD_COMMAND ""