From 9c18f482641fa54add4201ffa44639c5c6b25267 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Wed, 26 Jul 2023 14:23:19 +0800 Subject: [PATCH 1/5] fix a potential bug of str initialization order --- include/pocketpy/str.h | 5 ++--- src/str.cpp | 32 +++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index eaaea76b..430e0582 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -115,11 +115,10 @@ struct StrName { return this->index > other.index; } - inline static std::map> _interned; - inline static std::vector _r_interned; - static bool is_valid(int index); static StrName get(std::string_view s); + static std::map>& _interned(); + static std::vector& _r_interned(); }; struct FastStrStream{ diff --git a/src/str.cpp b/src/str.cpp index 7d00a15b..ab8a254b 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -312,22 +312,33 @@ int utf8len(unsigned char c, bool suppress){ return os << sn.sv(); } + std::map>& StrName::_interned(){ + static std::map> interned; + return interned; + } + + std::vector& StrName::_r_interned(){ + static std::vector r_interned; + return r_interned; + } + StrName StrName::get(std::string_view s){ - auto it = _interned.find(s); - if(it != _interned.end()) return StrName(it->second); - uint16_t index = (uint16_t)(_r_interned.size() + 1); - _interned[s] = index; - _r_interned.push_back(s); + auto it = _interned().find(s); + if(it != _interned().end()) return StrName(it->second); + uint16_t index = (uint16_t)(_r_interned().size() + 1); + std::string str(s); + _interned()[str] = index; + _r_interned().push_back(str); return StrName(index); } Str StrName::escape() const { - return _r_interned[index-1].escape(); + return Str(sv()).escape(); } bool StrName::is_valid(int index) { - // check _r_interned[index-1] is valid - return index > 0 && index <= _r_interned.size(); + // check _r_interned()[index-1] is valid + return index > 0 && index <= _r_interned().size(); } StrName::StrName(): index(0) {} @@ -337,7 +348,10 @@ int utf8len(unsigned char c, bool suppress){ index = get(s.sv()).index; } - std::string_view StrName::sv() const { return _r_interned[index-1].sv(); } + std::string_view StrName::sv() const { + const std::string& str = _r_interned()[index-1]; + return std::string_view(str); + } FastStrStream& FastStrStream::operator<<(const Str& s){ parts.push_back(&s); From fd8f121ceac18e884f692a01127f3ad2ca04e024 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Wed, 26 Jul 2023 14:36:55 +0800 Subject: [PATCH 2/5] ... --- CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91a87ebc..f6f98f56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,13 @@ if(MSVC) add_compile_options("/utf-8") endif() +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + +set(CMAKE_CXX_FLAGS_DEBUG "-g") +set(CMAKE_CXX_FLAGS_RELEASE "-O2") + include_directories(${CMAKE_CURRENT_LIST_DIR}/include) aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/src POCKETPY_SRC) From 97f8ec81e65b4444a5a6cee8c2e449d7b52a1f05 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Wed, 26 Jul 2023 15:16:55 +0800 Subject: [PATCH 3/5] ... --- include/pocketpy/cffi.h | 1 + include/pocketpy/pocketpy.h | 1 - src/random.cpp | 1 - 3 files changed, 1 insertion(+), 2 deletions(-) diff --git a/include/pocketpy/cffi.h b/include/pocketpy/cffi.h index bff32bdd..f4adfb9f 100644 --- a/include/pocketpy/cffi.h +++ b/include/pocketpy/cffi.h @@ -2,6 +2,7 @@ #include "common.h" #include "vm.h" +#include "_generated.h" namespace pkpy { diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index c991e30e..14ac1464 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -9,7 +9,6 @@ #include "linalg.h" #include "easing.h" #include "io.h" -#include "_generated.h" #include "vm.h" #include "re.h" #include "random.h" diff --git a/src/random.cpp b/src/random.cpp index 7165f256..064f7d10 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -3,7 +3,6 @@ #if PK_MODULE_RANDOM #include -#include "pocketpy/_generated.h" namespace pkpy{ From 973a1d2945703f39a71b2e4c4f7c8a1d387cb8a7 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Wed, 26 Jul 2023 15:28:08 +0800 Subject: [PATCH 4/5] ... --- include/pocketpy/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 8d771bf0..5ce4cc74 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -20,7 +20,7 @@ #include #include -#define PK_VERSION "1.1.0" +#define PK_VERSION "1.1.1" #include "config.h" #include "export.h" From 2a7fde26404624da93b639c85b4b5a9122133945 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Wed, 26 Jul 2023 15:57:46 +0800 Subject: [PATCH 5/5] add test for macos --- .github/workflows/main.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 55924c2e..670240a6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -68,6 +68,10 @@ jobs: runs-on: macos-latest steps: - uses: actions/checkout@v3 + - name: Compile and Test + run: | + bash build.sh + python3 scripts/run_tests.py - run: | python3 amalgamate.py cd plugins/macos/pocketpy @@ -107,4 +111,4 @@ jobs: rm -rf tmp - uses: actions/upload-artifact@v3 with: - path: plugins/flutter/example/build/app/outputs/flutter-apk/output \ No newline at end of file + path: plugins/flutter/example/build/app/outputs/flutter-apk/output