This commit is contained in:
blueloveTH 2023-07-09 02:54:45 +08:00
parent c0b5fdc979
commit 48da917caf
7 changed files with 42 additions and 26 deletions

View File

@ -21,6 +21,9 @@ if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR- /EHsc /utf-8 /O2") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR- /EHsc /utf-8 /O2")
else() else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fexceptions -O2") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fexceptions -O2")
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
endif() endif()
option(PK_EXPORT_C_API "Export C API" ON) option(PK_EXPORT_C_API "Export C API" ON)

View File

@ -41,6 +41,7 @@ To compile it with your project, these flags must be set:
+ `--std=c++17` flag must be set + `--std=c++17` flag must be set
+ Exception must be enabled + Exception must be enabled
+ RTTI is not required + RTTI is not required
+ If clang is used, `-stdlib=libc++` must be set
!!! !!!
For maximum performance, we recommend to use `clang++` with `-O2` flag. For maximum performance, we recommend to use `clang++` with `-O2` flag.

View File

@ -23,6 +23,7 @@ To compile it with your project, these flags must be set:
+ `--std=c++17` flag must be set + `--std=c++17` flag must be set
+ Exception must be enabled + Exception must be enabled
+ RTTI is not required + RTTI is not required
+ If clang is used, `-stdlib=libc++` must be set
### Example ### Example

View File

@ -163,27 +163,18 @@ inline bool is_unicode_Lo_char(uint32_t c) {
struct StrName { struct StrName {
uint16_t index; uint16_t index;
StrName(): index(0) {} StrName();
explicit StrName(uint16_t index): index(index) {} explicit StrName(uint16_t index);
StrName(const char* s): index(get(s).index) {} StrName(const char* s);
StrName(const Str& s){ StrName(const Str& s);
index = get(s.sv()).index; std::string_view sv() const;
}
std::string_view sv() const { return _r_interned[index-1].sv(); }
bool empty() const { return index == 0; } bool empty() const { return index == 0; }
friend std::ostream& operator<<(std::ostream& os, const StrName& sn){ friend std::ostream& operator<<(std::ostream& os, const StrName& sn){
return os << sn.sv(); return os << sn.sv();
} }
static bool is_valid(int index) { Str escape() const;
// check _r_interned[index-1] is valid
return index > 0 && index <= _r_interned.size();
}
Str escape() const {
return _r_interned[index-1].escape();
}
bool operator==(const StrName& other) const noexcept { bool operator==(const StrName& other) const noexcept {
return this->index == other.index; return this->index == other.index;
@ -204,14 +195,8 @@ struct StrName {
inline static std::map<Str, uint16_t, std::less<>> _interned; inline static std::map<Str, uint16_t, std::less<>> _interned;
inline static std::vector<Str> _r_interned; inline static std::vector<Str> _r_interned;
static StrName get(std::string_view s){ static bool is_valid(int index);
auto it = _interned.find(s); static StrName get(std::string_view 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);
return StrName(index);
}
}; };
struct FastStrStream{ struct FastStrStream{

View File

@ -145,10 +145,10 @@ void init_builtins(VM* _vm) {
_vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) { _vm->bind_builtin_func<1>("__import__", [](VM* vm, ArgsView args) {
const Str& name = CAST(Str&, args[0]); const Str& name = CAST(Str&, args[0]);
auto dot = name.sv().find_first_of("."); auto dot = name.sv().find_last_of(".");
if(dot != std::string_view::npos){ if(dot != std::string_view::npos){
auto ext = name.sv().substr(dot); auto ext = name.sv().substr(dot);
if(ext == ".so" || ext == ".dll" || ext == ".dylib" || ext == ".pyd"){ if(ext == ".so" || ext == ".dll" || ext == ".dylib"){
dylib_entry_t entry = load_dylib(name.c_str()); dylib_entry_t entry = load_dylib(name.c_str());
if(!entry){ if(!entry){
vm->_error("ImportError", "cannot load dynamic library: " + name.escape()); vm->_error("ImportError", "cannot load dynamic library: " + name.escape());

View File

@ -239,4 +239,31 @@ namespace pkpy {
int Str::u8_length() const { int Str::u8_length() const {
return _byte_index_to_unicode(size); return _byte_index_to_unicode(size);
} }
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);
return StrName(index);
}
Str StrName::escape() const {
return _r_interned[index-1].escape();
}
bool StrName::is_valid(int index) {
// check _r_interned[index-1] is valid
return index > 0 && index <= _r_interned.size();
}
StrName::StrName(): index(0) {}
StrName::StrName(uint16_t index): index(index) {}
StrName::StrName(const char* s): index(get(s).index) {}
StrName::StrName(const Str& s){
index = get(s.sv()).index;
}
std::string_view StrName::sv() const { return _r_interned[index-1].sv(); }
} // namespace pkpy } // namespace pkpy

View File

@ -1,2 +1 @@
SRC=$(find ../../src/ -name "*.cpp")
clang++ -std=c++17 -fno-rtti -O2 -stdlib=libc++ -Wfatal-errors -o libtest.so test.cpp -I../../include -fPIC -shared clang++ -std=c++17 -fno-rtti -O2 -stdlib=libc++ -Wfatal-errors -o libtest.so test.cpp -I../../include -fPIC -shared