diff --git a/README.md b/README.md index 0f3aea98..64c8c286 100644 --- a/README.md +++ b/README.md @@ -517,3 +517,59 @@ A: Enable `CompileConfig::debug_mode` to inspect logs and C code; use `GetLastRe ### Q: Is multithreading supported? A: Each `State` is currently thread-local; in multithreaded environments, create an independent `State` per thread. + +### GCC JIT filesystem cache + +GCC JIT caches compiled shared libraries by default. `StateConfig::gcc_config` +has `enable_cache` (default `true`) and `cache_directory`. An empty directory uses +`/fakelua/cache/uid-` on POSIX (typically +`/tmp/fakelua/cache/uid-1000` on Linux), or `/fakelua/cache` on Windows. +The system temp directory is selected by the same helper as transient JIT files, +including platform temp environment settings; macOS/Windows may use a per-user +temp directory rather than `/tmp`. An explicit `cache_directory` overrides this. +POSIX cache directories must belong to the current user and have mode 0700; +untrusted writable/foreign-owned parent directories are rejected. Cache failures +fall back to normal compilation. + +The key includes the complete generated C (including Lua debug path), optimization +mode, compiler, relevant compiler environment, and a source/build ABI fingerprint. +System header and compiler/backend/assembler/linker/support-library contents are +validated before reuse. Custom GCC include/library configurations, compiler search-path environment +overrides, wrapper scripts and coverage builds bypass caching. TCC remains in-memory; INTERP is unchanged. Compiler wrappers +with hidden inputs must disable caching or use a separate cache directory when +those inputs change. Do not modify a toolchain concurrently with compilation. + +`cache_max_entries` defaults to **256 complete entries per cache directory**. +Set it to `0` for unlimited entries, or set `enable_cache=false` to disable caching. +Lookup/publication trims excess entries in publication order (FIFO, not LRU): hits +do not refresh an entry's age. A directory-wide thread/process lock coordinates +restore, publication and eviction; actual C compilation stays outside that lock. +Use the same limit for clients sharing a directory; each operation applies its +client's configured limit. Deletion failures are best effort and can temporarily +leave the directory above the limit; a later operation tries again. This is an +entry-count limit, not a byte limit; partial/staging and unrelated files do not count. + +Striped key thread/process locks and atomic directory publication prevent partial hits; +corrupt/stale entries are rebuilt. Every load gets a private image, then binds State, +heap slots and interned string IDs once to generated C statics. This preserves +cross-module string identity, independent globals/closures and allocation lifetimes; +no runtime cache lookup is added. Hits skip C compilation/linking, not the Lua frontend; a cheap compiler-version +query still verifies the selected toolchain. + +Cache directories contain executable code: keep them private and on a local +filesystem supporting advisory locks and atomic rename. System temp cleanup is +OS/admin-policy dependent, not guaranteed. Removing entries or the cache subtree +causes cache misses/recompilation, not loss of loaded modules (their images are +private copies outside the cache). If a cleaner unlinks a live POSIX lock inode, +advisory exclusion alone no longer holds: detached locks are detected where possible, +writer-private staging, atomic publication and image validation keep cleanup races +best effort rather than a source of partial executable images. Redundant compilation +and temporary cap overshoot are possible during external cleanup. Lock metadata +is bounded to 256 key stripes plus one index lock; eviction never unlinks locks. +For deterministic manual cleanup, stop cache users before removing its directory. +Interrupted writers may leave writer-private `.tmp.*` directories; these do not count +toward the limit and can be removed with the cache when it is idle. Old-format +`.tmp` staging for a key is cleared on its next publication. See +[the release benchmark harness](tools/benchmark_jit_cache.cpp) for measurement commands. + +Cache synchronization does not make a State itself safe for concurrent use. diff --git a/README.zh.md b/README.zh.md index 71255f38..1bc4ad57 100644 --- a/README.zh.md +++ b/README.zh.md @@ -512,3 +512,42 @@ A: 启用 `CompileConfig::debug_mode` 查看日志和 C 代码;使用 `GetLast ### Q: 支持多线程吗? A: 每个 `State` 当前为线程本地对象,多线程环境中应为每个线程创建独立的 `State`。 + +### GCC JIT 文件系统缓存 + +GCC JIT 默认缓存编译后的动态库。`StateConfig::gcc_config.enable_cache` 可关闭; +`cache_directory` 为空时使用原有临时目录下的 `cache` 子目录:POSIX 为 +`<系统临时目录>/fakelua/cache/uid-`(Linux 通常如 `/tmp/fakelua/cache/uid-1000`), +Windows 为 `<系统临时目录>/fakelua/cache`。与临时 JIT 文件复用同一目录选择逻辑, +遵循平台临时目录环境设置;macOS/Windows 可能使用用户级临时目录而非 `/tmp`。 +显式设置 `cache_directory` 仍可覆盖默认值。POSIX 下目录必须属于当前用户且权限为 0700, +不可信的可写/其他用户所有的父目录会被拒绝。缓存不可用时自动回退正常编译。 + +缓存键覆盖完整生成 C(含 Lua 调试路径)、优化模式、编译器、相关编译环境和源码/构建 ABI +指纹;命中时校验系统头文件、编译后端、汇编器、链接器及编译支持库内容。 +自定义 GCC 头文件/库配置、编译搜索路径环境覆盖、包装脚本及覆盖率构建不使用缓存;TCC 仍在内存编译,INTERP 不变。 +有隐藏输入的编译器包装脚本需要关闭缓存,或在输入变化时换用新缓存目录。 +不要在编译期间同时修改工具链。 + +`cache_max_entries` 默认限制每个目录 **256 个完整缓存条目**;`0` 表示不限数量, +完全禁用仍用 `enable_cache=false`。查询和发布时按生成/发布顺序淘汰最老条目(FIFO, +不是 LRU,命中不刷新顺序)。目录级线程/进程锁协调读取、发布和淘汰,实际 C 编译不持有 +目录锁。共享目录的客户端应使用相同上限,每次操作按当前客户端配置执行;删除失败时尽力回收, +可能暂时超限,下次操作再尝试。这是条目数限制,不是字节上限,半成品、暂存目录和无关文件不计入。 + +分条带的键级线程/进程锁及原子发布保证正常并发不会读取半成品,损坏或过期条目自动重建。每次加载均使用独立动态库副本, +一次性绑定 State、堆槽和驻留字符串 ID 到生成 C 的静态变量,避免不同 State 的可变静态 +数据互串,保持跨模块字符串身份及生命周期;执行热路径不查询文件缓存。 +命中跳过 C 的实际编译/链接,但仍进行 Lua 前端处理及轻量的编译器版本校验。 + +缓存中是可执行代码,请使用私有本地目录(支持文件锁及原子重命名)。系统是否清理临时目录 +取决于操作系统/管理员策略,并不保证清理。条目或缓存子树被删除会变成未命中/重新编译, +不会影响已加载模块(实际加载的是缓存目录外的独立副本)。POSIX 活动锁文件若被外部清理器 +删除,不能单靠文件锁保证互斥;实现会尽量识别脱离路径的锁,并通过每个写入者独立暂存、原子 +发布和产物校验回退,避免执行半成品。外部清理期间可能重复编译或暂时超限。 +锁元数据最多为 256 个键条带锁及 1 个目录锁,淘汰永不删除锁文件。 +手动清理最好先停止缓存使用者。进程中断可能遗留 `.tmp.*` 独立暂存目录,不计入上限, +可在缓存闲置时一起清理;旧格式 `.tmp` 由该键下一次发布回收。 +可复现的 Release 测量见[基准代码](tools/benchmark_jit_cache.cpp)。 + +缓存的并发保护不改变 State 本身的线程安全约束。 diff --git a/cmake/JitCacheIdentity.cmake b/cmake/JitCacheIdentity.cmake new file mode 100644 index 00000000..6077f161 --- /dev/null +++ b/cmake/JitCacheIdentity.cmake @@ -0,0 +1,45 @@ +# A content/build fingerprint for the host/JIT ABI, generated at build time. +# Reconfigure after source edits, including dirty/uncommitted changes; a version +# number or git revision alone would silently reuse stale machine code. +get_filename_component(_fl_jit_root "${CMAKE_CURRENT_LIST_DIR}/.." REALPATH) +file(GLOB_RECURSE _fl_jit_inputs + "${_fl_jit_root}/src/*.hh" "${_fl_jit_root}/src/*.hpp" "${_fl_jit_root}/src/*.h" "${_fl_jit_root}/src/*.cpp" + "${_fl_jit_root}/src/*.y" "${_fl_jit_root}/src/*.l" + "${_fl_jit_root}/include/*.h" "${_fl_jit_root}/cmake/*.cmake") +list(APPEND _fl_jit_inputs "${_fl_jit_root}/CMakeLists.txt" "${_fl_jit_root}/src/CMakeLists.txt") +list(SORT _fl_jit_inputs) +set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${_fl_jit_inputs}) +set(_fl_jit_identity "${CMAKE_SYSTEM_NAME};${CMAKE_SYSTEM_PROCESSOR};${CMAKE_SIZEOF_VOID_P};${CMAKE_CXX_COMPILER_ID};${CMAKE_CXX_COMPILER_VERSION};${CMAKE_BUILD_TYPE}") +foreach(_input IN LISTS _fl_jit_inputs) + file(SHA256 "${_input}" _hash) + string(APPEND _fl_jit_identity ";${_input}:${_hash}") +endforeach() +foreach(_flag CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_MINSIZEREL + CMAKE_CXX_COMPILER_TARGET CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN CMAKE_CXX_STANDARD + CMAKE_SHARED_LINKER_FLAGS CMAKE_OSX_ARCHITECTURES CMAKE_OSX_SYSROOT + CMAKE_OSX_DEPLOYMENT_TARGET USE_ASAN USE_COV FAKELUA_NATIVE_OBJECT_DEBUG_HASH) + string(APPEND _fl_jit_identity ";${_flag}=${${_flag}}") +endforeach() +# Include custom configuration flags as well as the standard configurations. +get_cmake_property(_fl_jit_variables VARIABLES) +foreach(_flag IN LISTS _fl_jit_variables) + if(_flag MATCHES "^CMAKE_(CXX_FLAGS|SHARED_LINKER_FLAGS)(_|$)") + string(APPEND _fl_jit_identity ";${_flag}=${${_flag}}") + endif() +endforeach() +foreach(_property COMPILE_OPTIONS COMPILE_DEFINITIONS COMPILE_FEATURES CXX_STANDARD CXX_EXTENSIONS + POSITION_INDEPENDENT_CODE INCLUDE_DIRECTORIES LINK_OPTIONS LINK_LIBRARIES) + get_target_property(_value fakelua ${_property}) + string(APPEND _fl_jit_identity ";${_property}=${_value}") +endforeach() +file(SHA256 "${CMAKE_CXX_COMPILER}" _fl_host_compiler_hash) +string(SHA256 _fl_jit_hash "${_fl_jit_identity};${_fl_host_compiler_hash}") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/jit_build_identity.h.tmp" "#pragma once\n#define FAKELUA_JIT_BUILD_ID \"${_fl_jit_hash}\"\n") +configure_file("${CMAKE_CURRENT_BINARY_DIR}/jit_build_identity.h.tmp" "${CMAKE_CURRENT_BINARY_DIR}/jit_build_identity.h" COPYONLY) +target_include_directories(fakelua PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") +# CMAKE_BUILD_TYPE is empty for multi-config generators. A per-source definition +# records the actual configuration without a shared header that concurrent +# Debug/Release builds could overwrite. +set_property(SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/jit/gcc_cache.cpp" APPEND PROPERTY + COMPILE_DEFINITIONS "FAKELUA_JIT_BUILD_CONFIGURATION=\"$\"") diff --git a/cmd/main.cpp b/cmd/main.cpp index 3eca8934..0fe3fc44 100644 --- a/cmd/main.cpp +++ b/cmd/main.cpp @@ -67,6 +67,7 @@ constexpr const char *kFiniEntry = "main.fini"; std::atomic g_exit_requested{false}; std::atomic g_exit_code{0}; + bool ExitRequested() { return g_exit_requested.load(std::memory_order_acquire); } @@ -117,7 +118,7 @@ std::vector CollectLuaFiles(const std::string &path) { } return files; } - return { path }; + return {path}; } [[noreturn]] void ReportMissingEntry(const char *name) { @@ -199,7 +200,7 @@ int RunLifecycle(State *s, JITType jit) { return exit_code; } -} // namespace +}// namespace int main(int argc, char **argv) { gflags::SetUsageMessage("usage: ./flua [options]\n" diff --git a/include/fakelua.h b/include/fakelua.h index c033314b..b3cb7179 100644 --- a/include/fakelua.h +++ b/include/fakelua.h @@ -302,8 +302,14 @@ enum JITType { struct JitTypeSpan { JITType data[JIT_MAX]{}; int size = 0; - const JITType *begin() const { return data; } - const JITType *end() const { return data + size; } + + const JITType *begin() const { + return data; + } + + const JITType *end() const { + return data + size; + } }; inline const char *JitTypeName(JITType t) { @@ -400,6 +406,14 @@ struct StateGCCConfig { std::vector include_paths; std::vector library_paths; std::vector libraries; + // Best-effort persistent shared-library cache. Empty directory uses the + // system-temp/fakelua/cache (POSIX: /uid-), not a home cache. + // System cleanup is policy-dependent. Custom include/library configs bypass it. + bool enable_cache = true; + std::string cache_directory; + // Maximum complete entries per directory, evicted in publication order (FIFO). + // Zero means unlimited; enable_cache=false disables caching entirely. + size_t cache_max_entries = 256; }; struct StateConfig { @@ -968,9 +982,8 @@ void RegisterNativeVarFunction(State *s, const std::string &name, int arg_count, // 可变参数时,用上面的 5 参回调形式自己读参数(GetNativeIntArg / GetNativeStringArg)。 template void RegisterNativeFunction(State *s, const std::string &name, bool is_vararg, std::function func) { - static_assert(!std::is_function_v, - "RegisterNativeFunction: Ret is a function type, so the signature was passed as a template argument. " - "Write std::function instead - see the example above RegisterNativeFunction."); + static_assert(!std::is_function_v, "RegisterNativeFunction: Ret is a function type, so the signature was passed as a template argument. " + "Write std::function instead - see the example above RegisterNativeFunction."); constexpr int arg_count = static_cast(sizeof...(Args)); RegisterNativeFunction(s, name, arg_count, is_vararg, [func](State *state, CVar *args, int n) -> CVar { auto unpack_helper = [state, args, n](std::index_sequence) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc7d5783..f2f899e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -609,3 +609,5 @@ install(TARGETS fakelua RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) + +include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/JitCacheIdentity.cmake) diff --git a/src/compile/c_gen.cpp b/src/compile/c_gen.cpp index 844119f3..e4f18e39 100644 --- a/src/compile/c_gen.cpp +++ b/src/compile/c_gen.cpp @@ -1,5 +1,7 @@ #include "compile/c_gen.h" +#include "var/var_string.h" +#include #include #include "compile/c_runtime_header.h" @@ -10,6 +12,16 @@ namespace fakelua { +std::string CGen::StringId(const std::string_view &value) { + auto [it, inserted] = string_indices_.try_emplace(std::string(value), string_literals_.size()); + if (inserted) { + string_literals_.emplace_back(value); + SectionGuard sg(*this, Section::StringDecls); + Out() << "static int64_t __fakelua_cache_sid_" << it->second << ";\n"; + } + return "__fakelua_cache_sid_" + std::to_string(it->second); +} + bool CGen::IsFuncLocalName(const std::string &lua_name) const { if (!cur_func_info_) { return false; @@ -76,7 +88,7 @@ std::string CGen::LookupFuncCName(const std::string &lua_name) const { } void CGen::UniquifyFuncCNames() { - file_c_ident_used_.insert({"_S", "__fakelua_init_flag__", "fl_temp_heap", "fl_const_heap", "fl_lua_loc", "fl_lua_file", "kNil", "kTrue", "kFalse", kInitFunctionName}); + file_c_ident_used_.insert({"__fakelua_bind", "_S", "__fakelua_init_flag__", "fl_temp_heap", "fl_const_heap", "fl_lua_loc", "fl_lua_file", "kNil", "kTrue", "kFalse", kInitFunctionName}); FuncInfo *real_init = nullptr; for (auto it = all_funcs_.rbegin(); it != all_funcs_.rend(); ++it) { @@ -183,6 +195,7 @@ GenResult CGen::Generate(const ParseResult &pr, const InferResult &ir, const Ana LOG_DEBUG(s_, "engine", "start CGen::Generate {}", pr.file_name); file_name_ = pr.file_name; + code_file_name_ = pr.from_string ? "" : pr.file_name; ir_ = ir; ar_ = ar; @@ -272,10 +285,22 @@ GenResult CGen::Build(const ParseResult &pr, const CompileConfig &cfg) { if (cfg.record_c_code) { gr.recorded_c_code = GetSectionStr(Section::Globals) + GetSectionStr(Section::Decls) + GetSectionStr(Section::Impls); } - gr.c_code = GetSectionStr(Section::Headers) + GetSectionStr(Section::Globals) + GetSectionStr(Section::Decls) + GetSectionStr(Section::Impls); + GenerateBindings(); + gr.c_code = GetSectionStr(Section::Includes) + GetSectionStr(Section::StringDecls) + GetSectionStr(Section::Headers) + GetSectionStr(Section::Globals) + GetSectionStr(Section::Decls) + + GetSectionStr(Section::Impls) + GetSectionStr(Section::Bindings); return gr; } +void CGen::GenerateBindings() { + SectionGuard sg(*this, Section::Bindings); + Out() << "void *__fakelua_bind(void *s, void *temp, void *constant, const char *file, int64_t (*intern)(void *, const char *, size_t)) {\n" + << " _S = s; fl_temp_heap = temp; fl_const_heap = constant; fl_lua_file = file;\n"; + for (size_t i = 0; i < string_literals_.size(); ++i) { + Out() << " __fakelua_cache_sid_" << i << " = intern(s, \"" << EscapeCStringLiteral(string_literals_[i]) << "\", " << string_literals_[i].size() << ");\n"; + } + Out() << " return &fl_lua_loc;\n}\n"; +} + // EmitSpecAccessorBody —— 按 key kind(string / int / float / bool)发射 // spec get/set 访问器内层的 if-else / switch 条件逻辑。 // is_get: true 时生成 get 的 match action,false 时生成 set 的 match action。 @@ -283,31 +308,50 @@ void CGen::EmitSpecAccessorBody(const SpecTypeMetadata &meta, bool is_get) { // ---- string keys ---- if (meta.has_string_keys) { Out() << " if (LIKELY(k.type_ == VAR_STRINGID)) {\n"; - Out() << " switch (k.data_.i) {\n"; - int f_idx = 0; - for (const auto &f: meta.fields) { + + auto emit_id_match = [&](const TableFieldInfo &f, int index) { + Out() << " if (k.data_.i == " << StringId(f.key) << ") { "; + if (is_get) { + Out() << "*__finish = true; return s->" << f.c_field_name << "; }\n"; + } else { + Out() << "s->" << f.c_field_name << " = v; tbl->spec_vals[" << index << "] = v; tbl->spec_keys[" << index << "] = k; *__finish = true; return; }\n"; + } + }; + std::map> hash_cases; + int string_count = 0; + for (size_t i = 0; i < meta.fields.size(); ++i) { + const auto &f = meta.fields[i]; if (f.key_kind == TableKeyKind::kString) { - if (is_get) { - Out() << " case " << s_->GetConstString().Alloc(f.key) << ": *__finish = true; return s->" << f.c_field_name << ";\n"; - } else { - Out() << " case " << s_->GetConstString().Alloc(f.key) << ": s->" << f.c_field_name << " = v; tbl->spec_vals[" << f_idx << "] = v; tbl->spec_keys[" << f_idx - << "] = k; *__finish = true; return;\n"; - } + hash_cases[VarString::HashOf(f.key)].push_back(static_cast(i)); + ++string_count; } - f_idx++; } - Out() << " default: break;\n"; - Out() << " }\n"; + if (string_count <= 4) { + for (size_t i = 0; i < meta.fields.size(); ++i) + if (meta.fields[i].key_kind == TableKeyKind::kString) emit_id_match(meta.fields[i], static_cast(i)); + } else { + // Interned strings already have a cached hash. A constant hash + // switch keeps wide shapes O(1)/O(log N), rather than turning the + // old immediate-address switch into N runtime-static comparisons. + // Always guard by canonical identity, including hash collisions. + Out() << " switch (((VarString *)k.data_.i)->hash_) {\n"; + for (const auto &[hash, indices]: hash_cases) { + Out() << " case " << hash << "u:\n"; + for (int index: indices) emit_id_match(meta.fields[index], index); + Out() << " break;\n"; + } + Out() << " default: break;\n }\n"; + } Out() << " } else if (k.type_ == VAR_STRING) {\n"; Out() << " VarString *__vs = k.data_.s;\n"; - f_idx = 0; + int f_idx = 0; for (const auto &f: meta.fields) { if (f.key_kind == TableKeyKind::kString) { if (is_get) { - Out() << " if (__vs->size_ == " << f.key.size() << " && memcmp(__vs->data_, \"" << EscapeCStringLiteral(f.key) << "\", " << f.key.size() << ") == 0) { *__finish = true; return s->" - << f.c_field_name << "; }\n"; + Out() << " if (__vs->size_ == " << f.key.size() << " && memcmp(__vs->data_, \"" << EscapeCStringLiteral(f.key) << "\", " << f.key.size() + << ") == 0) { *__finish = true; return s->" << f.c_field_name << "; }\n"; } else { - const auto id = s_->GetConstString().Alloc(f.key); + const auto id = StringId(f.key); Out() << " if (__vs->size_ == " << f.key.size() << " && memcmp(__vs->data_, \"" << EscapeCStringLiteral(f.key) << "\", " << f.key.size() << ") == 0) { s->" << f.c_field_name << " = v; tbl->spec_vals[" << f_idx << "] = v; tbl->spec_keys[" << f_idx << "] = (CVar){.type_ = VAR_STRINGID, .data_.i = " << id << "}; *__finish = true; return; }\n"; } @@ -376,8 +420,8 @@ void CGen::EmitSpecAccessorBody(const SpecTypeMetadata &meta, bool is_get) { if (is_get) { Out() << " if ((double)__ival == " << FormatCDoubleLiteral(f.float_value) << ") { *__finish = true; return s->" << f.c_field_name << "; }\n"; } else { - Out() << " if ((double)__ival == " << FormatCDoubleLiteral(f.float_value) << ") { s->" << f.c_field_name << " = v; tbl->spec_vals[" << f_idx << "] = v; tbl->spec_keys[" << f_idx - << "] = k; *__finish = true; return; }\n"; + Out() << " if ((double)__ival == " << FormatCDoubleLiteral(f.float_value) << ") { s->" << f.c_field_name << " = v; tbl->spec_vals[" << f_idx << "] = v; tbl->spec_keys[" + << f_idx << "] = k; *__finish = true; return; }\n"; } } f_idx++; @@ -435,7 +479,7 @@ void CGen::EmitSpecTypeBoilerplate(const std::string &spec_type, const SpecTypeM } void CGen::GenerateHeader() { - SectionGuard sg(*this, Section::Headers); + SectionGuard sg(*this, Section::Includes); Out() << R"(// Generated by FakeLua CGen #include #include @@ -448,19 +492,18 @@ void CGen::GenerateHeader() { )"; - // 硬编码 state 指针到生成的 C 代码中。 - // 生命周期保证:生成的代码最终由 TCCState/GCC 产物承载,这些产物通过 TCCHandle/JITHandle - // 被 VmFunction 以 shared_ptr 持有,而 VmFunction 注册在本 State 的 Vm 中(见 - // src/jit/tcc_jit.cpp 与 src/jit/vm_function.h)。因此生成代码里的 _S 永远不会 - // 比其宿主 State 活得更久——State 析构即销毁 Vm,进而释放 handle 和代码页。 - // 同一份代码也不会被跨 State 复用(每次编译都会重新生成)。 - Out() << "static void * _S = (void *) " << s_ << ";\n"; + SectionGuard runtime_header(*this, Section::Headers); + // StringDecls is assembled between Includes and Headers: spec helpers below + // can use interned IDs even when those literals are discovered later. + // Each loaded image is private to one compilation/State. Bind once, never + // embed process addresses in persistent code or look them up on hot paths. + Out() << "static void * _S;\n"; Out() << "static bool __fakelua_init_flag__ = false;\n"; - Out() << "static void * fl_temp_heap = (void *) " << reinterpret_cast(&s_->GetTempHeapSlot()) << ";\n"; - Out() << "static void * fl_const_heap = (void *) " << reinterpret_cast(&s_->GetConstHeapSlot()) << ";\n"; + Out() << "static void * fl_temp_heap;\n"; + Out() << "static void * fl_const_heap;\n"; // C 运行时类型定义、宏 and 函数(从 c_runtime_header.h 提取,便于独立维护) - Out() << "static const char * const fl_lua_file = \"" << EscapeCStringLiteral(file_name_) << "\";\n"; + Out() << "static const char *fl_lua_file;\n"; // Attribute the pasted Fl* runtime to the checked-in view header so USE_COV // gcov → lcov → Codecov can display hits on a real repo path (not @@ -475,9 +518,10 @@ void CGen::GenerateHeader() { // 前置发射所有 spec 类型的 typedef + 特化 get/set 函数(读取 TypeInferencer 预计算的 // ir.spec_type_metadata)。CGen 不再在 CompileTableconstructor 里懒发射这些样板代码。 - for (const auto &[spec_type, meta]: ir().spec_type_metadata) { - EmitSpecTypeBoilerplate(spec_type, meta); - } + std::vector spec_types; + for (const auto &[name, meta]: ir().spec_type_metadata) spec_types.push_back(name); + std::sort(spec_types.begin(), spec_types.end()); + for (const auto &name: spec_types) EmitSpecTypeBoilerplate(name, ir().spec_type_metadata.at(name)); } void CGen::GenerateGlobal(const SyntaxTreeInterfacePtr &chunk) { @@ -537,7 +581,8 @@ void CGen::GenerateGlobal(const SyntaxTreeInterfacePtr &chunk) { // 文件级绑定:推断期禁整槽再赋;表在 init 里经 SET_TABLE 创建即带 CONST_FLAG。 // 记入 global_const_table_vars_,供 CompileVar 识别已声明的文件级 CVar。 const std::string cvar_init = exp ? CompileExp(exp) : "(CVar){.type_ = VAR_NIL}"; - Out() << "static CVar " << cname << " = " << cvar_init << ";\n"; + Out() << "static CVar " << cname << ";\n"; + global_initializers_ << " " << cname << " = " << cvar_init << ";\n"; global_const_table_vars_.insert(cname); } } @@ -766,7 +811,7 @@ void CGen::GenerateImpl(const SyntaxTreeInterfacePtr &chunk, GenResult &gr) { } void CGen::GenerateEntryDispatcher(const std::string &c_name, const std::string &lua_name, const std::vector &func_params, const std::vector &math_param_indices, - const std::string &dyn_name) { + const std::string &dyn_name) { const int k = static_cast(math_param_indices.size()); const int num_specs = 1 << k; @@ -839,7 +884,7 @@ void CGen::EmitLuaLine(const SyntaxTreeInterface *node, bool emit_loc_set) { if (line <= 0) { return; } - Out() << "#line " << line << " \"" << EscapeCStringLiteral(file_name_) << "\"\n"; + Out() << "#line " << line << " \"" << EscapeCStringLiteral(code_file_name_) << "\"\n"; if (emit_loc_set) { Out() << GenTab() << "FL_LOC_SET(fl_lua_file, " << line << ");\n"; } @@ -1072,6 +1117,7 @@ void CGen::CompileFuncBody(const std::string &func_name, const SyntaxTreeInterfa out << " FakeluaRegisterLuaLoc(_S, FAKELUA_JIT_TYPE, &fl_lua_loc);\n"; } out << func_temp_decls_.str(); + if (func_name == kInitFunctionName) out << global_initializers_.str(); out << body_ss.str(); if (func_name == kInitFunctionName) { // init 内 SET_TABLE 已给整棵表树打 CONST_FLAG;此处只标记 init 结束, @@ -1163,8 +1209,7 @@ std::string CGen::TryCompileNativeExpr(const SyntaxTreeInterfacePtr &exp) { static const std::unordered_map kCmpOpMap = {{BinOpKind::kLess, "<"}, {BinOpKind::kLessEqual, "<="}, {BinOpKind::kMore, ">"}, {BinOpKind::kMoreEqual, ">="}, {BinOpKind::kEqual, "=="}, {BinOpKind::kNotEqual, "!="}}; -static std::string FormatNativeNumericComparison(const std::string &left, const std::string &right, InferredType left_type, InferredType right_type, - BinOpKind op_kind) { +static std::string FormatNativeNumericComparison(const std::string &left, const std::string &right, InferredType left_type, InferredType right_type, BinOpKind op_kind) { const auto cmp_it = kCmpOpMap.find(op_kind); DEBUG_ASSERT(cmp_it != kCmpOpMap.end()); if (left_type == right_type) { @@ -1182,8 +1227,8 @@ static std::string FormatNativeNumericComparison(const std::string &left, const // FlCompareIntDouble compares integer against floating. Reverse the // relation when the source operands are float/int. NaN makes every // ordered comparison false, including <= and >=. - const auto relation = left_is_int ? std::format("FlCompareIntDouble(({}), ({})) {} 0", integer, floating, cmp_it->second) - : std::format("0 {} FlCompareIntDouble(({}), ({}))", cmp_it->second, integer, floating); + const auto relation = + left_is_int ? std::format("FlCompareIntDouble(({}), ({})) {} 0", integer, floating, cmp_it->second) : std::format("0 {} FlCompareIntDouble(({}), ({}))", cmp_it->second, integer, floating); return std::format("(!isnan(({})) && ({}))", floating, relation); } @@ -1788,12 +1833,8 @@ std::string CGen::CompileCondBoolExpr(const SyntaxTreeInterfacePtr &exp, const s if (op) { const auto op_kind = op->GetOpKind(); static const std::unordered_map kCmpFunctions = { - {BinOpKind::kLess, "FlCmpLt"}, - {BinOpKind::kLessEqual, "FlCmpLe"}, - {BinOpKind::kMore, "FlCmpGt"}, - {BinOpKind::kMoreEqual, "FlCmpGe"}, - {BinOpKind::kEqual, "FlCmpEq"}, - {BinOpKind::kNotEqual, "FlCmpNe"}, + {BinOpKind::kLess, "FlCmpLt"}, {BinOpKind::kLessEqual, "FlCmpLe"}, {BinOpKind::kMore, "FlCmpGt"}, + {BinOpKind::kMoreEqual, "FlCmpGe"}, {BinOpKind::kEqual, "FlCmpEq"}, {BinOpKind::kNotEqual, "FlCmpNe"}, }; if (auto it = kCmpFunctions.find(op_kind); it != kCmpFunctions.end()) { const auto left_expr = CompileExp(e->Left()); @@ -2088,11 +2129,11 @@ void CGen::CompileTypedNumericForLoop(const std::shared_ptr & if (loop_type == T_INT) { Out() << GenTab() << stop_var << " = 0;\n"; if (step_int_val > 0) { - Out() << GenTab() << "for (; " << ctrl_var << " <= " << end_var << " && !" << stop_var << "; " - << ctrl_var << " >= INT64_MAX - " << (step_int_val - 1) << "LL ? (" << stop_var << " = 1) : (" << ctrl_var << " += " << step_int_val << "LL)) {\n"; + Out() << GenTab() << "for (; " << ctrl_var << " <= " << end_var << " && !" << stop_var << "; " << ctrl_var << " >= INT64_MAX - " << (step_int_val - 1) << "LL ? (" << stop_var + << " = 1) : (" << ctrl_var << " += " << step_int_val << "LL)) {\n"; } else { - Out() << GenTab() << "for (; " << ctrl_var << " >= " << end_var << " && !" << stop_var << "; " - << ctrl_var << " <= INT64_MIN - (" << (step_int_val + 1) << "LL) ? (" << stop_var << " = 1) : (" << ctrl_var << " += " << step_int_val << "LL)) {\n"; + Out() << GenTab() << "for (; " << ctrl_var << " >= " << end_var << " && !" << stop_var << "; " << ctrl_var << " <= INT64_MIN - (" << (step_int_val + 1) << "LL) ? (" << stop_var + << " = 1) : (" << ctrl_var << " += " << step_int_val << "LL)) {\n"; } } else if (step_double_val > 0.0) { const std::string incr = (step_double_val == 1.0) ? (ctrl_var + "++") : (ctrl_var + " += " + std::to_string(step_double_val)); @@ -2537,7 +2578,7 @@ std::string CGen::CompileExp(const SyntaxTreeInterfacePtr &exp, bool preserve_mu return std::format("(CVar){{.type_ = VAR_FLOAT, .data_.f = {}}}", ToFloat(value)); } case ExpKind::kString: - return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", s_->GetConstString().Alloc(value)); + return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", StringId(value)); case ExpKind::kPrefixExp: return CompilePrefixexp(e->Right(), preserve_multi); case ExpKind::kTableConstructor: @@ -2549,7 +2590,8 @@ std::string CGen::CompileExp(const SyntaxTreeInterfacePtr &exp, bool preserve_mu return std::format("FlMakeClosure0(_S, (void*){}, {}, {})", func->unique_c_name, func->params.size(), func->is_vararg ? "true" : "false"); } if (func->captured_vars.size() == 1) { - return std::format("FlMakeClosure1(_S, (void*){}, {}, {}, {})", func->unique_c_name, func->params.size(), func->is_vararg ? "true" : "false", CompileUpvaluePointer(func->captured_vars[0])); + return std::format("FlMakeClosure1(_S, (void*){}, {}, {}, {})", func->unique_c_name, func->params.size(), func->is_vararg ? "true" : "false", + CompileUpvaluePointer(func->captured_vars[0])); } std::string closure_expr = std::format("FlMakeClosure(_S, (void*){}, {}, {}, {}", func->unique_c_name, func->captured_vars.size(), func->params.size(), func->is_vararg ? "true" : "false"); for (VarDef *up: func->captured_vars) { @@ -2642,7 +2684,7 @@ std::string CGen::CompileTableconstructor(const SyntaxTreeInterfacePtr &tc) { if (fp->GetFieldKind() == FieldKind::kObject) { const auto key_name = fp->Name(); - const auto id = s_->GetConstString().Alloc(key_name); + const auto id = StringId(key_name); Out() << GenTab() << std::format("FlSetTableStrId({}, {}, {});\n", var_name, id, value_str); } else { if (fp->Key() == nullptr) { @@ -2655,7 +2697,7 @@ std::string CGen::CompileTableconstructor(const SyntaxTreeInterfacePtr &tc) { if (ClassifyLiteralKey(fp->Key(), key_info)) { switch (key_info.kind) { case TableKeyKind::kString: { - const auto id = s_->GetConstString().Alloc(key_info.repr); + const auto id = StringId(key_info.repr); Out() << GenTab() << std::format("FlSetTableStrId({}, {}, {});\n", var_name, id, value_str); break; } @@ -2721,13 +2763,13 @@ std::string CGen::CompileTableconstructor(const SyntaxTreeInterfacePtr &tc) { if (fkind == FieldKind::kObject) { const auto name = field_ptr->Name(); - const auto id = s_->GetConstString().Alloc(name); + const auto id = StringId(name); Out() << GenTab() << std::format("FlSetTableStrId({}, {}, {});\n", var_name, id, value_str); } else { DEBUG_ASSERT(fkind == FieldKind::kArray); if (const auto key = field_ptr->Key()) { if (const auto key_exp = std::dynamic_pointer_cast(key); key_exp && key_exp->GetExpKind() == ExpKind::kString) { - const auto id = s_->GetConstString().Alloc(key_exp->ExpValue()); + const auto id = StringId(key_exp->ExpValue()); Out() << GenTab() << std::format("FlSetTableStrId({}, {}, {});\n", var_name, id, value_str); } else { if (const auto key_type = GetType(key); key_type == T_INT) { @@ -3149,7 +3191,7 @@ std::string CGen::CompileVar(const SyntaxTreeInterfacePtr &v) { // 拦截 _VERSION 全局常量(编译期替换为 "Fakelua x.y.z") if (name == "_VERSION") { std::string ver = std::string("Fakelua ") + FAKELUA_VERSION_STRING; - return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", s_->GetConstString().Alloc(ver)); + return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", StringId(ver)); } // 未声明/未定义的简单变量:Lua 语义中未定义变量求值为 nil。 @@ -3178,7 +3220,7 @@ std::string CGen::CompileVar(const SyntaxTreeInterfacePtr &v) { } switch (info.kind) { case TableKeyKind::kString: { - const auto id = s_->GetConstString().Alloc(info.repr); + const auto id = StringId(info.repr); return std::format("FlGetTableStrId({}, {})", pe_ret, id); } case TableKeyKind::kInt: @@ -3236,7 +3278,7 @@ std::string CGen::CompileVar(const SyntaxTreeInterfacePtr &v) { p.push_back(']'); return p; }(); - return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", s_->GetConstString().Alloc(kCharPattern)); + return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", StringId(kCharPattern)); } } // 拦截 utf8 静态库常量访问 (utf8.charpattern) @@ -3262,8 +3304,7 @@ std::string CGen::CompileVar(const SyntaxTreeInterfacePtr &v) { p.push_back('*'); return p; }(); - return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", - s_->GetConstString().Alloc(kUtf8Pattern)); + return std::format("(CVar){{.type_ = VAR_STRINGID, .data_.i = {}}}", StringId(kUtf8Pattern)); } } } @@ -3279,7 +3320,7 @@ std::string CGen::CompileVar(const SyntaxTreeInterfacePtr &v) { } // String constant key fast path: use FlGetTableStrId directly. - const auto id = s_->GetConstString().Alloc(name); + const auto id = StringId(name); return std::format("FlGetTableStrId({}, {})", pe_ret, id); } } @@ -3668,9 +3709,8 @@ std::string CGen::TryCompileBuiltinMathCall(const std::shared_ptr(explist_arg); const auto &raw_args = explist_arg_ptr->Exps(); - static const std::unordered_set math_builtins = {"abs", "floor", "ceil", "max", "min", "sqrt", "sin", "cos", "tan", "pow", "deg", "rad", "random", - "randomseed", "modf", "frexp", "exp", "log", "log10", "asin", "acos", "atan", "sinh", "cosh", "tanh", - "fmod", "ldexp", "type", "tointeger", "ult"}; + static const std::unordered_set math_builtins = {"abs", "floor", "ceil", "max", "min", "sqrt", "sin", "cos", "tan", "pow", "deg", "rad", "random", "randomseed", "modf", + "frexp", "exp", "log", "log10", "asin", "acos", "atan", "sinh", "cosh", "tanh", "fmod", "ldexp", "type", "tointeger", "ult"}; if (!math_builtins.contains(method_name)) { return {}; @@ -3739,8 +3779,8 @@ std::string CGen::TryCompileBuiltinMathCall(const std::shared_ptrLoc().begin.line; std::string func_name = (cur_func_info_ && !cur_func_info_->name.empty()) ? cur_func_info_->name : "global"; @@ -4698,7 +4737,7 @@ std::string CGen::TryCompileBuiltinLogCall(const std::shared_ptrGetConstString().Alloc(info.repr); + const auto id = StringId(info.repr); Out() << GenTab() << std::format("{}.data_.t->spec_keys[{}] = (CVar){{.type_ = VAR_STRINGID, .data_.i = {}}};\n", tbl_str, index, id); break; } @@ -4841,7 +4880,7 @@ std::string CGen::TryCompileSetTableCall(const std::shared_ptrGetConstString().Alloc(info.repr); + const auto id = StringId(info.repr); Out() << GenTab() << std::format("FlSetTableStrId({}, {}, {});\n", tbl_str, id, val_str); break; } @@ -5080,7 +5119,7 @@ std::string CGen::BuildMethodCall(const std::shared_ptr // 避免 FlGetTableStrId 对非表值(如 string)触发 "attempt to index a non-table value"。 return forwarded; } else { - const auto id = s_->GetConstString().Alloc(method_name); + const auto id = StringId(method_name); callee_expr = std::format("FlGetTableStrId({}, {})", obj_tmp, id); } @@ -5463,7 +5502,8 @@ void CGen::CompileStmtLocalFunction(const SyntaxTreeInterfacePtr &stmt) { if (func->captured_vars.empty()) { closure_expr = std::format("FlMakeClosure0(_S, (void*){}, {}, {})", func->unique_c_name, func->params.size(), func->is_vararg ? "true" : "false"); } else if (func->captured_vars.size() == 1) { - closure_expr = std::format("FlMakeClosure1(_S, (void*){}, {}, {}, {})", func->unique_c_name, func->params.size(), func->is_vararg ? "true" : "false", CompileUpvaluePointer(func->captured_vars[0])); + closure_expr = + std::format("FlMakeClosure1(_S, (void*){}, {}, {}, {})", func->unique_c_name, func->params.size(), func->is_vararg ? "true" : "false", CompileUpvaluePointer(func->captured_vars[0])); } else { closure_expr = std::format("FlMakeClosure(_S, (void*){}, {}, {}, {}", func->unique_c_name, func->captured_vars.size(), func->params.size(), func->is_vararg ? "true" : "false"); for (VarDef *up: func->captured_vars) { diff --git a/src/compile/c_gen.h b/src/compile/c_gen.h index 4b0333c7..2f37d272 100644 --- a/src/compile/c_gen.h +++ b/src/compile/c_gen.h @@ -26,7 +26,7 @@ class CGen { private: // 当前写入的输出 section - enum class Section { Headers, Globals, Decls, Impls, Body, Count }; + enum class Section { Includes, StringDecls, Headers, Globals, Decls, Impls, Bindings, Body, Count }; // RAII guard:临时切换 section,析构时自动恢复,天然异常安全 struct SectionGuard { @@ -58,6 +58,8 @@ class CGen { void GenerateDecls(const SyntaxTreeInterfacePtr &chunk, GenResult &gr); // 生成所有函数具体的 C 语言代码实现 void GenerateImpl(const SyntaxTreeInterfacePtr &chunk, GenResult &gr); + // 加载时一次性绑定 State、堆槽及字符串驻留 ID。 + void GenerateBindings(); // 根据 AST 节点生成符合规范的 C 函数名称 std::string CompileFuncName(const SyntaxTreeInterfacePtr &ptr); @@ -286,6 +288,11 @@ class CGen { } private: + std::string StringId(const std::string_view &value); + std::vector string_literals_; + std::unordered_map string_indices_; + std::ostringstream global_initializers_; + std::string code_file_name_; State *s_; // 全局虚拟机状态 State 引用 std::string file_name_; // 当前编译的源码文件名称 std::optional> ir_; // 全局类型推断结果快照引用 @@ -305,14 +312,14 @@ class CGen { const struct SpecFuncContext *cur_spec_ctx_ = nullptr;// 当前特化版本的上下文(func_name/bitmask/snapshot) - std::stringstream func_temp_decls_; // 用于临时存放函数体内部临时 C 变量声明的代码流 - int cur_tab_ = 0; // 当前 C 代码生成器所处的缩进级别深度 - int repeat_depth_ = 0; // repeat-until 循环嵌套深度(0 表示不在 repeat 内) - int repeat_label_counter_ = 0; // repeat 循环标签计数器,保证每个 flua_until_ 标签唯一 - std::vector repeat_label_stack_;// 当前活跃的 repeat 标签 id 栈,栈顶即最近外层 repeat - int for_cont_id_ = 0; // 动态 numeric for 的 continue 标签计数器 - std::vector for_cont_stack_; // 动态 numeric for 的 continue 标签栈(while(1)+后置步进) - int label_uniq_ = 0; // Lua ::label:: 生成的 C 标签后缀,保证兄弟块同名不冲突 + std::stringstream func_temp_decls_; // 用于临时存放函数体内部临时 C 变量声明的代码流 + int cur_tab_ = 0; // 当前 C 代码生成器所处的缩进级别深度 + int repeat_depth_ = 0; // repeat-until 循环嵌套深度(0 表示不在 repeat 内) + int repeat_label_counter_ = 0; // repeat 循环标签计数器,保证每个 flua_until_ 标签唯一 + std::vector repeat_label_stack_; // 当前活跃的 repeat 标签 id 栈,栈顶即最近外层 repeat + int for_cont_id_ = 0; // 动态 numeric for 的 continue 标签计数器 + std::vector for_cont_stack_; // 动态 numeric for 的 continue 标签栈(while(1)+后置步进) + int label_uniq_ = 0; // Lua ::label:: 生成的 C 标签后缀,保证兄弟块同名不冲突 std::vector> label_c_stack_;// Lua 名 → 当前块唯一 C 标签 private: @@ -361,9 +368,9 @@ class CGen { std::unordered_map, VarDef *, PairHash> stmt_var_to_def_; std::unordered_map var_to_def_map_; - FuncInfo *cur_func_info_ = nullptr;// The function currently being compiled + FuncInfo *cur_func_info_ = nullptr; // The function currently being compiled const SyntaxTreeInterface *cur_stmt_node_ = nullptr;// 当前正在编译的语句节点,用于在调用返回后重新锚定 Lua 源码位置 - std::string cur_package_name_; // Current package name declared in chunk + std::string cur_package_name_; // Current package name declared in chunk // 文件级非数值 CVar 全局名:供 CompileVar 识别已声明绑定 std::unordered_set global_const_table_vars_; diff --git a/src/compile/c_runtime_cov.h b/src/compile/c_runtime_cov.h index 82756f3f..5464cc8d 100644 --- a/src/compile/c_runtime_cov.h +++ b/src/compile/c_runtime_cov.h @@ -20,6 +20,7 @@ typedef struct VarClosure VarClosure; typedef struct CVar { int type_; int flag_; + union { bool b; int64_t i; @@ -87,7 +88,7 @@ struct VarTable { typedef struct State State; -#define LIKELY(x) __builtin_expect(!!(x), 1) +#define LIKELY(x) __builtin_expect(!!(x), 1) #define UNLIKELY(x) __builtin_expect(!!(x), 0) #define STR_SIZE(s) ((s)->size_) @@ -110,23 +111,29 @@ enum { // init 期间 NEWTABLE/SET_TABLE 创建的表直接打 CONST_FLAG(整棵树,含嵌套); // SETTABLE 仅在 init 结束后才因 CONST_FLAG 抛错,以便构造器写字段。 #define CONST_FLAG 0x1 -#define FL_CHECK_CONST_TABLE(t) do { \ - if (UNLIKELY(((t).flag_ & CONST_FLAG) && __fakelua_init_flag__)) { \ - FakeluaThrowError(_S, "attempt to modify a const table"); \ - } \ -} while(0) +#define FL_CHECK_CONST_TABLE(t) \ + do { \ + if (UNLIKELY(((t).flag_ & CONST_FLAG) && __fakelua_init_flag__)) { \ + FakeluaThrowError(_S, "attempt to modify a const table"); \ + } \ + } while (0) /* 探测整数键 k 是否存在且值非 nil。查找逻辑与 FlGetTableInt 一致,但直接作用于 VarTable*,供序列长度计算与缓存维护复用。必须查 spec:特化表的 1..n 只在 spec 里,不在 quick/nodes。以前从 spec_count 起扫,等于把字段个数当成 #。 */ static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { if (t->spec_get) { - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; bool __finish = false; CVar __r = t->spec_get(t, key_cvar, &__finish); if (__finish) return __r.type_ != VAR_NIL; } - if ((uint64_t)k - 1 < (uint64_t)t->arr_size_) { return t->arr_[k - 1].type_ != VAR_NIL; } + if ((uint64_t) k - 1 < (uint64_t) t->arr_size_) { + return t->arr_[k - 1].type_ != VAR_NIL; + } if (t->spec_count > 0 && t->spec_keys && t->spec_vals) { uint32_t i; for (i = 0; i < t->spec_count; ++i) { @@ -144,7 +151,7 @@ static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { } return false; } else { - uint32_t h = (uint32_t)(k ^ (k >> 32)); + uint32_t h = (uint32_t) (k ^ (k >> 32)); uint32_t mask = t->bucket_count_ - 1; TableNode *curr = &t->nodes_[h & mask]; while (1) { @@ -159,7 +166,9 @@ static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { /* 从 base 起向上数连续存在的整数键,返回前缀长度。*/ static inline int64_t FlSeqScanFrom(VarTable *t, int64_t base) { - while (base < INT64_MAX && FlTableHasIntKey(t, base + 1)) { base++; } + while (base < INT64_MAX && FlTableHasIntKey(t, base + 1)) { + base++; + } return base; } @@ -187,18 +196,26 @@ static inline int64_t FlGetTableSeqLen(VarTable *t) { 本函数对同一次写入重复调用是幂等的。 */ static inline void FlSeqNoteIntSet(VarTable *t, int64_t k, bool is_nil) { if (!t || t->seq_len_valid_ == 0) return; - if (UNLIKELY(!FlSeqCacheable(t))) { t->seq_len_valid_ = 0; return; } + if (UNLIKELY(!FlSeqCacheable(t))) { + t->seq_len_valid_ = 0; + return; + } if (is_nil) { /* t[k] 变为 nil,前缀在 k-1 处截断;1..k-1 都还在,故新长度恰为 k-1。*/ - if (k >= 1 && k <= t->seq_len_) { t->seq_len_ = k - 1; } + if (k >= 1 && k <= t->seq_len_) { + t->seq_len_ = k - 1; + } return; } /* 只有正好补上 t[len+1] 才会延长前缀,随后继续吸收先前写入的更高键。*/ - if (t->seq_len_ < INT64_MAX && k == t->seq_len_ + 1) { t->seq_len_ = FlSeqScanFrom(t, k); } + if (t->seq_len_ < INT64_MAX && k == t->seq_len_ + 1) { + t->seq_len_ = FlSeqScanFrom(t, k); + } } /* NativeObject Wrap 惰性快照:pairs/GET_TABLE_ENTRY 读 spec_count 前调用。非 Native 表 no-op。 */ extern void FlEnsureNativeSpecSnapshot(VarTable *t); + static inline void FlEnsureNativeSpecSnapshotInline(VarTable *t) { if (UNLIKELY(t != NULL && t->spec != NULL)) { FlEnsureNativeSpecSnapshot(t); @@ -217,23 +234,23 @@ typedef struct FlHeapSlot { char *end; } FlHeapSlot; -extern void* FakeluaAlloc(State *s, size_t size, bool is_const); +extern void *FakeluaAlloc(State *s, size_t size, bool is_const); -static inline void* FlFastAlloc(State *state, size_t size, bool is_const) { - FlHeapSlot *hs = (is_const || !__fakelua_init_flag__) ? (FlHeapSlot *)fl_const_heap : (FlHeapSlot *)fl_temp_heap; +static inline void *FlFastAlloc(State *state, size_t size, bool is_const) { + FlHeapSlot *hs = (is_const || !__fakelua_init_flag__) ? (FlHeapSlot *) fl_const_heap : (FlHeapSlot *) fl_temp_heap; if (LIKELY(hs != NULL)) { - size_t aligned_size = (size + 15) & ~(size_t)15; + size_t aligned_size = (size + 15) & ~(size_t) 15; char *c = hs->curr; if (LIKELY(c != NULL && aligned_size >= size && c + aligned_size > c && c + aligned_size <= hs->end)) { hs->curr = c + aligned_size; - return (void *)c; + return (void *) c; } } return FakeluaAlloc(state, size, is_const); } static inline CVar FlAllocMulti(State *state, uint32_t count) { - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); m->count = count; CVar res; res.type_ = VAR_MULTI; @@ -245,13 +262,13 @@ static inline CVar FlAllocMulti(State *state, uint32_t count) { // 设置 multi-value 的元素,避免链接 inter::SetMultiCVarElement(Windows TCC 找不到符号) static inline void FlSetMultiCVarElement(CVar multi, int idx, CVar val) { VarMulti *m = multi.data_.m; - if (idx >= 0 && idx < (int)m->count) { + if (idx >= 0 && idx < (int) m->count) { m->vars[idx] = val; } } static inline CVar FlMakeMulti(State *state, uint32_t count, ...) { - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); m->count = count; va_list args_list; va_start(args_list, count); @@ -276,12 +293,12 @@ static inline CVar FlCombineMulti(State *state, uint32_t prefix_count, const CVa } uint32_t total_count = prefix_count + last_count; - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + total_count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + total_count * sizeof(CVar), !__fakelua_init_flag__); m->count = total_count; for (uint32_t i = 0; i < prefix_count; ++i) { if (prefix_vars[i].type_ == VAR_MULTI) { - m->vars[i] = prefix_vars[i].data_.m->count > 0 ? prefix_vars[i].data_.m->vars[0] : (CVar){VAR_NIL}; + m->vars[i] = prefix_vars[i].data_.m->count > 0 ? prefix_vars[i].data_.m->vars[0] : (CVar) {VAR_NIL}; } else { m->vars[i] = prefix_vars[i]; } @@ -297,25 +314,24 @@ static inline CVar FlCombineMulti(State *state, uint32_t prefix_count, const CVa return res; } - static inline CVar FlUnboxMulti(CVar v, uint32_t idx) { if (LIKELY(v.type_ != VAR_MULTI)) { - return (idx == 0) ? v : (CVar){VAR_NIL}; + return (idx == 0) ? v : (CVar) {VAR_NIL}; } VarMulti *m = v.data_.m; - return (idx < m->count) ? m->vars[idx] : (CVar){VAR_NIL}; + return (idx < m->count) ? m->vars[idx] : (CVar) {VAR_NIL}; } static inline CVar FlSliceMulti(State *state, CVar v, uint32_t start_idx) { if (v.type_ != VAR_MULTI) { - return (start_idx == 0) ? v : (CVar){VAR_NIL}; + return (start_idx == 0) ? v : (CVar) {VAR_NIL}; } VarMulti *m = v.data_.m; if (start_idx >= m->count) { - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } uint32_t count = m->count - start_idx; - VarMulti *nm = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *nm = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); nm->count = count; for (uint32_t i = 0; i < count; ++i) { nm->vars[i] = m->vars[start_idx + i]; @@ -327,14 +343,27 @@ static inline CVar FlSliceMulti(State *state, CVar v, uint32_t start_idx) { return res; } -#define SET_NIL(v) do { (v).type_ = VAR_NIL; } while(0) -#define SET_BOOL(v, val) do { (v).type_ = VAR_BOOL; (v).data_.i = 0; (v).data_.b = (val); } while(0) -#define SET_INT(v, val) do { (v).type_ = VAR_INT; (v).data_.i = (val); } while(0) -#define SET_FLOAT(v, val) do { \ - double __f = (val); \ - (v).type_ = VAR_FLOAT; \ - (v).data_.f = __f; \ -} while(0) +#define SET_NIL(v) \ + do { \ + (v).type_ = VAR_NIL; \ + } while (0) +#define SET_BOOL(v, val) \ + do { \ + (v).type_ = VAR_BOOL; \ + (v).data_.i = 0; \ + (v).data_.b = (val); \ + } while (0) +#define SET_INT(v, val) \ + do { \ + (v).type_ = VAR_INT; \ + (v).data_.i = (val); \ + } while (0) +#define SET_FLOAT(v, val) \ + do { \ + double __f = (val); \ + (v).type_ = VAR_FLOAT; \ + (v).data_.f = __f; \ + } while (0) /* 2^63:精确 double,也是第一个无法放入 int64 的整数。 * (double)INT64_MAX 会向上舍入成这个值,因此不能用 <= (double)INT64_MAX。 */ @@ -344,27 +373,28 @@ static inline int FlDoubleFitsInt64(double d, int64_t *out) { double ip; if (!isfinite(d)) return 0; if (modf(d, &ip) != 0.0) return 0; - if (ip < (double)INT64_MIN || ip >= FL_INT64_FLOAT_EXCL) return 0; - *out = (int64_t)ip; + if (ip < (double) INT64_MIN || ip >= FL_INT64_FLOAT_EXCL) return 0; + *out = (int64_t) ip; return 1; } -#define NORMALIZE_TABLE_KEY(key) ({ \ - CVar __k = (key); \ - if (UNLIKELY(__k.type_ == VAR_FLOAT)) { \ - int64_t __i; \ - if (FlDoubleFitsInt64(__k.data_.f, &__i)) { \ - __k.type_ = VAR_INT; \ - __k.data_.i = __i; \ - } \ - } \ - __k; \ -}) +#define NORMALIZE_TABLE_KEY(key) \ + ({ \ + CVar __k = (key); \ + if (UNLIKELY(__k.type_ == VAR_FLOAT)) { \ + int64_t __i; \ + if (FlDoubleFitsInt64(__k.data_.f, &__i)) { \ + __k.type_ = VAR_INT; \ + __k.data_.i = __i; \ + } \ + } \ + __k; \ + }) #ifdef __cplusplus extern "C" { #endif -extern void* FakeluaAlloc(State *s, size_t size, bool is_const); +extern void *FakeluaAlloc(State *s, size_t size, bool is_const); extern void FakeluaThrowError(State *s, const char *msg); extern CVar FakeluaCallByName(State *s, int jit_type, const char *name, int arg_num, ...); extern CVar FakeluaAllocMultiCVar(State *s, int count); @@ -382,11 +412,12 @@ extern void FakeluaRegisterLuaLoc(State *s, int jit_type, FlLuaLoc *loc); // 记录当前执行的 Lua 语句位置(文件级全局变量 fl_lua_loc 在 __fakelua_init 时把地址传给宿主 State)。 // C++ 侧的报错拿不到 Lua 栈,直接通过注册的变量地址读取;#line 则负责把编译告警映射回 Lua 源码。 -static FlLuaLoc fl_lua_loc = { 0, 0 }; -#define FL_LOC_SET(f, l) do { \ - (void)(f); \ - fl_lua_loc.line = (l); \ -} while (0) +static FlLuaLoc fl_lua_loc = {0, 0}; +#define FL_LOC_SET(f, l) \ + do { \ + (void) (f); \ + fl_lua_loc.line = (l); \ + } while (0) #define kMaxFunctionInputParams 32 @@ -394,7 +425,7 @@ static FlLuaLoc fl_lua_loc = { 0, 0 }; // 具名形参若类型会受默认实参提升影响(bool/_Bool 会被提升为 int),行为未定义。 // 调用处传入的 true/false 会隐式转换为 int。 static inline CVar FlMakeClosure(State *state, void *func_ptr, int upvalue_count, int expected_arg_count, int is_vararg, ...) { - VarClosure *cl = (VarClosure *)FlFastAlloc(state, sizeof(VarClosure) + upvalue_count * sizeof(CVar *), !__fakelua_init_flag__); + VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure) + upvalue_count * sizeof(CVar *), !__fakelua_init_flag__); cl->func_ptr = func_ptr; cl->upvalue_count = upvalue_count; cl->expected_arg_count = expected_arg_count; @@ -414,7 +445,7 @@ static inline CVar FlMakeClosure(State *state, void *func_ptr, int upvalue_count } static inline CVar FlMakeClosure0(State *state, void *func_ptr, int expected_arg_count, int is_vararg) { - VarClosure *cl = (VarClosure *)FlFastAlloc(state, sizeof(VarClosure), !__fakelua_init_flag__); + VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure), !__fakelua_init_flag__); cl->func_ptr = func_ptr; cl->upvalue_count = 0; cl->expected_arg_count = expected_arg_count; @@ -428,7 +459,7 @@ static inline CVar FlMakeClosure0(State *state, void *func_ptr, int expected_arg } static inline CVar FlMakeClosure1(State *state, void *func_ptr, int expected_arg_count, int is_vararg, CVar *up0) { - VarClosure *cl = (VarClosure *)FlFastAlloc(state, sizeof(VarClosure) + sizeof(CVar *), !__fakelua_init_flag__); + VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure) + sizeof(CVar *), !__fakelua_init_flag__); cl->func_ptr = func_ptr; cl->upvalue_count = 1; cl->expected_arg_count = expected_arg_count; @@ -448,20 +479,20 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { } // 与 FakeluaCallByName 一致:先拦超限,再读 varargs。 // raw_arg_arr 只有 32 槽,arg_num>32 会栈缓冲溢出。 - if (UNLIKELY(arg_num < 0 || arg_num > (int)kMaxFunctionInputParams)) { + if (UNLIKELY(arg_num < 0 || arg_num > (int) kMaxFunctionInputParams)) { FakeluaThrowError(state, "too many arguments"); } VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; if (UNLIKELY(addr == NULL)) { - return FlEvalLoadClosure(state, cl, arg_num, cl_var.type_ == VAR_CLOSURE ? (const CVar *)&cl_var : NULL); + return FlEvalLoadClosure(state, cl, arg_num, cl_var.type_ == VAR_CLOSURE ? (const CVar *) &cl_var : NULL); } const bool is_vararg = cl->is_vararg; const int expected_arg_count = cl->expected_arg_count; const int fixed_arg_count = is_vararg ? expected_arg_count - 1 : expected_arg_count; // 形参个数也要限:下面按 expected_arg_count 往 temp_arg_arr(定长 32)里写, // 声明了超过上限的形参会栈缓冲溢出(与 vm.cpp 的 CallByNameImpl 一致)。 - if (UNLIKELY(expected_arg_count < 0 || expected_arg_count > (int)kMaxFunctionInputParams)) { + if (UNLIKELY(expected_arg_count < 0 || expected_arg_count > (int) kMaxFunctionInputParams)) { FakeluaThrowError(state, "too many arguments"); } @@ -484,8 +515,8 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { for (int i = 0; i < arg_num; ++i) { if (i == arg_num - 1 && raw_arg_arr[i].type_ == VAR_MULTI) { VarMulti *m = raw_arg_arr[i].data_.m; - const int extra = m ? (int)m->count : 0; - if (flat_count + extra > (int)kMaxFunctionInputParams) { + const int extra = m ? (int) m->count : 0; + if (flat_count + extra > (int) kMaxFunctionInputParams) { FakeluaThrowError(state, "too many arguments"); } if (m) { @@ -495,12 +526,12 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { } } else if (raw_arg_arr[i].type_ == VAR_MULTI) { VarMulti *m = raw_arg_arr[i].data_.m; - if (flat_count >= (int)kMaxFunctionInputParams) { + if (flat_count >= (int) kMaxFunctionInputParams) { FakeluaThrowError(state, "too many arguments"); } - flat_args_buf[flat_count++] = (m && m->count > 0) ? m->vars[0] : (CVar){VAR_NIL}; + flat_args_buf[flat_count++] = (m && m->count > 0) ? m->vars[0] : (CVar) {VAR_NIL}; } else { - if (flat_count >= (int)kMaxFunctionInputParams) { + if (flat_count >= (int) kMaxFunctionInputParams) { FakeluaThrowError(state, "too many arguments"); } flat_args_buf[flat_count++] = raw_arg_arr[i]; @@ -509,10 +540,10 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { if (UNLIKELY(is_vararg)) { for (int i = 0; i < fixed_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar){VAR_NIL}; + temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {VAR_NIL}; } const int vararg_count = flat_count - fixed_arg_count; - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + (vararg_count > 0 ? vararg_count : 0) * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + (vararg_count > 0 ? vararg_count : 0) * sizeof(CVar), !__fakelua_init_flag__); m->count = vararg_count > 0 ? vararg_count : 0; for (int i = 0; i < vararg_count; ++i) { m->vars[i] = flat_args_buf[fixed_arg_count + i]; @@ -524,13 +555,13 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { temp_arg_arr[fixed_arg_count] = vararg_cvar; } else { for (int i = 0; i < expected_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar){VAR_NIL}; + temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {VAR_NIL}; } } arg_arr = temp_arg_arr; } - if (cl->code_str == (const char *)1) { + if (cl->code_str == (const char *) 1) { int npass = expected_arg_count; if (is_vararg) { npass = fixed_arg_count + 1; @@ -607,11 +638,11 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { #define FCARG_31 FCARG_30, arg_arr[30] #define FCARG_32 FCARG_31, arg_arr[31] -#define FCCASE(N) \ +#define FCCASE(N) \ case N: { \ int __fl_saved_line = fl_lua_loc.line; \ fl_lua_loc.line = 0; \ - int __fl_prev_jit = FakeluaJitContextPush(state, FAKELUA_JIT_TYPE); \ + int __fl_prev_jit = FakeluaJitContextPush(state, FAKELUA_JIT_TYPE); \ CVar __fl_ret = ((CVar (*)(VarClosure * FCCVAR_##N))(addr))(cl FCARG_##N); \ FakeluaJitContextPop(state, __fl_prev_jit); \ fl_lua_loc.line = __fl_saved_line; \ @@ -619,13 +650,32 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { } switch (expected_arg_count) { - FCCASE(0) FCCASE(1) FCCASE(2) FCCASE(3) FCCASE(4) FCCASE(5) - FCCASE(6) FCCASE(7) FCCASE(8) FCCASE(9) FCCASE(10) FCCASE(11) - FCCASE(12) FCCASE(13) FCCASE(14) FCCASE(15) FCCASE(16) FCCASE(17) - FCCASE(18) FCCASE(19) FCCASE(20) FCCASE(21) FCCASE(22) FCCASE(23) - FCCASE(24) FCCASE(25) FCCASE(26) FCCASE(27) FCCASE(28) FCCASE(29) - FCCASE(30) FCCASE(31) FCCASE(32) - default: FakeluaThrowError(state, "FlCallClosure: expected_arg_count out of range"); return (CVar){VAR_NIL}; + FCCASE(0) + FCCASE(1) + FCCASE(2) + FCCASE(3) + FCCASE(4) + FCCASE(5) + FCCASE(6) + FCCASE(7) + FCCASE(8) + FCCASE(9) + FCCASE(10) + FCCASE(11) + FCCASE(12) + FCCASE(13) + FCCASE(14) + FCCASE(15) + FCCASE(16) + FCCASE(17) + FCCASE(18) + FCCASE(19) + FCCASE(20) + FCCASE(21) + FCCASE(22) + FCCASE(23) + FCCASE(24) FCCASE(25) FCCASE(26) FCCASE(27) FCCASE(28) FCCASE(29) FCCASE(30) FCCASE(31) FCCASE(32) default : FakeluaThrowError(state, "FlCallClosure: expected_arg_count out of range"); + return (CVar) {VAR_NIL}; } #undef FCCASE @@ -701,7 +751,7 @@ static inline CVar FlCallClosure0(State *state, CVar cl_var) { if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 0 && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 0 && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *))(addr))(cl); @@ -716,7 +766,7 @@ static inline CVar FlCallClosure1(State *state, CVar cl_var, CVar a0) { if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 1 && a0.type_ != VAR_MULTI && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 1 && a0.type_ != VAR_MULTI && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *, CVar))(addr))(cl, a0); @@ -731,7 +781,7 @@ static inline CVar FlCallClosure2(State *state, CVar cl_var, CVar a0, CVar a1) { if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 2 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 2 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *, CVar, CVar))(addr))(cl, a0, a1); @@ -746,7 +796,7 @@ static inline CVar FlCallClosure3(State *state, CVar cl_var, CVar a0, CVar a1, C if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 3 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && a2.type_ != VAR_MULTI && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 3 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && a2.type_ != VAR_MULTI && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *, CVar, CVar, CVar))(addr))(cl, a0, a1, a2); @@ -769,7 +819,7 @@ static inline uint32_t FlHashString(const char *str, int len) { uint32_t hash = 5381; int i; for (i = 0; i < len; ++i) { - hash = ((hash << 5) + hash) + (uint8_t)str[i]; + hash = ((hash << 5) + hash) + (uint8_t) str[i]; } if (hash == 0) { hash = 1; @@ -777,80 +827,88 @@ static inline uint32_t FlHashString(const char *str, int len) { return hash; } -#define SET_STRING(v, str, len) do { \ - int __len = (len); \ - const char *__s_ptr = (str); \ - VarString *__vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + __len, !__fakelua_init_flag__); \ - __vs->size_ = __len; \ - __vs->hash_ = 0; \ - memcpy(__vs->data_, __s_ptr, __len); \ - (v).type_ = VAR_STRING; \ - (v).data_.s = __vs; \ -} while(0) - -#define SET_TABLE(v) do { \ - VarTable *__t = (VarTable *)FlFastAlloc(_S, sizeof(VarTable), !__fakelua_init_flag__); \ - __t->count_ = 0; \ - __t->bucket_count_ = 0; \ - __t->nodes_ = NULL; \ - __t->active_list_ = NULL; \ - __t->free_list_idx_ = 0xFFFFFFFF; \ - __t->spec = NULL; \ - __t->spec_get = NULL; \ - __t->spec_set = NULL; \ - __t->spec_keys = NULL; \ - __t->spec_vals = NULL; \ - __t->spec_count = 0; \ - __t->seq_len_valid_ = 1; \ - __t->seq_len_ = 0; \ - __t->arr_ = NULL; \ - __t->arr_size_ = 0; \ - __t->arr_cap_ = 0; \ - assert(sizeof(__t->quick_data_) == 8 * sizeof(VarEntry)); \ - { int __i; for (__i = 0; __i < 8; ++__i) { \ - __t->quick_data_[__i].key.type_ = VAR_NIL; \ - __t->quick_data_[__i].val.type_ = VAR_NIL; \ - __t->quick_data_[__i].hash = 0; \ - } } \ - (v).type_ = VAR_TABLE; \ - /* init 中创建 → CONST_FLAG(嵌套表同样走 SET_TABLE,整树只读);init 后创建 → 可变 */ \ - (v).flag_ = __fakelua_init_flag__ ? 0 : CONST_FLAG; \ - (v).data_.t = __t; \ -} while(0) - -#define SET_TABLE_SPEC(v, SpecType, get_fn, set_fn, field_count) do { \ - SET_TABLE(v); \ - SpecType *__sp = (SpecType *)FlFastAlloc(_S, sizeof(SpecType), !__fakelua_init_flag__); \ - (v).data_.t->spec = __sp; \ - (v).data_.t->spec_get = (get_fn); \ - (v).data_.t->spec_set = (set_fn); \ - (v).data_.t->spec_keys = (CVar *)FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ - (v).data_.t->spec_vals = (CVar *)FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ - (v).data_.t->spec_count = (field_count); \ - assert(sizeof(SpecType) == (field_count) * sizeof(CVar)); \ -} while(0) - -#define FL_SPEC(SpecType, v, field) (((SpecType *)(v).data_.t->spec)->field) -#define FL_SET_SPEC(SpecType, v, field, index, val) do { \ - CVar __flsv = (val); \ - FL_CHECK_CONST_TABLE(v); \ - FL_SPEC(SpecType, (v), field) = __flsv; \ - (v).data_.t->spec_vals[(index)] = __flsv; \ -} while(0) - -#define IsTrue(v, result) do { \ - CVar __tv = (v); \ - (result) = (LIKELY(__tv.type_ != VAR_NIL) && (__tv.type_ != VAR_BOOL || __tv.data_.b)); \ -} while(0) +#define SET_STRING(v, str, len) \ + do { \ + int __len = (len); \ + const char *__s_ptr = (str); \ + VarString *__vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + __len, !__fakelua_init_flag__); \ + __vs->size_ = __len; \ + __vs->hash_ = 0; \ + memcpy(__vs->data_, __s_ptr, __len); \ + (v).type_ = VAR_STRING; \ + (v).data_.s = __vs; \ + } while (0) + +#define SET_TABLE(v) \ + do { \ + VarTable *__t = (VarTable *) FlFastAlloc(_S, sizeof(VarTable), !__fakelua_init_flag__); \ + __t->count_ = 0; \ + __t->bucket_count_ = 0; \ + __t->nodes_ = NULL; \ + __t->active_list_ = NULL; \ + __t->free_list_idx_ = 0xFFFFFFFF; \ + __t->spec = NULL; \ + __t->spec_get = NULL; \ + __t->spec_set = NULL; \ + __t->spec_keys = NULL; \ + __t->spec_vals = NULL; \ + __t->spec_count = 0; \ + __t->seq_len_valid_ = 1; \ + __t->seq_len_ = 0; \ + __t->arr_ = NULL; \ + __t->arr_size_ = 0; \ + __t->arr_cap_ = 0; \ + assert(sizeof(__t->quick_data_) == 8 * sizeof(VarEntry)); \ + { \ + int __i; \ + for (__i = 0; __i < 8; ++__i) { \ + __t->quick_data_[__i].key.type_ = VAR_NIL; \ + __t->quick_data_[__i].val.type_ = VAR_NIL; \ + __t->quick_data_[__i].hash = 0; \ + } \ + } \ + (v).type_ = VAR_TABLE; \ + /* init 中创建 → CONST_FLAG(嵌套表同样走 SET_TABLE,整树只读);init 后创建 → 可变 */ \ + (v).flag_ = __fakelua_init_flag__ ? 0 : CONST_FLAG; \ + (v).data_.t = __t; \ + } while (0) + +#define SET_TABLE_SPEC(v, SpecType, get_fn, set_fn, field_count) \ + do { \ + SET_TABLE(v); \ + SpecType *__sp = (SpecType *) FlFastAlloc(_S, sizeof(SpecType), !__fakelua_init_flag__); \ + (v).data_.t->spec = __sp; \ + (v).data_.t->spec_get = (get_fn); \ + (v).data_.t->spec_set = (set_fn); \ + (v).data_.t->spec_keys = (CVar *) FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ + (v).data_.t->spec_vals = (CVar *) FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ + (v).data_.t->spec_count = (field_count); \ + assert(sizeof(SpecType) == (field_count) * sizeof(CVar)); \ + } while (0) + +#define FL_SPEC(SpecType, v, field) (((SpecType *) (v).data_.t->spec)->field) +#define FL_SET_SPEC(SpecType, v, field, index, val) \ + do { \ + CVar __flsv = (val); \ + FL_CHECK_CONST_TABLE(v); \ + FL_SPEC(SpecType, (v), field) = __flsv; \ + (v).data_.t->spec_vals[(index)] = __flsv; \ + } while (0) + +#define IsTrue(v, result) \ + do { \ + CVar __tv = (v); \ + (result) = (LIKELY(__tv.type_ != VAR_NIL) && (__tv.type_ != VAR_BOOL || __tv.data_.b)); \ + } while (0) static inline int FlCompareIntDouble(int64_t integer, double floating) { if (isnan(floating)) return 0; if (floating >= 9223372036854775808.0) return -1; if (floating < -9223372036854775808.0) return 1; - int64_t truncated = (int64_t)floating; + int64_t truncated = (int64_t) floating; if (integer < truncated) return -1; if (integer > truncated) return 1; - double truncated_as_double = (double)truncated; + double truncated_as_double = (double) truncated; if (floating > truncated_as_double) return -1; if (floating < truncated_as_double) return 1; return 0; @@ -858,102 +916,223 @@ static inline int FlCompareIntDouble(int64_t integer, double floating) { #define FL_INT_FLOAT_EQUAL(i, f) (!isnan((f)) && FlCompareIntDouble((i), (f)) == 0) -#define VarEqual(_fa, _fb, result) do { \ - CVar __a = (_fa), __b = (_fb); \ - if (__a.type_ != __b.type_) { \ - if ((__a.type_ == VAR_STRING || __a.type_ == VAR_STRINGID) && \ - (__b.type_ == VAR_STRING || __b.type_ == VAR_STRINGID)) { \ - VarString *__sa = (__a.type_ == VAR_STRING) ? __a.data_.s : (VarString *)__a.data_.i; \ - VarString *__sb = (__b.type_ == VAR_STRING) ? __b.data_.s : (VarString *)__b.data_.i; \ - (result) = (__sa == __sb) || (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ - } else if (__a.type_ == VAR_INT && __b.type_ == VAR_FLOAT) { \ - (result) = FL_INT_FLOAT_EQUAL(__a.data_.i, __b.data_.f); \ - } else if (__a.type_ == VAR_FLOAT && __b.type_ == VAR_INT) { \ - (result) = FL_INT_FLOAT_EQUAL(__b.data_.i, __a.data_.f); \ - } else { \ - (result) = false; \ - } \ - break; \ - } \ - switch (__a.type_) { \ - case VAR_NIL: (result) = true; break; \ - case VAR_BOOL: (result) = (__a.data_.b == __b.data_.b); break; \ - case VAR_INT: (result) = (__a.data_.i == __b.data_.i); break; \ - case VAR_FLOAT: (result) = (__a.data_.f == __b.data_.f); break; \ - case VAR_STRING: { \ - if (__a.data_.s == __b.data_.s) { (result) = true; break; } \ - (result) = (__a.data_.s->size_ == __b.data_.s->size_ && memcmp(__a.data_.s->data_, __b.data_.s->data_, __a.data_.s->size_) == 0); \ - break; \ - } \ - case VAR_STRINGID: { \ - if (__a.data_.i == __b.data_.i) { (result) = true; break; } \ - VarString *__sa = (VarString *)__a.data_.i; \ - VarString *__sb = (VarString *)__b.data_.i; \ - (result) = (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ - break; \ - } \ - case VAR_TABLE: (result) = (__a.data_.t == __b.data_.t); break; \ - case VAR_CLOSURE: (result) = (__a.data_.cl == __b.data_.cl); break; \ - default: (result) = false; break; \ - } \ -} while(0) - -#define VarHash(v, hash) do { \ - CVar __v = (v); \ - switch (__v.type_) { \ - case VAR_NIL: (hash) = 0; break; \ - case VAR_BOOL: (hash) = (__v.data_.b ? 1 : 0); break; \ - case VAR_INT: (hash) = (uint32_t)(__v.data_.i ^ (__v.data_.i >> 32)); break; \ - case VAR_FLOAT: { union { double __d; uint64_t __u; } __fu; __fu.__d = __v.data_.f; (hash) = (uint32_t)(__fu.__u ^ (__fu.__u >> 32)); break; } \ - case VAR_STRING: { if (__v.data_.s->hash_ == 0) { __v.data_.s->hash_ = FlHashString(__v.data_.s->data_, __v.data_.s->size_); } (hash) = __v.data_.s->hash_; break; } \ - case VAR_STRINGID: { VarString *__vs = (VarString *)__v.data_.i; if (__vs->hash_ == 0) { __vs->hash_ = FlHashString(__vs->data_, __vs->size_); } (hash) = __vs->hash_; break; } \ - case VAR_TABLE: (hash) = (uint32_t)((uintptr_t)__v.data_.t ^ ((uintptr_t)__v.data_.t >> 32)); break; \ - case VAR_CLOSURE: (hash) = (uint32_t)((uintptr_t)__v.data_.cl ^ ((uintptr_t)__v.data_.cl >> 32)); break; \ - default: (hash) = 0; break; \ - } \ -} while(0) +#define VarEqual(_fa, _fb, result) \ + do { \ + CVar __a = (_fa), __b = (_fb); \ + if (__a.type_ != __b.type_) { \ + if ((__a.type_ == VAR_STRING || __a.type_ == VAR_STRINGID) && (__b.type_ == VAR_STRING || __b.type_ == VAR_STRINGID)) { \ + VarString *__sa = (__a.type_ == VAR_STRING) ? __a.data_.s : (VarString *) __a.data_.i; \ + VarString *__sb = (__b.type_ == VAR_STRING) ? __b.data_.s : (VarString *) __b.data_.i; \ + (result) = (__sa == __sb) || (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ + } else if (__a.type_ == VAR_INT && __b.type_ == VAR_FLOAT) { \ + (result) = FL_INT_FLOAT_EQUAL(__a.data_.i, __b.data_.f); \ + } else if (__a.type_ == VAR_FLOAT && __b.type_ == VAR_INT) { \ + (result) = FL_INT_FLOAT_EQUAL(__b.data_.i, __a.data_.f); \ + } else { \ + (result) = false; \ + } \ + break; \ + } \ + switch (__a.type_) { \ + case VAR_NIL: \ + (result) = true; \ + break; \ + case VAR_BOOL: \ + (result) = (__a.data_.b == __b.data_.b); \ + break; \ + case VAR_INT: \ + (result) = (__a.data_.i == __b.data_.i); \ + break; \ + case VAR_FLOAT: \ + (result) = (__a.data_.f == __b.data_.f); \ + break; \ + case VAR_STRING: { \ + if (__a.data_.s == __b.data_.s) { \ + (result) = true; \ + break; \ + } \ + (result) = (__a.data_.s->size_ == __b.data_.s->size_ && memcmp(__a.data_.s->data_, __b.data_.s->data_, __a.data_.s->size_) == 0); \ + break; \ + } \ + case VAR_STRINGID: { \ + if (__a.data_.i == __b.data_.i) { \ + (result) = true; \ + break; \ + } \ + VarString *__sa = (VarString *) __a.data_.i; \ + VarString *__sb = (VarString *) __b.data_.i; \ + (result) = (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ + break; \ + } \ + case VAR_TABLE: \ + (result) = (__a.data_.t == __b.data_.t); \ + break; \ + case VAR_CLOSURE: \ + (result) = (__a.data_.cl == __b.data_.cl); \ + break; \ + default: \ + (result) = false; \ + break; \ + } \ + } while (0) + +#define VarHash(v, hash) \ + do { \ + CVar __v = (v); \ + switch (__v.type_) { \ + case VAR_NIL: \ + (hash) = 0; \ + break; \ + case VAR_BOOL: \ + (hash) = (__v.data_.b ? 1 : 0); \ + break; \ + case VAR_INT: \ + (hash) = (uint32_t) (__v.data_.i ^ (__v.data_.i >> 32)); \ + break; \ + case VAR_FLOAT: { \ + union { \ + double __d; \ + uint64_t __u; \ + } __fu; \ + __fu.__d = __v.data_.f; \ + (hash) = (uint32_t) (__fu.__u ^ (__fu.__u >> 32)); \ + break; \ + } \ + case VAR_STRING: { \ + if (__v.data_.s->hash_ == 0) { \ + __v.data_.s->hash_ = FlHashString(__v.data_.s->data_, __v.data_.s->size_); \ + } \ + (hash) = __v.data_.s->hash_; \ + break; \ + } \ + case VAR_STRINGID: { \ + VarString *__vs = (VarString *) __v.data_.i; \ + if (__vs->hash_ == 0) { \ + __vs->hash_ = FlHashString(__vs->data_, __vs->size_); \ + } \ + (hash) = __vs->hash_; \ + break; \ + } \ + case VAR_TABLE: \ + (hash) = (uint32_t) ((uintptr_t) __v.data_.t ^ ((uintptr_t) __v.data_.t >> 32)); \ + break; \ + case VAR_CLOSURE: \ + (hash) = (uint32_t) ((uintptr_t) __v.data_.cl ^ ((uintptr_t) __v.data_.cl >> 32)); \ + break; \ + default: \ + (hash) = 0; \ + break; \ + } \ + } while (0) static inline CVar FlGetTable(CVar t, CVar k) { k = NORMALIZE_TABLE_KEY(k); - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } - if (UNLIKELY(k.type_ == VAR_NIL)) { FakeluaThrowError(_S, "table index is nil"); } - if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { FakeluaThrowError(_S, "table index is NaN"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } + if (UNLIKELY(k.type_ == VAR_NIL)) { + FakeluaThrowError(_S, "table index is nil"); + } + if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { + FakeluaThrowError(_S, "table index is NaN"); + } VarTable *tbl = t.data_.t; if (tbl->spec_get) { bool __finish = false; CVar __r = tbl->spec_get(tbl, k, &__finish); if (__finish) return __r; } - if (k.type_ == VAR_INT && (uint64_t)k.data_.i - 1 < (uint64_t)tbl->arr_size_) { return tbl->arr_[k.data_.i - 1]; } - if (UNLIKELY(tbl->count_ == 0)) { return (CVar){VAR_NIL}; } - uint32_t h; VarHash(k, h); + if (k.type_ == VAR_INT && (uint64_t) k.data_.i - 1 < (uint64_t) tbl->arr_size_) { + return tbl->arr_[k.data_.i - 1]; + } + if (UNLIKELY(tbl->count_ == 0)) { + return (CVar) {VAR_NIL}; + } + uint32_t h; + VarHash(k, h); if (LIKELY(tbl->bucket_count_ == 0)) { bool __eq; - if (tbl->quick_data_[0].hash == h) { VarEqual(tbl->quick_data_[0].key, k, __eq); if (__eq) { return tbl->quick_data_[0].val; } } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { VarEqual(tbl->quick_data_[1].key, k, __eq); if (__eq) { return tbl->quick_data_[1].val; } } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { VarEqual(tbl->quick_data_[2].key, k, __eq); if (__eq) { return tbl->quick_data_[2].val; } } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { VarEqual(tbl->quick_data_[3].key, k, __eq); if (__eq) { return tbl->quick_data_[3].val; } } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { VarEqual(tbl->quick_data_[4].key, k, __eq); if (__eq) { return tbl->quick_data_[4].val; } } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { VarEqual(tbl->quick_data_[5].key, k, __eq); if (__eq) { return tbl->quick_data_[5].val; } } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { VarEqual(tbl->quick_data_[6].key, k, __eq); if (__eq) { return tbl->quick_data_[6].val; } } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { VarEqual(tbl->quick_data_[7].key, k, __eq); if (__eq) { return tbl->quick_data_[7].val; } } + if (tbl->quick_data_[0].hash == h) { + VarEqual(tbl->quick_data_[0].key, k, __eq); + if (__eq) { + return tbl->quick_data_[0].val; + } + } + if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { + VarEqual(tbl->quick_data_[1].key, k, __eq); + if (__eq) { + return tbl->quick_data_[1].val; + } + } + if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { + VarEqual(tbl->quick_data_[2].key, k, __eq); + if (__eq) { + return tbl->quick_data_[2].val; + } + } + if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { + VarEqual(tbl->quick_data_[3].key, k, __eq); + if (__eq) { + return tbl->quick_data_[3].val; + } + } + if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { + VarEqual(tbl->quick_data_[4].key, k, __eq); + if (__eq) { + return tbl->quick_data_[4].val; + } + } + if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { + VarEqual(tbl->quick_data_[5].key, k, __eq); + if (__eq) { + return tbl->quick_data_[5].val; + } + } + if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { + VarEqual(tbl->quick_data_[6].key, k, __eq); + if (__eq) { + return tbl->quick_data_[6].val; + } + } + if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { + VarEqual(tbl->quick_data_[7].key, k, __eq); + if (__eq) { + return tbl->quick_data_[7].val; + } + } } else { uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return (CVar){VAR_NIL}; } + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return (CVar) {VAR_NIL}; + } while (1) { - if (curr->entry.hash == h) { bool __eq; VarEqual(curr->entry.key, k, __eq); if (__eq) { return curr->entry.val; } } + if (curr->entry.hash == h) { + bool __eq; + VarEqual(curr->entry.key, k, __eq); + if (__eq) { + return curr->entry.val; + } + } uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { break; } + if (next == 0xFFFFFFFF) { + break; + } curr = &tbl->nodes_[next]; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } static inline uint32_t NextPowerOfTwo(uint32_t v) { - v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; return v; } @@ -964,25 +1143,46 @@ static inline bool FlTableInsertRaw(VarTable *tbl, CVar key, CVar val, uint32_t uint32_t idx = hash & mask; TableNode *main_node = &tbl->nodes_[idx]; if (LIKELY(main_node->entry.key.type_ == VAR_NIL)) { - main_node->entry.key = key; main_node->entry.val = val; main_node->entry.hash = hash; main_node->next = 0xFFFFFFFF; - main_node->active_pos = tbl->count_; tbl->active_list_[tbl->count_] = idx; - tbl->count_++; return true; + main_node->entry.key = key; + main_node->entry.val = val; + main_node->entry.hash = hash; + main_node->next = 0xFFFFFFFF; + main_node->active_pos = tbl->count_; + tbl->active_list_[tbl->count_] = idx; + tbl->count_++; + return true; } uint32_t curr_idx = idx; while (1) { TableNode *curr = &tbl->nodes_[curr_idx]; - if (curr->entry.hash == hash) { bool __eq; VarEqual(curr->entry.key, key, __eq); if (__eq) { curr->entry.val = val; return true; } } - if (curr->next == 0xFFFFFFFF) { break; } + if (curr->entry.hash == hash) { + bool __eq; + VarEqual(curr->entry.key, key, __eq); + if (__eq) { + curr->entry.val = val; + return true; + } + } + if (curr->next == 0xFFFFFFFF) { + break; + } curr_idx = curr->next; } - if (UNLIKELY(tbl->free_list_idx_ == 0xFFFFFFFF)) { return false; } + if (UNLIKELY(tbl->free_list_idx_ == 0xFFFFFFFF)) { + return false; + } uint32_t new_node_idx = tbl->free_list_idx_; TableNode *new_node = &tbl->nodes_[new_node_idx]; tbl->free_list_idx_ = new_node->next; - new_node->entry.key = key; new_node->entry.val = val; new_node->entry.hash = hash; - new_node->next = main_node->next; main_node->next = new_node_idx; - new_node->active_pos = tbl->count_; tbl->active_list_[tbl->count_] = new_node_idx; - tbl->count_++; return true; + new_node->entry.key = key; + new_node->entry.val = val; + new_node->entry.hash = hash; + new_node->next = main_node->next; + main_node->next = new_node_idx; + new_node->active_pos = tbl->count_; + tbl->active_list_[tbl->count_] = new_node_idx; + tbl->count_++; + return true; } static inline void FlTableRehash(VarTable *tbl) { @@ -990,20 +1190,41 @@ static inline void FlTableRehash(VarTable *tbl) { uint32_t old_bucket_count = tbl->bucket_count_; TableNode *old_nodes = tbl->nodes_; uint32_t new_bucket_count = NextPowerOfTwo(old_count + 1); - if (new_bucket_count <= old_bucket_count) { new_bucket_count = old_bucket_count * 2; } + if (new_bucket_count <= old_bucket_count) { + new_bucket_count = old_bucket_count * 2; + } while (1) { uint32_t overflow_count = new_bucket_count / 2; uint32_t total_nodes = new_bucket_count + overflow_count; size_t nodes_size = total_nodes * sizeof(TableNode); size_t active_list_size = total_nodes * sizeof(uint32_t); - char *buffer = (char *)FlFastAlloc(_S, nodes_size + active_list_size, !__fakelua_init_flag__); - TableNode *new_nodes = (TableNode *)buffer; - uint32_t *new_active_list = (uint32_t *)(buffer + nodes_size); - { uint32_t i; for (i = 0; i < total_nodes; ++i) { new_nodes[i].entry.key.type_ = VAR_NIL; new_nodes[i].next = 0xFFFFFFFF; new_nodes[i].active_pos = 0xFFFFFFFF; } } - TableNode *prev_nodes = tbl->nodes_; uint32_t *prev_active_list = tbl->active_list_; uint32_t prev_bucket_count = tbl->bucket_count_; uint32_t prev_count = tbl->count_; uint32_t prev_free_list_idx = tbl->free_list_idx_; - tbl->nodes_ = new_nodes; tbl->active_list_ = new_active_list; tbl->bucket_count_ = new_bucket_count; tbl->count_ = 0; + char *buffer = (char *) FlFastAlloc(_S, nodes_size + active_list_size, !__fakelua_init_flag__); + TableNode *new_nodes = (TableNode *) buffer; + uint32_t *new_active_list = (uint32_t *) (buffer + nodes_size); + { + uint32_t i; + for (i = 0; i < total_nodes; ++i) { + new_nodes[i].entry.key.type_ = VAR_NIL; + new_nodes[i].next = 0xFFFFFFFF; + new_nodes[i].active_pos = 0xFFFFFFFF; + } + } + TableNode *prev_nodes = tbl->nodes_; + uint32_t *prev_active_list = tbl->active_list_; + uint32_t prev_bucket_count = tbl->bucket_count_; + uint32_t prev_count = tbl->count_; + uint32_t prev_free_list_idx = tbl->free_list_idx_; + tbl->nodes_ = new_nodes; + tbl->active_list_ = new_active_list; + tbl->bucket_count_ = new_bucket_count; + tbl->count_ = 0; if (LIKELY(overflow_count > 0)) { - { uint32_t i; for (i = 0; i < overflow_count - 1; ++i) { tbl->nodes_[new_bucket_count + i].next = new_bucket_count + i + 1; } } + { + uint32_t i; + for (i = 0; i < overflow_count - 1; ++i) { + tbl->nodes_[new_bucket_count + i].next = new_bucket_count + i + 1; + } + } tbl->nodes_[new_bucket_count + overflow_count - 1].next = 0xFFFFFFFF; tbl->free_list_idx_ = new_bucket_count; } else { @@ -1011,91 +1232,288 @@ static inline void FlTableRehash(VarTable *tbl) { } bool success = true; if (LIKELY(old_bucket_count == 0)) { - { uint32_t i; for (i = 0; i < old_count; ++i) { if (!FlTableInsertRaw(tbl, tbl->quick_data_[i].key, tbl->quick_data_[i].val, tbl->quick_data_[i].hash)) { success = false; break; } } } + { + uint32_t i; + for (i = 0; i < old_count; ++i) { + if (!FlTableInsertRaw(tbl, tbl->quick_data_[i].key, tbl->quick_data_[i].val, tbl->quick_data_[i].hash)) { + success = false; + break; + } + } + } } else { - { uint32_t i; for (i = 0; i < old_bucket_count; ++i) { - uint32_t curr_idx = i; - while (curr_idx != 0xFFFFFFFF) { - TableNode *old_node = &old_nodes[curr_idx]; - if (LIKELY(old_node->entry.key.type_ != VAR_NIL)) { if (!FlTableInsertRaw(tbl, old_node->entry.key, old_node->entry.val, old_node->entry.hash)) { success = false; break; } } - curr_idx = old_node->next; + { + uint32_t i; + for (i = 0; i < old_bucket_count; ++i) { + uint32_t curr_idx = i; + while (curr_idx != 0xFFFFFFFF) { + TableNode *old_node = &old_nodes[curr_idx]; + if (LIKELY(old_node->entry.key.type_ != VAR_NIL)) { + if (!FlTableInsertRaw(tbl, old_node->entry.key, old_node->entry.val, old_node->entry.hash)) { + success = false; + break; + } + } + curr_idx = old_node->next; + } + if (!success) { + break; + } } - if (!success) { break; } - } } + } + } + if (LIKELY(success)) { + break; + } else { + tbl->nodes_ = prev_nodes; + tbl->active_list_ = prev_active_list; + tbl->bucket_count_ = prev_bucket_count; + tbl->count_ = prev_count; + tbl->free_list_idx_ = prev_free_list_idx; + new_bucket_count *= 2; } - if (LIKELY(success)) { break; } else { tbl->nodes_ = prev_nodes; tbl->active_list_ = prev_active_list; tbl->bucket_count_ = prev_bucket_count; tbl->count_ = prev_count; tbl->free_list_idx_ = prev_free_list_idx; new_bucket_count *= 2; } } } static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v); static inline void FlHashSet(VarTable *tbl, CVar k, CVar v) { - uint32_t h; VarHash(k, h); + uint32_t h; + VarHash(k, h); if (UNLIKELY(v.type_ == VAR_NIL)) { - if (UNLIKELY(tbl->count_ == 0)) { return; } + if (UNLIKELY(tbl->count_ == 0)) { + return; + } if (LIKELY(tbl->bucket_count_ == 0)) { bool __eq; - if (tbl->quick_data_[0].hash == h) { VarEqual(tbl->quick_data_[0].key, k, __eq); if (__eq) { if (tbl->count_ > 1) { tbl->quick_data_[0] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { VarEqual(tbl->quick_data_[1].key, k, __eq); if (__eq) { if (tbl->count_ > 2) { tbl->quick_data_[1] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { VarEqual(tbl->quick_data_[2].key, k, __eq); if (__eq) { if (tbl->count_ > 3) { tbl->quick_data_[2] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { VarEqual(tbl->quick_data_[3].key, k, __eq); if (__eq) { if (tbl->count_ > 4) { tbl->quick_data_[3] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { VarEqual(tbl->quick_data_[4].key, k, __eq); if (__eq) { if (tbl->count_ > 5) { tbl->quick_data_[4] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { VarEqual(tbl->quick_data_[5].key, k, __eq); if (__eq) { if (tbl->count_ > 6) { tbl->quick_data_[5] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { VarEqual(tbl->quick_data_[6].key, k, __eq); if (__eq) { if (tbl->count_ > 7) { tbl->quick_data_[6] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { VarEqual(tbl->quick_data_[7].key, k, __eq); if (__eq) { tbl->count_--; return; } } + if (tbl->quick_data_[0].hash == h) { + VarEqual(tbl->quick_data_[0].key, k, __eq); + if (__eq) { + if (tbl->count_ > 1) { + tbl->quick_data_[0] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { + VarEqual(tbl->quick_data_[1].key, k, __eq); + if (__eq) { + if (tbl->count_ > 2) { + tbl->quick_data_[1] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { + VarEqual(tbl->quick_data_[2].key, k, __eq); + if (__eq) { + if (tbl->count_ > 3) { + tbl->quick_data_[2] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { + VarEqual(tbl->quick_data_[3].key, k, __eq); + if (__eq) { + if (tbl->count_ > 4) { + tbl->quick_data_[3] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { + VarEqual(tbl->quick_data_[4].key, k, __eq); + if (__eq) { + if (tbl->count_ > 5) { + tbl->quick_data_[4] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { + VarEqual(tbl->quick_data_[5].key, k, __eq); + if (__eq) { + if (tbl->count_ > 6) { + tbl->quick_data_[5] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { + VarEqual(tbl->quick_data_[6].key, k, __eq); + if (__eq) { + if (tbl->count_ > 7) { + tbl->quick_data_[6] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { + VarEqual(tbl->quick_data_[7].key, k, __eq); + if (__eq) { + tbl->count_--; + return; + } + } } else { - uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return; } - { bool __eq; VarEqual(curr->entry.key, k, __eq); if (curr->entry.hash == h && __eq) { - if (curr->next != 0xFFFFFFFF) { - uint32_t next_idx = curr->next; TableNode *next_node = &tbl->nodes_[next_idx]; - uint32_t pos = next_node->active_pos; uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (next_idx != last_node_idx) { tbl->active_list_[pos] = last_node_idx; tbl->nodes_[last_node_idx].active_pos = pos; } - next_node->active_pos = 0xFFFFFFFF; curr->entry = next_node->entry; curr->next = next_node->next; next_node->next = tbl->free_list_idx_; tbl->free_list_idx_ = next_idx; - } else { - uint32_t pos = curr->active_pos; uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (idx != last_node_idx) { tbl->active_list_[pos] = last_node_idx; tbl->nodes_[last_node_idx].active_pos = pos; } - curr->active_pos = 0xFFFFFFFF; curr->entry.key.type_ = VAR_NIL; + uint32_t mask = tbl->bucket_count_ - 1; + uint32_t idx = h & mask; + TableNode *curr = &tbl->nodes_[idx]; + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return; + } + { + bool __eq; + VarEqual(curr->entry.key, k, __eq); + if (curr->entry.hash == h && __eq) { + if (curr->next != 0xFFFFFFFF) { + uint32_t next_idx = curr->next; + TableNode *next_node = &tbl->nodes_[next_idx]; + uint32_t pos = next_node->active_pos; + uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; + if (next_idx != last_node_idx) { + tbl->active_list_[pos] = last_node_idx; + tbl->nodes_[last_node_idx].active_pos = pos; + } + next_node->active_pos = 0xFFFFFFFF; + curr->entry = next_node->entry; + curr->next = next_node->next; + next_node->next = tbl->free_list_idx_; + tbl->free_list_idx_ = next_idx; + } else { + uint32_t pos = curr->active_pos; + uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; + if (idx != last_node_idx) { + tbl->active_list_[pos] = last_node_idx; + tbl->nodes_[last_node_idx].active_pos = pos; + } + curr->active_pos = 0xFFFFFFFF; + curr->entry.key.type_ = VAR_NIL; + } + tbl->count_--; + return; } - tbl->count_--; return; - } } - uint32_t prev_idx = idx; uint32_t curr_idx = curr->next; + } + uint32_t prev_idx = idx; + uint32_t curr_idx = curr->next; while (curr_idx != 0xFFFFFFFF) { TableNode *node = &tbl->nodes_[curr_idx]; - { bool __eq; VarEqual(node->entry.key, k, __eq); if (node->entry.hash == h && __eq) { - uint32_t pos = node->active_pos; uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (curr_idx != last_node_idx) { tbl->active_list_[pos] = last_node_idx; tbl->nodes_[last_node_idx].active_pos = pos; } - node->active_pos = 0xFFFFFFFF; tbl->nodes_[prev_idx].next = node->next; node->next = tbl->free_list_idx_; tbl->free_list_idx_ = curr_idx; tbl->count_--; return; - } } - prev_idx = curr_idx; curr_idx = node->next; + { + bool __eq; + VarEqual(node->entry.key, k, __eq); + if (node->entry.hash == h && __eq) { + uint32_t pos = node->active_pos; + uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; + if (curr_idx != last_node_idx) { + tbl->active_list_[pos] = last_node_idx; + tbl->nodes_[last_node_idx].active_pos = pos; + } + node->active_pos = 0xFFFFFFFF; + tbl->nodes_[prev_idx].next = node->next; + node->next = tbl->free_list_idx_; + tbl->free_list_idx_ = curr_idx; + tbl->count_--; + return; + } + } + prev_idx = curr_idx; + curr_idx = node->next; } } return; } if (LIKELY(tbl->bucket_count_ == 0)) { bool __eq; - if (tbl->count_ > 0 && tbl->quick_data_[0].hash == h) { VarEqual(tbl->quick_data_[0].key, k, __eq); if (__eq) { tbl->quick_data_[0].val = v; return; } } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { VarEqual(tbl->quick_data_[1].key, k, __eq); if (__eq) { tbl->quick_data_[1].val = v; return; } } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { VarEqual(tbl->quick_data_[2].key, k, __eq); if (__eq) { tbl->quick_data_[2].val = v; return; } } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { VarEqual(tbl->quick_data_[3].key, k, __eq); if (__eq) { tbl->quick_data_[3].val = v; return; } } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { VarEqual(tbl->quick_data_[4].key, k, __eq); if (__eq) { tbl->quick_data_[4].val = v; return; } } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { VarEqual(tbl->quick_data_[5].key, k, __eq); if (__eq) { tbl->quick_data_[5].val = v; return; } } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { VarEqual(tbl->quick_data_[6].key, k, __eq); if (__eq) { tbl->quick_data_[6].val = v; return; } } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { VarEqual(tbl->quick_data_[7].key, k, __eq); if (__eq) { tbl->quick_data_[7].val = v; return; } } - if (tbl->count_ < 8) { tbl->quick_data_[tbl->count_].key = k; tbl->quick_data_[tbl->count_].val = v; tbl->quick_data_[tbl->count_].hash = h; tbl->count_++; return; } - if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { return; } + if (tbl->count_ > 0 && tbl->quick_data_[0].hash == h) { + VarEqual(tbl->quick_data_[0].key, k, __eq); + if (__eq) { + tbl->quick_data_[0].val = v; + return; + } + } + if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { + VarEqual(tbl->quick_data_[1].key, k, __eq); + if (__eq) { + tbl->quick_data_[1].val = v; + return; + } + } + if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { + VarEqual(tbl->quick_data_[2].key, k, __eq); + if (__eq) { + tbl->quick_data_[2].val = v; + return; + } + } + if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { + VarEqual(tbl->quick_data_[3].key, k, __eq); + if (__eq) { + tbl->quick_data_[3].val = v; + return; + } + } + if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { + VarEqual(tbl->quick_data_[4].key, k, __eq); + if (__eq) { + tbl->quick_data_[4].val = v; + return; + } + } + if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { + VarEqual(tbl->quick_data_[5].key, k, __eq); + if (__eq) { + tbl->quick_data_[5].val = v; + return; + } + } + if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { + VarEqual(tbl->quick_data_[6].key, k, __eq); + if (__eq) { + tbl->quick_data_[6].val = v; + return; + } + } + if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { + VarEqual(tbl->quick_data_[7].key, k, __eq); + if (__eq) { + tbl->quick_data_[7].val = v; + return; + } + } + if (tbl->count_ < 8) { + tbl->quick_data_[tbl->count_].key = k; + tbl->quick_data_[tbl->count_].val = v; + tbl->quick_data_[tbl->count_].hash = h; + tbl->count_++; + return; + } + if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { + return; + } FlTableRehash(tbl); } if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { return; } + if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { + return; + } FlTableRehash(tbl); } FlTableInsertRaw(tbl, k, v, h); } static inline CVar FlHashGetInt(VarTable *tbl, int64_t k) { - if (UNLIKELY(tbl->count_ == 0)) { return (CVar){VAR_NIL}; } - uint32_t h = (uint32_t)(k ^ (k >> 32)); + if (UNLIKELY(tbl->count_ == 0)) { + return (CVar) {VAR_NIL}; + } + uint32_t h = (uint32_t) (k ^ (k >> 32)); if (LIKELY(tbl->bucket_count_ == 0)) { for (uint32_t i = 0; i < tbl->count_; ++i) { if (tbl->quick_data_[i].hash == h && tbl->quick_data_[i].key.type_ == VAR_INT && tbl->quick_data_[i].key.data_.i == k) { @@ -1106,23 +1524,29 @@ static inline CVar FlHashGetInt(VarTable *tbl, int64_t k) { uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return (CVar){VAR_NIL}; } + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return (CVar) {VAR_NIL}; + } while (1) { if (curr->entry.hash == h && curr->entry.key.type_ == VAR_INT && curr->entry.key.data_.i == k) { return curr->entry.val; } uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { break; } + if (next == 0xFFFFFFFF) { + break; + } curr = &tbl->nodes_[next]; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } __attribute__((noinline)) static void FlArrGrow(VarTable *tbl) { uint32_t cap = tbl->arr_cap_ ? tbl->arr_cap_ * 2 : 4; - CVar *na = (CVar *)FlFastAlloc(_S, (size_t)cap * sizeof(CVar), !__fakelua_init_flag__); - if (tbl->arr_size_ > 0) { memcpy(na, tbl->arr_, (size_t)tbl->arr_size_ * sizeof(CVar)); } + CVar *na = (CVar *) FlFastAlloc(_S, (size_t) cap * sizeof(CVar), !__fakelua_init_flag__); + if (tbl->arr_size_ > 0) { + memcpy(na, tbl->arr_, (size_t) tbl->arr_size_ * sizeof(CVar)); + } tbl->arr_ = na; tbl->arr_cap_ = cap; } @@ -1130,24 +1554,38 @@ __attribute__((noinline)) static void FlArrGrow(VarTable *tbl) { /* 数组末尾追加后,把哈希部分中紧随其后的整数键迁入数组,维持「哈希不含 [1, arr_size_+1]」。*/ __attribute__((noinline)) static void FlArrMigrate(VarTable *tbl) { while (tbl->count_ != 0) { - int64_t nk = (int64_t)tbl->arr_size_ + 1; + int64_t nk = (int64_t) tbl->arr_size_ + 1; CVar hv = FlHashGetInt(tbl, nk); - if (hv.type_ == VAR_NIL) { break; } - CVar key; key.type_ = VAR_INT; key.flag_ = 0; key.data_.i = nk; - FlHashSet(tbl, key, (CVar){VAR_NIL}); - if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { FlArrGrow(tbl); } + if (hv.type_ == VAR_NIL) { + break; + } + CVar key; + key.type_ = VAR_INT; + key.flag_ = 0; + key.data_.i = nk; + FlHashSet(tbl, key, (CVar) {VAR_NIL}); + if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { + FlArrGrow(tbl); + } tbl->arr_[tbl->arr_size_++] = hv; } } /* 整数键写入的数组部分快路径。返回 true 表示已处理。*/ static inline bool FlArrSetInt(VarTable *tbl, int64_t k, CVar v) { - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_)) { tbl->arr_[idx] = v; return true; } - if (idx == (uint64_t)tbl->arr_size_ && v.type_ != VAR_NIL && !tbl->spec_set) { - if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { FlArrGrow(tbl); } + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_)) { + tbl->arr_[idx] = v; + return true; + } + if (idx == (uint64_t) tbl->arr_size_ && v.type_ != VAR_NIL && !tbl->spec_set) { + if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { + FlArrGrow(tbl); + } tbl->arr_[tbl->arr_size_++] = v; - if (UNLIKELY(tbl->count_ != 0)) { FlArrMigrate(tbl); } + if (UNLIKELY(tbl->count_ != 0)) { + FlArrMigrate(tbl); + } return true; } return false; @@ -1160,19 +1598,31 @@ __attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) uint64_t total = 0; uint32_t i; memset(nums, 0, sizeof(nums)); -#define FL_COUNT_INT_KEY(kk) do { \ - int64_t __ck = (kk); \ - if (__ck >= 1 && __ck <= (1LL << 30)) { nums[__ck == 1 ? 0 : 64 - __builtin_clzll((uint64_t)__ck - 1)]++; total++; } \ -} while(0) - for (i = 0; i < tbl->arr_size_; ++i) { if (tbl->arr_[i].type_ != VAR_NIL) { FL_COUNT_INT_KEY((int64_t)i + 1); } } +#define FL_COUNT_INT_KEY(kk) \ + do { \ + int64_t __ck = (kk); \ + if (__ck >= 1 && __ck <= (1LL << 30)) { \ + nums[__ck == 1 ? 0 : 64 - __builtin_clzll((uint64_t) __ck - 1)]++; \ + total++; \ + } \ + } while (0) + for (i = 0; i < tbl->arr_size_; ++i) { + if (tbl->arr_[i].type_ != VAR_NIL) { + FL_COUNT_INT_KEY((int64_t) i + 1); + } + } if (tbl->bucket_count_ == 0) { for (i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].key.type_ == VAR_INT) { FL_COUNT_INT_KEY(tbl->quick_data_[i].key.data_.i); } + if (tbl->quick_data_[i].key.type_ == VAR_INT) { + FL_COUNT_INT_KEY(tbl->quick_data_[i].key.data_.i); + } } } else { for (i = 0; i < tbl->count_; ++i) { VarEntry *e = &tbl->nodes_[tbl->active_list_[i]].entry; - if (e->key.type_ == VAR_INT) { FL_COUNT_INT_KEY(e->key.data_.i); } + if (e->key.type_ == VAR_INT) { + FL_COUNT_INT_KEY(e->key.data_.i); + } } } FL_COUNT_INT_KEY(nk); @@ -1180,24 +1630,35 @@ __attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) uint64_t a = 0, optimal = 0, two = 1; for (i = 0; i < 31 && total > two / 2; ++i, two <<= 1) { a += nums[i]; - if (a > two / 2) { optimal = two; } + if (a > two / 2) { + optimal = two; + } + } + if (optimal <= (uint64_t) tbl->arr_size_) { + return false; } - if (optimal <= (uint64_t)tbl->arr_size_) { return false; } { uint32_t old_size = tbl->arr_size_; - CVar *na = (CVar *)FlFastAlloc(_S, (size_t)optimal * sizeof(CVar), !__fakelua_init_flag__); - if (old_size > 0) { memcpy(na, tbl->arr_, (size_t)old_size * sizeof(CVar)); } - memset(na + old_size, 0, (size_t)(optimal - old_size) * sizeof(CVar)); + CVar *na = (CVar *) FlFastAlloc(_S, (size_t) optimal * sizeof(CVar), !__fakelua_init_flag__); + if (old_size > 0) { + memcpy(na, tbl->arr_, (size_t) old_size * sizeof(CVar)); + } + memset(na + old_size, 0, (size_t) (optimal - old_size) * sizeof(CVar)); tbl->arr_ = na; - tbl->arr_cap_ = (uint32_t)optimal; - tbl->arr_size_ = (uint32_t)optimal; + tbl->arr_cap_ = (uint32_t) optimal; + tbl->arr_size_ = (uint32_t) optimal; if (tbl->count_ != 0) { uint64_t j; for (j = old_size + 1; j <= optimal && tbl->count_ != 0; ++j) { - CVar hv = FlHashGetInt(tbl, (int64_t)j); - if (hv.type_ == VAR_NIL) { continue; } - CVar key; key.type_ = VAR_INT; key.flag_ = 0; key.data_.i = (int64_t)j; - FlHashSet(tbl, key, (CVar){VAR_NIL}); + CVar hv = FlHashGetInt(tbl, (int64_t) j); + if (hv.type_ == VAR_NIL) { + continue; + } + CVar key; + key.type_ = VAR_INT; + key.flag_ = 0; + key.data_.i = (int64_t) j; + FlHashSet(tbl, key, (CVar) {VAR_NIL}); tbl->arr_[j - 1] = hv; } FlArrMigrate(tbl); @@ -1208,10 +1669,16 @@ __attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) /* 整数键 k 要插入已写满的哈希部分前调用;返回 true 表示 k 已写入数组或哈希空位,调用方直接返回。*/ static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v) { - if (k.type_ != VAR_INT || tbl->spec_set) { return false; } - if (!FlArrTryResize(tbl, k.data_.i)) { return false; } - if (FlArrSetInt(tbl, k.data_.i, v)) { return true; } - uint32_t h = (uint32_t)(k.data_.i ^ (k.data_.i >> 32)); + if (k.type_ != VAR_INT || tbl->spec_set) { + return false; + } + if (!FlArrTryResize(tbl, k.data_.i)) { + return false; + } + if (FlArrSetInt(tbl, k.data_.i, v)) { + return true; + } + uint32_t h = (uint32_t) (k.data_.i ^ (k.data_.i >> 32)); if (tbl->bucket_count_ == 0) { if (tbl->count_ < 8) { tbl->quick_data_[tbl->count_].key = k; @@ -1231,17 +1698,25 @@ static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v) { static inline void FlSetTableImpl(CVar t, CVar k, CVar v) { k = NORMALIZE_TABLE_KEY(k); - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } FL_CHECK_CONST_TABLE(t); - if (UNLIKELY(k.type_ == VAR_NIL)) { FakeluaThrowError(_S, "table index is nil"); } - if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { FakeluaThrowError(_S, "table index is NaN"); } + if (UNLIKELY(k.type_ == VAR_NIL)) { + FakeluaThrowError(_S, "table index is nil"); + } + if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { + FakeluaThrowError(_S, "table index is NaN"); + } VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_set)) { bool __finish = false; tbl->spec_set(tbl, k, v, &__finish); if (__finish) return; } - if (k.type_ == VAR_INT && FlArrSetInt(tbl, k.data_.i, v)) { return; } + if (k.type_ == VAR_INT && FlArrSetInt(tbl, k.data_.i, v)) { + return; + } FlHashSet(tbl, k, v); } @@ -1249,16 +1724,25 @@ static inline void FlSetTable(CVar t, CVar k, CVar v) { k = NORMALIZE_TABLE_KEY(k); FlSetTableImpl(t, k, v); if (LIKELY(t.type_ == VAR_TABLE)) { - if (k.type_ == VAR_INT) { FlSeqNoteIntSet(t.data_.t, k.data_.i, v.type_ == VAR_NIL); } + if (k.type_ == VAR_INT) { + FlSeqNoteIntSet(t.data_.t, k.data_.i, v.type_ == VAR_NIL); + } } } static inline CVar FlGetTableInt(CVar t, int64_t k) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } VarTable *tbl = t.data_.t; - if (LIKELY((uint64_t)k - 1 < (uint64_t)tbl->arr_size_)) { return tbl->arr_[k - 1]; } + if (LIKELY((uint64_t) k - 1 < (uint64_t) tbl->arr_size_)) { + return tbl->arr_[k - 1]; + } if (UNLIKELY(tbl->spec_get)) { - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; bool __finish = false; CVar __r = tbl->spec_get(tbl, key_cvar, &__finish); if (__finish) return __r; @@ -1267,18 +1751,28 @@ static inline CVar FlGetTableInt(CVar t, int64_t k) { } static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } FL_CHECK_CONST_TABLE(t); VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_set)) { - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; bool __finish = false; tbl->spec_set(tbl, key_cvar, v, &__finish); if (__finish) return; } - if (FlArrSetInt(tbl, k, v)) { return; } - uint32_t h = (uint32_t)(k ^ (k >> 32)); - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + if (FlArrSetInt(tbl, k, v)) { + return; + } + uint32_t h = (uint32_t) (k ^ (k >> 32)); + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; if (UNLIKELY(v.type_ == VAR_NIL)) { FlHashSet(tbl, key_cvar, v); return; @@ -1286,20 +1780,26 @@ static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v) { if (LIKELY(tbl->bucket_count_ == 0)) { for (uint32_t i = 0; i < tbl->count_; ++i) { if (tbl->quick_data_[i].hash == h && tbl->quick_data_[i].key.type_ == VAR_INT && tbl->quick_data_[i].key.data_.i == k) { - tbl->quick_data_[i].val = v; return; + tbl->quick_data_[i].val = v; + return; } } if (tbl->count_ < 8) { tbl->quick_data_[tbl->count_].key = key_cvar; tbl->quick_data_[tbl->count_].val = v; tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; return; + tbl->count_++; + return; + } + if (FlArrResizeInsert(tbl, key_cvar, v)) { + return; } - if (FlArrResizeInsert(tbl, key_cvar, v)) { return; } FlTableRehash(tbl); } if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - if (FlArrResizeInsert(tbl, key_cvar, v)) { return; } + if (FlArrResizeInsert(tbl, key_cvar, v)) { + return; + } FlTableRehash(tbl); } FlTableInsertRaw(tbl, key_cvar, v, h); @@ -1308,88 +1808,118 @@ static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v) { static inline void FlSetTableInt(CVar t, int64_t k, CVar v) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; + uint64_t idx = (uint64_t) k - 1; /* 新旧值都非 nil 时序列长度不变,可跳过缓存维护。*/ - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && v.type_ != VAR_NIL && tbl->arr_[idx].type_ != VAR_NIL)) { + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && v.type_ != VAR_NIL && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx] = v; return; } } FlSetTableIntImpl(t, k, v); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, v.type_ == VAR_NIL); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, v.type_ == VAR_NIL); + } } static inline void FlSetTableInt_Int(CVar t, int64_t k, int64_t v) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx].type_ = VAR_INT; tbl->arr_[idx].flag_ = 0; tbl->arr_[idx].data_.i = v; return; } } - CVar cv; cv.type_ = VAR_INT; cv.flag_ = 0; cv.data_.i = v; + CVar cv; + cv.type_ = VAR_INT; + cv.flag_ = 0; + cv.data_.i = v; FlSetTableIntImpl(t, k, cv); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, false); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, false); + } } static inline void FlSetTableInt_Float(CVar t, int64_t k, double v) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx].type_ = VAR_FLOAT; tbl->arr_[idx].flag_ = 0; tbl->arr_[idx].data_.f = v; return; } } - CVar cv; cv.type_ = VAR_FLOAT; cv.flag_ = 0; cv.data_.f = v; + CVar cv; + cv.type_ = VAR_FLOAT; + cv.flag_ = 0; + cv.data_.f = v; FlSetTableIntImpl(t, k, cv); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, false); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, false); + } } static inline void FlSetTableInt_Bool(CVar t, int64_t k, bool b) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx].type_ = VAR_BOOL; tbl->arr_[idx].flag_ = 0; tbl->arr_[idx].data_.b = b; return; } } - CVar cv; cv.type_ = VAR_BOOL; cv.flag_ = 0; cv.data_.b = b; + CVar cv; + cv.type_ = VAR_BOOL; + cv.flag_ = 0; + cv.data_.b = b; FlSetTableIntImpl(t, k, cv); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, false); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, false); + } } static inline CVar FlGetTableStrId(CVar t, int64_t str_id) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_get)) { - CVar k; k.type_ = VAR_STRINGID; k.flag_ = 0; k.data_.i = str_id; + CVar k; + k.type_ = VAR_STRINGID; + k.flag_ = 0; + k.data_.i = str_id; bool __finish = false; CVar __r = tbl->spec_get(tbl, k, &__finish); if (__finish) return __r; } // hash fallback - if (UNLIKELY(tbl->count_ == 0)) { return (CVar){VAR_NIL}; } - VarString *vs = (VarString *)str_id; + if (UNLIKELY(tbl->count_ == 0)) { + return (CVar) {VAR_NIL}; + } + VarString *vs = (VarString *) str_id; uint32_t h = LIKELY(vs->hash_ != 0) ? vs->hash_ : (vs->hash_ = FlHashString(vs->data_, vs->size_)); if (LIKELY(tbl->bucket_count_ == 0)) { for (uint32_t i = 0; i < tbl->count_; ++i) { if (tbl->quick_data_[i].hash == h) { CVar ek = tbl->quick_data_[i].key; if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { return tbl->quick_data_[i].val; } - VarString *evs = (VarString *)ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { return tbl->quick_data_[i].val; } + if (ek.data_.i == str_id) { + return tbl->quick_data_[i].val; + } + VarString *evs = (VarString *) ek.data_.i; + if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { + return tbl->quick_data_[i].val; + } } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { return tbl->quick_data_[i].val; } + if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { + return tbl->quick_data_[i].val; + } } } } @@ -1397,42 +1927,60 @@ static inline CVar FlGetTableStrId(CVar t, int64_t str_id) { uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return (CVar){VAR_NIL}; } + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return (CVar) {VAR_NIL}; + } while (1) { if (curr->entry.hash == h) { CVar ek = curr->entry.key; if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { return curr->entry.val; } - VarString *evs = (VarString *)ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { return curr->entry.val; } + if (ek.data_.i == str_id) { + return curr->entry.val; + } + VarString *evs = (VarString *) ek.data_.i; + if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { + return curr->entry.val; + } } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { return curr->entry.val; } + if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { + return curr->entry.val; + } } } uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { break; } + if (next == 0xFFFFFFFF) { + break; + } curr = &tbl->nodes_[next]; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } static inline void FlSetTableStrId(CVar t, int64_t str_id, CVar v) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } FL_CHECK_CONST_TABLE(t); VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_set)) { - CVar k; k.type_ = VAR_STRINGID; k.flag_ = 0; k.data_.i = str_id; + CVar k; + k.type_ = VAR_STRINGID; + k.flag_ = 0; + k.data_.i = str_id; bool __finish = false; tbl->spec_set(tbl, k, v, &__finish); if (__finish) return; } // hash fallback - VarString *vs = (VarString *)str_id; + VarString *vs = (VarString *) str_id; uint32_t h = LIKELY(vs->hash_ != 0) ? vs->hash_ : (vs->hash_ = FlHashString(vs->data_, vs->size_)); - CVar key_cvar; key_cvar.type_ = VAR_STRINGID; key_cvar.flag_ = 0; key_cvar.data_.i = str_id; + CVar key_cvar; + key_cvar.type_ = VAR_STRINGID; + key_cvar.flag_ = 0; + key_cvar.data_.i = str_id; if (UNLIKELY(v.type_ == VAR_NIL)) { - FlSetTable((CVar){.type_ = VAR_TABLE, .data_.t = tbl}, key_cvar, v); + FlSetTable((CVar) {.type_ = VAR_TABLE, .data_.t = tbl}, key_cvar, v); return; } if (LIKELY(tbl->bucket_count_ == 0)) { @@ -1440,11 +1988,20 @@ static inline void FlSetTableStrId(CVar t, int64_t str_id, CVar v) { if (tbl->quick_data_[i].hash == h) { CVar ek = tbl->quick_data_[i].key; if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { tbl->quick_data_[i].val = v; return; } - VarString *evs = (VarString *)ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { tbl->quick_data_[i].val = v; return; } + if (ek.data_.i == str_id) { + tbl->quick_data_[i].val = v; + return; + } + VarString *evs = (VarString *) ek.data_.i; + if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { + tbl->quick_data_[i].val = v; + return; + } } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { tbl->quick_data_[i].val = v; return; } + if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { + tbl->quick_data_[i].val = v; + return; + } } } } @@ -1452,11 +2009,14 @@ static inline void FlSetTableStrId(CVar t, int64_t str_id, CVar v) { tbl->quick_data_[tbl->count_].key = key_cvar; tbl->quick_data_[tbl->count_].val = v; tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; return; + tbl->count_++; + return; } FlTableRehash(tbl); } - if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { FlTableRehash(tbl); } + if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { + FlTableRehash(tbl); + } FlTableInsertRaw(tbl, key_cvar, v, h); } @@ -1471,135 +2031,222 @@ static inline void FlTableExpandMulti(CVar t, int64_t start_idx, CVar v) { } } - -#define CheckNum(v) do { \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ - FakeluaThrowError(_S, "attempt to perform arithmetic on non-numeric value"); \ - } \ -} while(0) - -#define CheckInt(v, result) do { \ - if (LIKELY((v).type_ == VAR_INT)) { (result) = (v).data_.i; } \ - else if (UNLIKELY((v).type_ == VAR_FLOAT)) { \ - if (!FlDoubleFitsInt64((v).data_.f, &(result))) { \ - FakeluaThrowError(_S, "number has no integer representation"); \ - } \ - } else { \ - FakeluaThrowError(_S, "attempt to perform bitwise operation on non-numeric value"); \ - (result) = 0; \ - } \ -} while(0) - -#define CVAR_TO_DOUBLE(v) ((v).type_ == VAR_INT ? (double)(v).data_.i : (v).data_.f) - -#define FL_INT_ADD(a, b) ((int64_t)((uint64_t)(int64_t)(a) + (uint64_t)(int64_t)(b))) -#define FL_INT_SUB(a, b) ((int64_t)((uint64_t)(int64_t)(a) - (uint64_t)(int64_t)(b))) -#define FL_INT_MUL(a, b) ((int64_t)((uint64_t)(int64_t)(a) * (uint64_t)(int64_t)(b))) - -#define OP_ARITH_IMPL(a, b, res, op) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, _ra.data_.i op _rb.data_.i); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) op CVAR_TO_DOUBLE(_rb)); } \ -} while(0) - -#define OpAdd(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, FL_INT_ADD(_ra.data_.i, _rb.data_.i)); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) + CVAR_TO_DOUBLE(_rb)); } \ -} while(0) -#define OpSub(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, FL_INT_SUB(_ra.data_.i, _rb.data_.i)); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) - CVAR_TO_DOUBLE(_rb)); } \ -} while(0) -#define OpMul(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, FL_INT_MUL(_ra.data_.i, _rb.data_.i)); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) * CVAR_TO_DOUBLE(_rb)); } \ -} while(0) - -#define OpDiv(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); CheckNum(_ra); CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb)); \ -} while(0) - -#define OpFloorDiv(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - int64_t _q; \ - FlFloorDivInt(_ra.data_.i, _rb.data_.i, _q); \ - SET_INT(res, _q); \ - } else { \ - CheckNum(_ra); CheckNum(_rb); \ - SET_FLOAT(res, floor(CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb))); \ - } \ -} while(0) - -#define OpPow(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); CheckNum(_ra); CheckNum(_rb); \ - SET_FLOAT(res, pow(CVAR_TO_DOUBLE(_ra), CVAR_TO_DOUBLE(_rb))); \ -} while(0) - -#define OpMod(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - int64_t _r; \ - FlModInt(_ra.data_.i, _rb.data_.i, _r); \ - SET_INT(res, _r); \ - } else { \ - CheckNum(_ra); CheckNum(_rb); \ - double _fa = CVAR_TO_DOUBLE(_ra); \ - double _fb = CVAR_TO_DOUBLE(_rb); \ - SET_FLOAT(res, _fa - _fb * floor(_fa / _fb)); \ - } \ -} while(0) - -#define OP_BIT_IMPL(a, b, res, op) do { int64_t _ai; int64_t _bi; CheckInt(a, _ai); CheckInt(b, _bi); SET_INT(res, _ai op _bi); } while(0) +#define CheckNum(v) \ + do { \ + if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ + FakeluaThrowError(_S, "attempt to perform arithmetic on non-numeric value"); \ + } \ + } while (0) + +#define CheckInt(v, result) \ + do { \ + if (LIKELY((v).type_ == VAR_INT)) { \ + (result) = (v).data_.i; \ + } else if (UNLIKELY((v).type_ == VAR_FLOAT)) { \ + if (!FlDoubleFitsInt64((v).data_.f, &(result))) { \ + FakeluaThrowError(_S, "number has no integer representation"); \ + } \ + } else { \ + FakeluaThrowError(_S, "attempt to perform bitwise operation on non-numeric value"); \ + (result) = 0; \ + } \ + } while (0) + +#define CVAR_TO_DOUBLE(v) ((v).type_ == VAR_INT ? (double) (v).data_.i : (v).data_.f) + +#define FL_INT_ADD(a, b) ((int64_t) ((uint64_t) (int64_t) (a) + (uint64_t) (int64_t) (b))) +#define FL_INT_SUB(a, b) ((int64_t) ((uint64_t) (int64_t) (a) - (uint64_t) (int64_t) (b))) +#define FL_INT_MUL(a, b) ((int64_t) ((uint64_t) (int64_t) (a) * (uint64_t) (int64_t) (b))) + +#define OP_ARITH_IMPL(a, b, res, op) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, _ra.data_.i op _rb.data_.i); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) op CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) + +#define OpAdd(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_ADD(_ra.data_.i, _rb.data_.i)); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) + CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) +#define OpSub(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_SUB(_ra.data_.i, _rb.data_.i)); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) - CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) +#define OpMul(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_MUL(_ra.data_.i, _rb.data_.i)); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) * CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) + +#define OpDiv(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb)); \ + } while (0) + +#define OpFloorDiv(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + int64_t _q; \ + FlFloorDivInt(_ra.data_.i, _rb.data_.i, _q); \ + SET_INT(res, _q); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, floor(CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb))); \ + } \ + } while (0) + +#define OpPow(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, pow(CVAR_TO_DOUBLE(_ra), CVAR_TO_DOUBLE(_rb))); \ + } while (0) + +#define OpMod(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + int64_t _r; \ + FlModInt(_ra.data_.i, _rb.data_.i, _r); \ + SET_INT(res, _r); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + double _fa = CVAR_TO_DOUBLE(_ra); \ + double _fb = CVAR_TO_DOUBLE(_rb); \ + SET_FLOAT(res, _fa - _fb * floor(_fa / _fb)); \ + } \ + } while (0) + +#define OP_BIT_IMPL(a, b, res, op) \ + do { \ + int64_t _ai; \ + int64_t _bi; \ + CheckInt(a, _ai); \ + CheckInt(b, _bi); \ + SET_INT(res, _ai op _bi); \ + } while (0) #define OpBitAnd(a, b, res) OP_BIT_IMPL(a, b, res, &) #define OpBitXor(a, b, res) OP_BIT_IMPL(a, b, res, ^) #define OpBitOr(a, b, res) OP_BIT_IMPL(a, b, res, |) -#define OP_SHIFT_IMPL(a, b, res, _right) do { \ - int64_t _ai; int64_t _bi; CheckInt(a, _ai); CheckInt(b, _bi); \ - if (_bi >= 64 || _bi <= -64) { SET_INT(res, 0); } \ - else if (_bi >= 0) { SET_INT(res, (int64_t)((_right) ? ((uint64_t)_ai >> _bi) : ((uint64_t)_ai << _bi))); } \ - else { SET_INT(res, (int64_t)((_right) ? ((uint64_t)_ai << (-_bi)) : ((uint64_t)_ai >> (-_bi)))); } \ -} while(0) +#define OP_SHIFT_IMPL(a, b, res, _right) \ + do { \ + int64_t _ai; \ + int64_t _bi; \ + CheckInt(a, _ai); \ + CheckInt(b, _bi); \ + if (_bi >= 64 || _bi <= -64) { \ + SET_INT(res, 0); \ + } else if (_bi >= 0) { \ + SET_INT(res, (int64_t) ((_right) ? ((uint64_t) _ai >> _bi) : ((uint64_t) _ai << _bi))); \ + } else { \ + SET_INT(res, (int64_t) ((_right) ? ((uint64_t) _ai << (-_bi)) : ((uint64_t) _ai >> (-_bi)))); \ + } \ + } while (0) #define OpRightShift(a, b, res) OP_SHIFT_IMPL(a, b, res, 1) #define OpLeftShift(a, b, res) OP_SHIFT_IMPL(a, b, res, 0) static inline bool FlCmpLt(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i < b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) < 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 < FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f < b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i < b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) < 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 < FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f < b.data_.f; + } } static inline bool FlCmpGt(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i > b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) > 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 > FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f > b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i > b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) > 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 > FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f > b.data_.f; + } } static inline bool FlCmpLe(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i <= b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) <= 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 <= FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f <= b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i <= b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) <= 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 <= FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f <= b.data_.f; + } } static inline bool FlCmpGe(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i >= b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) >= 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 >= FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f >= b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i >= b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) >= 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 >= FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f >= b.data_.f; + } } static inline bool FlCmpEq(CVar a, CVar b) { @@ -1623,25 +2270,49 @@ static inline bool FlCmpNe(CVar a, CVar b) { #define OpNe(a, b, res) SET_BOOL(res, FlCmpNe(a, b)) -#define OpNot(a, res) do { bool _bt; IsTrue(a, _bt); SET_BOOL(res, !_bt); } while(0) - -#define OpUnaryMinus(a, res) do { \ - CVar _ra = (a); \ - if (LIKELY(_ra.type_ == VAR_INT)) { SET_INT(res, FL_INT_SUB(0, _ra.data_.i)); } \ - else { CheckNum(_ra); SET_FLOAT(res, -_ra.data_.f); } \ -} while(0) - -#define OpBitNot(a, res) do { int64_t _ai; CheckInt(a, _ai); SET_INT(res, ~_ai); } while(0) - -#define OpLen(a, res) do { \ - CVar _lv = (a); \ - if (LIKELY(_lv.type_ == VAR_STRING)) { SET_INT(res, STR_SIZE(_lv.data_.s)); } \ - else if (_lv.type_ == VAR_STRINGID) { SET_INT(res, STR_SIZE((VarString *)_lv.data_.i)); } \ - else if (_lv.type_ == VAR_TABLE) { SET_INT(res, FlGetTableSeqLen(_lv.data_.t)); } \ - else { FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); } \ -} while(0) - -#define OpConcat(a, b, res) do { res = FlConcat(a, b); } while(0) +#define OpNot(a, res) \ + do { \ + bool _bt; \ + IsTrue(a, _bt); \ + SET_BOOL(res, !_bt); \ + } while (0) + +#define OpUnaryMinus(a, res) \ + do { \ + CVar _ra = (a); \ + if (LIKELY(_ra.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_SUB(0, _ra.data_.i)); \ + } else { \ + CheckNum(_ra); \ + SET_FLOAT(res, -_ra.data_.f); \ + } \ + } while (0) + +#define OpBitNot(a, res) \ + do { \ + int64_t _ai; \ + CheckInt(a, _ai); \ + SET_INT(res, ~_ai); \ + } while (0) + +#define OpLen(a, res) \ + do { \ + CVar _lv = (a); \ + if (LIKELY(_lv.type_ == VAR_STRING)) { \ + SET_INT(res, STR_SIZE(_lv.data_.s)); \ + } else if (_lv.type_ == VAR_STRINGID) { \ + SET_INT(res, STR_SIZE((VarString *) _lv.data_.i)); \ + } else if (_lv.type_ == VAR_TABLE) { \ + SET_INT(res, FlGetTableSeqLen(_lv.data_.t)); \ + } else { \ + FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); \ + } \ + } while (0) + +#define OpConcat(a, b, res) \ + do { \ + res = FlConcat(a, b); \ + } while (0) static inline int FlFastItoA(int64_t v, char *buf) { if (v == 0) { @@ -1654,12 +2325,12 @@ static inline int FlFastItoA(int64_t v, char *buf) { uint64_t u; if (v < 0) { neg = true; - u = 0ULL - (uint64_t)v; + u = 0ULL - (uint64_t) v; } else { - u = (uint64_t)v; + u = (uint64_t) v; } while (u > 0) { - tmp[i++] = (char)('0' + (u % 10)); + tmp[i++] = (char) ('0' + (u % 10)); u /= 10; } int len = 0; @@ -1674,9 +2345,22 @@ static inline int FlFastItoA(int64_t v, char *buf) { static inline int FlVarToStr(CVar v, char *buf, int buf_size) { switch (v.type_) { - case VAR_NIL: memcpy(buf, "nil", 3); buf[3] = '\0'; return 3; - case VAR_BOOL: if (v.data_.b) { memcpy(buf, "true", 4); buf[4] = '\0'; return 4; } else { memcpy(buf, "false", 5); buf[5] = '\0'; return 5; } - case VAR_INT: return FlFastItoA(v.data_.i, buf); + case VAR_NIL: + memcpy(buf, "nil", 3); + buf[3] = '\0'; + return 3; + case VAR_BOOL: + if (v.data_.b) { + memcpy(buf, "true", 4); + buf[4] = '\0'; + return 4; + } else { + memcpy(buf, "false", 5); + buf[5] = '\0'; + return 5; + } + case VAR_INT: + return FlFastItoA(v.data_.i, buf); case VAR_FLOAT: { /* Lua 5.4 tostringbuff: %.14g,结果只含数字和符号时补 .0 */ int flen = snprintf(buf, buf_size, "%.14g", v.data_.f); @@ -1695,37 +2379,82 @@ static inline int FlVarToStr(CVar v, char *buf, int buf_size) { return flen; } case VAR_STRING: - case VAR_STRINGID: FakeluaThrowError(_S, "FlVarToStr: string type should be handled by caller"); return 0; - case VAR_TABLE: { int __n = snprintf(buf, buf_size - 1, "table(0x%llx", (unsigned long long)(uintptr_t)v.data_.t); if (__n > 0 && __n < buf_size - 1) { buf[__n] = ')'; buf[__n + 1] = '\0'; return __n + 1; } return __n; } - case VAR_CLOSURE: { int __n = snprintf(buf, buf_size - 1, "function(0x%llx", (unsigned long long)(uintptr_t)v.data_.cl); if (__n > 0 && __n < buf_size - 1) { buf[__n] = ')'; buf[__n + 1] = '\0'; return __n + 1; } return __n; } - default: return 0; + case VAR_STRINGID: + FakeluaThrowError(_S, "FlVarToStr: string type should be handled by caller"); + return 0; + case VAR_TABLE: { + int __n = snprintf(buf, buf_size - 1, "table(0x%llx", (unsigned long long) (uintptr_t) v.data_.t); + if (__n > 0 && __n < buf_size - 1) { + buf[__n] = ')'; + buf[__n + 1] = '\0'; + return __n + 1; + } + return __n; + } + case VAR_CLOSURE: { + int __n = snprintf(buf, buf_size - 1, "function(0x%llx", (unsigned long long) (uintptr_t) v.data_.cl); + if (__n > 0 && __n < buf_size - 1) { + buf[__n] = ')'; + buf[__n + 1] = '\0'; + return __n + 1; + } + return __n; + } + default: + return 0; } } // Lua 的 .. 只接受字符串和数字,nil / bool / table / function 一律报错。 // 检查放在非字符串分支里,字符串拼接的快路径不受影响。 -#define CONCAT_CHECK_NUMERIC(v) \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { FakeluaThrowError(_S, "attempt to concatenate a non-string value"); } +#define CONCAT_CHECK_NUMERIC(v) \ + if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ + FakeluaThrowError(_S, "attempt to concatenate a non-string value"); \ + } static inline CVar FlConcat(CVar a, CVar b) { char buf_a[64], buf_b[64]; - const char *sa = buf_a; int la; - const char *sb = buf_b; int lb; - if (LIKELY(a.type_ == VAR_STRING)) { sa = STR_DATA(a.data_.s); la = STR_SIZE(a.data_.s); } - else if (a.type_ == VAR_STRINGID) { VarString *vs = (VarString *)a.data_.i; sa = STR_DATA(vs); la = STR_SIZE(vs); } - else if (a.type_ == VAR_INT) { la = FlFastItoA(a.data_.i, buf_a); } - else if (a.type_ == VAR_FLOAT) { la = FlVarToStr(a, buf_a, sizeof(buf_a)); } - else { FakeluaThrowError(_S, "attempt to concatenate a non-string value"); return (CVar){VAR_NIL}; } - - if (LIKELY(b.type_ == VAR_STRING)) { sb = STR_DATA(b.data_.s); lb = STR_SIZE(b.data_.s); } - else if (b.type_ == VAR_STRINGID) { VarString *vs = (VarString *)b.data_.i; sb = STR_DATA(vs); lb = STR_SIZE(vs); } - else if (b.type_ == VAR_INT) { lb = FlFastItoA(b.data_.i, buf_b); } - else if (b.type_ == VAR_FLOAT) { lb = FlVarToStr(b, buf_b, sizeof(buf_b)); } - else { FakeluaThrowError(_S, "attempt to concatenate a non-string value"); return (CVar){VAR_NIL}; } + const char *sa = buf_a; + int la; + const char *sb = buf_b; + int lb; + if (LIKELY(a.type_ == VAR_STRING)) { + sa = STR_DATA(a.data_.s); + la = STR_SIZE(a.data_.s); + } else if (a.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) a.data_.i; + sa = STR_DATA(vs); + la = STR_SIZE(vs); + } else if (a.type_ == VAR_INT) { + la = FlFastItoA(a.data_.i, buf_a); + } else if (a.type_ == VAR_FLOAT) { + la = FlVarToStr(a, buf_a, sizeof(buf_a)); + } else { + FakeluaThrowError(_S, "attempt to concatenate a non-string value"); + return (CVar) {VAR_NIL}; + } + + if (LIKELY(b.type_ == VAR_STRING)) { + sb = STR_DATA(b.data_.s); + lb = STR_SIZE(b.data_.s); + } else if (b.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) b.data_.i; + sb = STR_DATA(vs); + lb = STR_SIZE(vs); + } else if (b.type_ == VAR_INT) { + lb = FlFastItoA(b.data_.i, buf_b); + } else if (b.type_ == VAR_FLOAT) { + lb = FlVarToStr(b, buf_b, sizeof(buf_b)); + } else { + FakeluaThrowError(_S, "attempt to concatenate a non-string value"); + return (CVar) {VAR_NIL}; + } int total = la + lb; - if (UNLIKELY(total < la || total < lb)) { FakeluaThrowError(_S, "string concatenation result too long"); } - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); + if (UNLIKELY(total < la || total < lb)) { + FakeluaThrowError(_S, "string concatenation result too long"); + } + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); vs->size_ = total; vs->hash_ = 0; memcpy(vs->data_, sa, la); @@ -1742,10 +2471,10 @@ static inline CVar FlStringChar1(int64_t c) { if (UNLIKELY(c < 0 || c > 255)) { FakeluaThrowError(_S, "bad argument to string.char: value out of range"); } - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + 1, !__fakelua_init_flag__); + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + 1, !__fakelua_init_flag__); vs->size_ = 1; vs->hash_ = 0; - vs->data_[0] = (char)c; + vs->data_[0] = (char) c; CVar r; r.type_ = VAR_STRING; r.flag_ = 0; @@ -1757,34 +2486,54 @@ static inline CVar FlStringChar1(int64_t c) { static inline CVar FlStringLower(CVar s) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.lower", 1, s); } - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.lower", 1, s); + } + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); out->size_ = len; out->hash_ = 0; for (int i = 0; i < len; ++i) { - unsigned char c = (unsigned char)src[i]; - out->data_[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; + unsigned char c = (unsigned char) src[i]; + out->data_[i] = (c >= 'A' && c <= 'Z') ? (char) (c + 32) : (char) c; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } static inline CVar FlStringUpper(CVar s) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.upper", 1, s); } - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.upper", 1, s); + } + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); out->size_ = len; out->hash_ = 0; for (int i = 0; i < len; ++i) { - unsigned char c = (unsigned char)src[i]; - out->data_[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : (char)c; + unsigned char c = (unsigned char) src[i]; + out->data_[i] = (c >= 'a' && c <= 'z') ? (char) (c - 32) : (char) c; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } @@ -1792,32 +2541,52 @@ static inline CVar FlStringUpper(CVar s) { static inline CVar FlStringByte1(CVar s, int64_t i) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.byte", 2, s, (CVar){.type_ = VAR_INT, .data_.i = i}); } + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.byte", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = i}); + } if (i < 0) i = len + i + 1; - if (i < 1 || i > len) return (CVar){VAR_NIL}; - return (CVar){.type_ = VAR_INT, .data_.i = (unsigned char)src[i - 1]}; + if (i < 1 || i > len) return (CVar) {VAR_NIL}; + return (CVar) {.type_ = VAR_INT, .data_.i = (unsigned char) src[i - 1]}; } // string.rep(s, n):s 重复 n 次(无分隔符,最常见形式) static inline CVar FlStringRep(CVar s, int64_t n) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar){.type_ = VAR_INT, .data_.i = n}); } + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = n}); + } if (n <= 0 || len == 0) { - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString), !__fakelua_init_flag__); - vs->size_ = 0; vs->hash_ = 0; vs->data_[0] = '\0'; - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = vs; return r; + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString), !__fakelua_init_flag__); + vs->size_ = 0; + vs->hash_ = 0; + vs->data_[0] = '\0'; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = vs; + return r; } // 防溢出:总长度超过 1G 回退 native。(n, len) 均转为 uint64_t 避免截断 - if ((uint64_t)n > (1073741824ULL / (uint64_t)len)) { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar){.type_ = VAR_INT, .data_.i = n}); + if ((uint64_t) n > (1073741824ULL / (uint64_t) len)) { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = n}); } - int64_t total = (int64_t)len * n; - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); + int64_t total = (int64_t) len * n; + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); out->size_ = total; out->hash_ = 0; char *dst = out->data_; @@ -1825,7 +2594,10 @@ static inline CVar FlStringRep(CVar s, int64_t n) { memcpy(dst, src, len); dst += len; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } @@ -1833,16 +2605,26 @@ static inline CVar FlStringRep(CVar s, int64_t n) { static inline CVar FlStringReverse(CVar s) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.reverse", 1, s); } - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.reverse", 1, s); + } + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); out->size_ = len; out->hash_ = 0; for (int i = 0; i < len; ++i) { out->data_[i] = src[len - 1 - i]; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } @@ -1850,34 +2632,51 @@ static inline CVar FlStringReverse(CVar s) { static inline CVar FlStringFindPlain(CVar s, CVar pat) { const char *src = 0; int slen = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); slen = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); slen = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar){.type_ = VAR_INT, .data_.i = 1}, (CVar){.type_ = VAR_BOOL, .data_.b = true}); } + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + slen = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + slen = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar) {.type_ = VAR_INT, .data_.i = 1}, (CVar) {.type_ = VAR_BOOL, .data_.b = true}); + } const char *psrc = 0; int plen = 0; - if (LIKELY(pat.type_ == VAR_STRING)) { psrc = STR_DATA(pat.data_.s); plen = STR_SIZE(pat.data_.s); } - else if (pat.type_ == VAR_STRINGID) { VarString *vs = (VarString *)pat.data_.i; psrc = STR_DATA(vs); plen = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar){.type_ = VAR_INT, .data_.i = 1}, (CVar){.type_ = VAR_BOOL, .data_.b = true}); } - if (plen == 0) return (CVar){.type_ = VAR_INT, .data_.i = 1}; - if (plen > slen) return (CVar){VAR_NIL}; + if (LIKELY(pat.type_ == VAR_STRING)) { + psrc = STR_DATA(pat.data_.s); + plen = STR_SIZE(pat.data_.s); + } else if (pat.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) pat.data_.i; + psrc = STR_DATA(vs); + plen = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar) {.type_ = VAR_INT, .data_.i = 1}, (CVar) {.type_ = VAR_BOOL, .data_.b = true}); + } + if (plen == 0) return (CVar) {.type_ = VAR_INT, .data_.i = 1}; + if (plen > slen) return (CVar) {VAR_NIL}; int limit = slen - plen; for (int i = 0; i <= limit; ++i) { if (src[i] == psrc[0] && memcmp(src + i, psrc, plen) == 0) { - return (CVar){.type_ = VAR_INT, .data_.i = i + 1}; + return (CVar) {.type_ = VAR_INT, .data_.i = i + 1}; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } // string.format("%d", int) static inline CVar FlFormatInt(int64_t v) { char buf[32]; int n = FlFastItoA(v, buf); - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + n, !__fakelua_init_flag__); + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + n, !__fakelua_init_flag__); vs->size_ = n; vs->hash_ = 0; - memcpy(vs->data_, buf, (size_t)n); - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = vs; + memcpy(vs->data_, buf, (size_t) n); + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = vs; return r; } @@ -1886,24 +2685,24 @@ static inline CVar FlFormatInt(int64_t v) { // 其它单个值是 1 个值。 static inline CVar FlSelectHash(CVar vararg) { if (vararg.type_ == VAR_MULTI) { - if (vararg.data_.m == NULL) return (CVar){.type_ = VAR_INT, .data_.i = 0}; - return (CVar){.type_ = VAR_INT, .data_.i = (int64_t)vararg.data_.m->count}; + if (vararg.data_.m == NULL) return (CVar) {.type_ = VAR_INT, .data_.i = 0}; + return (CVar) {.type_ = VAR_INT, .data_.i = (int64_t) vararg.data_.m->count}; } - if (vararg.type_ == VAR_NIL) return (CVar){.type_ = VAR_INT, .data_.i = 0}; - return (CVar){.type_ = VAR_INT, .data_.i = 1}; + if (vararg.type_ == VAR_NIL) return (CVar) {.type_ = VAR_INT, .data_.i = 0}; + return (CVar) {.type_ = VAR_INT, .data_.i = 1}; } static inline CVar FlSelectIndex(int64_t idx, CVar vararg) { if (vararg.type_ == VAR_MULTI) { VarMulti *m = vararg.data_.m; - int64_t count = (m != NULL) ? (int64_t)m->count : 0; + int64_t count = (m != NULL) ? (int64_t) m->count : 0; if (idx < 0) { if (idx < -count - 1) FakeluaThrowError(_S, "bad argument #1 to 'select': index out of range"); idx = count + idx + 1; } if (idx < 1) FakeluaThrowError(_S, "bad argument #1 to 'select': index out of range"); if (idx > count || m == NULL) return FlAllocMulti(_S, 0); - uint32_t start = (uint32_t)(idx - 1); + uint32_t start = (uint32_t) (idx - 1); uint32_t nret = m->count - start; if (nret == 1) return m->vars[start]; return FlSliceMulti(_S, vararg, start); @@ -1933,12 +2732,24 @@ static inline CVar FlTonumber(CVar v) { if (LIKELY(v.type_ == VAR_INT || v.type_ == VAR_FLOAT)) return v; const char *p = 0; int len = 0; - if (LIKELY(v.type_ == VAR_STRING)) { p = STR_DATA(v.data_.s); len = STR_SIZE(v.data_.s); } - else if (v.type_ == VAR_STRINGID) { VarString *vs = (VarString *)v.data_.i; p = STR_DATA(vs); len = STR_SIZE(vs); } - else { return (CVar){VAR_NIL}; } - while (len > 0 && FlIsSpaceByte((unsigned char)p[0])) { ++p; --len; } - while (len > 0 && FlIsSpaceByte((unsigned char)p[len - 1])) { --len; } - if (len <= 0) return (CVar){VAR_NIL}; + if (LIKELY(v.type_ == VAR_STRING)) { + p = STR_DATA(v.data_.s); + len = STR_SIZE(v.data_.s); + } else if (v.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) v.data_.i; + p = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return (CVar) {VAR_NIL}; + } + while (len > 0 && FlIsSpaceByte((unsigned char) p[0])) { + ++p; + --len; + } + while (len > 0 && FlIsSpaceByte((unsigned char) p[len - 1])) { + --len; + } + if (len <= 0) return (CVar) {VAR_NIL}; // 0x 十六进制等复杂形式走 native if (len >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); @@ -1948,17 +2759,24 @@ static inline CVar FlTonumber(CVar v) { } int neg = 0; int i = 0; - if (p[0] == '+') { i = 1; } - else if (p[0] == '-') { neg = 1; i = 1; } - if (i >= len) return (CVar){VAR_NIL}; + if (p[0] == '+') { + i = 1; + } else if (p[0] == '-') { + neg = 1; + i = 1; + } + if (i >= len) return (CVar) {VAR_NIL}; // 含小数点/指数 → native - { int j; for (j = i; j < len; ++j) { - unsigned char c = (unsigned char)p[j]; - if (c == '.' || c == 'e' || c == 'E') { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); + { + int j; + for (j = i; j < len; ++j) { + unsigned char c = (unsigned char) p[j]; + if (c == '.' || c == 'e' || c == 'E') { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); + } + if (c < '0' || c > '9') return (CVar) {VAR_NIL}; } - if (c < '0' || c > '9') return (CVar){VAR_NIL}; - } } + } int64_t val = 0; for (; i < len; ++i) { // 溢出检查:val * 10 + digit > INT64_MAX 时回退到 native(返回 float) @@ -1970,15 +2788,19 @@ static inline CVar FlTonumber(CVar v) { val = val * 10 + digit; } if (neg) val = -val; - return (CVar){.type_ = VAR_INT, .data_.i = val}; + return (CVar) {.type_ = VAR_INT, .data_.i = val}; } // 空表预扩到至少 need 个桶(反复 FlTableRehash,空表拷贝成本可忽略) static inline void FlEnsureTableBuckets(VarTable *tbl, uint32_t need) { if (need < 1) need = 1; uint32_t target = need - 1; - target |= target >> 1; target |= target >> 2; target |= target >> 4; - target |= target >> 8; target |= target >> 16; target += 1; + target |= target >> 1; + target |= target >> 2; + target |= target >> 4; + target |= target >> 8; + target |= target >> 16; + target += 1; while (tbl->bucket_count_ < target) { FlTableRehash(tbl); if (UNLIKELY(tbl->bucket_count_ == 0)) break; @@ -1988,12 +2810,16 @@ static inline void FlEnsureTableBuckets(VarTable *tbl, uint32_t need) { // 空表预扩容的节点数组必须能一次分配出来(与 native_table.cpp 的 MovePresizeFits 一致)。 static inline int FlMovePresizeFits(uint64_t count) { if (count == 0 || count > 0xFFFFFFFFULL) return 0; - uint32_t min_buckets = (uint32_t)count; + uint32_t min_buckets = (uint32_t) count; uint32_t buckets = min_buckets - 1; - buckets |= buckets >> 1; buckets |= buckets >> 2; buckets |= buckets >> 4; - buckets |= buckets >> 8; buckets |= buckets >> 16; buckets += 1; + buckets |= buckets >> 1; + buckets |= buckets >> 2; + buckets |= buckets >> 4; + buckets |= buckets >> 8; + buckets |= buckets >> 16; + buckets += 1; if (buckets < min_buckets) return 0; - uint64_t total = (uint64_t)buckets + (uint64_t)(buckets / 2); + uint64_t total = (uint64_t) buckets + (uint64_t) (buckets / 2); uint64_t bytes = total * sizeof(TableNode) + total * sizeof(uint32_t); return bytes <= (1024ULL * 1024ULL); } @@ -2006,30 +2832,30 @@ static inline CVar FlTableMove(CVar src, int64_t f, int64_t e, int64_t t, CVar d } FL_CHECK_CONST_TABLE(dst); if (e >= f) { - uint64_t count = (uint64_t)e - (uint64_t)f + 1; - if (UNLIKELY(count == 0 || count > (uint64_t)INT64_MAX)) { + uint64_t count = (uint64_t) e - (uint64_t) f + 1; + if (UNLIKELY(count == 0 || count > (uint64_t) INT64_MAX)) { FakeluaThrowError(_S, "bad argument #3 to 'table.move' (too many elements to move)"); } uint64_t last_off = count - 1; - if (UNLIKELY(t > 0 && (uint64_t)t > (uint64_t)INT64_MAX - last_off)) { + if (UNLIKELY(t > 0 && (uint64_t) t > (uint64_t) INT64_MAX - last_off)) { FakeluaThrowError(_S, "bad argument #4 to 'table.move' (destination wrap around)"); } - int64_t icount = (int64_t)count; + int64_t icount = (int64_t) count; int same = (src.data_.t == dst.data_.t); - if (!same && dst.data_.t->count_ == 0 && icount > 8 && t != (int64_t)dst.data_.t->arr_size_ + 1 && FlMovePresizeFits(count)) { - FlEnsureTableBuckets(dst.data_.t, (uint32_t)icount); + if (!same && dst.data_.t->count_ == 0 && icount > 8 && t != (int64_t) dst.data_.t->arr_size_ + 1 && FlMovePresizeFits(count)) { + FlEnsureTableBuckets(dst.data_.t, (uint32_t) icount); } if (!same || t <= f || t > e) { int64_t i; for (i = 0; i < icount; ++i) { - CVar val = FlGetTableInt(src, (int64_t)((uint64_t)f + (uint64_t)i)); - FlSetTableInt(dst, (int64_t)((uint64_t)t + (uint64_t)i), val); + CVar val = FlGetTableInt(src, (int64_t) ((uint64_t) f + (uint64_t) i)); + FlSetTableInt(dst, (int64_t) ((uint64_t) t + (uint64_t) i), val); } } else { int64_t i; for (i = icount - 1; i >= 0; --i) { - CVar val = FlGetTableInt(src, (int64_t)((uint64_t)f + (uint64_t)i)); - FlSetTableInt(dst, (int64_t)((uint64_t)t + (uint64_t)i), val); + CVar val = FlGetTableInt(src, (int64_t) ((uint64_t) f + (uint64_t) i)); + FlSetTableInt(dst, (int64_t) ((uint64_t) t + (uint64_t) i), val); } } } @@ -2039,80 +2865,98 @@ static inline CVar FlTableMove(CVar src, int64_t f, int64_t e, int64_t t, CVar d // table.unpack 内联实现(完整对齐 Lua 5.4 区间回绕与单块分配上限检查) static inline CVar FlTableUnpack(CVar tbl, int64_t start_i, int64_t end_j) { if (start_i > end_j) return FlAllocMulti(_S, 0); - uint64_t count64 = (uint64_t)end_j - (uint64_t)start_i + 1; + uint64_t count64 = (uint64_t) end_j - (uint64_t) start_i + 1; uint64_t max_unpack = (1024ULL * 1024ULL - sizeof(VarMulti)) / sizeof(CVar); if (UNLIKELY(count64 == 0 || count64 > 2147483647ULL || count64 > max_unpack)) { FakeluaThrowError(_S, "too many results to unpack"); } - uint32_t count = (uint32_t)count64; + uint32_t count = (uint32_t) count64; CVar multi = FlAllocMulti(_S, count); uint32_t i; for (i = 0; i < count; ++i) { - multi.data_.m->vars[i] = FlGetTableInt(tbl, (int64_t)((uint64_t)start_i + (uint64_t)i)); + multi.data_.m->vars[i] = FlGetTableInt(tbl, (int64_t) ((uint64_t) start_i + (uint64_t) i)); } return multi; } -#define GET_TABLE_ENTRY(tbl, idx, k, v) do { \ - VarTable *__gt = (tbl).data_.t; \ - uint32_t __gi = (idx); \ - uint32_t __spec_cnt = __gt->spec_count; \ - if (LIKELY(__gi < __spec_cnt)) { \ - (k) = __gt->spec_keys[__gi]; \ - (v) = __gt->spec_vals[__gi]; \ - } else { \ - __gi -= __spec_cnt; \ - if (__gi < __gt->arr_size_) { \ - (k).type_ = VAR_INT; (k).flag_ = 0; (k).data_.i = (int64_t)__gi + 1; \ - (v) = __gt->arr_[__gi]; \ - } else { \ - __gi -= __gt->arr_size_; \ - if (UNLIKELY(__gi >= __gt->count_)) { \ - (k).type_ = VAR_NIL; (v).type_ = VAR_NIL; \ - } else if (LIKELY(__gt->bucket_count_ == 0)) { \ - (k) = __gt->quick_data_[__gi].key; \ - (v) = __gt->quick_data_[__gi].val; \ - } else { \ - uint32_t __gti_node_idx = __gt->active_list_[__gi]; \ - (k) = __gt->nodes_[__gti_node_idx].entry.key; \ - (v) = __gt->nodes_[__gti_node_idx].entry.val; \ - } \ - } \ - } \ -} while(0) - -#define FlFloorDivQuotient(a, b, q) do { \ - if (UNLIKELY((b) == (int64_t)-1)) { \ - (q) = (int64_t)((uint64_t)0 - (uint64_t)(int64_t)(a)); \ - } else { \ - (q) = (a) / (b); \ - if (((a) ^ (b)) < 0 && (a) % (b) != 0) { (q) -= 1; } \ - } \ -} while(0) - -#define FlFloorDivInt(a, b, result) do { \ - int64_t __fl_a = (a); int64_t __fl_b = (b); \ - if (UNLIKELY(__fl_b == 0)) { FakeluaThrowError(_S, "floor division by zero"); } \ - int64_t __fl_q; \ - FlFloorDivQuotient(__fl_a, __fl_b, __fl_q); \ - (result) = __fl_q; \ -} while(0) - -#define FlModInt(a, b, result) do { \ - int64_t __fm_a = (a); int64_t __fm_b = (b); \ - if (UNLIKELY(__fm_b == 0)) { FakeluaThrowError(_S, "modulo by zero"); } \ - if (UNLIKELY(__fm_b == (int64_t)-1)) { (result) = 0; } \ - else { \ - int64_t __fm_q; \ - FlFloorDivQuotient(__fm_a, __fm_b, __fm_q); \ - (result) = __fm_a - __fm_b * __fm_q; \ - } \ -} while(0) - -#define FlModFloat(a, b, result) do { \ - double __fmf_a = (a); double __fmf_b = (b); \ - (result) = __fmf_a - __fmf_b * floor(__fmf_a / __fmf_b); \ -} while(0) +#define GET_TABLE_ENTRY(tbl, idx, k, v) \ + do { \ + VarTable *__gt = (tbl).data_.t; \ + uint32_t __gi = (idx); \ + uint32_t __spec_cnt = __gt->spec_count; \ + if (LIKELY(__gi < __spec_cnt)) { \ + (k) = __gt->spec_keys[__gi]; \ + (v) = __gt->spec_vals[__gi]; \ + } else { \ + __gi -= __spec_cnt; \ + if (__gi < __gt->arr_size_) { \ + (k).type_ = VAR_INT; \ + (k).flag_ = 0; \ + (k).data_.i = (int64_t) __gi + 1; \ + (v) = __gt->arr_[__gi]; \ + } else { \ + __gi -= __gt->arr_size_; \ + if (UNLIKELY(__gi >= __gt->count_)) { \ + (k).type_ = VAR_NIL; \ + (v).type_ = VAR_NIL; \ + } else if (LIKELY(__gt->bucket_count_ == 0)) { \ + (k) = __gt->quick_data_[__gi].key; \ + (v) = __gt->quick_data_[__gi].val; \ + } else { \ + uint32_t __gti_node_idx = __gt->active_list_[__gi]; \ + (k) = __gt->nodes_[__gti_node_idx].entry.key; \ + (v) = __gt->nodes_[__gti_node_idx].entry.val; \ + } \ + } \ + } \ + } while (0) + +#define FlFloorDivQuotient(a, b, q) \ + do { \ + if (UNLIKELY((b) == (int64_t) -1)) { \ + (q) = (int64_t) ((uint64_t) 0 - (uint64_t) (int64_t) (a)); \ + } else { \ + (q) = (a) / (b); \ + if (((a) ^ (b)) < 0 && (a) % (b) != 0) { \ + (q) -= 1; \ + } \ + } \ + } while (0) + +#define FlFloorDivInt(a, b, result) \ + do { \ + int64_t __fl_a = (a); \ + int64_t __fl_b = (b); \ + if (UNLIKELY(__fl_b == 0)) { \ + FakeluaThrowError(_S, "floor division by zero"); \ + } \ + int64_t __fl_q; \ + FlFloorDivQuotient(__fl_a, __fl_b, __fl_q); \ + (result) = __fl_q; \ + } while (0) + +#define FlModInt(a, b, result) \ + do { \ + int64_t __fm_a = (a); \ + int64_t __fm_b = (b); \ + if (UNLIKELY(__fm_b == 0)) { \ + FakeluaThrowError(_S, "modulo by zero"); \ + } \ + if (UNLIKELY(__fm_b == (int64_t) -1)) { \ + (result) = 0; \ + } else { \ + int64_t __fm_q; \ + FlFloorDivQuotient(__fm_a, __fm_b, __fm_q); \ + (result) = __fm_a - __fm_b * __fm_q; \ + } \ + } while (0) + +#define FlModFloat(a, b, result) \ + do { \ + double __fmf_a = (a); \ + double __fmf_b = (b); \ + (result) = __fmf_a - __fmf_b * floor(__fmf_a / __fmf_b); \ + } while (0) // Lua 5.4 pushnumint:d 能无损落进 int64 时返回 integer,否则保留 float // (inf/-inf/nan/超出 [-2^63, 2^63) 均不报错,仍是 float)。 @@ -2120,7 +2964,7 @@ static inline CVar FlPushNumInt(double d) { CVar r; if (isfinite(d) && d >= -9223372036854775808.0 && d < 9223372036854775808.0) { r.type_ = VAR_INT; - r.data_.i = (int64_t)d; + r.data_.i = (int64_t) d; } else { r.type_ = VAR_FLOAT; r.data_.f = d; @@ -2130,12 +2974,12 @@ static inline CVar FlPushNumInt(double d) { static inline CVar FlMathFloor(CVar v) { if (v.type_ == VAR_INT) return v; - return FlPushNumInt(floor((double)v.data_.f)); + return FlPushNumInt(floor((double) v.data_.f)); } static inline CVar FlMathCeil(CVar v) { if (v.type_ == VAR_INT) return v; - return FlPushNumInt(ceil((double)v.data_.f)); + return FlPushNumInt(ceil((double) v.data_.f)); } // Lua 5.4 math.fmod:两整数走整数取模(0 报错、-1 特判为 0 避开溢出), @@ -2168,36 +3012,36 @@ static inline CVar FlMathModf(CVar v) { r = FlAllocMulti(_S, 2); if (v.type_ == VAR_INT) { r.data_.m->vars[0] = v; - r.data_.m->vars[1] = (CVar){.type_ = VAR_FLOAT, .data_.f = 0.0}; + r.data_.m->vars[1] = (CVar) {.type_ = VAR_FLOAT, .data_.f = 0.0}; return r; } - n = (double)v.data_.f; + n = (double) v.data_.f; ip = (n < 0) ? ceil(n) : floor(n); fp = (n == ip) ? 0.0 : n - ip; r.data_.m->vars[0] = FlPushNumInt(ip); - r.data_.m->vars[1] = (CVar){.type_ = VAR_FLOAT, .data_.f = fp}; + r.data_.m->vars[1] = (CVar) {.type_ = VAR_FLOAT, .data_.f = fp}; return r; } static inline double FlMathLdexp(double value, int64_t exponent) { if (exponent > 2147483647LL) exponent = 2147483647LL; if (exponent < (-2147483647LL - 1)) exponent = (-2147483647LL - 1); - return ldexp(value, (int)exponent); + return ldexp(value, (int) exponent); } static inline int64_t FlRandomInt64(void) { uint64_t value = 0; for (int i = 0; i < 5; ++i) { - value = (value << 15) | (uint64_t)(rand() & 0x7fff); + value = (value << 15) | (uint64_t) (rand() & 0x7fff); } - return (int64_t)value; + return (int64_t) value; } // 整数 for 步进:用无符号加法探测有符号溢出。溢出返回 0 且不改 *ctrl //(避免 maxinteger++ 变成 mininteger 后死循环)。continue 仍走 for 的 incr 子句。 static inline int FlForIntAdvance(int64_t *ctrl, int64_t step) { int64_t cur = *ctrl; - int64_t next = (int64_t)((uint64_t)cur + (uint64_t)step); + int64_t next = (int64_t) ((uint64_t) cur + (uint64_t) step); if ((cur ^ step) >= 0 && (next ^ cur) < 0) return 0; *ctrl = next; return 1; @@ -2226,49 +3070,53 @@ static inline int FlForLimitToInt(double flim, int64_t step, int64_t *out) { *out = INT64_MAX; return 0; } - if (flim < (double)INT64_MIN) { + if (flim < (double) INT64_MIN) { if (step > 0) return 1; *out = INT64_MIN; return 0; } - *out = step > 0 ? (int64_t)floor(flim) : (int64_t)ceil(flim); + *out = step > 0 ? (int64_t) floor(flim) : (int64_t) ceil(flim); return 0; } -#define FlToIntChecked(v, result) do { \ - if (!FlDoubleFitsInt64((double)(v), &(result))) { \ - FakeluaThrowError(_S, "number has no integer representation"); \ - } \ -} while(0) - -#define FlShiftIntImpl(a, b, result, _right) do { \ - int64_t __sh_a = (a); int64_t __sh_b = (b); \ - if (UNLIKELY(__sh_b >= 64 || __sh_b <= -64)) { \ - (result) = (int64_t)0; \ - } else if (__sh_b >= 0) { \ - (result) = (int64_t)((_right) ? ((uint64_t)__sh_a >> __sh_b) : ((uint64_t)__sh_a << __sh_b)); \ - } else { \ - (result) = (int64_t)((_right) ? ((uint64_t)__sh_a << (-__sh_b)) : ((uint64_t)__sh_a >> (-__sh_b))); \ - } \ -} while(0) +#define FlToIntChecked(v, result) \ + do { \ + if (!FlDoubleFitsInt64((double) (v), &(result))) { \ + FakeluaThrowError(_S, "number has no integer representation"); \ + } \ + } while (0) + +#define FlShiftIntImpl(a, b, result, _right) \ + do { \ + int64_t __sh_a = (a); \ + int64_t __sh_b = (b); \ + if (UNLIKELY(__sh_b >= 64 || __sh_b <= -64)) { \ + (result) = (int64_t) 0; \ + } else if (__sh_b >= 0) { \ + (result) = (int64_t) ((_right) ? ((uint64_t) __sh_a >> __sh_b) : ((uint64_t) __sh_a << __sh_b)); \ + } else { \ + (result) = (int64_t) ((_right) ? ((uint64_t) __sh_a << (-__sh_b)) : ((uint64_t) __sh_a >> (-__sh_b))); \ + } \ + } while (0) #define FlLShiftInt(a, b, result) FlShiftIntImpl(a, b, result, 0) #define FlRShiftInt(a, b, result) FlShiftIntImpl(a, b, result, 1) -#define FlLenInt(v, result) do { \ - CVar __fl_v = (v); \ - if (LIKELY(__fl_v.type_ == VAR_STRING)) { \ - (result) = (int64_t)STR_SIZE(__fl_v.data_.s); \ - } else if (UNLIKELY(__fl_v.type_ == VAR_STRINGID)) { \ - (result) = (int64_t)STR_SIZE((VarString *)__fl_v.data_.i); \ - } else if (UNLIKELY(__fl_v.type_ == VAR_TABLE)) { \ - (result) = (int64_t)FlGetTableSeqLen(__fl_v.data_.t); \ - } else { \ - FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); \ - (result) = 0; \ - } \ -} while(0) +#define FlLenInt(v, result) \ + do { \ + CVar __fl_v = (v); \ + if (LIKELY(__fl_v.type_ == VAR_STRING)) { \ + (result) = (int64_t) STR_SIZE(__fl_v.data_.s); \ + } else if (UNLIKELY(__fl_v.type_ == VAR_STRINGID)) { \ + (result) = (int64_t) STR_SIZE((VarString *) __fl_v.data_.i); \ + } else if (UNLIKELY(__fl_v.type_ == VAR_TABLE)) { \ + (result) = (int64_t) FlGetTableSeqLen(__fl_v.data_.t); \ + } else { \ + FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); \ + (result) = 0; \ + } \ + } while (0) // 获取本 State 的日志级别(由 C++ 侧初始化和设置) // 返回值: 0=Trace, 1=Debug, 2=Info, 3=Warn, 4=Error, 5=Critical, 6=Off @@ -2295,16 +3143,27 @@ enum { // 日志宏:级别检查 + 调用 C++ 日志函数 // 参数 msg 只在级别启用时才会被求值 -#define FAKELUA_LOG_TRACE(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_TRACE)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_TRACE, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_DEBUG(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_DEBUG)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_DEBUG, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_INFO(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_INFO)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_INFO, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_WARN(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_WARN)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_WARN, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_ERROR(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_ERROR)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_ERROR, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_CRITICAL(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_CRITICAL)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_CRITICAL, msg, file, line, fname); } while (0) - +#define FAKELUA_LOG_TRACE(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_TRACE)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_TRACE, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_DEBUG(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_DEBUG)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_DEBUG, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_INFO(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_INFO)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_INFO, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_WARN(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_WARN)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_WARN, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_ERROR(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_ERROR)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_ERROR, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_CRITICAL(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_CRITICAL)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_CRITICAL, msg, file, line, fname); \ + } while (0) diff --git a/src/compile/c_runtime_header.h b/src/compile/c_runtime_header.h index eabe9ba4..ad3bd16c 100644 --- a/src/compile/c_runtime_header.h +++ b/src/compile/c_runtime_header.h @@ -42,6 +42,7 @@ typedef struct VarClosure VarClosure; typedef struct CVar { int type_; int flag_; + union { bool b; int64_t i; @@ -109,7 +110,7 @@ struct VarTable { typedef struct State State; -#define LIKELY(x) __builtin_expect(!!(x), 1) +#define LIKELY(x) __builtin_expect(!!(x), 1) #define UNLIKELY(x) __builtin_expect(!!(x), 0) #define STR_SIZE(s) ((s)->size_) @@ -132,23 +133,29 @@ enum { // init 期间 NEWTABLE/SET_TABLE 创建的表直接打 CONST_FLAG(整棵树,含嵌套); // SETTABLE 仅在 init 结束后才因 CONST_FLAG 抛错,以便构造器写字段。 #define CONST_FLAG 0x1 -#define FL_CHECK_CONST_TABLE(t) do { \ - if (UNLIKELY(((t).flag_ & CONST_FLAG) && __fakelua_init_flag__)) { \ - FakeluaThrowError(_S, "attempt to modify a const table"); \ - } \ -} while(0) +#define FL_CHECK_CONST_TABLE(t) \ + do { \ + if (UNLIKELY(((t).flag_ & CONST_FLAG) && __fakelua_init_flag__)) { \ + FakeluaThrowError(_S, "attempt to modify a const table"); \ + } \ + } while (0) /* 探测整数键 k 是否存在且值非 nil。查找逻辑与 FlGetTableInt 一致,但直接作用于 VarTable*,供序列长度计算与缓存维护复用。必须查 spec:特化表的 1..n 只在 spec 里,不在 quick/nodes。以前从 spec_count 起扫,等于把字段个数当成 #。 */ static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { if (t->spec_get) { - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; bool __finish = false; CVar __r = t->spec_get(t, key_cvar, &__finish); if (__finish) return __r.type_ != VAR_NIL; } - if ((uint64_t)k - 1 < (uint64_t)t->arr_size_) { return t->arr_[k - 1].type_ != VAR_NIL; } + if ((uint64_t) k - 1 < (uint64_t) t->arr_size_) { + return t->arr_[k - 1].type_ != VAR_NIL; + } if (t->spec_count > 0 && t->spec_keys && t->spec_vals) { uint32_t i; for (i = 0; i < t->spec_count; ++i) { @@ -166,7 +173,7 @@ static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { } return false; } else { - uint32_t h = (uint32_t)(k ^ (k >> 32)); + uint32_t h = (uint32_t) (k ^ (k >> 32)); uint32_t mask = t->bucket_count_ - 1; TableNode *curr = &t->nodes_[h & mask]; while (1) { @@ -181,7 +188,9 @@ static inline bool FlTableHasIntKey(VarTable *t, int64_t k) { /* 从 base 起向上数连续存在的整数键,返回前缀长度。*/ static inline int64_t FlSeqScanFrom(VarTable *t, int64_t base) { - while (base < INT64_MAX && FlTableHasIntKey(t, base + 1)) { base++; } + while (base < INT64_MAX && FlTableHasIntKey(t, base + 1)) { + base++; + } return base; } @@ -209,18 +218,26 @@ static inline int64_t FlGetTableSeqLen(VarTable *t) { 本函数对同一次写入重复调用是幂等的。 */ static inline void FlSeqNoteIntSet(VarTable *t, int64_t k, bool is_nil) { if (!t || t->seq_len_valid_ == 0) return; - if (UNLIKELY(!FlSeqCacheable(t))) { t->seq_len_valid_ = 0; return; } + if (UNLIKELY(!FlSeqCacheable(t))) { + t->seq_len_valid_ = 0; + return; + } if (is_nil) { /* t[k] 变为 nil,前缀在 k-1 处截断;1..k-1 都还在,故新长度恰为 k-1。*/ - if (k >= 1 && k <= t->seq_len_) { t->seq_len_ = k - 1; } + if (k >= 1 && k <= t->seq_len_) { + t->seq_len_ = k - 1; + } return; } /* 只有正好补上 t[len+1] 才会延长前缀,随后继续吸收先前写入的更高键。*/ - if (t->seq_len_ < INT64_MAX && k == t->seq_len_ + 1) { t->seq_len_ = FlSeqScanFrom(t, k); } + if (t->seq_len_ < INT64_MAX && k == t->seq_len_ + 1) { + t->seq_len_ = FlSeqScanFrom(t, k); + } } /* NativeObject Wrap 惰性快照:pairs/GET_TABLE_ENTRY 读 spec_count 前调用。非 Native 表 no-op。 */ extern void FlEnsureNativeSpecSnapshot(VarTable *t); + static inline void FlEnsureNativeSpecSnapshotInline(VarTable *t) { if (UNLIKELY(t != NULL && t->spec != NULL)) { FlEnsureNativeSpecSnapshot(t); @@ -239,23 +256,23 @@ typedef struct FlHeapSlot { char *end; } FlHeapSlot; -extern void* FakeluaAlloc(State *s, size_t size, bool is_const); +extern void *FakeluaAlloc(State *s, size_t size, bool is_const); -static inline void* FlFastAlloc(State *state, size_t size, bool is_const) { - FlHeapSlot *hs = (is_const || !__fakelua_init_flag__) ? (FlHeapSlot *)fl_const_heap : (FlHeapSlot *)fl_temp_heap; +static inline void *FlFastAlloc(State *state, size_t size, bool is_const) { + FlHeapSlot *hs = (is_const || !__fakelua_init_flag__) ? (FlHeapSlot *) fl_const_heap : (FlHeapSlot *) fl_temp_heap; if (LIKELY(hs != NULL)) { - size_t aligned_size = (size + 15) & ~(size_t)15; + size_t aligned_size = (size + 15) & ~(size_t) 15; char *c = hs->curr; if (LIKELY(c != NULL && aligned_size >= size && c + aligned_size > c && c + aligned_size <= hs->end)) { hs->curr = c + aligned_size; - return (void *)c; + return (void *) c; } } return FakeluaAlloc(state, size, is_const); } static inline CVar FlAllocMulti(State *state, uint32_t count) { - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); m->count = count; CVar res; res.type_ = VAR_MULTI; @@ -267,13 +284,13 @@ static inline CVar FlAllocMulti(State *state, uint32_t count) { // 设置 multi-value 的元素,避免链接 inter::SetMultiCVarElement(Windows TCC 找不到符号) static inline void FlSetMultiCVarElement(CVar multi, int idx, CVar val) { VarMulti *m = multi.data_.m; - if (idx >= 0 && idx < (int)m->count) { + if (idx >= 0 && idx < (int) m->count) { m->vars[idx] = val; } } static inline CVar FlMakeMulti(State *state, uint32_t count, ...) { - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); m->count = count; va_list args_list; va_start(args_list, count); @@ -298,12 +315,12 @@ static inline CVar FlCombineMulti(State *state, uint32_t prefix_count, const CVa } uint32_t total_count = prefix_count + last_count; - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + total_count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + total_count * sizeof(CVar), !__fakelua_init_flag__); m->count = total_count; for (uint32_t i = 0; i < prefix_count; ++i) { if (prefix_vars[i].type_ == VAR_MULTI) { - m->vars[i] = prefix_vars[i].data_.m->count > 0 ? prefix_vars[i].data_.m->vars[0] : (CVar){VAR_NIL}; + m->vars[i] = prefix_vars[i].data_.m->count > 0 ? prefix_vars[i].data_.m->vars[0] : (CVar) {VAR_NIL}; } else { m->vars[i] = prefix_vars[i]; } @@ -319,25 +336,24 @@ static inline CVar FlCombineMulti(State *state, uint32_t prefix_count, const CVa return res; } - static inline CVar FlUnboxMulti(CVar v, uint32_t idx) { if (LIKELY(v.type_ != VAR_MULTI)) { - return (idx == 0) ? v : (CVar){VAR_NIL}; + return (idx == 0) ? v : (CVar) {VAR_NIL}; } VarMulti *m = v.data_.m; - return (idx < m->count) ? m->vars[idx] : (CVar){VAR_NIL}; + return (idx < m->count) ? m->vars[idx] : (CVar) {VAR_NIL}; } static inline CVar FlSliceMulti(State *state, CVar v, uint32_t start_idx) { if (v.type_ != VAR_MULTI) { - return (start_idx == 0) ? v : (CVar){VAR_NIL}; + return (start_idx == 0) ? v : (CVar) {VAR_NIL}; } VarMulti *m = v.data_.m; if (start_idx >= m->count) { - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } uint32_t count = m->count - start_idx; - VarMulti *nm = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *nm = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + count * sizeof(CVar), !__fakelua_init_flag__); nm->count = count; for (uint32_t i = 0; i < count; ++i) { nm->vars[i] = m->vars[start_idx + i]; @@ -349,14 +365,27 @@ static inline CVar FlSliceMulti(State *state, CVar v, uint32_t start_idx) { return res; } -#define SET_NIL(v) do { (v).type_ = VAR_NIL; } while(0) -#define SET_BOOL(v, val) do { (v).type_ = VAR_BOOL; (v).data_.i = 0; (v).data_.b = (val); } while(0) -#define SET_INT(v, val) do { (v).type_ = VAR_INT; (v).data_.i = (val); } while(0) -#define SET_FLOAT(v, val) do { \ - double __f = (val); \ - (v).type_ = VAR_FLOAT; \ - (v).data_.f = __f; \ -} while(0) +#define SET_NIL(v) \ + do { \ + (v).type_ = VAR_NIL; \ + } while (0) +#define SET_BOOL(v, val) \ + do { \ + (v).type_ = VAR_BOOL; \ + (v).data_.i = 0; \ + (v).data_.b = (val); \ + } while (0) +#define SET_INT(v, val) \ + do { \ + (v).type_ = VAR_INT; \ + (v).data_.i = (val); \ + } while (0) +#define SET_FLOAT(v, val) \ + do { \ + double __f = (val); \ + (v).type_ = VAR_FLOAT; \ + (v).data_.f = __f; \ + } while (0) /* 2^63:精确 double,也是第一个无法放入 int64 的整数。 * (double)INT64_MAX 会向上舍入成这个值,因此不能用 <= (double)INT64_MAX。 */ @@ -366,27 +395,28 @@ static inline int FlDoubleFitsInt64(double d, int64_t *out) { double ip; if (!isfinite(d)) return 0; if (modf(d, &ip) != 0.0) return 0; - if (ip < (double)INT64_MIN || ip >= FL_INT64_FLOAT_EXCL) return 0; - *out = (int64_t)ip; + if (ip < (double) INT64_MIN || ip >= FL_INT64_FLOAT_EXCL) return 0; + *out = (int64_t) ip; return 1; } -#define NORMALIZE_TABLE_KEY(key) ({ \ - CVar __k = (key); \ - if (UNLIKELY(__k.type_ == VAR_FLOAT)) { \ - int64_t __i; \ - if (FlDoubleFitsInt64(__k.data_.f, &__i)) { \ - __k.type_ = VAR_INT; \ - __k.data_.i = __i; \ - } \ - } \ - __k; \ -}) +#define NORMALIZE_TABLE_KEY(key) \ + ({ \ + CVar __k = (key); \ + if (UNLIKELY(__k.type_ == VAR_FLOAT)) { \ + int64_t __i; \ + if (FlDoubleFitsInt64(__k.data_.f, &__i)) { \ + __k.type_ = VAR_INT; \ + __k.data_.i = __i; \ + } \ + } \ + __k; \ + }) #ifdef __cplusplus extern "C" { #endif -extern void* FakeluaAlloc(State *s, size_t size, bool is_const); +extern void *FakeluaAlloc(State *s, size_t size, bool is_const); extern void FakeluaThrowError(State *s, const char *msg); extern CVar FakeluaCallByName(State *s, int jit_type, const char *name, int arg_num, ...); extern CVar FakeluaAllocMultiCVar(State *s, int count); @@ -404,11 +434,12 @@ extern void FakeluaRegisterLuaLoc(State *s, int jit_type, FlLuaLoc *loc); // 记录当前执行的 Lua 语句位置(文件级全局变量 fl_lua_loc 在 __fakelua_init 时把地址传给宿主 State)。 // C++ 侧的报错拿不到 Lua 栈,直接通过注册的变量地址读取;#line 则负责把编译告警映射回 Lua 源码。 -static FlLuaLoc fl_lua_loc = { 0, 0 }; -#define FL_LOC_SET(f, l) do { \ - (void)(f); \ - fl_lua_loc.line = (l); \ -} while (0) +static FlLuaLoc fl_lua_loc = {0, 0}; +#define FL_LOC_SET(f, l) \ + do { \ + (void) (f); \ + fl_lua_loc.line = (l); \ + } while (0) #define kMaxFunctionInputParams 32 @@ -416,7 +447,7 @@ static FlLuaLoc fl_lua_loc = { 0, 0 }; // 具名形参若类型会受默认实参提升影响(bool/_Bool 会被提升为 int),行为未定义。 // 调用处传入的 true/false 会隐式转换为 int。 static inline CVar FlMakeClosure(State *state, void *func_ptr, int upvalue_count, int expected_arg_count, int is_vararg, ...) { - VarClosure *cl = (VarClosure *)FlFastAlloc(state, sizeof(VarClosure) + upvalue_count * sizeof(CVar *), !__fakelua_init_flag__); + VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure) + upvalue_count * sizeof(CVar *), !__fakelua_init_flag__); cl->func_ptr = func_ptr; cl->upvalue_count = upvalue_count; cl->expected_arg_count = expected_arg_count; @@ -436,7 +467,7 @@ static inline CVar FlMakeClosure(State *state, void *func_ptr, int upvalue_count } static inline CVar FlMakeClosure0(State *state, void *func_ptr, int expected_arg_count, int is_vararg) { - VarClosure *cl = (VarClosure *)FlFastAlloc(state, sizeof(VarClosure), !__fakelua_init_flag__); + VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure), !__fakelua_init_flag__); cl->func_ptr = func_ptr; cl->upvalue_count = 0; cl->expected_arg_count = expected_arg_count; @@ -450,7 +481,7 @@ static inline CVar FlMakeClosure0(State *state, void *func_ptr, int expected_arg } static inline CVar FlMakeClosure1(State *state, void *func_ptr, int expected_arg_count, int is_vararg, CVar *up0) { - VarClosure *cl = (VarClosure *)FlFastAlloc(state, sizeof(VarClosure) + sizeof(CVar *), !__fakelua_init_flag__); + VarClosure *cl = (VarClosure *) FlFastAlloc(state, sizeof(VarClosure) + sizeof(CVar *), !__fakelua_init_flag__); cl->func_ptr = func_ptr; cl->upvalue_count = 1; cl->expected_arg_count = expected_arg_count; @@ -470,20 +501,20 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { } // 与 FakeluaCallByName 一致:先拦超限,再读 varargs。 // raw_arg_arr 只有 32 槽,arg_num>32 会栈缓冲溢出。 - if (UNLIKELY(arg_num < 0 || arg_num > (int)kMaxFunctionInputParams)) { + if (UNLIKELY(arg_num < 0 || arg_num > (int) kMaxFunctionInputParams)) { FakeluaThrowError(state, "too many arguments"); } VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; if (UNLIKELY(addr == NULL)) { - return FlEvalLoadClosure(state, cl, arg_num, cl_var.type_ == VAR_CLOSURE ? (const CVar *)&cl_var : NULL); + return FlEvalLoadClosure(state, cl, arg_num, cl_var.type_ == VAR_CLOSURE ? (const CVar *) &cl_var : NULL); } const bool is_vararg = cl->is_vararg; const int expected_arg_count = cl->expected_arg_count; const int fixed_arg_count = is_vararg ? expected_arg_count - 1 : expected_arg_count; // 形参个数也要限:下面按 expected_arg_count 往 temp_arg_arr(定长 32)里写, // 声明了超过上限的形参会栈缓冲溢出(与 vm.cpp 的 CallByNameImpl 一致)。 - if (UNLIKELY(expected_arg_count < 0 || expected_arg_count > (int)kMaxFunctionInputParams)) { + if (UNLIKELY(expected_arg_count < 0 || expected_arg_count > (int) kMaxFunctionInputParams)) { FakeluaThrowError(state, "too many arguments"); } @@ -506,8 +537,8 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { for (int i = 0; i < arg_num; ++i) { if (i == arg_num - 1 && raw_arg_arr[i].type_ == VAR_MULTI) { VarMulti *m = raw_arg_arr[i].data_.m; - const int extra = m ? (int)m->count : 0; - if (flat_count + extra > (int)kMaxFunctionInputParams) { + const int extra = m ? (int) m->count : 0; + if (flat_count + extra > (int) kMaxFunctionInputParams) { FakeluaThrowError(state, "too many arguments"); } if (m) { @@ -517,12 +548,12 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { } } else if (raw_arg_arr[i].type_ == VAR_MULTI) { VarMulti *m = raw_arg_arr[i].data_.m; - if (flat_count >= (int)kMaxFunctionInputParams) { + if (flat_count >= (int) kMaxFunctionInputParams) { FakeluaThrowError(state, "too many arguments"); } - flat_args_buf[flat_count++] = (m && m->count > 0) ? m->vars[0] : (CVar){VAR_NIL}; + flat_args_buf[flat_count++] = (m && m->count > 0) ? m->vars[0] : (CVar) {VAR_NIL}; } else { - if (flat_count >= (int)kMaxFunctionInputParams) { + if (flat_count >= (int) kMaxFunctionInputParams) { FakeluaThrowError(state, "too many arguments"); } flat_args_buf[flat_count++] = raw_arg_arr[i]; @@ -531,10 +562,10 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { if (UNLIKELY(is_vararg)) { for (int i = 0; i < fixed_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar){VAR_NIL}; + temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {VAR_NIL}; } const int vararg_count = flat_count - fixed_arg_count; - VarMulti *m = (VarMulti *)FlFastAlloc(state, sizeof(VarMulti) + (vararg_count > 0 ? vararg_count : 0) * sizeof(CVar), !__fakelua_init_flag__); + VarMulti *m = (VarMulti *) FlFastAlloc(state, sizeof(VarMulti) + (vararg_count > 0 ? vararg_count : 0) * sizeof(CVar), !__fakelua_init_flag__); m->count = vararg_count > 0 ? vararg_count : 0; for (int i = 0; i < vararg_count; ++i) { m->vars[i] = flat_args_buf[fixed_arg_count + i]; @@ -546,13 +577,13 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { temp_arg_arr[fixed_arg_count] = vararg_cvar; } else { for (int i = 0; i < expected_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar){VAR_NIL}; + temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {VAR_NIL}; } } arg_arr = temp_arg_arr; } - if (cl->code_str == (const char *)1) { + if (cl->code_str == (const char *) 1) { int npass = expected_arg_count; if (is_vararg) { npass = fixed_arg_count + 1; @@ -629,11 +660,11 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { #define FCARG_31 FCARG_30, arg_arr[30] #define FCARG_32 FCARG_31, arg_arr[31] -#define FCCASE(N) \ +#define FCCASE(N) \ case N: { \ int __fl_saved_line = fl_lua_loc.line; \ fl_lua_loc.line = 0; \ - int __fl_prev_jit = FakeluaJitContextPush(state, FAKELUA_JIT_TYPE); \ + int __fl_prev_jit = FakeluaJitContextPush(state, FAKELUA_JIT_TYPE); \ CVar __fl_ret = ((CVar (*)(VarClosure * FCCVAR_##N))(addr))(cl FCARG_##N); \ FakeluaJitContextPop(state, __fl_prev_jit); \ fl_lua_loc.line = __fl_saved_line; \ @@ -641,13 +672,32 @@ static inline CVar FlCallClosure(State *state, CVar cl_var, int arg_num, ...) { } switch (expected_arg_count) { - FCCASE(0) FCCASE(1) FCCASE(2) FCCASE(3) FCCASE(4) FCCASE(5) - FCCASE(6) FCCASE(7) FCCASE(8) FCCASE(9) FCCASE(10) FCCASE(11) - FCCASE(12) FCCASE(13) FCCASE(14) FCCASE(15) FCCASE(16) FCCASE(17) - FCCASE(18) FCCASE(19) FCCASE(20) FCCASE(21) FCCASE(22) FCCASE(23) - FCCASE(24) FCCASE(25) FCCASE(26) FCCASE(27) FCCASE(28) FCCASE(29) - FCCASE(30) FCCASE(31) FCCASE(32) - default: FakeluaThrowError(state, "FlCallClosure: expected_arg_count out of range"); return (CVar){VAR_NIL}; + FCCASE(0) + FCCASE(1) + FCCASE(2) + FCCASE(3) + FCCASE(4) + FCCASE(5) + FCCASE(6) + FCCASE(7) + FCCASE(8) + FCCASE(9) + FCCASE(10) + FCCASE(11) + FCCASE(12) + FCCASE(13) + FCCASE(14) + FCCASE(15) + FCCASE(16) + FCCASE(17) + FCCASE(18) + FCCASE(19) + FCCASE(20) + FCCASE(21) + FCCASE(22) + FCCASE(23) + FCCASE(24) FCCASE(25) FCCASE(26) FCCASE(27) FCCASE(28) FCCASE(29) FCCASE(30) FCCASE(31) FCCASE(32) default : FakeluaThrowError(state, "FlCallClosure: expected_arg_count out of range"); + return (CVar) {VAR_NIL}; } #undef FCCASE @@ -723,7 +773,7 @@ static inline CVar FlCallClosure0(State *state, CVar cl_var) { if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 0 && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 0 && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *))(addr))(cl); @@ -738,7 +788,7 @@ static inline CVar FlCallClosure1(State *state, CVar cl_var, CVar a0) { if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 1 && a0.type_ != VAR_MULTI && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 1 && a0.type_ != VAR_MULTI && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *, CVar))(addr))(cl, a0); @@ -753,7 +803,7 @@ static inline CVar FlCallClosure2(State *state, CVar cl_var, CVar a0, CVar a1) { if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 2 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 2 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *, CVar, CVar))(addr))(cl, a0, a1); @@ -768,7 +818,7 @@ static inline CVar FlCallClosure3(State *state, CVar cl_var, CVar a0, CVar a1, C if (LIKELY(cl_var.type_ == VAR_CLOSURE)) { VarClosure *cl = cl_var.data_.cl; void *addr = cl->func_ptr; - if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 3 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && a2.type_ != VAR_MULTI && cl->code_str != (const char *)1)) { + if (LIKELY(addr != NULL && !cl->is_vararg && cl->expected_arg_count == 3 && a0.type_ != VAR_MULTI && a1.type_ != VAR_MULTI && a2.type_ != VAR_MULTI && cl->code_str != (const char *) 1)) { int __fl_saved_line = fl_lua_loc.line; fl_lua_loc.line = 0; CVar __fl_ret = ((CVar (*)(VarClosure *, CVar, CVar, CVar))(addr))(cl, a0, a1, a2); @@ -791,7 +841,7 @@ static inline uint32_t FlHashString(const char *str, int len) { uint32_t hash = 5381; int i; for (i = 0; i < len; ++i) { - hash = ((hash << 5) + hash) + (uint8_t)str[i]; + hash = ((hash << 5) + hash) + (uint8_t) str[i]; } if (hash == 0) { hash = 1; @@ -799,80 +849,88 @@ static inline uint32_t FlHashString(const char *str, int len) { return hash; } -#define SET_STRING(v, str, len) do { \ - int __len = (len); \ - const char *__s_ptr = (str); \ - VarString *__vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + __len, !__fakelua_init_flag__); \ - __vs->size_ = __len; \ - __vs->hash_ = 0; \ - memcpy(__vs->data_, __s_ptr, __len); \ - (v).type_ = VAR_STRING; \ - (v).data_.s = __vs; \ -} while(0) - -#define SET_TABLE(v) do { \ - VarTable *__t = (VarTable *)FlFastAlloc(_S, sizeof(VarTable), !__fakelua_init_flag__); \ - __t->count_ = 0; \ - __t->bucket_count_ = 0; \ - __t->nodes_ = NULL; \ - __t->active_list_ = NULL; \ - __t->free_list_idx_ = 0xFFFFFFFF; \ - __t->spec = NULL; \ - __t->spec_get = NULL; \ - __t->spec_set = NULL; \ - __t->spec_keys = NULL; \ - __t->spec_vals = NULL; \ - __t->spec_count = 0; \ - __t->seq_len_valid_ = 1; \ - __t->seq_len_ = 0; \ - __t->arr_ = NULL; \ - __t->arr_size_ = 0; \ - __t->arr_cap_ = 0; \ - assert(sizeof(__t->quick_data_) == 8 * sizeof(VarEntry)); \ - { int __i; for (__i = 0; __i < 8; ++__i) { \ - __t->quick_data_[__i].key.type_ = VAR_NIL; \ - __t->quick_data_[__i].val.type_ = VAR_NIL; \ - __t->quick_data_[__i].hash = 0; \ - } } \ - (v).type_ = VAR_TABLE; \ - /* init 中创建 → CONST_FLAG(嵌套表同样走 SET_TABLE,整树只读);init 后创建 → 可变 */ \ - (v).flag_ = __fakelua_init_flag__ ? 0 : CONST_FLAG; \ - (v).data_.t = __t; \ -} while(0) - -#define SET_TABLE_SPEC(v, SpecType, get_fn, set_fn, field_count) do { \ - SET_TABLE(v); \ - SpecType *__sp = (SpecType *)FlFastAlloc(_S, sizeof(SpecType), !__fakelua_init_flag__); \ - (v).data_.t->spec = __sp; \ - (v).data_.t->spec_get = (get_fn); \ - (v).data_.t->spec_set = (set_fn); \ - (v).data_.t->spec_keys = (CVar *)FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ - (v).data_.t->spec_vals = (CVar *)FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ - (v).data_.t->spec_count = (field_count); \ - assert(sizeof(SpecType) == (field_count) * sizeof(CVar)); \ -} while(0) - -#define FL_SPEC(SpecType, v, field) (((SpecType *)(v).data_.t->spec)->field) -#define FL_SET_SPEC(SpecType, v, field, index, val) do { \ - CVar __flsv = (val); \ - FL_CHECK_CONST_TABLE(v); \ - FL_SPEC(SpecType, (v), field) = __flsv; \ - (v).data_.t->spec_vals[(index)] = __flsv; \ -} while(0) - -#define IsTrue(v, result) do { \ - CVar __tv = (v); \ - (result) = (LIKELY(__tv.type_ != VAR_NIL) && (__tv.type_ != VAR_BOOL || __tv.data_.b)); \ -} while(0) +#define SET_STRING(v, str, len) \ + do { \ + int __len = (len); \ + const char *__s_ptr = (str); \ + VarString *__vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + __len, !__fakelua_init_flag__); \ + __vs->size_ = __len; \ + __vs->hash_ = 0; \ + memcpy(__vs->data_, __s_ptr, __len); \ + (v).type_ = VAR_STRING; \ + (v).data_.s = __vs; \ + } while (0) + +#define SET_TABLE(v) \ + do { \ + VarTable *__t = (VarTable *) FlFastAlloc(_S, sizeof(VarTable), !__fakelua_init_flag__); \ + __t->count_ = 0; \ + __t->bucket_count_ = 0; \ + __t->nodes_ = NULL; \ + __t->active_list_ = NULL; \ + __t->free_list_idx_ = 0xFFFFFFFF; \ + __t->spec = NULL; \ + __t->spec_get = NULL; \ + __t->spec_set = NULL; \ + __t->spec_keys = NULL; \ + __t->spec_vals = NULL; \ + __t->spec_count = 0; \ + __t->seq_len_valid_ = 1; \ + __t->seq_len_ = 0; \ + __t->arr_ = NULL; \ + __t->arr_size_ = 0; \ + __t->arr_cap_ = 0; \ + assert(sizeof(__t->quick_data_) == 8 * sizeof(VarEntry)); \ + { \ + int __i; \ + for (__i = 0; __i < 8; ++__i) { \ + __t->quick_data_[__i].key.type_ = VAR_NIL; \ + __t->quick_data_[__i].val.type_ = VAR_NIL; \ + __t->quick_data_[__i].hash = 0; \ + } \ + } \ + (v).type_ = VAR_TABLE; \ + /* init 中创建 → CONST_FLAG(嵌套表同样走 SET_TABLE,整树只读);init 后创建 → 可变 */ \ + (v).flag_ = __fakelua_init_flag__ ? 0 : CONST_FLAG; \ + (v).data_.t = __t; \ + } while (0) + +#define SET_TABLE_SPEC(v, SpecType, get_fn, set_fn, field_count) \ + do { \ + SET_TABLE(v); \ + SpecType *__sp = (SpecType *) FlFastAlloc(_S, sizeof(SpecType), !__fakelua_init_flag__); \ + (v).data_.t->spec = __sp; \ + (v).data_.t->spec_get = (get_fn); \ + (v).data_.t->spec_set = (set_fn); \ + (v).data_.t->spec_keys = (CVar *) FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ + (v).data_.t->spec_vals = (CVar *) FlFastAlloc(_S, sizeof(CVar) * (field_count), !__fakelua_init_flag__); \ + (v).data_.t->spec_count = (field_count); \ + assert(sizeof(SpecType) == (field_count) * sizeof(CVar)); \ + } while (0) + +#define FL_SPEC(SpecType, v, field) (((SpecType *) (v).data_.t->spec)->field) +#define FL_SET_SPEC(SpecType, v, field, index, val) \ + do { \ + CVar __flsv = (val); \ + FL_CHECK_CONST_TABLE(v); \ + FL_SPEC(SpecType, (v), field) = __flsv; \ + (v).data_.t->spec_vals[(index)] = __flsv; \ + } while (0) + +#define IsTrue(v, result) \ + do { \ + CVar __tv = (v); \ + (result) = (LIKELY(__tv.type_ != VAR_NIL) && (__tv.type_ != VAR_BOOL || __tv.data_.b)); \ + } while (0) static inline int FlCompareIntDouble(int64_t integer, double floating) { if (isnan(floating)) return 0; if (floating >= 9223372036854775808.0) return -1; if (floating < -9223372036854775808.0) return 1; - int64_t truncated = (int64_t)floating; + int64_t truncated = (int64_t) floating; if (integer < truncated) return -1; if (integer > truncated) return 1; - double truncated_as_double = (double)truncated; + double truncated_as_double = (double) truncated; if (floating > truncated_as_double) return -1; if (floating < truncated_as_double) return 1; return 0; @@ -880,102 +938,223 @@ static inline int FlCompareIntDouble(int64_t integer, double floating) { #define FL_INT_FLOAT_EQUAL(i, f) (!isnan((f)) && FlCompareIntDouble((i), (f)) == 0) -#define VarEqual(_fa, _fb, result) do { \ - CVar __a = (_fa), __b = (_fb); \ - if (__a.type_ != __b.type_) { \ - if ((__a.type_ == VAR_STRING || __a.type_ == VAR_STRINGID) && \ - (__b.type_ == VAR_STRING || __b.type_ == VAR_STRINGID)) { \ - VarString *__sa = (__a.type_ == VAR_STRING) ? __a.data_.s : (VarString *)__a.data_.i; \ - VarString *__sb = (__b.type_ == VAR_STRING) ? __b.data_.s : (VarString *)__b.data_.i; \ - (result) = (__sa == __sb) || (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ - } else if (__a.type_ == VAR_INT && __b.type_ == VAR_FLOAT) { \ - (result) = FL_INT_FLOAT_EQUAL(__a.data_.i, __b.data_.f); \ - } else if (__a.type_ == VAR_FLOAT && __b.type_ == VAR_INT) { \ - (result) = FL_INT_FLOAT_EQUAL(__b.data_.i, __a.data_.f); \ - } else { \ - (result) = false; \ - } \ - break; \ - } \ - switch (__a.type_) { \ - case VAR_NIL: (result) = true; break; \ - case VAR_BOOL: (result) = (__a.data_.b == __b.data_.b); break; \ - case VAR_INT: (result) = (__a.data_.i == __b.data_.i); break; \ - case VAR_FLOAT: (result) = (__a.data_.f == __b.data_.f); break; \ - case VAR_STRING: { \ - if (__a.data_.s == __b.data_.s) { (result) = true; break; } \ - (result) = (__a.data_.s->size_ == __b.data_.s->size_ && memcmp(__a.data_.s->data_, __b.data_.s->data_, __a.data_.s->size_) == 0); \ - break; \ - } \ - case VAR_STRINGID: { \ - if (__a.data_.i == __b.data_.i) { (result) = true; break; } \ - VarString *__sa = (VarString *)__a.data_.i; \ - VarString *__sb = (VarString *)__b.data_.i; \ - (result) = (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ - break; \ - } \ - case VAR_TABLE: (result) = (__a.data_.t == __b.data_.t); break; \ - case VAR_CLOSURE: (result) = (__a.data_.cl == __b.data_.cl); break; \ - default: (result) = false; break; \ - } \ -} while(0) - -#define VarHash(v, hash) do { \ - CVar __v = (v); \ - switch (__v.type_) { \ - case VAR_NIL: (hash) = 0; break; \ - case VAR_BOOL: (hash) = (__v.data_.b ? 1 : 0); break; \ - case VAR_INT: (hash) = (uint32_t)(__v.data_.i ^ (__v.data_.i >> 32)); break; \ - case VAR_FLOAT: { union { double __d; uint64_t __u; } __fu; __fu.__d = __v.data_.f; (hash) = (uint32_t)(__fu.__u ^ (__fu.__u >> 32)); break; } \ - case VAR_STRING: { if (__v.data_.s->hash_ == 0) { __v.data_.s->hash_ = FlHashString(__v.data_.s->data_, __v.data_.s->size_); } (hash) = __v.data_.s->hash_; break; } \ - case VAR_STRINGID: { VarString *__vs = (VarString *)__v.data_.i; if (__vs->hash_ == 0) { __vs->hash_ = FlHashString(__vs->data_, __vs->size_); } (hash) = __vs->hash_; break; } \ - case VAR_TABLE: (hash) = (uint32_t)((uintptr_t)__v.data_.t ^ ((uintptr_t)__v.data_.t >> 32)); break; \ - case VAR_CLOSURE: (hash) = (uint32_t)((uintptr_t)__v.data_.cl ^ ((uintptr_t)__v.data_.cl >> 32)); break; \ - default: (hash) = 0; break; \ - } \ -} while(0) +#define VarEqual(_fa, _fb, result) \ + do { \ + CVar __a = (_fa), __b = (_fb); \ + if (__a.type_ != __b.type_) { \ + if ((__a.type_ == VAR_STRING || __a.type_ == VAR_STRINGID) && (__b.type_ == VAR_STRING || __b.type_ == VAR_STRINGID)) { \ + VarString *__sa = (__a.type_ == VAR_STRING) ? __a.data_.s : (VarString *) __a.data_.i; \ + VarString *__sb = (__b.type_ == VAR_STRING) ? __b.data_.s : (VarString *) __b.data_.i; \ + (result) = (__sa == __sb) || (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ + } else if (__a.type_ == VAR_INT && __b.type_ == VAR_FLOAT) { \ + (result) = FL_INT_FLOAT_EQUAL(__a.data_.i, __b.data_.f); \ + } else if (__a.type_ == VAR_FLOAT && __b.type_ == VAR_INT) { \ + (result) = FL_INT_FLOAT_EQUAL(__b.data_.i, __a.data_.f); \ + } else { \ + (result) = false; \ + } \ + break; \ + } \ + switch (__a.type_) { \ + case VAR_NIL: \ + (result) = true; \ + break; \ + case VAR_BOOL: \ + (result) = (__a.data_.b == __b.data_.b); \ + break; \ + case VAR_INT: \ + (result) = (__a.data_.i == __b.data_.i); \ + break; \ + case VAR_FLOAT: \ + (result) = (__a.data_.f == __b.data_.f); \ + break; \ + case VAR_STRING: { \ + if (__a.data_.s == __b.data_.s) { \ + (result) = true; \ + break; \ + } \ + (result) = (__a.data_.s->size_ == __b.data_.s->size_ && memcmp(__a.data_.s->data_, __b.data_.s->data_, __a.data_.s->size_) == 0); \ + break; \ + } \ + case VAR_STRINGID: { \ + if (__a.data_.i == __b.data_.i) { \ + (result) = true; \ + break; \ + } \ + VarString *__sa = (VarString *) __a.data_.i; \ + VarString *__sb = (VarString *) __b.data_.i; \ + (result) = (__sa->size_ == __sb->size_ && memcmp(__sa->data_, __sb->data_, __sa->size_) == 0); \ + break; \ + } \ + case VAR_TABLE: \ + (result) = (__a.data_.t == __b.data_.t); \ + break; \ + case VAR_CLOSURE: \ + (result) = (__a.data_.cl == __b.data_.cl); \ + break; \ + default: \ + (result) = false; \ + break; \ + } \ + } while (0) + +#define VarHash(v, hash) \ + do { \ + CVar __v = (v); \ + switch (__v.type_) { \ + case VAR_NIL: \ + (hash) = 0; \ + break; \ + case VAR_BOOL: \ + (hash) = (__v.data_.b ? 1 : 0); \ + break; \ + case VAR_INT: \ + (hash) = (uint32_t) (__v.data_.i ^ (__v.data_.i >> 32)); \ + break; \ + case VAR_FLOAT: { \ + union { \ + double __d; \ + uint64_t __u; \ + } __fu; \ + __fu.__d = __v.data_.f; \ + (hash) = (uint32_t) (__fu.__u ^ (__fu.__u >> 32)); \ + break; \ + } \ + case VAR_STRING: { \ + if (__v.data_.s->hash_ == 0) { \ + __v.data_.s->hash_ = FlHashString(__v.data_.s->data_, __v.data_.s->size_); \ + } \ + (hash) = __v.data_.s->hash_; \ + break; \ + } \ + case VAR_STRINGID: { \ + VarString *__vs = (VarString *) __v.data_.i; \ + if (__vs->hash_ == 0) { \ + __vs->hash_ = FlHashString(__vs->data_, __vs->size_); \ + } \ + (hash) = __vs->hash_; \ + break; \ + } \ + case VAR_TABLE: \ + (hash) = (uint32_t) ((uintptr_t) __v.data_.t ^ ((uintptr_t) __v.data_.t >> 32)); \ + break; \ + case VAR_CLOSURE: \ + (hash) = (uint32_t) ((uintptr_t) __v.data_.cl ^ ((uintptr_t) __v.data_.cl >> 32)); \ + break; \ + default: \ + (hash) = 0; \ + break; \ + } \ + } while (0) static inline CVar FlGetTable(CVar t, CVar k) { k = NORMALIZE_TABLE_KEY(k); - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } - if (UNLIKELY(k.type_ == VAR_NIL)) { FakeluaThrowError(_S, "table index is nil"); } - if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { FakeluaThrowError(_S, "table index is NaN"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } + if (UNLIKELY(k.type_ == VAR_NIL)) { + FakeluaThrowError(_S, "table index is nil"); + } + if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { + FakeluaThrowError(_S, "table index is NaN"); + } VarTable *tbl = t.data_.t; if (tbl->spec_get) { bool __finish = false; CVar __r = tbl->spec_get(tbl, k, &__finish); if (__finish) return __r; } - if (k.type_ == VAR_INT && (uint64_t)k.data_.i - 1 < (uint64_t)tbl->arr_size_) { return tbl->arr_[k.data_.i - 1]; } - if (UNLIKELY(tbl->count_ == 0)) { return (CVar){VAR_NIL}; } - uint32_t h; VarHash(k, h); + if (k.type_ == VAR_INT && (uint64_t) k.data_.i - 1 < (uint64_t) tbl->arr_size_) { + return tbl->arr_[k.data_.i - 1]; + } + if (UNLIKELY(tbl->count_ == 0)) { + return (CVar) {VAR_NIL}; + } + uint32_t h; + VarHash(k, h); if (LIKELY(tbl->bucket_count_ == 0)) { bool __eq; - if (tbl->quick_data_[0].hash == h) { VarEqual(tbl->quick_data_[0].key, k, __eq); if (__eq) { return tbl->quick_data_[0].val; } } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { VarEqual(tbl->quick_data_[1].key, k, __eq); if (__eq) { return tbl->quick_data_[1].val; } } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { VarEqual(tbl->quick_data_[2].key, k, __eq); if (__eq) { return tbl->quick_data_[2].val; } } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { VarEqual(tbl->quick_data_[3].key, k, __eq); if (__eq) { return tbl->quick_data_[3].val; } } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { VarEqual(tbl->quick_data_[4].key, k, __eq); if (__eq) { return tbl->quick_data_[4].val; } } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { VarEqual(tbl->quick_data_[5].key, k, __eq); if (__eq) { return tbl->quick_data_[5].val; } } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { VarEqual(tbl->quick_data_[6].key, k, __eq); if (__eq) { return tbl->quick_data_[6].val; } } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { VarEqual(tbl->quick_data_[7].key, k, __eq); if (__eq) { return tbl->quick_data_[7].val; } } + if (tbl->quick_data_[0].hash == h) { + VarEqual(tbl->quick_data_[0].key, k, __eq); + if (__eq) { + return tbl->quick_data_[0].val; + } + } + if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { + VarEqual(tbl->quick_data_[1].key, k, __eq); + if (__eq) { + return tbl->quick_data_[1].val; + } + } + if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { + VarEqual(tbl->quick_data_[2].key, k, __eq); + if (__eq) { + return tbl->quick_data_[2].val; + } + } + if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { + VarEqual(tbl->quick_data_[3].key, k, __eq); + if (__eq) { + return tbl->quick_data_[3].val; + } + } + if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { + VarEqual(tbl->quick_data_[4].key, k, __eq); + if (__eq) { + return tbl->quick_data_[4].val; + } + } + if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { + VarEqual(tbl->quick_data_[5].key, k, __eq); + if (__eq) { + return tbl->quick_data_[5].val; + } + } + if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { + VarEqual(tbl->quick_data_[6].key, k, __eq); + if (__eq) { + return tbl->quick_data_[6].val; + } + } + if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { + VarEqual(tbl->quick_data_[7].key, k, __eq); + if (__eq) { + return tbl->quick_data_[7].val; + } + } } else { uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return (CVar){VAR_NIL}; } + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return (CVar) {VAR_NIL}; + } while (1) { - if (curr->entry.hash == h) { bool __eq; VarEqual(curr->entry.key, k, __eq); if (__eq) { return curr->entry.val; } } + if (curr->entry.hash == h) { + bool __eq; + VarEqual(curr->entry.key, k, __eq); + if (__eq) { + return curr->entry.val; + } + } uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { break; } + if (next == 0xFFFFFFFF) { + break; + } curr = &tbl->nodes_[next]; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } static inline uint32_t NextPowerOfTwo(uint32_t v) { - v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; return v; } @@ -986,25 +1165,46 @@ static inline bool FlTableInsertRaw(VarTable *tbl, CVar key, CVar val, uint32_t uint32_t idx = hash & mask; TableNode *main_node = &tbl->nodes_[idx]; if (LIKELY(main_node->entry.key.type_ == VAR_NIL)) { - main_node->entry.key = key; main_node->entry.val = val; main_node->entry.hash = hash; main_node->next = 0xFFFFFFFF; - main_node->active_pos = tbl->count_; tbl->active_list_[tbl->count_] = idx; - tbl->count_++; return true; + main_node->entry.key = key; + main_node->entry.val = val; + main_node->entry.hash = hash; + main_node->next = 0xFFFFFFFF; + main_node->active_pos = tbl->count_; + tbl->active_list_[tbl->count_] = idx; + tbl->count_++; + return true; } uint32_t curr_idx = idx; while (1) { TableNode *curr = &tbl->nodes_[curr_idx]; - if (curr->entry.hash == hash) { bool __eq; VarEqual(curr->entry.key, key, __eq); if (__eq) { curr->entry.val = val; return true; } } - if (curr->next == 0xFFFFFFFF) { break; } + if (curr->entry.hash == hash) { + bool __eq; + VarEqual(curr->entry.key, key, __eq); + if (__eq) { + curr->entry.val = val; + return true; + } + } + if (curr->next == 0xFFFFFFFF) { + break; + } curr_idx = curr->next; } - if (UNLIKELY(tbl->free_list_idx_ == 0xFFFFFFFF)) { return false; } + if (UNLIKELY(tbl->free_list_idx_ == 0xFFFFFFFF)) { + return false; + } uint32_t new_node_idx = tbl->free_list_idx_; TableNode *new_node = &tbl->nodes_[new_node_idx]; tbl->free_list_idx_ = new_node->next; - new_node->entry.key = key; new_node->entry.val = val; new_node->entry.hash = hash; - new_node->next = main_node->next; main_node->next = new_node_idx; - new_node->active_pos = tbl->count_; tbl->active_list_[tbl->count_] = new_node_idx; - tbl->count_++; return true; + new_node->entry.key = key; + new_node->entry.val = val; + new_node->entry.hash = hash; + new_node->next = main_node->next; + main_node->next = new_node_idx; + new_node->active_pos = tbl->count_; + tbl->active_list_[tbl->count_] = new_node_idx; + tbl->count_++; + return true; } static inline void FlTableRehash(VarTable *tbl) { @@ -1012,20 +1212,41 @@ static inline void FlTableRehash(VarTable *tbl) { uint32_t old_bucket_count = tbl->bucket_count_; TableNode *old_nodes = tbl->nodes_; uint32_t new_bucket_count = NextPowerOfTwo(old_count + 1); - if (new_bucket_count <= old_bucket_count) { new_bucket_count = old_bucket_count * 2; } + if (new_bucket_count <= old_bucket_count) { + new_bucket_count = old_bucket_count * 2; + } while (1) { uint32_t overflow_count = new_bucket_count / 2; uint32_t total_nodes = new_bucket_count + overflow_count; size_t nodes_size = total_nodes * sizeof(TableNode); size_t active_list_size = total_nodes * sizeof(uint32_t); - char *buffer = (char *)FlFastAlloc(_S, nodes_size + active_list_size, !__fakelua_init_flag__); - TableNode *new_nodes = (TableNode *)buffer; - uint32_t *new_active_list = (uint32_t *)(buffer + nodes_size); - { uint32_t i; for (i = 0; i < total_nodes; ++i) { new_nodes[i].entry.key.type_ = VAR_NIL; new_nodes[i].next = 0xFFFFFFFF; new_nodes[i].active_pos = 0xFFFFFFFF; } } - TableNode *prev_nodes = tbl->nodes_; uint32_t *prev_active_list = tbl->active_list_; uint32_t prev_bucket_count = tbl->bucket_count_; uint32_t prev_count = tbl->count_; uint32_t prev_free_list_idx = tbl->free_list_idx_; - tbl->nodes_ = new_nodes; tbl->active_list_ = new_active_list; tbl->bucket_count_ = new_bucket_count; tbl->count_ = 0; + char *buffer = (char *) FlFastAlloc(_S, nodes_size + active_list_size, !__fakelua_init_flag__); + TableNode *new_nodes = (TableNode *) buffer; + uint32_t *new_active_list = (uint32_t *) (buffer + nodes_size); + { + uint32_t i; + for (i = 0; i < total_nodes; ++i) { + new_nodes[i].entry.key.type_ = VAR_NIL; + new_nodes[i].next = 0xFFFFFFFF; + new_nodes[i].active_pos = 0xFFFFFFFF; + } + } + TableNode *prev_nodes = tbl->nodes_; + uint32_t *prev_active_list = tbl->active_list_; + uint32_t prev_bucket_count = tbl->bucket_count_; + uint32_t prev_count = tbl->count_; + uint32_t prev_free_list_idx = tbl->free_list_idx_; + tbl->nodes_ = new_nodes; + tbl->active_list_ = new_active_list; + tbl->bucket_count_ = new_bucket_count; + tbl->count_ = 0; if (LIKELY(overflow_count > 0)) { - { uint32_t i; for (i = 0; i < overflow_count - 1; ++i) { tbl->nodes_[new_bucket_count + i].next = new_bucket_count + i + 1; } } + { + uint32_t i; + for (i = 0; i < overflow_count - 1; ++i) { + tbl->nodes_[new_bucket_count + i].next = new_bucket_count + i + 1; + } + } tbl->nodes_[new_bucket_count + overflow_count - 1].next = 0xFFFFFFFF; tbl->free_list_idx_ = new_bucket_count; } else { @@ -1033,91 +1254,288 @@ static inline void FlTableRehash(VarTable *tbl) { } bool success = true; if (LIKELY(old_bucket_count == 0)) { - { uint32_t i; for (i = 0; i < old_count; ++i) { if (!FlTableInsertRaw(tbl, tbl->quick_data_[i].key, tbl->quick_data_[i].val, tbl->quick_data_[i].hash)) { success = false; break; } } } + { + uint32_t i; + for (i = 0; i < old_count; ++i) { + if (!FlTableInsertRaw(tbl, tbl->quick_data_[i].key, tbl->quick_data_[i].val, tbl->quick_data_[i].hash)) { + success = false; + break; + } + } + } } else { - { uint32_t i; for (i = 0; i < old_bucket_count; ++i) { - uint32_t curr_idx = i; - while (curr_idx != 0xFFFFFFFF) { - TableNode *old_node = &old_nodes[curr_idx]; - if (LIKELY(old_node->entry.key.type_ != VAR_NIL)) { if (!FlTableInsertRaw(tbl, old_node->entry.key, old_node->entry.val, old_node->entry.hash)) { success = false; break; } } - curr_idx = old_node->next; + { + uint32_t i; + for (i = 0; i < old_bucket_count; ++i) { + uint32_t curr_idx = i; + while (curr_idx != 0xFFFFFFFF) { + TableNode *old_node = &old_nodes[curr_idx]; + if (LIKELY(old_node->entry.key.type_ != VAR_NIL)) { + if (!FlTableInsertRaw(tbl, old_node->entry.key, old_node->entry.val, old_node->entry.hash)) { + success = false; + break; + } + } + curr_idx = old_node->next; + } + if (!success) { + break; + } } - if (!success) { break; } - } } + } + } + if (LIKELY(success)) { + break; + } else { + tbl->nodes_ = prev_nodes; + tbl->active_list_ = prev_active_list; + tbl->bucket_count_ = prev_bucket_count; + tbl->count_ = prev_count; + tbl->free_list_idx_ = prev_free_list_idx; + new_bucket_count *= 2; } - if (LIKELY(success)) { break; } else { tbl->nodes_ = prev_nodes; tbl->active_list_ = prev_active_list; tbl->bucket_count_ = prev_bucket_count; tbl->count_ = prev_count; tbl->free_list_idx_ = prev_free_list_idx; new_bucket_count *= 2; } } } static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v); static inline void FlHashSet(VarTable *tbl, CVar k, CVar v) { - uint32_t h; VarHash(k, h); + uint32_t h; + VarHash(k, h); if (UNLIKELY(v.type_ == VAR_NIL)) { - if (UNLIKELY(tbl->count_ == 0)) { return; } + if (UNLIKELY(tbl->count_ == 0)) { + return; + } if (LIKELY(tbl->bucket_count_ == 0)) { bool __eq; - if (tbl->quick_data_[0].hash == h) { VarEqual(tbl->quick_data_[0].key, k, __eq); if (__eq) { if (tbl->count_ > 1) { tbl->quick_data_[0] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { VarEqual(tbl->quick_data_[1].key, k, __eq); if (__eq) { if (tbl->count_ > 2) { tbl->quick_data_[1] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { VarEqual(tbl->quick_data_[2].key, k, __eq); if (__eq) { if (tbl->count_ > 3) { tbl->quick_data_[2] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { VarEqual(tbl->quick_data_[3].key, k, __eq); if (__eq) { if (tbl->count_ > 4) { tbl->quick_data_[3] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { VarEqual(tbl->quick_data_[4].key, k, __eq); if (__eq) { if (tbl->count_ > 5) { tbl->quick_data_[4] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { VarEqual(tbl->quick_data_[5].key, k, __eq); if (__eq) { if (tbl->count_ > 6) { tbl->quick_data_[5] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { VarEqual(tbl->quick_data_[6].key, k, __eq); if (__eq) { if (tbl->count_ > 7) { tbl->quick_data_[6] = tbl->quick_data_[tbl->count_ - 1]; } tbl->count_--; return; } } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { VarEqual(tbl->quick_data_[7].key, k, __eq); if (__eq) { tbl->count_--; return; } } + if (tbl->quick_data_[0].hash == h) { + VarEqual(tbl->quick_data_[0].key, k, __eq); + if (__eq) { + if (tbl->count_ > 1) { + tbl->quick_data_[0] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { + VarEqual(tbl->quick_data_[1].key, k, __eq); + if (__eq) { + if (tbl->count_ > 2) { + tbl->quick_data_[1] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { + VarEqual(tbl->quick_data_[2].key, k, __eq); + if (__eq) { + if (tbl->count_ > 3) { + tbl->quick_data_[2] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { + VarEqual(tbl->quick_data_[3].key, k, __eq); + if (__eq) { + if (tbl->count_ > 4) { + tbl->quick_data_[3] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { + VarEqual(tbl->quick_data_[4].key, k, __eq); + if (__eq) { + if (tbl->count_ > 5) { + tbl->quick_data_[4] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { + VarEqual(tbl->quick_data_[5].key, k, __eq); + if (__eq) { + if (tbl->count_ > 6) { + tbl->quick_data_[5] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { + VarEqual(tbl->quick_data_[6].key, k, __eq); + if (__eq) { + if (tbl->count_ > 7) { + tbl->quick_data_[6] = tbl->quick_data_[tbl->count_ - 1]; + } + tbl->count_--; + return; + } + } + if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { + VarEqual(tbl->quick_data_[7].key, k, __eq); + if (__eq) { + tbl->count_--; + return; + } + } } else { - uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return; } - { bool __eq; VarEqual(curr->entry.key, k, __eq); if (curr->entry.hash == h && __eq) { - if (curr->next != 0xFFFFFFFF) { - uint32_t next_idx = curr->next; TableNode *next_node = &tbl->nodes_[next_idx]; - uint32_t pos = next_node->active_pos; uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (next_idx != last_node_idx) { tbl->active_list_[pos] = last_node_idx; tbl->nodes_[last_node_idx].active_pos = pos; } - next_node->active_pos = 0xFFFFFFFF; curr->entry = next_node->entry; curr->next = next_node->next; next_node->next = tbl->free_list_idx_; tbl->free_list_idx_ = next_idx; - } else { - uint32_t pos = curr->active_pos; uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (idx != last_node_idx) { tbl->active_list_[pos] = last_node_idx; tbl->nodes_[last_node_idx].active_pos = pos; } - curr->active_pos = 0xFFFFFFFF; curr->entry.key.type_ = VAR_NIL; + uint32_t mask = tbl->bucket_count_ - 1; + uint32_t idx = h & mask; + TableNode *curr = &tbl->nodes_[idx]; + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return; + } + { + bool __eq; + VarEqual(curr->entry.key, k, __eq); + if (curr->entry.hash == h && __eq) { + if (curr->next != 0xFFFFFFFF) { + uint32_t next_idx = curr->next; + TableNode *next_node = &tbl->nodes_[next_idx]; + uint32_t pos = next_node->active_pos; + uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; + if (next_idx != last_node_idx) { + tbl->active_list_[pos] = last_node_idx; + tbl->nodes_[last_node_idx].active_pos = pos; + } + next_node->active_pos = 0xFFFFFFFF; + curr->entry = next_node->entry; + curr->next = next_node->next; + next_node->next = tbl->free_list_idx_; + tbl->free_list_idx_ = next_idx; + } else { + uint32_t pos = curr->active_pos; + uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; + if (idx != last_node_idx) { + tbl->active_list_[pos] = last_node_idx; + tbl->nodes_[last_node_idx].active_pos = pos; + } + curr->active_pos = 0xFFFFFFFF; + curr->entry.key.type_ = VAR_NIL; + } + tbl->count_--; + return; } - tbl->count_--; return; - } } - uint32_t prev_idx = idx; uint32_t curr_idx = curr->next; + } + uint32_t prev_idx = idx; + uint32_t curr_idx = curr->next; while (curr_idx != 0xFFFFFFFF) { TableNode *node = &tbl->nodes_[curr_idx]; - { bool __eq; VarEqual(node->entry.key, k, __eq); if (node->entry.hash == h && __eq) { - uint32_t pos = node->active_pos; uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; - if (curr_idx != last_node_idx) { tbl->active_list_[pos] = last_node_idx; tbl->nodes_[last_node_idx].active_pos = pos; } - node->active_pos = 0xFFFFFFFF; tbl->nodes_[prev_idx].next = node->next; node->next = tbl->free_list_idx_; tbl->free_list_idx_ = curr_idx; tbl->count_--; return; - } } - prev_idx = curr_idx; curr_idx = node->next; + { + bool __eq; + VarEqual(node->entry.key, k, __eq); + if (node->entry.hash == h && __eq) { + uint32_t pos = node->active_pos; + uint32_t last_node_idx = tbl->active_list_[tbl->count_ - 1]; + if (curr_idx != last_node_idx) { + tbl->active_list_[pos] = last_node_idx; + tbl->nodes_[last_node_idx].active_pos = pos; + } + node->active_pos = 0xFFFFFFFF; + tbl->nodes_[prev_idx].next = node->next; + node->next = tbl->free_list_idx_; + tbl->free_list_idx_ = curr_idx; + tbl->count_--; + return; + } + } + prev_idx = curr_idx; + curr_idx = node->next; } } return; } if (LIKELY(tbl->bucket_count_ == 0)) { bool __eq; - if (tbl->count_ > 0 && tbl->quick_data_[0].hash == h) { VarEqual(tbl->quick_data_[0].key, k, __eq); if (__eq) { tbl->quick_data_[0].val = v; return; } } - if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { VarEqual(tbl->quick_data_[1].key, k, __eq); if (__eq) { tbl->quick_data_[1].val = v; return; } } - if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { VarEqual(tbl->quick_data_[2].key, k, __eq); if (__eq) { tbl->quick_data_[2].val = v; return; } } - if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { VarEqual(tbl->quick_data_[3].key, k, __eq); if (__eq) { tbl->quick_data_[3].val = v; return; } } - if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { VarEqual(tbl->quick_data_[4].key, k, __eq); if (__eq) { tbl->quick_data_[4].val = v; return; } } - if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { VarEqual(tbl->quick_data_[5].key, k, __eq); if (__eq) { tbl->quick_data_[5].val = v; return; } } - if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { VarEqual(tbl->quick_data_[6].key, k, __eq); if (__eq) { tbl->quick_data_[6].val = v; return; } } - if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { VarEqual(tbl->quick_data_[7].key, k, __eq); if (__eq) { tbl->quick_data_[7].val = v; return; } } - if (tbl->count_ < 8) { tbl->quick_data_[tbl->count_].key = k; tbl->quick_data_[tbl->count_].val = v; tbl->quick_data_[tbl->count_].hash = h; tbl->count_++; return; } - if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { return; } + if (tbl->count_ > 0 && tbl->quick_data_[0].hash == h) { + VarEqual(tbl->quick_data_[0].key, k, __eq); + if (__eq) { + tbl->quick_data_[0].val = v; + return; + } + } + if (tbl->count_ > 1 && tbl->quick_data_[1].hash == h) { + VarEqual(tbl->quick_data_[1].key, k, __eq); + if (__eq) { + tbl->quick_data_[1].val = v; + return; + } + } + if (tbl->count_ > 2 && tbl->quick_data_[2].hash == h) { + VarEqual(tbl->quick_data_[2].key, k, __eq); + if (__eq) { + tbl->quick_data_[2].val = v; + return; + } + } + if (tbl->count_ > 3 && tbl->quick_data_[3].hash == h) { + VarEqual(tbl->quick_data_[3].key, k, __eq); + if (__eq) { + tbl->quick_data_[3].val = v; + return; + } + } + if (tbl->count_ > 4 && tbl->quick_data_[4].hash == h) { + VarEqual(tbl->quick_data_[4].key, k, __eq); + if (__eq) { + tbl->quick_data_[4].val = v; + return; + } + } + if (tbl->count_ > 5 && tbl->quick_data_[5].hash == h) { + VarEqual(tbl->quick_data_[5].key, k, __eq); + if (__eq) { + tbl->quick_data_[5].val = v; + return; + } + } + if (tbl->count_ > 6 && tbl->quick_data_[6].hash == h) { + VarEqual(tbl->quick_data_[6].key, k, __eq); + if (__eq) { + tbl->quick_data_[6].val = v; + return; + } + } + if (tbl->count_ > 7 && tbl->quick_data_[7].hash == h) { + VarEqual(tbl->quick_data_[7].key, k, __eq); + if (__eq) { + tbl->quick_data_[7].val = v; + return; + } + } + if (tbl->count_ < 8) { + tbl->quick_data_[tbl->count_].key = k; + tbl->quick_data_[tbl->count_].val = v; + tbl->quick_data_[tbl->count_].hash = h; + tbl->count_++; + return; + } + if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { + return; + } FlTableRehash(tbl); } if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { return; } + if (k.type_ == VAR_INT && FlArrResizeInsert(tbl, k, v)) { + return; + } FlTableRehash(tbl); } FlTableInsertRaw(tbl, k, v, h); } static inline CVar FlHashGetInt(VarTable *tbl, int64_t k) { - if (UNLIKELY(tbl->count_ == 0)) { return (CVar){VAR_NIL}; } - uint32_t h = (uint32_t)(k ^ (k >> 32)); + if (UNLIKELY(tbl->count_ == 0)) { + return (CVar) {VAR_NIL}; + } + uint32_t h = (uint32_t) (k ^ (k >> 32)); if (LIKELY(tbl->bucket_count_ == 0)) { for (uint32_t i = 0; i < tbl->count_; ++i) { if (tbl->quick_data_[i].hash == h && tbl->quick_data_[i].key.type_ == VAR_INT && tbl->quick_data_[i].key.data_.i == k) { @@ -1128,23 +1546,29 @@ static inline CVar FlHashGetInt(VarTable *tbl, int64_t k) { uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return (CVar){VAR_NIL}; } + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return (CVar) {VAR_NIL}; + } while (1) { if (curr->entry.hash == h && curr->entry.key.type_ == VAR_INT && curr->entry.key.data_.i == k) { return curr->entry.val; } uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { break; } + if (next == 0xFFFFFFFF) { + break; + } curr = &tbl->nodes_[next]; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } __attribute__((noinline)) static void FlArrGrow(VarTable *tbl) { uint32_t cap = tbl->arr_cap_ ? tbl->arr_cap_ * 2 : 4; - CVar *na = (CVar *)FlFastAlloc(_S, (size_t)cap * sizeof(CVar), !__fakelua_init_flag__); - if (tbl->arr_size_ > 0) { memcpy(na, tbl->arr_, (size_t)tbl->arr_size_ * sizeof(CVar)); } + CVar *na = (CVar *) FlFastAlloc(_S, (size_t) cap * sizeof(CVar), !__fakelua_init_flag__); + if (tbl->arr_size_ > 0) { + memcpy(na, tbl->arr_, (size_t) tbl->arr_size_ * sizeof(CVar)); + } tbl->arr_ = na; tbl->arr_cap_ = cap; } @@ -1152,24 +1576,38 @@ __attribute__((noinline)) static void FlArrGrow(VarTable *tbl) { /* 数组末尾追加后,把哈希部分中紧随其后的整数键迁入数组,维持「哈希不含 [1, arr_size_+1]」。*/ __attribute__((noinline)) static void FlArrMigrate(VarTable *tbl) { while (tbl->count_ != 0) { - int64_t nk = (int64_t)tbl->arr_size_ + 1; + int64_t nk = (int64_t) tbl->arr_size_ + 1; CVar hv = FlHashGetInt(tbl, nk); - if (hv.type_ == VAR_NIL) { break; } - CVar key; key.type_ = VAR_INT; key.flag_ = 0; key.data_.i = nk; - FlHashSet(tbl, key, (CVar){VAR_NIL}); - if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { FlArrGrow(tbl); } + if (hv.type_ == VAR_NIL) { + break; + } + CVar key; + key.type_ = VAR_INT; + key.flag_ = 0; + key.data_.i = nk; + FlHashSet(tbl, key, (CVar) {VAR_NIL}); + if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { + FlArrGrow(tbl); + } tbl->arr_[tbl->arr_size_++] = hv; } } /* 整数键写入的数组部分快路径。返回 true 表示已处理。*/ static inline bool FlArrSetInt(VarTable *tbl, int64_t k, CVar v) { - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_)) { tbl->arr_[idx] = v; return true; } - if (idx == (uint64_t)tbl->arr_size_ && v.type_ != VAR_NIL && !tbl->spec_set) { - if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { FlArrGrow(tbl); } + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_)) { + tbl->arr_[idx] = v; + return true; + } + if (idx == (uint64_t) tbl->arr_size_ && v.type_ != VAR_NIL && !tbl->spec_set) { + if (UNLIKELY(tbl->arr_size_ == tbl->arr_cap_)) { + FlArrGrow(tbl); + } tbl->arr_[tbl->arr_size_++] = v; - if (UNLIKELY(tbl->count_ != 0)) { FlArrMigrate(tbl); } + if (UNLIKELY(tbl->count_ != 0)) { + FlArrMigrate(tbl); + } return true; } return false; @@ -1182,19 +1620,31 @@ __attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) uint64_t total = 0; uint32_t i; memset(nums, 0, sizeof(nums)); -#define FL_COUNT_INT_KEY(kk) do { \ - int64_t __ck = (kk); \ - if (__ck >= 1 && __ck <= (1LL << 30)) { nums[__ck == 1 ? 0 : 64 - __builtin_clzll((uint64_t)__ck - 1)]++; total++; } \ -} while(0) - for (i = 0; i < tbl->arr_size_; ++i) { if (tbl->arr_[i].type_ != VAR_NIL) { FL_COUNT_INT_KEY((int64_t)i + 1); } } +#define FL_COUNT_INT_KEY(kk) \ + do { \ + int64_t __ck = (kk); \ + if (__ck >= 1 && __ck <= (1LL << 30)) { \ + nums[__ck == 1 ? 0 : 64 - __builtin_clzll((uint64_t) __ck - 1)]++; \ + total++; \ + } \ + } while (0) + for (i = 0; i < tbl->arr_size_; ++i) { + if (tbl->arr_[i].type_ != VAR_NIL) { + FL_COUNT_INT_KEY((int64_t) i + 1); + } + } if (tbl->bucket_count_ == 0) { for (i = 0; i < tbl->count_; ++i) { - if (tbl->quick_data_[i].key.type_ == VAR_INT) { FL_COUNT_INT_KEY(tbl->quick_data_[i].key.data_.i); } + if (tbl->quick_data_[i].key.type_ == VAR_INT) { + FL_COUNT_INT_KEY(tbl->quick_data_[i].key.data_.i); + } } } else { for (i = 0; i < tbl->count_; ++i) { VarEntry *e = &tbl->nodes_[tbl->active_list_[i]].entry; - if (e->key.type_ == VAR_INT) { FL_COUNT_INT_KEY(e->key.data_.i); } + if (e->key.type_ == VAR_INT) { + FL_COUNT_INT_KEY(e->key.data_.i); + } } } FL_COUNT_INT_KEY(nk); @@ -1202,24 +1652,35 @@ __attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) uint64_t a = 0, optimal = 0, two = 1; for (i = 0; i < 31 && total > two / 2; ++i, two <<= 1) { a += nums[i]; - if (a > two / 2) { optimal = two; } + if (a > two / 2) { + optimal = two; + } + } + if (optimal <= (uint64_t) tbl->arr_size_) { + return false; } - if (optimal <= (uint64_t)tbl->arr_size_) { return false; } { uint32_t old_size = tbl->arr_size_; - CVar *na = (CVar *)FlFastAlloc(_S, (size_t)optimal * sizeof(CVar), !__fakelua_init_flag__); - if (old_size > 0) { memcpy(na, tbl->arr_, (size_t)old_size * sizeof(CVar)); } - memset(na + old_size, 0, (size_t)(optimal - old_size) * sizeof(CVar)); + CVar *na = (CVar *) FlFastAlloc(_S, (size_t) optimal * sizeof(CVar), !__fakelua_init_flag__); + if (old_size > 0) { + memcpy(na, tbl->arr_, (size_t) old_size * sizeof(CVar)); + } + memset(na + old_size, 0, (size_t) (optimal - old_size) * sizeof(CVar)); tbl->arr_ = na; - tbl->arr_cap_ = (uint32_t)optimal; - tbl->arr_size_ = (uint32_t)optimal; + tbl->arr_cap_ = (uint32_t) optimal; + tbl->arr_size_ = (uint32_t) optimal; if (tbl->count_ != 0) { uint64_t j; for (j = old_size + 1; j <= optimal && tbl->count_ != 0; ++j) { - CVar hv = FlHashGetInt(tbl, (int64_t)j); - if (hv.type_ == VAR_NIL) { continue; } - CVar key; key.type_ = VAR_INT; key.flag_ = 0; key.data_.i = (int64_t)j; - FlHashSet(tbl, key, (CVar){VAR_NIL}); + CVar hv = FlHashGetInt(tbl, (int64_t) j); + if (hv.type_ == VAR_NIL) { + continue; + } + CVar key; + key.type_ = VAR_INT; + key.flag_ = 0; + key.data_.i = (int64_t) j; + FlHashSet(tbl, key, (CVar) {VAR_NIL}); tbl->arr_[j - 1] = hv; } FlArrMigrate(tbl); @@ -1230,10 +1691,16 @@ __attribute__((noinline)) static bool FlArrTryResize(VarTable *tbl, int64_t nk) /* 整数键 k 要插入已写满的哈希部分前调用;返回 true 表示 k 已写入数组或哈希空位,调用方直接返回。*/ static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v) { - if (k.type_ != VAR_INT || tbl->spec_set) { return false; } - if (!FlArrTryResize(tbl, k.data_.i)) { return false; } - if (FlArrSetInt(tbl, k.data_.i, v)) { return true; } - uint32_t h = (uint32_t)(k.data_.i ^ (k.data_.i >> 32)); + if (k.type_ != VAR_INT || tbl->spec_set) { + return false; + } + if (!FlArrTryResize(tbl, k.data_.i)) { + return false; + } + if (FlArrSetInt(tbl, k.data_.i, v)) { + return true; + } + uint32_t h = (uint32_t) (k.data_.i ^ (k.data_.i >> 32)); if (tbl->bucket_count_ == 0) { if (tbl->count_ < 8) { tbl->quick_data_[tbl->count_].key = k; @@ -1253,17 +1720,25 @@ static inline bool FlArrResizeInsert(VarTable *tbl, CVar k, CVar v) { static inline void FlSetTableImpl(CVar t, CVar k, CVar v) { k = NORMALIZE_TABLE_KEY(k); - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } FL_CHECK_CONST_TABLE(t); - if (UNLIKELY(k.type_ == VAR_NIL)) { FakeluaThrowError(_S, "table index is nil"); } - if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { FakeluaThrowError(_S, "table index is NaN"); } + if (UNLIKELY(k.type_ == VAR_NIL)) { + FakeluaThrowError(_S, "table index is nil"); + } + if (UNLIKELY(k.type_ == VAR_FLOAT && isnan(k.data_.f))) { + FakeluaThrowError(_S, "table index is NaN"); + } VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_set)) { bool __finish = false; tbl->spec_set(tbl, k, v, &__finish); if (__finish) return; } - if (k.type_ == VAR_INT && FlArrSetInt(tbl, k.data_.i, v)) { return; } + if (k.type_ == VAR_INT && FlArrSetInt(tbl, k.data_.i, v)) { + return; + } FlHashSet(tbl, k, v); } @@ -1271,16 +1746,25 @@ static inline void FlSetTable(CVar t, CVar k, CVar v) { k = NORMALIZE_TABLE_KEY(k); FlSetTableImpl(t, k, v); if (LIKELY(t.type_ == VAR_TABLE)) { - if (k.type_ == VAR_INT) { FlSeqNoteIntSet(t.data_.t, k.data_.i, v.type_ == VAR_NIL); } + if (k.type_ == VAR_INT) { + FlSeqNoteIntSet(t.data_.t, k.data_.i, v.type_ == VAR_NIL); + } } } static inline CVar FlGetTableInt(CVar t, int64_t k) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } VarTable *tbl = t.data_.t; - if (LIKELY((uint64_t)k - 1 < (uint64_t)tbl->arr_size_)) { return tbl->arr_[k - 1]; } + if (LIKELY((uint64_t) k - 1 < (uint64_t) tbl->arr_size_)) { + return tbl->arr_[k - 1]; + } if (UNLIKELY(tbl->spec_get)) { - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; bool __finish = false; CVar __r = tbl->spec_get(tbl, key_cvar, &__finish); if (__finish) return __r; @@ -1289,18 +1773,28 @@ static inline CVar FlGetTableInt(CVar t, int64_t k) { } static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } FL_CHECK_CONST_TABLE(t); VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_set)) { - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; bool __finish = false; tbl->spec_set(tbl, key_cvar, v, &__finish); if (__finish) return; } - if (FlArrSetInt(tbl, k, v)) { return; } - uint32_t h = (uint32_t)(k ^ (k >> 32)); - CVar key_cvar; key_cvar.type_ = VAR_INT; key_cvar.flag_ = 0; key_cvar.data_.i = k; + if (FlArrSetInt(tbl, k, v)) { + return; + } + uint32_t h = (uint32_t) (k ^ (k >> 32)); + CVar key_cvar; + key_cvar.type_ = VAR_INT; + key_cvar.flag_ = 0; + key_cvar.data_.i = k; if (UNLIKELY(v.type_ == VAR_NIL)) { FlHashSet(tbl, key_cvar, v); return; @@ -1308,20 +1802,26 @@ static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v) { if (LIKELY(tbl->bucket_count_ == 0)) { for (uint32_t i = 0; i < tbl->count_; ++i) { if (tbl->quick_data_[i].hash == h && tbl->quick_data_[i].key.type_ == VAR_INT && tbl->quick_data_[i].key.data_.i == k) { - tbl->quick_data_[i].val = v; return; + tbl->quick_data_[i].val = v; + return; } } if (tbl->count_ < 8) { tbl->quick_data_[tbl->count_].key = key_cvar; tbl->quick_data_[tbl->count_].val = v; tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; return; + tbl->count_++; + return; + } + if (FlArrResizeInsert(tbl, key_cvar, v)) { + return; } - if (FlArrResizeInsert(tbl, key_cvar, v)) { return; } FlTableRehash(tbl); } if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { - if (FlArrResizeInsert(tbl, key_cvar, v)) { return; } + if (FlArrResizeInsert(tbl, key_cvar, v)) { + return; + } FlTableRehash(tbl); } FlTableInsertRaw(tbl, key_cvar, v, h); @@ -1330,88 +1830,118 @@ static inline void FlSetTableIntImpl(CVar t, int64_t k, CVar v) { static inline void FlSetTableInt(CVar t, int64_t k, CVar v) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; + uint64_t idx = (uint64_t) k - 1; /* 新旧值都非 nil 时序列长度不变,可跳过缓存维护。*/ - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && v.type_ != VAR_NIL && tbl->arr_[idx].type_ != VAR_NIL)) { + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && v.type_ != VAR_NIL && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx] = v; return; } } FlSetTableIntImpl(t, k, v); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, v.type_ == VAR_NIL); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, v.type_ == VAR_NIL); + } } static inline void FlSetTableInt_Int(CVar t, int64_t k, int64_t v) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx].type_ = VAR_INT; tbl->arr_[idx].flag_ = 0; tbl->arr_[idx].data_.i = v; return; } } - CVar cv; cv.type_ = VAR_INT; cv.flag_ = 0; cv.data_.i = v; + CVar cv; + cv.type_ = VAR_INT; + cv.flag_ = 0; + cv.data_.i = v; FlSetTableIntImpl(t, k, cv); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, false); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, false); + } } static inline void FlSetTableInt_Float(CVar t, int64_t k, double v) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx].type_ = VAR_FLOAT; tbl->arr_[idx].flag_ = 0; tbl->arr_[idx].data_.f = v; return; } } - CVar cv; cv.type_ = VAR_FLOAT; cv.flag_ = 0; cv.data_.f = v; + CVar cv; + cv.type_ = VAR_FLOAT; + cv.flag_ = 0; + cv.data_.f = v; FlSetTableIntImpl(t, k, cv); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, false); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, false); + } } static inline void FlSetTableInt_Bool(CVar t, int64_t k, bool b) { if (LIKELY(t.type_ == VAR_TABLE && (!(t.flag_ & CONST_FLAG) || !__fakelua_init_flag__))) { VarTable *tbl = t.data_.t; - uint64_t idx = (uint64_t)k - 1; - if (LIKELY(idx < (uint64_t)tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { + uint64_t idx = (uint64_t) k - 1; + if (LIKELY(idx < (uint64_t) tbl->arr_size_ && tbl->arr_[idx].type_ != VAR_NIL)) { tbl->arr_[idx].type_ = VAR_BOOL; tbl->arr_[idx].flag_ = 0; tbl->arr_[idx].data_.b = b; return; } } - CVar cv; cv.type_ = VAR_BOOL; cv.flag_ = 0; cv.data_.b = b; + CVar cv; + cv.type_ = VAR_BOOL; + cv.flag_ = 0; + cv.data_.b = b; FlSetTableIntImpl(t, k, cv); - if (LIKELY(t.type_ == VAR_TABLE)) { FlSeqNoteIntSet(t.data_.t, k, false); } + if (LIKELY(t.type_ == VAR_TABLE)) { + FlSeqNoteIntSet(t.data_.t, k, false); + } } static inline CVar FlGetTableStrId(CVar t, int64_t str_id) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_get)) { - CVar k; k.type_ = VAR_STRINGID; k.flag_ = 0; k.data_.i = str_id; + CVar k; + k.type_ = VAR_STRINGID; + k.flag_ = 0; + k.data_.i = str_id; bool __finish = false; CVar __r = tbl->spec_get(tbl, k, &__finish); if (__finish) return __r; } // hash fallback - if (UNLIKELY(tbl->count_ == 0)) { return (CVar){VAR_NIL}; } - VarString *vs = (VarString *)str_id; + if (UNLIKELY(tbl->count_ == 0)) { + return (CVar) {VAR_NIL}; + } + VarString *vs = (VarString *) str_id; uint32_t h = LIKELY(vs->hash_ != 0) ? vs->hash_ : (vs->hash_ = FlHashString(vs->data_, vs->size_)); if (LIKELY(tbl->bucket_count_ == 0)) { for (uint32_t i = 0; i < tbl->count_; ++i) { if (tbl->quick_data_[i].hash == h) { CVar ek = tbl->quick_data_[i].key; if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { return tbl->quick_data_[i].val; } - VarString *evs = (VarString *)ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { return tbl->quick_data_[i].val; } + if (ek.data_.i == str_id) { + return tbl->quick_data_[i].val; + } + VarString *evs = (VarString *) ek.data_.i; + if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { + return tbl->quick_data_[i].val; + } } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { return tbl->quick_data_[i].val; } + if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { + return tbl->quick_data_[i].val; + } } } } @@ -1419,42 +1949,60 @@ static inline CVar FlGetTableStrId(CVar t, int64_t str_id) { uint32_t mask = tbl->bucket_count_ - 1; uint32_t idx = h & mask; TableNode *curr = &tbl->nodes_[idx]; - if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { return (CVar){VAR_NIL}; } + if (UNLIKELY(curr->entry.key.type_ == VAR_NIL)) { + return (CVar) {VAR_NIL}; + } while (1) { if (curr->entry.hash == h) { CVar ek = curr->entry.key; if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { return curr->entry.val; } - VarString *evs = (VarString *)ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { return curr->entry.val; } + if (ek.data_.i == str_id) { + return curr->entry.val; + } + VarString *evs = (VarString *) ek.data_.i; + if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { + return curr->entry.val; + } } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { return curr->entry.val; } + if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { + return curr->entry.val; + } } } uint32_t next = curr->next; - if (next == 0xFFFFFFFF) { break; } + if (next == 0xFFFFFFFF) { + break; + } curr = &tbl->nodes_[next]; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } static inline void FlSetTableStrId(CVar t, int64_t str_id, CVar v) { - if (UNLIKELY(t.type_ != VAR_TABLE)) { FakeluaThrowError(_S, "attempt to index a non-table value"); } + if (UNLIKELY(t.type_ != VAR_TABLE)) { + FakeluaThrowError(_S, "attempt to index a non-table value"); + } FL_CHECK_CONST_TABLE(t); VarTable *tbl = t.data_.t; if (UNLIKELY(tbl->spec_set)) { - CVar k; k.type_ = VAR_STRINGID; k.flag_ = 0; k.data_.i = str_id; + CVar k; + k.type_ = VAR_STRINGID; + k.flag_ = 0; + k.data_.i = str_id; bool __finish = false; tbl->spec_set(tbl, k, v, &__finish); if (__finish) return; } // hash fallback - VarString *vs = (VarString *)str_id; + VarString *vs = (VarString *) str_id; uint32_t h = LIKELY(vs->hash_ != 0) ? vs->hash_ : (vs->hash_ = FlHashString(vs->data_, vs->size_)); - CVar key_cvar; key_cvar.type_ = VAR_STRINGID; key_cvar.flag_ = 0; key_cvar.data_.i = str_id; + CVar key_cvar; + key_cvar.type_ = VAR_STRINGID; + key_cvar.flag_ = 0; + key_cvar.data_.i = str_id; if (UNLIKELY(v.type_ == VAR_NIL)) { - FlSetTable((CVar){.type_ = VAR_TABLE, .data_.t = tbl}, key_cvar, v); + FlSetTable((CVar) {.type_ = VAR_TABLE, .data_.t = tbl}, key_cvar, v); return; } if (LIKELY(tbl->bucket_count_ == 0)) { @@ -1462,11 +2010,20 @@ static inline void FlSetTableStrId(CVar t, int64_t str_id, CVar v) { if (tbl->quick_data_[i].hash == h) { CVar ek = tbl->quick_data_[i].key; if (LIKELY(ek.type_ == VAR_STRINGID)) { - if (ek.data_.i == str_id) { tbl->quick_data_[i].val = v; return; } - VarString *evs = (VarString *)ek.data_.i; - if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { tbl->quick_data_[i].val = v; return; } + if (ek.data_.i == str_id) { + tbl->quick_data_[i].val = v; + return; + } + VarString *evs = (VarString *) ek.data_.i; + if (evs->size_ == vs->size_ && memcmp(evs->data_, vs->data_, vs->size_) == 0) { + tbl->quick_data_[i].val = v; + return; + } } else if (UNLIKELY(ek.type_ == VAR_STRING)) { - if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { tbl->quick_data_[i].val = v; return; } + if (ek.data_.s->size_ == vs->size_ && memcmp(ek.data_.s->data_, vs->data_, vs->size_) == 0) { + tbl->quick_data_[i].val = v; + return; + } } } } @@ -1474,11 +2031,14 @@ static inline void FlSetTableStrId(CVar t, int64_t str_id, CVar v) { tbl->quick_data_[tbl->count_].key = key_cvar; tbl->quick_data_[tbl->count_].val = v; tbl->quick_data_[tbl->count_].hash = h; - tbl->count_++; return; + tbl->count_++; + return; } FlTableRehash(tbl); } - if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { FlTableRehash(tbl); } + if (UNLIKELY(tbl->count_ >= tbl->bucket_count_ || tbl->free_list_idx_ == 0xFFFFFFFF)) { + FlTableRehash(tbl); + } FlTableInsertRaw(tbl, key_cvar, v, h); } @@ -1493,135 +2053,222 @@ static inline void FlTableExpandMulti(CVar t, int64_t start_idx, CVar v) { } } - -#define CheckNum(v) do { \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ - FakeluaThrowError(_S, "attempt to perform arithmetic on non-numeric value"); \ - } \ -} while(0) - -#define CheckInt(v, result) do { \ - if (LIKELY((v).type_ == VAR_INT)) { (result) = (v).data_.i; } \ - else if (UNLIKELY((v).type_ == VAR_FLOAT)) { \ - if (!FlDoubleFitsInt64((v).data_.f, &(result))) { \ - FakeluaThrowError(_S, "number has no integer representation"); \ - } \ - } else { \ - FakeluaThrowError(_S, "attempt to perform bitwise operation on non-numeric value"); \ - (result) = 0; \ - } \ -} while(0) - -#define CVAR_TO_DOUBLE(v) ((v).type_ == VAR_INT ? (double)(v).data_.i : (v).data_.f) - -#define FL_INT_ADD(a, b) ((int64_t)((uint64_t)(int64_t)(a) + (uint64_t)(int64_t)(b))) -#define FL_INT_SUB(a, b) ((int64_t)((uint64_t)(int64_t)(a) - (uint64_t)(int64_t)(b))) -#define FL_INT_MUL(a, b) ((int64_t)((uint64_t)(int64_t)(a) * (uint64_t)(int64_t)(b))) - -#define OP_ARITH_IMPL(a, b, res, op) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, _ra.data_.i op _rb.data_.i); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) op CVAR_TO_DOUBLE(_rb)); } \ -} while(0) - -#define OpAdd(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, FL_INT_ADD(_ra.data_.i, _rb.data_.i)); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) + CVAR_TO_DOUBLE(_rb)); } \ -} while(0) -#define OpSub(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, FL_INT_SUB(_ra.data_.i, _rb.data_.i)); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) - CVAR_TO_DOUBLE(_rb)); } \ -} while(0) -#define OpMul(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { SET_INT(res, FL_INT_MUL(_ra.data_.i, _rb.data_.i)); } \ - else { CheckNum(_ra); CheckNum(_rb); SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) * CVAR_TO_DOUBLE(_rb)); } \ -} while(0) - -#define OpDiv(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); CheckNum(_ra); CheckNum(_rb); \ - SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb)); \ -} while(0) - -#define OpFloorDiv(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - int64_t _q; \ - FlFloorDivInt(_ra.data_.i, _rb.data_.i, _q); \ - SET_INT(res, _q); \ - } else { \ - CheckNum(_ra); CheckNum(_rb); \ - SET_FLOAT(res, floor(CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb))); \ - } \ -} while(0) - -#define OpPow(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); CheckNum(_ra); CheckNum(_rb); \ - SET_FLOAT(res, pow(CVAR_TO_DOUBLE(_ra), CVAR_TO_DOUBLE(_rb))); \ -} while(0) - -#define OpMod(a, b, res) do { \ - CVar _ra = (a); CVar _rb = (b); \ - if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ - int64_t _r; \ - FlModInt(_ra.data_.i, _rb.data_.i, _r); \ - SET_INT(res, _r); \ - } else { \ - CheckNum(_ra); CheckNum(_rb); \ - double _fa = CVAR_TO_DOUBLE(_ra); \ - double _fb = CVAR_TO_DOUBLE(_rb); \ - SET_FLOAT(res, _fa - _fb * floor(_fa / _fb)); \ - } \ -} while(0) - -#define OP_BIT_IMPL(a, b, res, op) do { int64_t _ai; int64_t _bi; CheckInt(a, _ai); CheckInt(b, _bi); SET_INT(res, _ai op _bi); } while(0) +#define CheckNum(v) \ + do { \ + if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ + FakeluaThrowError(_S, "attempt to perform arithmetic on non-numeric value"); \ + } \ + } while (0) + +#define CheckInt(v, result) \ + do { \ + if (LIKELY((v).type_ == VAR_INT)) { \ + (result) = (v).data_.i; \ + } else if (UNLIKELY((v).type_ == VAR_FLOAT)) { \ + if (!FlDoubleFitsInt64((v).data_.f, &(result))) { \ + FakeluaThrowError(_S, "number has no integer representation"); \ + } \ + } else { \ + FakeluaThrowError(_S, "attempt to perform bitwise operation on non-numeric value"); \ + (result) = 0; \ + } \ + } while (0) + +#define CVAR_TO_DOUBLE(v) ((v).type_ == VAR_INT ? (double) (v).data_.i : (v).data_.f) + +#define FL_INT_ADD(a, b) ((int64_t) ((uint64_t) (int64_t) (a) + (uint64_t) (int64_t) (b))) +#define FL_INT_SUB(a, b) ((int64_t) ((uint64_t) (int64_t) (a) - (uint64_t) (int64_t) (b))) +#define FL_INT_MUL(a, b) ((int64_t) ((uint64_t) (int64_t) (a) * (uint64_t) (int64_t) (b))) + +#define OP_ARITH_IMPL(a, b, res, op) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, _ra.data_.i op _rb.data_.i); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) op CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) + +#define OpAdd(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_ADD(_ra.data_.i, _rb.data_.i)); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) + CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) +#define OpSub(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_SUB(_ra.data_.i, _rb.data_.i)); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) - CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) +#define OpMul(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_MUL(_ra.data_.i, _rb.data_.i)); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) * CVAR_TO_DOUBLE(_rb)); \ + } \ + } while (0) + +#define OpDiv(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb)); \ + } while (0) + +#define OpFloorDiv(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + int64_t _q; \ + FlFloorDivInt(_ra.data_.i, _rb.data_.i, _q); \ + SET_INT(res, _q); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, floor(CVAR_TO_DOUBLE(_ra) / CVAR_TO_DOUBLE(_rb))); \ + } \ + } while (0) + +#define OpPow(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + CheckNum(_ra); \ + CheckNum(_rb); \ + SET_FLOAT(res, pow(CVAR_TO_DOUBLE(_ra), CVAR_TO_DOUBLE(_rb))); \ + } while (0) + +#define OpMod(a, b, res) \ + do { \ + CVar _ra = (a); \ + CVar _rb = (b); \ + if (LIKELY(_ra.type_ == VAR_INT && _rb.type_ == VAR_INT)) { \ + int64_t _r; \ + FlModInt(_ra.data_.i, _rb.data_.i, _r); \ + SET_INT(res, _r); \ + } else { \ + CheckNum(_ra); \ + CheckNum(_rb); \ + double _fa = CVAR_TO_DOUBLE(_ra); \ + double _fb = CVAR_TO_DOUBLE(_rb); \ + SET_FLOAT(res, _fa - _fb * floor(_fa / _fb)); \ + } \ + } while (0) + +#define OP_BIT_IMPL(a, b, res, op) \ + do { \ + int64_t _ai; \ + int64_t _bi; \ + CheckInt(a, _ai); \ + CheckInt(b, _bi); \ + SET_INT(res, _ai op _bi); \ + } while (0) #define OpBitAnd(a, b, res) OP_BIT_IMPL(a, b, res, &) #define OpBitXor(a, b, res) OP_BIT_IMPL(a, b, res, ^) #define OpBitOr(a, b, res) OP_BIT_IMPL(a, b, res, |) -#define OP_SHIFT_IMPL(a, b, res, _right) do { \ - int64_t _ai; int64_t _bi; CheckInt(a, _ai); CheckInt(b, _bi); \ - if (_bi >= 64 || _bi <= -64) { SET_INT(res, 0); } \ - else if (_bi >= 0) { SET_INT(res, (int64_t)((_right) ? ((uint64_t)_ai >> _bi) : ((uint64_t)_ai << _bi))); } \ - else { SET_INT(res, (int64_t)((_right) ? ((uint64_t)_ai << (-_bi)) : ((uint64_t)_ai >> (-_bi)))); } \ -} while(0) +#define OP_SHIFT_IMPL(a, b, res, _right) \ + do { \ + int64_t _ai; \ + int64_t _bi; \ + CheckInt(a, _ai); \ + CheckInt(b, _bi); \ + if (_bi >= 64 || _bi <= -64) { \ + SET_INT(res, 0); \ + } else if (_bi >= 0) { \ + SET_INT(res, (int64_t) ((_right) ? ((uint64_t) _ai >> _bi) : ((uint64_t) _ai << _bi))); \ + } else { \ + SET_INT(res, (int64_t) ((_right) ? ((uint64_t) _ai << (-_bi)) : ((uint64_t) _ai >> (-_bi)))); \ + } \ + } while (0) #define OpRightShift(a, b, res) OP_SHIFT_IMPL(a, b, res, 1) #define OpLeftShift(a, b, res) OP_SHIFT_IMPL(a, b, res, 0) static inline bool FlCmpLt(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i < b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) < 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 < FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f < b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i < b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) < 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 < FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f < b.data_.f; + } } static inline bool FlCmpGt(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i > b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) > 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 > FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f > b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i > b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) > 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 > FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f > b.data_.f; + } } static inline bool FlCmpLe(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i <= b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) <= 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 <= FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f <= b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i <= b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) <= 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 <= FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f <= b.data_.f; + } } static inline bool FlCmpGe(CVar a, CVar b) { - if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { return a.data_.i >= b.data_.i; } - CheckNum(a); CheckNum(b); - if (a.type_ == VAR_INT) { return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) >= 0; } - else if (b.type_ == VAR_INT) { return !isnan(a.data_.f) && 0 >= FlCompareIntDouble(b.data_.i, a.data_.f); } - else { return a.data_.f >= b.data_.f; } + if (LIKELY(a.type_ == VAR_INT && b.type_ == VAR_INT)) { + return a.data_.i >= b.data_.i; + } + CheckNum(a); + CheckNum(b); + if (a.type_ == VAR_INT) { + return !isnan(b.data_.f) && FlCompareIntDouble(a.data_.i, b.data_.f) >= 0; + } else if (b.type_ == VAR_INT) { + return !isnan(a.data_.f) && 0 >= FlCompareIntDouble(b.data_.i, a.data_.f); + } else { + return a.data_.f >= b.data_.f; + } } static inline bool FlCmpEq(CVar a, CVar b) { @@ -1645,25 +2292,49 @@ static inline bool FlCmpNe(CVar a, CVar b) { #define OpNe(a, b, res) SET_BOOL(res, FlCmpNe(a, b)) -#define OpNot(a, res) do { bool _bt; IsTrue(a, _bt); SET_BOOL(res, !_bt); } while(0) - -#define OpUnaryMinus(a, res) do { \ - CVar _ra = (a); \ - if (LIKELY(_ra.type_ == VAR_INT)) { SET_INT(res, FL_INT_SUB(0, _ra.data_.i)); } \ - else { CheckNum(_ra); SET_FLOAT(res, -_ra.data_.f); } \ -} while(0) - -#define OpBitNot(a, res) do { int64_t _ai; CheckInt(a, _ai); SET_INT(res, ~_ai); } while(0) - -#define OpLen(a, res) do { \ - CVar _lv = (a); \ - if (LIKELY(_lv.type_ == VAR_STRING)) { SET_INT(res, STR_SIZE(_lv.data_.s)); } \ - else if (_lv.type_ == VAR_STRINGID) { SET_INT(res, STR_SIZE((VarString *)_lv.data_.i)); } \ - else if (_lv.type_ == VAR_TABLE) { SET_INT(res, FlGetTableSeqLen(_lv.data_.t)); } \ - else { FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); } \ -} while(0) - -#define OpConcat(a, b, res) do { res = FlConcat(a, b); } while(0) +#define OpNot(a, res) \ + do { \ + bool _bt; \ + IsTrue(a, _bt); \ + SET_BOOL(res, !_bt); \ + } while (0) + +#define OpUnaryMinus(a, res) \ + do { \ + CVar _ra = (a); \ + if (LIKELY(_ra.type_ == VAR_INT)) { \ + SET_INT(res, FL_INT_SUB(0, _ra.data_.i)); \ + } else { \ + CheckNum(_ra); \ + SET_FLOAT(res, -_ra.data_.f); \ + } \ + } while (0) + +#define OpBitNot(a, res) \ + do { \ + int64_t _ai; \ + CheckInt(a, _ai); \ + SET_INT(res, ~_ai); \ + } while (0) + +#define OpLen(a, res) \ + do { \ + CVar _lv = (a); \ + if (LIKELY(_lv.type_ == VAR_STRING)) { \ + SET_INT(res, STR_SIZE(_lv.data_.s)); \ + } else if (_lv.type_ == VAR_STRINGID) { \ + SET_INT(res, STR_SIZE((VarString *) _lv.data_.i)); \ + } else if (_lv.type_ == VAR_TABLE) { \ + SET_INT(res, FlGetTableSeqLen(_lv.data_.t)); \ + } else { \ + FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); \ + } \ + } while (0) + +#define OpConcat(a, b, res) \ + do { \ + res = FlConcat(a, b); \ + } while (0) static inline int FlFastItoA(int64_t v, char *buf) { if (v == 0) { @@ -1676,12 +2347,12 @@ static inline int FlFastItoA(int64_t v, char *buf) { uint64_t u; if (v < 0) { neg = true; - u = 0ULL - (uint64_t)v; + u = 0ULL - (uint64_t) v; } else { - u = (uint64_t)v; + u = (uint64_t) v; } while (u > 0) { - tmp[i++] = (char)('0' + (u % 10)); + tmp[i++] = (char) ('0' + (u % 10)); u /= 10; } int len = 0; @@ -1696,9 +2367,22 @@ static inline int FlFastItoA(int64_t v, char *buf) { static inline int FlVarToStr(CVar v, char *buf, int buf_size) { switch (v.type_) { - case VAR_NIL: memcpy(buf, "nil", 3); buf[3] = '\0'; return 3; - case VAR_BOOL: if (v.data_.b) { memcpy(buf, "true", 4); buf[4] = '\0'; return 4; } else { memcpy(buf, "false", 5); buf[5] = '\0'; return 5; } - case VAR_INT: return FlFastItoA(v.data_.i, buf); + case VAR_NIL: + memcpy(buf, "nil", 3); + buf[3] = '\0'; + return 3; + case VAR_BOOL: + if (v.data_.b) { + memcpy(buf, "true", 4); + buf[4] = '\0'; + return 4; + } else { + memcpy(buf, "false", 5); + buf[5] = '\0'; + return 5; + } + case VAR_INT: + return FlFastItoA(v.data_.i, buf); case VAR_FLOAT: { /* Lua 5.4 tostringbuff: %.14g,结果只含数字和符号时补 .0 */ int flen = snprintf(buf, buf_size, "%.14g", v.data_.f); @@ -1717,37 +2401,82 @@ static inline int FlVarToStr(CVar v, char *buf, int buf_size) { return flen; } case VAR_STRING: - case VAR_STRINGID: FakeluaThrowError(_S, "FlVarToStr: string type should be handled by caller"); return 0; - case VAR_TABLE: { int __n = snprintf(buf, buf_size - 1, "table(0x%llx", (unsigned long long)(uintptr_t)v.data_.t); if (__n > 0 && __n < buf_size - 1) { buf[__n] = ')'; buf[__n + 1] = '\0'; return __n + 1; } return __n; } - case VAR_CLOSURE: { int __n = snprintf(buf, buf_size - 1, "function(0x%llx", (unsigned long long)(uintptr_t)v.data_.cl); if (__n > 0 && __n < buf_size - 1) { buf[__n] = ')'; buf[__n + 1] = '\0'; return __n + 1; } return __n; } - default: return 0; + case VAR_STRINGID: + FakeluaThrowError(_S, "FlVarToStr: string type should be handled by caller"); + return 0; + case VAR_TABLE: { + int __n = snprintf(buf, buf_size - 1, "table(0x%llx", (unsigned long long) (uintptr_t) v.data_.t); + if (__n > 0 && __n < buf_size - 1) { + buf[__n] = ')'; + buf[__n + 1] = '\0'; + return __n + 1; + } + return __n; + } + case VAR_CLOSURE: { + int __n = snprintf(buf, buf_size - 1, "function(0x%llx", (unsigned long long) (uintptr_t) v.data_.cl); + if (__n > 0 && __n < buf_size - 1) { + buf[__n] = ')'; + buf[__n + 1] = '\0'; + return __n + 1; + } + return __n; + } + default: + return 0; } } // Lua 的 .. 只接受字符串和数字,nil / bool / table / function 一律报错。 // 检查放在非字符串分支里,字符串拼接的快路径不受影响。 -#define CONCAT_CHECK_NUMERIC(v) \ - if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { FakeluaThrowError(_S, "attempt to concatenate a non-string value"); } +#define CONCAT_CHECK_NUMERIC(v) \ + if (UNLIKELY((v).type_ != VAR_INT && (v).type_ != VAR_FLOAT)) { \ + FakeluaThrowError(_S, "attempt to concatenate a non-string value"); \ + } static inline CVar FlConcat(CVar a, CVar b) { char buf_a[64], buf_b[64]; - const char *sa = buf_a; int la; - const char *sb = buf_b; int lb; - if (LIKELY(a.type_ == VAR_STRING)) { sa = STR_DATA(a.data_.s); la = STR_SIZE(a.data_.s); } - else if (a.type_ == VAR_STRINGID) { VarString *vs = (VarString *)a.data_.i; sa = STR_DATA(vs); la = STR_SIZE(vs); } - else if (a.type_ == VAR_INT) { la = FlFastItoA(a.data_.i, buf_a); } - else if (a.type_ == VAR_FLOAT) { la = FlVarToStr(a, buf_a, sizeof(buf_a)); } - else { FakeluaThrowError(_S, "attempt to concatenate a non-string value"); return (CVar){VAR_NIL}; } - - if (LIKELY(b.type_ == VAR_STRING)) { sb = STR_DATA(b.data_.s); lb = STR_SIZE(b.data_.s); } - else if (b.type_ == VAR_STRINGID) { VarString *vs = (VarString *)b.data_.i; sb = STR_DATA(vs); lb = STR_SIZE(vs); } - else if (b.type_ == VAR_INT) { lb = FlFastItoA(b.data_.i, buf_b); } - else if (b.type_ == VAR_FLOAT) { lb = FlVarToStr(b, buf_b, sizeof(buf_b)); } - else { FakeluaThrowError(_S, "attempt to concatenate a non-string value"); return (CVar){VAR_NIL}; } + const char *sa = buf_a; + int la; + const char *sb = buf_b; + int lb; + if (LIKELY(a.type_ == VAR_STRING)) { + sa = STR_DATA(a.data_.s); + la = STR_SIZE(a.data_.s); + } else if (a.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) a.data_.i; + sa = STR_DATA(vs); + la = STR_SIZE(vs); + } else if (a.type_ == VAR_INT) { + la = FlFastItoA(a.data_.i, buf_a); + } else if (a.type_ == VAR_FLOAT) { + la = FlVarToStr(a, buf_a, sizeof(buf_a)); + } else { + FakeluaThrowError(_S, "attempt to concatenate a non-string value"); + return (CVar) {VAR_NIL}; + } + + if (LIKELY(b.type_ == VAR_STRING)) { + sb = STR_DATA(b.data_.s); + lb = STR_SIZE(b.data_.s); + } else if (b.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) b.data_.i; + sb = STR_DATA(vs); + lb = STR_SIZE(vs); + } else if (b.type_ == VAR_INT) { + lb = FlFastItoA(b.data_.i, buf_b); + } else if (b.type_ == VAR_FLOAT) { + lb = FlVarToStr(b, buf_b, sizeof(buf_b)); + } else { + FakeluaThrowError(_S, "attempt to concatenate a non-string value"); + return (CVar) {VAR_NIL}; + } int total = la + lb; - if (UNLIKELY(total < la || total < lb)) { FakeluaThrowError(_S, "string concatenation result too long"); } - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); + if (UNLIKELY(total < la || total < lb)) { + FakeluaThrowError(_S, "string concatenation result too long"); + } + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); vs->size_ = total; vs->hash_ = 0; memcpy(vs->data_, sa, la); @@ -1764,10 +2493,10 @@ static inline CVar FlStringChar1(int64_t c) { if (UNLIKELY(c < 0 || c > 255)) { FakeluaThrowError(_S, "bad argument to string.char: value out of range"); } - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + 1, !__fakelua_init_flag__); + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + 1, !__fakelua_init_flag__); vs->size_ = 1; vs->hash_ = 0; - vs->data_[0] = (char)c; + vs->data_[0] = (char) c; CVar r; r.type_ = VAR_STRING; r.flag_ = 0; @@ -1779,34 +2508,54 @@ static inline CVar FlStringChar1(int64_t c) { static inline CVar FlStringLower(CVar s) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.lower", 1, s); } - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.lower", 1, s); + } + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); out->size_ = len; out->hash_ = 0; for (int i = 0; i < len; ++i) { - unsigned char c = (unsigned char)src[i]; - out->data_[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : (char)c; + unsigned char c = (unsigned char) src[i]; + out->data_[i] = (c >= 'A' && c <= 'Z') ? (char) (c + 32) : (char) c; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } static inline CVar FlStringUpper(CVar s) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.upper", 1, s); } - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.upper", 1, s); + } + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); out->size_ = len; out->hash_ = 0; for (int i = 0; i < len; ++i) { - unsigned char c = (unsigned char)src[i]; - out->data_[i] = (c >= 'a' && c <= 'z') ? (char)(c - 32) : (char)c; + unsigned char c = (unsigned char) src[i]; + out->data_[i] = (c >= 'a' && c <= 'z') ? (char) (c - 32) : (char) c; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } @@ -1814,32 +2563,52 @@ static inline CVar FlStringUpper(CVar s) { static inline CVar FlStringByte1(CVar s, int64_t i) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.byte", 2, s, (CVar){.type_ = VAR_INT, .data_.i = i}); } + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.byte", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = i}); + } if (i < 0) i = len + i + 1; - if (i < 1 || i > len) return (CVar){VAR_NIL}; - return (CVar){.type_ = VAR_INT, .data_.i = (unsigned char)src[i - 1]}; + if (i < 1 || i > len) return (CVar) {VAR_NIL}; + return (CVar) {.type_ = VAR_INT, .data_.i = (unsigned char) src[i - 1]}; } // string.rep(s, n):s 重复 n 次(无分隔符,最常见形式) static inline CVar FlStringRep(CVar s, int64_t n) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar){.type_ = VAR_INT, .data_.i = n}); } + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = n}); + } if (n <= 0 || len == 0) { - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString), !__fakelua_init_flag__); - vs->size_ = 0; vs->hash_ = 0; vs->data_[0] = '\0'; - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = vs; return r; + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString), !__fakelua_init_flag__); + vs->size_ = 0; + vs->hash_ = 0; + vs->data_[0] = '\0'; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = vs; + return r; } // 防溢出:总长度超过 1G 回退 native。(n, len) 均转为 uint64_t 避免截断 - if ((uint64_t)n > (1073741824ULL / (uint64_t)len)) { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar){.type_ = VAR_INT, .data_.i = n}); + if ((uint64_t) n > (1073741824ULL / (uint64_t) len)) { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.rep", 2, s, (CVar) {.type_ = VAR_INT, .data_.i = n}); } - int64_t total = (int64_t)len * n; - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); + int64_t total = (int64_t) len * n; + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + total, !__fakelua_init_flag__); out->size_ = total; out->hash_ = 0; char *dst = out->data_; @@ -1847,7 +2616,10 @@ static inline CVar FlStringRep(CVar s, int64_t n) { memcpy(dst, src, len); dst += len; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } @@ -1855,16 +2627,26 @@ static inline CVar FlStringRep(CVar s, int64_t n) { static inline CVar FlStringReverse(CVar s) { const char *src = 0; int len = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); len = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); len = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.reverse", 1, s); } - VarString *out = (VarString *)FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + len = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.reverse", 1, s); + } + VarString *out = (VarString *) FlFastAlloc(_S, sizeof(VarString) + len, !__fakelua_init_flag__); out->size_ = len; out->hash_ = 0; for (int i = 0; i < len; ++i) { out->data_[i] = src[len - 1 - i]; } - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = out; + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = out; return r; } @@ -1872,34 +2654,51 @@ static inline CVar FlStringReverse(CVar s) { static inline CVar FlStringFindPlain(CVar s, CVar pat) { const char *src = 0; int slen = 0; - if (LIKELY(s.type_ == VAR_STRING)) { src = STR_DATA(s.data_.s); slen = STR_SIZE(s.data_.s); } - else if (s.type_ == VAR_STRINGID) { VarString *vs = (VarString *)s.data_.i; src = STR_DATA(vs); slen = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar){.type_ = VAR_INT, .data_.i = 1}, (CVar){.type_ = VAR_BOOL, .data_.b = true}); } + if (LIKELY(s.type_ == VAR_STRING)) { + src = STR_DATA(s.data_.s); + slen = STR_SIZE(s.data_.s); + } else if (s.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) s.data_.i; + src = STR_DATA(vs); + slen = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar) {.type_ = VAR_INT, .data_.i = 1}, (CVar) {.type_ = VAR_BOOL, .data_.b = true}); + } const char *psrc = 0; int plen = 0; - if (LIKELY(pat.type_ == VAR_STRING)) { psrc = STR_DATA(pat.data_.s); plen = STR_SIZE(pat.data_.s); } - else if (pat.type_ == VAR_STRINGID) { VarString *vs = (VarString *)pat.data_.i; psrc = STR_DATA(vs); plen = STR_SIZE(vs); } - else { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar){.type_ = VAR_INT, .data_.i = 1}, (CVar){.type_ = VAR_BOOL, .data_.b = true}); } - if (plen == 0) return (CVar){.type_ = VAR_INT, .data_.i = 1}; - if (plen > slen) return (CVar){VAR_NIL}; + if (LIKELY(pat.type_ == VAR_STRING)) { + psrc = STR_DATA(pat.data_.s); + plen = STR_SIZE(pat.data_.s); + } else if (pat.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) pat.data_.i; + psrc = STR_DATA(vs); + plen = STR_SIZE(vs); + } else { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "string.find", 4, s, pat, (CVar) {.type_ = VAR_INT, .data_.i = 1}, (CVar) {.type_ = VAR_BOOL, .data_.b = true}); + } + if (plen == 0) return (CVar) {.type_ = VAR_INT, .data_.i = 1}; + if (plen > slen) return (CVar) {VAR_NIL}; int limit = slen - plen; for (int i = 0; i <= limit; ++i) { if (src[i] == psrc[0] && memcmp(src + i, psrc, plen) == 0) { - return (CVar){.type_ = VAR_INT, .data_.i = i + 1}; + return (CVar) {.type_ = VAR_INT, .data_.i = i + 1}; } } - return (CVar){VAR_NIL}; + return (CVar) {VAR_NIL}; } // string.format("%d", int) static inline CVar FlFormatInt(int64_t v) { char buf[32]; int n = FlFastItoA(v, buf); - VarString *vs = (VarString *)FlFastAlloc(_S, sizeof(VarString) + n, !__fakelua_init_flag__); + VarString *vs = (VarString *) FlFastAlloc(_S, sizeof(VarString) + n, !__fakelua_init_flag__); vs->size_ = n; vs->hash_ = 0; - memcpy(vs->data_, buf, (size_t)n); - CVar r; r.type_ = VAR_STRING; r.flag_ = 0; r.data_.s = vs; + memcpy(vs->data_, buf, (size_t) n); + CVar r; + r.type_ = VAR_STRING; + r.flag_ = 0; + r.data_.s = vs; return r; } @@ -1908,24 +2707,24 @@ static inline CVar FlFormatInt(int64_t v) { // 其它单个值是 1 个值。 static inline CVar FlSelectHash(CVar vararg) { if (vararg.type_ == VAR_MULTI) { - if (vararg.data_.m == NULL) return (CVar){.type_ = VAR_INT, .data_.i = 0}; - return (CVar){.type_ = VAR_INT, .data_.i = (int64_t)vararg.data_.m->count}; + if (vararg.data_.m == NULL) return (CVar) {.type_ = VAR_INT, .data_.i = 0}; + return (CVar) {.type_ = VAR_INT, .data_.i = (int64_t) vararg.data_.m->count}; } - if (vararg.type_ == VAR_NIL) return (CVar){.type_ = VAR_INT, .data_.i = 0}; - return (CVar){.type_ = VAR_INT, .data_.i = 1}; + if (vararg.type_ == VAR_NIL) return (CVar) {.type_ = VAR_INT, .data_.i = 0}; + return (CVar) {.type_ = VAR_INT, .data_.i = 1}; } static inline CVar FlSelectIndex(int64_t idx, CVar vararg) { if (vararg.type_ == VAR_MULTI) { VarMulti *m = vararg.data_.m; - int64_t count = (m != NULL) ? (int64_t)m->count : 0; + int64_t count = (m != NULL) ? (int64_t) m->count : 0; if (idx < 0) { if (idx < -count - 1) FakeluaThrowError(_S, "bad argument #1 to 'select': index out of range"); idx = count + idx + 1; } if (idx < 1) FakeluaThrowError(_S, "bad argument #1 to 'select': index out of range"); if (idx > count || m == NULL) return FlAllocMulti(_S, 0); - uint32_t start = (uint32_t)(idx - 1); + uint32_t start = (uint32_t) (idx - 1); uint32_t nret = m->count - start; if (nret == 1) return m->vars[start]; return FlSliceMulti(_S, vararg, start); @@ -1955,12 +2754,24 @@ static inline CVar FlTonumber(CVar v) { if (LIKELY(v.type_ == VAR_INT || v.type_ == VAR_FLOAT)) return v; const char *p = 0; int len = 0; - if (LIKELY(v.type_ == VAR_STRING)) { p = STR_DATA(v.data_.s); len = STR_SIZE(v.data_.s); } - else if (v.type_ == VAR_STRINGID) { VarString *vs = (VarString *)v.data_.i; p = STR_DATA(vs); len = STR_SIZE(vs); } - else { return (CVar){VAR_NIL}; } - while (len > 0 && FlIsSpaceByte((unsigned char)p[0])) { ++p; --len; } - while (len > 0 && FlIsSpaceByte((unsigned char)p[len - 1])) { --len; } - if (len <= 0) return (CVar){VAR_NIL}; + if (LIKELY(v.type_ == VAR_STRING)) { + p = STR_DATA(v.data_.s); + len = STR_SIZE(v.data_.s); + } else if (v.type_ == VAR_STRINGID) { + VarString *vs = (VarString *) v.data_.i; + p = STR_DATA(vs); + len = STR_SIZE(vs); + } else { + return (CVar) {VAR_NIL}; + } + while (len > 0 && FlIsSpaceByte((unsigned char) p[0])) { + ++p; + --len; + } + while (len > 0 && FlIsSpaceByte((unsigned char) p[len - 1])) { + --len; + } + if (len <= 0) return (CVar) {VAR_NIL}; // 0x 十六进制等复杂形式走 native if (len >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); @@ -1970,17 +2781,24 @@ static inline CVar FlTonumber(CVar v) { } int neg = 0; int i = 0; - if (p[0] == '+') { i = 1; } - else if (p[0] == '-') { neg = 1; i = 1; } - if (i >= len) return (CVar){VAR_NIL}; + if (p[0] == '+') { + i = 1; + } else if (p[0] == '-') { + neg = 1; + i = 1; + } + if (i >= len) return (CVar) {VAR_NIL}; // 含小数点/指数 → native - { int j; for (j = i; j < len; ++j) { - unsigned char c = (unsigned char)p[j]; - if (c == '.' || c == 'e' || c == 'E') { - return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); + { + int j; + for (j = i; j < len; ++j) { + unsigned char c = (unsigned char) p[j]; + if (c == '.' || c == 'e' || c == 'E') { + return FakeluaCallByName(_S, FAKELUA_JIT_TYPE, "tonumber", 1, v); + } + if (c < '0' || c > '9') return (CVar) {VAR_NIL}; } - if (c < '0' || c > '9') return (CVar){VAR_NIL}; - } } + } int64_t val = 0; for (; i < len; ++i) { // 溢出检查:val * 10 + digit > INT64_MAX 时回退到 native(返回 float) @@ -1992,15 +2810,19 @@ static inline CVar FlTonumber(CVar v) { val = val * 10 + digit; } if (neg) val = -val; - return (CVar){.type_ = VAR_INT, .data_.i = val}; + return (CVar) {.type_ = VAR_INT, .data_.i = val}; } // 空表预扩到至少 need 个桶(反复 FlTableRehash,空表拷贝成本可忽略) static inline void FlEnsureTableBuckets(VarTable *tbl, uint32_t need) { if (need < 1) need = 1; uint32_t target = need - 1; - target |= target >> 1; target |= target >> 2; target |= target >> 4; - target |= target >> 8; target |= target >> 16; target += 1; + target |= target >> 1; + target |= target >> 2; + target |= target >> 4; + target |= target >> 8; + target |= target >> 16; + target += 1; while (tbl->bucket_count_ < target) { FlTableRehash(tbl); if (UNLIKELY(tbl->bucket_count_ == 0)) break; @@ -2010,12 +2832,16 @@ static inline void FlEnsureTableBuckets(VarTable *tbl, uint32_t need) { // 空表预扩容的节点数组必须能一次分配出来(与 native_table.cpp 的 MovePresizeFits 一致)。 static inline int FlMovePresizeFits(uint64_t count) { if (count == 0 || count > 0xFFFFFFFFULL) return 0; - uint32_t min_buckets = (uint32_t)count; + uint32_t min_buckets = (uint32_t) count; uint32_t buckets = min_buckets - 1; - buckets |= buckets >> 1; buckets |= buckets >> 2; buckets |= buckets >> 4; - buckets |= buckets >> 8; buckets |= buckets >> 16; buckets += 1; + buckets |= buckets >> 1; + buckets |= buckets >> 2; + buckets |= buckets >> 4; + buckets |= buckets >> 8; + buckets |= buckets >> 16; + buckets += 1; if (buckets < min_buckets) return 0; - uint64_t total = (uint64_t)buckets + (uint64_t)(buckets / 2); + uint64_t total = (uint64_t) buckets + (uint64_t) (buckets / 2); uint64_t bytes = total * sizeof(TableNode) + total * sizeof(uint32_t); return bytes <= (1024ULL * 1024ULL); } @@ -2028,30 +2854,30 @@ static inline CVar FlTableMove(CVar src, int64_t f, int64_t e, int64_t t, CVar d } FL_CHECK_CONST_TABLE(dst); if (e >= f) { - uint64_t count = (uint64_t)e - (uint64_t)f + 1; - if (UNLIKELY(count == 0 || count > (uint64_t)INT64_MAX)) { + uint64_t count = (uint64_t) e - (uint64_t) f + 1; + if (UNLIKELY(count == 0 || count > (uint64_t) INT64_MAX)) { FakeluaThrowError(_S, "bad argument #3 to 'table.move' (too many elements to move)"); } uint64_t last_off = count - 1; - if (UNLIKELY(t > 0 && (uint64_t)t > (uint64_t)INT64_MAX - last_off)) { + if (UNLIKELY(t > 0 && (uint64_t) t > (uint64_t) INT64_MAX - last_off)) { FakeluaThrowError(_S, "bad argument #4 to 'table.move' (destination wrap around)"); } - int64_t icount = (int64_t)count; + int64_t icount = (int64_t) count; int same = (src.data_.t == dst.data_.t); - if (!same && dst.data_.t->count_ == 0 && icount > 8 && t != (int64_t)dst.data_.t->arr_size_ + 1 && FlMovePresizeFits(count)) { - FlEnsureTableBuckets(dst.data_.t, (uint32_t)icount); + if (!same && dst.data_.t->count_ == 0 && icount > 8 && t != (int64_t) dst.data_.t->arr_size_ + 1 && FlMovePresizeFits(count)) { + FlEnsureTableBuckets(dst.data_.t, (uint32_t) icount); } if (!same || t <= f || t > e) { int64_t i; for (i = 0; i < icount; ++i) { - CVar val = FlGetTableInt(src, (int64_t)((uint64_t)f + (uint64_t)i)); - FlSetTableInt(dst, (int64_t)((uint64_t)t + (uint64_t)i), val); + CVar val = FlGetTableInt(src, (int64_t) ((uint64_t) f + (uint64_t) i)); + FlSetTableInt(dst, (int64_t) ((uint64_t) t + (uint64_t) i), val); } } else { int64_t i; for (i = icount - 1; i >= 0; --i) { - CVar val = FlGetTableInt(src, (int64_t)((uint64_t)f + (uint64_t)i)); - FlSetTableInt(dst, (int64_t)((uint64_t)t + (uint64_t)i), val); + CVar val = FlGetTableInt(src, (int64_t) ((uint64_t) f + (uint64_t) i)); + FlSetTableInt(dst, (int64_t) ((uint64_t) t + (uint64_t) i), val); } } } @@ -2061,80 +2887,98 @@ static inline CVar FlTableMove(CVar src, int64_t f, int64_t e, int64_t t, CVar d // table.unpack 内联实现(完整对齐 Lua 5.4 区间回绕与单块分配上限检查) static inline CVar FlTableUnpack(CVar tbl, int64_t start_i, int64_t end_j) { if (start_i > end_j) return FlAllocMulti(_S, 0); - uint64_t count64 = (uint64_t)end_j - (uint64_t)start_i + 1; + uint64_t count64 = (uint64_t) end_j - (uint64_t) start_i + 1; uint64_t max_unpack = (1024ULL * 1024ULL - sizeof(VarMulti)) / sizeof(CVar); if (UNLIKELY(count64 == 0 || count64 > 2147483647ULL || count64 > max_unpack)) { FakeluaThrowError(_S, "too many results to unpack"); } - uint32_t count = (uint32_t)count64; + uint32_t count = (uint32_t) count64; CVar multi = FlAllocMulti(_S, count); uint32_t i; for (i = 0; i < count; ++i) { - multi.data_.m->vars[i] = FlGetTableInt(tbl, (int64_t)((uint64_t)start_i + (uint64_t)i)); + multi.data_.m->vars[i] = FlGetTableInt(tbl, (int64_t) ((uint64_t) start_i + (uint64_t) i)); } return multi; } -#define GET_TABLE_ENTRY(tbl, idx, k, v) do { \ - VarTable *__gt = (tbl).data_.t; \ - uint32_t __gi = (idx); \ - uint32_t __spec_cnt = __gt->spec_count; \ - if (LIKELY(__gi < __spec_cnt)) { \ - (k) = __gt->spec_keys[__gi]; \ - (v) = __gt->spec_vals[__gi]; \ - } else { \ - __gi -= __spec_cnt; \ - if (__gi < __gt->arr_size_) { \ - (k).type_ = VAR_INT; (k).flag_ = 0; (k).data_.i = (int64_t)__gi + 1; \ - (v) = __gt->arr_[__gi]; \ - } else { \ - __gi -= __gt->arr_size_; \ - if (UNLIKELY(__gi >= __gt->count_)) { \ - (k).type_ = VAR_NIL; (v).type_ = VAR_NIL; \ - } else if (LIKELY(__gt->bucket_count_ == 0)) { \ - (k) = __gt->quick_data_[__gi].key; \ - (v) = __gt->quick_data_[__gi].val; \ - } else { \ - uint32_t __gti_node_idx = __gt->active_list_[__gi]; \ - (k) = __gt->nodes_[__gti_node_idx].entry.key; \ - (v) = __gt->nodes_[__gti_node_idx].entry.val; \ - } \ - } \ - } \ -} while(0) - -#define FlFloorDivQuotient(a, b, q) do { \ - if (UNLIKELY((b) == (int64_t)-1)) { \ - (q) = (int64_t)((uint64_t)0 - (uint64_t)(int64_t)(a)); \ - } else { \ - (q) = (a) / (b); \ - if (((a) ^ (b)) < 0 && (a) % (b) != 0) { (q) -= 1; } \ - } \ -} while(0) - -#define FlFloorDivInt(a, b, result) do { \ - int64_t __fl_a = (a); int64_t __fl_b = (b); \ - if (UNLIKELY(__fl_b == 0)) { FakeluaThrowError(_S, "floor division by zero"); } \ - int64_t __fl_q; \ - FlFloorDivQuotient(__fl_a, __fl_b, __fl_q); \ - (result) = __fl_q; \ -} while(0) - -#define FlModInt(a, b, result) do { \ - int64_t __fm_a = (a); int64_t __fm_b = (b); \ - if (UNLIKELY(__fm_b == 0)) { FakeluaThrowError(_S, "modulo by zero"); } \ - if (UNLIKELY(__fm_b == (int64_t)-1)) { (result) = 0; } \ - else { \ - int64_t __fm_q; \ - FlFloorDivQuotient(__fm_a, __fm_b, __fm_q); \ - (result) = __fm_a - __fm_b * __fm_q; \ - } \ -} while(0) - -#define FlModFloat(a, b, result) do { \ - double __fmf_a = (a); double __fmf_b = (b); \ - (result) = __fmf_a - __fmf_b * floor(__fmf_a / __fmf_b); \ -} while(0) +#define GET_TABLE_ENTRY(tbl, idx, k, v) \ + do { \ + VarTable *__gt = (tbl).data_.t; \ + uint32_t __gi = (idx); \ + uint32_t __spec_cnt = __gt->spec_count; \ + if (LIKELY(__gi < __spec_cnt)) { \ + (k) = __gt->spec_keys[__gi]; \ + (v) = __gt->spec_vals[__gi]; \ + } else { \ + __gi -= __spec_cnt; \ + if (__gi < __gt->arr_size_) { \ + (k).type_ = VAR_INT; \ + (k).flag_ = 0; \ + (k).data_.i = (int64_t) __gi + 1; \ + (v) = __gt->arr_[__gi]; \ + } else { \ + __gi -= __gt->arr_size_; \ + if (UNLIKELY(__gi >= __gt->count_)) { \ + (k).type_ = VAR_NIL; \ + (v).type_ = VAR_NIL; \ + } else if (LIKELY(__gt->bucket_count_ == 0)) { \ + (k) = __gt->quick_data_[__gi].key; \ + (v) = __gt->quick_data_[__gi].val; \ + } else { \ + uint32_t __gti_node_idx = __gt->active_list_[__gi]; \ + (k) = __gt->nodes_[__gti_node_idx].entry.key; \ + (v) = __gt->nodes_[__gti_node_idx].entry.val; \ + } \ + } \ + } \ + } while (0) + +#define FlFloorDivQuotient(a, b, q) \ + do { \ + if (UNLIKELY((b) == (int64_t) -1)) { \ + (q) = (int64_t) ((uint64_t) 0 - (uint64_t) (int64_t) (a)); \ + } else { \ + (q) = (a) / (b); \ + if (((a) ^ (b)) < 0 && (a) % (b) != 0) { \ + (q) -= 1; \ + } \ + } \ + } while (0) + +#define FlFloorDivInt(a, b, result) \ + do { \ + int64_t __fl_a = (a); \ + int64_t __fl_b = (b); \ + if (UNLIKELY(__fl_b == 0)) { \ + FakeluaThrowError(_S, "floor division by zero"); \ + } \ + int64_t __fl_q; \ + FlFloorDivQuotient(__fl_a, __fl_b, __fl_q); \ + (result) = __fl_q; \ + } while (0) + +#define FlModInt(a, b, result) \ + do { \ + int64_t __fm_a = (a); \ + int64_t __fm_b = (b); \ + if (UNLIKELY(__fm_b == 0)) { \ + FakeluaThrowError(_S, "modulo by zero"); \ + } \ + if (UNLIKELY(__fm_b == (int64_t) -1)) { \ + (result) = 0; \ + } else { \ + int64_t __fm_q; \ + FlFloorDivQuotient(__fm_a, __fm_b, __fm_q); \ + (result) = __fm_a - __fm_b * __fm_q; \ + } \ + } while (0) + +#define FlModFloat(a, b, result) \ + do { \ + double __fmf_a = (a); \ + double __fmf_b = (b); \ + (result) = __fmf_a - __fmf_b * floor(__fmf_a / __fmf_b); \ + } while (0) // Lua 5.4 pushnumint:d 能无损落进 int64 时返回 integer,否则保留 float // (inf/-inf/nan/超出 [-2^63, 2^63) 均不报错,仍是 float)。 @@ -2142,7 +2986,7 @@ static inline CVar FlPushNumInt(double d) { CVar r; if (isfinite(d) && d >= -9223372036854775808.0 && d < 9223372036854775808.0) { r.type_ = VAR_INT; - r.data_.i = (int64_t)d; + r.data_.i = (int64_t) d; } else { r.type_ = VAR_FLOAT; r.data_.f = d; @@ -2152,12 +2996,12 @@ static inline CVar FlPushNumInt(double d) { static inline CVar FlMathFloor(CVar v) { if (v.type_ == VAR_INT) return v; - return FlPushNumInt(floor((double)v.data_.f)); + return FlPushNumInt(floor((double) v.data_.f)); } static inline CVar FlMathCeil(CVar v) { if (v.type_ == VAR_INT) return v; - return FlPushNumInt(ceil((double)v.data_.f)); + return FlPushNumInt(ceil((double) v.data_.f)); } // Lua 5.4 math.fmod:两整数走整数取模(0 报错、-1 特判为 0 避开溢出), @@ -2190,36 +3034,36 @@ static inline CVar FlMathModf(CVar v) { r = FlAllocMulti(_S, 2); if (v.type_ == VAR_INT) { r.data_.m->vars[0] = v; - r.data_.m->vars[1] = (CVar){.type_ = VAR_FLOAT, .data_.f = 0.0}; + r.data_.m->vars[1] = (CVar) {.type_ = VAR_FLOAT, .data_.f = 0.0}; return r; } - n = (double)v.data_.f; + n = (double) v.data_.f; ip = (n < 0) ? ceil(n) : floor(n); fp = (n == ip) ? 0.0 : n - ip; r.data_.m->vars[0] = FlPushNumInt(ip); - r.data_.m->vars[1] = (CVar){.type_ = VAR_FLOAT, .data_.f = fp}; + r.data_.m->vars[1] = (CVar) {.type_ = VAR_FLOAT, .data_.f = fp}; return r; } static inline double FlMathLdexp(double value, int64_t exponent) { if (exponent > 2147483647LL) exponent = 2147483647LL; if (exponent < (-2147483647LL - 1)) exponent = (-2147483647LL - 1); - return ldexp(value, (int)exponent); + return ldexp(value, (int) exponent); } static inline int64_t FlRandomInt64(void) { uint64_t value = 0; for (int i = 0; i < 5; ++i) { - value = (value << 15) | (uint64_t)(rand() & 0x7fff); + value = (value << 15) | (uint64_t) (rand() & 0x7fff); } - return (int64_t)value; + return (int64_t) value; } // 整数 for 步进:用无符号加法探测有符号溢出。溢出返回 0 且不改 *ctrl //(避免 maxinteger++ 变成 mininteger 后死循环)。continue 仍走 for 的 incr 子句。 static inline int FlForIntAdvance(int64_t *ctrl, int64_t step) { int64_t cur = *ctrl; - int64_t next = (int64_t)((uint64_t)cur + (uint64_t)step); + int64_t next = (int64_t) ((uint64_t) cur + (uint64_t) step); if ((cur ^ step) >= 0 && (next ^ cur) < 0) return 0; *ctrl = next; return 1; @@ -2248,49 +3092,53 @@ static inline int FlForLimitToInt(double flim, int64_t step, int64_t *out) { *out = INT64_MAX; return 0; } - if (flim < (double)INT64_MIN) { + if (flim < (double) INT64_MIN) { if (step > 0) return 1; *out = INT64_MIN; return 0; } - *out = step > 0 ? (int64_t)floor(flim) : (int64_t)ceil(flim); + *out = step > 0 ? (int64_t) floor(flim) : (int64_t) ceil(flim); return 0; } -#define FlToIntChecked(v, result) do { \ - if (!FlDoubleFitsInt64((double)(v), &(result))) { \ - FakeluaThrowError(_S, "number has no integer representation"); \ - } \ -} while(0) - -#define FlShiftIntImpl(a, b, result, _right) do { \ - int64_t __sh_a = (a); int64_t __sh_b = (b); \ - if (UNLIKELY(__sh_b >= 64 || __sh_b <= -64)) { \ - (result) = (int64_t)0; \ - } else if (__sh_b >= 0) { \ - (result) = (int64_t)((_right) ? ((uint64_t)__sh_a >> __sh_b) : ((uint64_t)__sh_a << __sh_b)); \ - } else { \ - (result) = (int64_t)((_right) ? ((uint64_t)__sh_a << (-__sh_b)) : ((uint64_t)__sh_a >> (-__sh_b))); \ - } \ -} while(0) +#define FlToIntChecked(v, result) \ + do { \ + if (!FlDoubleFitsInt64((double) (v), &(result))) { \ + FakeluaThrowError(_S, "number has no integer representation"); \ + } \ + } while (0) + +#define FlShiftIntImpl(a, b, result, _right) \ + do { \ + int64_t __sh_a = (a); \ + int64_t __sh_b = (b); \ + if (UNLIKELY(__sh_b >= 64 || __sh_b <= -64)) { \ + (result) = (int64_t) 0; \ + } else if (__sh_b >= 0) { \ + (result) = (int64_t) ((_right) ? ((uint64_t) __sh_a >> __sh_b) : ((uint64_t) __sh_a << __sh_b)); \ + } else { \ + (result) = (int64_t) ((_right) ? ((uint64_t) __sh_a << (-__sh_b)) : ((uint64_t) __sh_a >> (-__sh_b))); \ + } \ + } while (0) #define FlLShiftInt(a, b, result) FlShiftIntImpl(a, b, result, 0) #define FlRShiftInt(a, b, result) FlShiftIntImpl(a, b, result, 1) -#define FlLenInt(v, result) do { \ - CVar __fl_v = (v); \ - if (LIKELY(__fl_v.type_ == VAR_STRING)) { \ - (result) = (int64_t)STR_SIZE(__fl_v.data_.s); \ - } else if (UNLIKELY(__fl_v.type_ == VAR_STRINGID)) { \ - (result) = (int64_t)STR_SIZE((VarString *)__fl_v.data_.i); \ - } else if (UNLIKELY(__fl_v.type_ == VAR_TABLE)) { \ - (result) = (int64_t)FlGetTableSeqLen(__fl_v.data_.t); \ - } else { \ - FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); \ - (result) = 0; \ - } \ -} while(0) +#define FlLenInt(v, result) \ + do { \ + CVar __fl_v = (v); \ + if (LIKELY(__fl_v.type_ == VAR_STRING)) { \ + (result) = (int64_t) STR_SIZE(__fl_v.data_.s); \ + } else if (UNLIKELY(__fl_v.type_ == VAR_STRINGID)) { \ + (result) = (int64_t) STR_SIZE((VarString *) __fl_v.data_.i); \ + } else if (UNLIKELY(__fl_v.type_ == VAR_TABLE)) { \ + (result) = (int64_t) FlGetTableSeqLen(__fl_v.data_.t); \ + } else { \ + FakeluaThrowError(_S, "attempt to get length of a non-string/table value"); \ + (result) = 0; \ + } \ + } while (0) // 获取本 State 的日志级别(由 C++ 侧初始化和设置) // 返回值: 0=Trace, 1=Debug, 2=Info, 3=Warn, 4=Error, 5=Critical, 6=Off @@ -2317,19 +3165,30 @@ enum { // 日志宏:级别检查 + 调用 C++ 日志函数 // 参数 msg 只在级别启用时才会被求值 -#define FAKELUA_LOG_TRACE(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_TRACE)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_TRACE, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_DEBUG(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_DEBUG)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_DEBUG, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_INFO(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_INFO)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_INFO, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_WARN(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_WARN)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_WARN, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_ERROR(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_ERROR)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_ERROR, msg, file, line, fname); } while (0) -#define FAKELUA_LOG_CRITICAL(msg, file, line, fname) \ - do { if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_CRITICAL)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_CRITICAL, msg, file, line, fname); } while (0) - +#define FAKELUA_LOG_TRACE(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_TRACE)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_TRACE, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_DEBUG(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_DEBUG)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_DEBUG, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_INFO(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_INFO)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_INFO, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_WARN(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_WARN)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_WARN, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_ERROR(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_ERROR)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_ERROR, msg, file, line, fname); \ + } while (0) +#define FAKELUA_LOG_CRITICAL(msg, file, line, fname) \ + do { \ + if (FAKELUA_LOG_CHECK(FAKELUA_LOGLEVEL_CRITICAL)) FakeluaLogLua(_S, FAKELUA_LOGLEVEL_CRITICAL, msg, file, line, fname); \ + } while (0) )FL_RUNTIME"; }// namespace fakelua diff --git a/src/compile/compile_common.h b/src/compile/compile_common.h index ee26209d..73348612 100644 --- a/src/compile/compile_common.h +++ b/src/compile/compile_common.h @@ -23,22 +23,20 @@ inline constexpr const char *kInitFunctionName = "__fakelua_init"; // Lua 标识符已是 [A-Za-z_][A-Za-z0-9_]*,用作 C 标识符时只需避开关键字和运行时保留名。 inline std::string SanitizeCIdent(const std::string &lua_name) { static const std::unordered_set kReserved = { - "auto", "break", "case", "char", "const", "continue", "default", "do", - "double", "else", "enum", "extern", "float", "for", "goto", "if", - "inline", "int", "long", "register", "restrict", "return", "short", "signed", - "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", - "volatile", "while", "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", - "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local", "bool", "class", "template", "typename", - "namespace", "new", "delete", "this", "operator", "private", "public", "protected", - "virtual", "override", "final", "constexpr", "nullptr", "true", "false", "catch", - "try", "throw", "using", "friend", "explicit", "export", "mutable", "wchar_t", - "char16_t", "char32_t", "char8_t", "noexcept", "static_assert", "thread_local", "alignas", "alignof", - "decltype", "consteval", "constinit", "concept", "requires", "co_await", "co_return", "co_yield", - "and", "or", "not", "xor", "bitand", "bitor", "compl", "and_eq", - "or_eq", "xor_eq", "not_eq", "CVar", "VarType", "VarTable", "VarClosure", "State", - "stdin", "stdout", "stderr", "_S", "_CL", "kNil", "kTrue", "kFalse", + "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", + "enum", "extern", "float", "for", "goto", "if", "inline", "int", "long", "register", + "restrict", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", + "unsigned", "void", "volatile", "while", "_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", + "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local", "bool", "class", "template", "typename", "namespace", "new", + "delete", "this", "operator", "private", "public", "protected", "virtual", "override", "final", "constexpr", + "nullptr", "true", "false", "catch", "try", "throw", "using", "friend", "explicit", "export", + "mutable", "wchar_t", "char16_t", "char32_t", "char8_t", "noexcept", "static_assert", "thread_local", "alignas", "alignof", + "decltype", "consteval", "constinit", "concept", "requires", "co_await", "co_return", "co_yield", "and", "or", + "not", "xor", "bitand", "bitor", "compl", "and_eq", "or_eq", "xor_eq", "not_eq", "CVar", + "VarType", "VarTable", "VarClosure", "State", "stdin", "stdout", "stderr", "_S", "_CL", "kNil", + "kTrue", "kFalse", }; - if (kReserved.contains(lua_name)) { + if (kReserved.contains(lua_name) || lua_name.starts_with("__fakelua_cache_")) { return "flua_id_" + lua_name; } return lua_name; @@ -286,6 +284,9 @@ struct ParseResult { std::string file_name; // 语法树根节点(代码块) SyntaxTreeInterfacePtr chunk; + // String input uses a stable C #line label, while runtime diagnostics still + // bind the original temporary source filename for each loaded instance. + bool from_string = false; }; // ---- 阶段三:语义与控制流分析结果 ----------------------------------------- diff --git a/src/compile/compiler.cpp b/src/compile/compiler.cpp index c3942069..b9760976 100644 --- a/src/compile/compiler.cpp +++ b/src/compile/compiler.cpp @@ -40,6 +40,7 @@ ParseResult Compiler::Compile(MyFlexer &f, const CompileConfig &cfg) { ParseResult pr; pr.file_name = f.GetFilename(); + pr.from_string = f.IsString(); // 1. 生成语法树(AST) LOG_DEBUG(s_, "engine", "step 1: parsing AST"); diff --git a/src/compile/my_flexer.cpp b/src/compile/my_flexer.cpp index cd0c1e63..f7f17c14 100644 --- a/src/compile/my_flexer.cpp +++ b/src/compile/my_flexer.cpp @@ -7,6 +7,7 @@ namespace fakelua { // 输入文件进行解析 void MyFlexer::InputFile(const std::string &file) { + is_string_ = false; file_.open(file.data(), std::ios::binary); if (file_.fail()) { ThrowFakeluaException("Read file failed, failed to open file " + file); @@ -19,6 +20,7 @@ void MyFlexer::InputFile(const std::string &file) { // 输入字符串进行解析,通过生成临时文件来实现 void MyFlexer::InputString(const std::string &str) { + is_string_ = true; filename_ = GenerateTmpFile(str); location_.initialize(&filename_); set_debug(0); diff --git a/src/compile/my_flexer.h b/src/compile/my_flexer.h index 25b33299..ce02d729 100644 --- a/src/compile/my_flexer.h +++ b/src/compile/my_flexer.h @@ -40,6 +40,10 @@ class MyFlexer : public yyFlexLexer { return filename_; } + [[nodiscard]] bool IsString() const { + return is_string_; + } + void SetState(State *s) { state_ = s; } @@ -53,8 +57,9 @@ class MyFlexer : public yyFlexLexer { std::string GenerateTmpFile(const std::string &str); private: - yy::location location_; // 记录当前 token 的行列位置 - int long_comment_level_ = 0; // 当前长注释的长括号层级,即 --[==[ 里 = 的个数 + yy::location location_; // 记录当前 token 的行列位置 + int long_comment_level_ = 0;// 当前长注释的长括号层级,即 --[==[ 里 = 的个数 + bool is_string_ = false; std::string filename_; // 当前解析的文件名或源标识 State *state_ = nullptr; // 所属 State,供 bison 打日志时带上 std::ifstream file_; // 文件输入流 diff --git a/src/compile/semantic_analysis.cpp b/src/compile/semantic_analysis.cpp index 3f6be484..b00b911f 100644 --- a/src/compile/semantic_analysis.cpp +++ b/src/compile/semantic_analysis.cpp @@ -1,10 +1,10 @@ #include "compile/semantic_analysis.h" #include "interp/opcode.h" #include "interp/runtime.h" -#include "util/string_util.h" #include "state/state.h" #include "util/common.h" #include "util/exception.h" +#include "util/string_util.h" #include #include #include @@ -106,11 +106,13 @@ struct ReturnCountGraph { bool file_level = false; const SyntaxTreeInterface *func = nullptr; }; + struct Tail { int prefix = 0; const SyntaxTreeInterface *callee = nullptr; bool unknown = false; }; + struct Info { bool has_exact = false; int exact = 0; @@ -121,6 +123,7 @@ struct ReturnCountGraph { std::vector tails; std::vector> sole_calls; }; + struct FileFunc { std::string name; const SyntaxTreeInterface *node = nullptr; @@ -672,8 +675,7 @@ void SemanticAnalysis::CheckUnsupportedSyntax(const SyntaxTreeInterfacePtr &chun ValidateConstAssignInBlock(chunk, {}); } -void SemanticAnalysis::ValidateConstInFuncbody(const SyntaxTreeInterfacePtr &funcbody, std::unordered_set const_names, - const std::unordered_set &extra_shadow) { +void SemanticAnalysis::ValidateConstInFuncbody(const SyntaxTreeInterfacePtr &funcbody, std::unordered_set const_names, const std::unordered_set &extra_shadow) { if (!funcbody) { return; } @@ -1050,8 +1052,8 @@ void SemanticAnalysis::ValidateGotoInBlock(const SyntaxTreeInterfacePtr &chunk, ThrowError("'continue' jumps over local variable declaration", stmt); } } - } else if (stmt->Type() == SyntaxTreeType::Block || stmt->Type() == SyntaxTreeType::While || stmt->Type() == SyntaxTreeType::Repeat || - stmt->Type() == SyntaxTreeType::If || stmt->Type() == SyntaxTreeType::ForLoop || stmt->Type() == SyntaxTreeType::ForIn) { + } else if (stmt->Type() == SyntaxTreeType::Block || stmt->Type() == SyntaxTreeType::While || stmt->Type() == SyntaxTreeType::Repeat || stmt->Type() == SyntaxTreeType::If || + stmt->Type() == SyntaxTreeType::ForLoop || stmt->Type() == SyntaxTreeType::ForIn) { std::vector> nested; CollectGotosInStmt(stmt, nested); for (const auto &[target_name, goto_node]: nested) { @@ -1273,8 +1275,7 @@ void SemanticAnalysis::CheckUndeclaredVars(const SyntaxTreeInterfacePtr &chunk, const auto func = std::dynamic_pointer_cast(stmt); const auto fname = std::dynamic_pointer_cast(func->Funcname()); if (fname && fname->ColonName().empty()) { - if (const auto fnl = std::dynamic_pointer_cast(fname->FuncNameList()); - fnl && fnl->Funcnames().size() == 1) { + if (const auto fnl = std::dynamic_pointer_cast(fname->FuncNameList()); fnl && fnl->Funcnames().size() == 1) { file_level_names.insert(fnl->Funcnames()[0]); } } @@ -1323,8 +1324,7 @@ void SemanticAnalysis::CheckUndeclaredVars(const SyntaxTreeInterfacePtr &chunk, // 文件首行;预处理会把它搬进 __fakelua_init):左值 package 是语法关键字位, // 不是脚本变量。package("X") 调用形式已由上面的调用链豁免覆盖。 if (std::string pkg_name; ExtractPackageName(n, pkg_name)) { - if (const auto vl = std::dynamic_pointer_cast( - std::dynamic_pointer_cast(n)->Varlist())) { + if (const auto vl = std::dynamic_pointer_cast(std::dynamic_pointer_cast(n)->Varlist())) { for (const auto &var_node: vl->Vars()) { exempt_vars.insert(var_node.get()); } @@ -1356,8 +1356,7 @@ void SemanticAnalysis::CheckUndeclaredVars(const SyntaxTreeInterfacePtr &chunk, while (cur && cur->GetVarKind() == VarKind::kDot) { cur = base_var_of(cur); } - if (cur && cur->GetVarKind() == VarKind::kSimple && - s_->GetVM().HasNativeFunctionWithPrefix(std::string(cur->GetName()) + ".")) { + if (cur && cur->GetVarKind() == VarKind::kSimple && s_->GetVM().HasNativeFunctionWithPrefix(std::string(cur->GetName()) + ".")) { exempt_vars.insert(cur.get()); } }); @@ -1366,8 +1365,7 @@ void SemanticAnalysis::CheckUndeclaredVars(const SyntaxTreeInterfacePtr &chunk, CheckVarScopes(chunk, scopes, file_level_names, exempt_vars, lvalue_vars); } -bool SemanticAnalysis::IsDeclaredSimpleName(const std::string &name, const std::vector> &scopes, - const std::unordered_set &file_level_names) const { +bool SemanticAnalysis::IsDeclaredSimpleName(const std::string &name, const std::vector> &scopes, const std::unordered_set &file_level_names) const { if (name == "_VERSION") { return true; } @@ -1386,10 +1384,8 @@ bool SemanticAnalysis::IsDeclaredSimpleName(const std::string &name, const std:: return false; } -void SemanticAnalysis::CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::vector> &scopes, - const std::unordered_set &file_level_names, - const std::unordered_set &exempt_vars, - const std::unordered_set &lvalue_vars) { +void SemanticAnalysis::CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::vector> &scopes, const std::unordered_set &file_level_names, + const std::unordered_set &exempt_vars, const std::unordered_set &lvalue_vars) { if (!node) { return; } @@ -1511,9 +1507,7 @@ void SemanticAnalysis::CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::v for (auto cur = var; cur && cur->GetVarKind() == VarKind::kDot;) { full = cur->GetName() + (full.empty() ? "" : "." + full); const auto pe = std::dynamic_pointer_cast(cur->GetPrefixexp()); - root = (pe && pe->GetPrefixKind() == PrefixExpKind::kVar) - ? std::dynamic_pointer_cast(pe->GetValue()) - : nullptr; + root = (pe && pe->GetPrefixKind() == PrefixExpKind::kVar) ? std::dynamic_pointer_cast(pe->GetValue()) : nullptr; cur = root; } if (root && root->GetVarKind() == VarKind::kSimple && !full.empty()) { @@ -1573,8 +1567,7 @@ void SemanticAnalysis::CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::v break; } case SyntaxTreeType::TableConstructor: { - CheckVarScopes(std::dynamic_pointer_cast(node)->Fieldlist(), scopes, file_level_names, exempt_vars, - lvalue_vars); + CheckVarScopes(std::dynamic_pointer_cast(node)->Fieldlist(), scopes, file_level_names, exempt_vars, lvalue_vars); break; } case SyntaxTreeType::FieldList: { @@ -1634,8 +1627,7 @@ void SemanticAnalysis::CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::v break; } case SyntaxTreeType::PrefixExp: { - CheckVarScopes(std::dynamic_pointer_cast(node)->GetValue(), scopes, file_level_names, exempt_vars, - lvalue_vars); + CheckVarScopes(std::dynamic_pointer_cast(node)->GetValue(), scopes, file_level_names, exempt_vars, lvalue_vars); break; } default: { @@ -1645,7 +1637,6 @@ void SemanticAnalysis::CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::v } } - namespace { // 语义分析内部的寄存器高峰检查:只按与 InterpCodegen 相同的 @@ -1698,6 +1689,7 @@ class RegisterPeakChecker { struct LoopInfo { enum Kind { kWhile, kRepeat, kFor, kForIn } kind = kWhile; + int continue_ip = -1; std::vector break_jmps; std::vector continue_jmps; @@ -1789,11 +1781,10 @@ class RegisterPeakChecker { std::unordered_map defined; std::unordered_map> pending; }; + std::vector label_scopes_; }; - - namespace { // 把「当前行」临时切到节点的源码行,离开作用域还原:嵌套表达式编译完后, @@ -1805,9 +1796,11 @@ class LineScope { cur_ = static_cast(node->Loc().begin.line); } } + ~LineScope() { cur_ = saved_; } + LineScope(const LineScope &) = delete; LineScope &operator=(const LineScope &) = delete; @@ -1818,7 +1811,8 @@ class LineScope { }// namespace -const std::unordered_set RegisterPeakChecker::kStringLibraryMethods = {"len", "sub", "rep", "reverse", "lower", "upper", "byte", "char", "format", "dump", "find", "match", "gmatch", "gsub"}; +const std::unordered_set RegisterPeakChecker::kStringLibraryMethods = {"len", "sub", "rep", "reverse", "lower", "upper", "byte", + "char", "format", "dump", "find", "match", "gmatch", "gsub"}; [[noreturn]] void RegisterPeakChecker::ThrowError(const std::string &msg, const SyntaxTreeInterfacePtr &ptr) { ThrowFakeluaException(std::format("Code generate failed, {} at {}", msg, SyntaxTreeLocationStr(file_name_, ptr))); @@ -1866,23 +1860,23 @@ int RegisterPeakChecker::CodeSize() const { } int64_t RegisterPeakChecker::StringId(const std::string &s) { - (void)s; + (void) s; return 0; } int RegisterPeakChecker::AddConst(const CVar &v) { - (void)v; + (void) v; return 0; } int RegisterPeakChecker::AddStringConst(const std::string &s) { - (void)s; + (void) s; return 0; } int RegisterPeakChecker::Emit(Op op, int a, int b, int c, int sbx) { - (void)op; - (void)sbx; + (void) op; + (void) sbx; if (a < 0 || a > kMaxFunctionRegisterIndex || b < 0 || b > kMaxFunctionRegisterIndex || c < 0 || c > kMaxFunctionRegisterIndex) { ThrowFakeluaException("interpreter: instruction operand out of range"); } @@ -1890,11 +1884,10 @@ int RegisterPeakChecker::Emit(Op op, int a, int b, int c, int sbx) { } void RegisterPeakChecker::PatchSbx(int ip, int dest) { - (void)ip; - (void)dest; + (void) ip; + (void) dest; } - int RegisterPeakChecker::LoadConstTo(int dest, const CVar &v) { if (v.type_ == static_cast(VarType::Nil)) { Emit(Op::LOADNIL, dest); @@ -1982,7 +1975,7 @@ bool RegisterPeakChecker::IsPackageHeaderStmt(const SyntaxTreeInterfacePtr &stmt } int RegisterPeakChecker::ResolveSimpleName(const SyntaxTreeVar *var, const std::string &name, bool as_callee) { - (void)as_callee; + (void) as_callee; if (const auto it = var_to_def_map_.find(var); it != var_to_def_map_.end()) { VarDef *def = it->second; if (def->defining_func == cur_func_ || def->defining_func != nullptr) { @@ -2948,7 +2941,7 @@ void RegisterPeakChecker::CompileStmtForIn(const SyntaxTreeInterfacePtr &stmt) { Emit(Op::UNBOX, iter_s, iter_s, 0); Emit(Op::UNBOX, iter_var, e2, 0); for (size_t i = 3; i < exps.size(); ++i) { - (void)CompileExp(exps[i], i + 1 == exps.size()); + (void) CompileExp(exps[i], i + 1 == exps.size()); } } @@ -3323,8 +3316,7 @@ int RegisterPeakChecker::CompileFunctioncall(const SyntaxTreeInterfacePtr &funct // 函数定义。文件级 local 变量(如 local gv = f)即使 defining_func // 为 nullptr 也不在 named_protos_ 里,必须按闭包调用,否则运行时报 // "function 'gv' not found"(C 后端此处按闭包处理,行为一致)。 - file_level_func = it->second->defining_func == nullptr && it->second->def_node != nullptr && - it->second->def_node->Type() == SyntaxTreeType::LocalFunction; + file_level_func = it->second->defining_func == nullptr && it->second->def_node != nullptr && it->second->def_node->Type() == SyntaxTreeType::LocalFunction; } } else if (var && var->GetVarKind() == VarKind::kDot) { const auto base_pe = var->GetPrefixexp(); @@ -3506,8 +3498,6 @@ int RegisterPeakChecker::CompileUnop(const SyntaxTreeInterfacePtr &exp, int dest return r; } - - void RegisterPeakChecker::Walk(const ParseResult &pr, const AnalysisResult &ar) { file_name_ = pr.file_name; ar_ = &ar; diff --git a/src/compile/semantic_analysis.h b/src/compile/semantic_analysis.h index e242fc13..50e798fa 100644 --- a/src/compile/semantic_analysis.h +++ b/src/compile/semantic_analysis.h @@ -39,8 +39,7 @@ class SemanticAnalysis { // 在表达式树中查找匿名 FunctionDef,并按外层词法 const 集合校验其函数体。 void ValidateConstInExprs(const SyntaxTreeInterfacePtr &node, const std::unordered_set &const_names); // 进入函数体:继承外层 const,参数名 / extra_shadow 遮蔽同名 const。 - void ValidateConstInFuncbody(const SyntaxTreeInterfacePtr &funcbody, std::unordered_set const_names, - const std::unordered_set &extra_shadow = {}); + void ValidateConstInFuncbody(const SyntaxTreeInterfacePtr &funcbody, std::unordered_set const_names, const std::unordered_set &extra_shadow = {}); void CollectBlockLabels(const SyntaxTreeInterfacePtr &block, std::unordered_map &labels); void CheckFunctionCall(const SyntaxTreeInterfacePtr &node); void CheckParList(const SyntaxTreeInterfacePtr &node, const AnalysisResult &ar); @@ -58,13 +57,9 @@ class SemanticAnalysis { // 声明(package "X" / package = "X")的名字。 void CheckUndeclaredVars(const SyntaxTreeInterfacePtr &chunk, const AnalysisResult &ar); // CheckUndeclaredVars 的词法作用域递归,作用域栈语义与 CGen::ResolveScopes 对齐。 - void CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::vector> &scopes, - const std::unordered_set &file_level_names, - const std::unordered_set &exempt_vars, - const std::unordered_set &lvalue_vars); - [[nodiscard]] bool IsDeclaredSimpleName(const std::string &name, - const std::vector> &scopes, - const std::unordered_set &file_level_names) const; + void CheckVarScopes(const SyntaxTreeInterfacePtr &node, std::vector> &scopes, const std::unordered_set &file_level_names, + const std::unordered_set &exempt_vars, const std::unordered_set &lvalue_vars); + [[nodiscard]] bool IsDeclaredSimpleName(const std::string &name, const std::vector> &scopes, const std::unordered_set &file_level_names) const; [[noreturn]] void ThrowError(const std::string &msg, const SyntaxTreeInterfacePtr &ptr); void AnalyzeFunctionReturnCounts(const SyntaxTreeInterfacePtr &chunk, AnalysisResult &ar); diff --git a/src/compile/type_inferencer.cpp b/src/compile/type_inferencer.cpp index f8ff554a..832a0096 100644 --- a/src/compile/type_inferencer.cpp +++ b/src/compile/type_inferencer.cpp @@ -2044,7 +2044,7 @@ void TypeInferencer::AnalyzeTableShapes(const SyntaxTreeInterfacePtr &chunk, Inf // 否则 optional FL_SET_SPEC 会写到错误的 struct 字段。 for (const auto &frame_ptr: frames) { for (auto &[name, fields]: frame_ptr->var_fields) { - (void)name; + (void) name; UniquifySpecFieldCNames(fields); } } diff --git a/src/interp/codegen.cpp b/src/interp/codegen.cpp index 536ee810..6388a1c6 100644 --- a/src/interp/codegen.cpp +++ b/src/interp/codegen.cpp @@ -1,12 +1,12 @@ #include "interp/codegen.h" #include "compile/compile_common.h" +#include "interp/interpreter.h" #include "interp/runtime.h" +#include "jit/vm.h" #include "state/const_string.h" #include "state/state.h" #include "util/exception.h" -#include "interp/interpreter.h" -#include "jit/vm.h" #include "util/string_util.h" #include "var/var.h" #include @@ -28,9 +28,11 @@ class LineScope { cur_ = static_cast(node->Loc().begin.line); } } + ~LineScope() { cur_ = saved_; } + LineScope(const LineScope &) = delete; LineScope &operator=(const LineScope &) = delete; @@ -278,7 +280,7 @@ CVar InterpCodegen::LiteralFromExp(const SyntaxTreeInterfacePtr &exp) { } int InterpCodegen::ResolveSimpleName(const SyntaxTreeVar *var, const std::string &name, bool as_callee) { - (void)as_callee; + (void) as_callee; if (const auto it = var_to_def_map_.find(var); it != var_to_def_map_.end()) { VarDef *def = it->second; if (def->defining_func == cur_func_ || def->defining_func != nullptr) { @@ -1310,7 +1312,7 @@ void InterpCodegen::CompileStmtForIn(const SyntaxTreeInterfacePtr &stmt) { Emit(Op::UNBOX, iter_s, iter_s, 0); Emit(Op::UNBOX, iter_var, e2, 0); for (size_t i = 3; i < exps.size(); ++i) { - (void)CompileExp(exps[i], i + 1 == exps.size()); + (void) CompileExp(exps[i], i + 1 == exps.size()); } } @@ -1710,8 +1712,7 @@ int InterpCodegen::CompileFunctioncall(const SyntaxTreeInterfacePtr &functioncal // 函数定义。文件级 local 变量(如 local gv = f)即使 defining_func // 为 nullptr 也不在 named_protos_ 里,必须按闭包调用,否则运行时报 // "function 'gv' not found"(C 后端此处按闭包处理,行为一致)。 - file_level_func = it->second->defining_func == nullptr && it->second->def_node != nullptr && - it->second->def_node->Type() == SyntaxTreeType::LocalFunction; + file_level_func = it->second->defining_func == nullptr && it->second->def_node != nullptr && it->second->def_node->Type() == SyntaxTreeType::LocalFunction; } } else if (var && var->GetVarKind() == VarKind::kDot) { const auto base_pe = var->GetPrefixexp(); @@ -1903,7 +1904,6 @@ void InterpCodegen::Build(const ParseResult &pr, const AnalysisResult &ar) { Walk(pr, ar); } - void InterpCodegen::Walk(const ParseResult &pr, const AnalysisResult &ar) { file_name_ = pr.file_name; ar_ = &ar; @@ -1985,8 +1985,6 @@ void InterpCodegen::Walk(const ParseResult &pr, const AnalysisResult &ar) { } } - - void InterpCodegen::RegisterAndInit() { if (!unit_hold_ || !unit_) { ThrowFakeluaException("interpreter: RegisterAndInit called before Build"); diff --git a/src/interp/codegen.h b/src/interp/codegen.h index c02c9959..bee9d587 100644 --- a/src/interp/codegen.h +++ b/src/interp/codegen.h @@ -70,6 +70,7 @@ class InterpCodegen { struct LoopInfo { enum Kind { kWhile, kRepeat, kFor, kForIn } kind = kWhile; + int continue_ip = -1; std::vector break_jmps; std::vector continue_jmps; @@ -140,7 +141,7 @@ class InterpCodegen { static const std::unordered_set kStringLibraryMethods; bool check_only_ = false; - int check_ip_ = 0; // 控制流位置占位,无指令流 + int check_ip_ = 0;// 控制流位置占位,无指令流 State *s_ = nullptr; std::string file_name_; const AnalysisResult *ar_ = nullptr; @@ -173,6 +174,7 @@ class InterpCodegen { std::unordered_map defined; std::unordered_map> pending; }; + std::vector label_scopes_; }; diff --git a/src/interp/func_proto.h b/src/interp/func_proto.h index 61f202b8..c689336f 100644 --- a/src/interp/func_proto.h +++ b/src/interp/func_proto.h @@ -19,9 +19,11 @@ struct FuncProto; // 一次编译单元:持有全部函数原型与文件级全局槽,生命周期绑定到 VmFunction 的 handle。 struct TransparentStringHash { using is_transparent = void; + std::size_t operator()(std::string_view sv) const noexcept { return std::hash{}(sv); } + std::size_t operator()(const std::string &s) const noexcept { return std::hash{}(s); } diff --git a/src/interp/interpreter.cpp b/src/interp/interpreter.cpp index 182ab235..a8f9063f 100644 --- a/src/interp/interpreter.cpp +++ b/src/interp/interpreter.cpp @@ -120,6 +120,7 @@ struct InterpTailJob { int arg_count = 0; CVar args[kMaxFunctionInputParams]{}; }; + thread_local InterpTailJob g_interp_tail; void RequestTailTrampoline(FuncProto *new_proto, VarClosure *new_cl, const CVar *args, int arg_count) { @@ -218,7 +219,8 @@ int PackNamedCallArgs(State *s, std::string_view name, bool is_vararg, int expec // 在本 InterpreterExecuteT 帧内切到新的 INTERP 原型;需要换 HasBoxes 模板或扩容时走 trampoline。 // 成功且应继续分发返回 true(已更新 proto/cl/ip/stk);应 return ret 则返回 false。 template -bool ApplyInterpTail(State *s, FuncProto *&proto, VarClosure *&cl, CVar *const stk, CVar **const boxp, size_t saved, const Inst *&ip, FuncProto *new_proto, VarClosure *new_cl, const CVar *args, int arg_count, CVar &ret) { +bool ApplyInterpTail(State *s, FuncProto *&proto, VarClosure *&cl, CVar *const stk, CVar **const boxp, size_t saved, const Inst *&ip, FuncProto *new_proto, VarClosure *new_cl, const CVar *args, + int arg_count, CVar &ret) { if (!new_proto) { ThrowFakeluaException("interpreter: null function prototype"); } @@ -279,7 +281,6 @@ bool ApplyInterpTail(State *s, FuncProto *&proto, VarClosure *&cl, CVar *const s return true; } - // 精确自尾调用:复用当前帧与 proto/cl,不做查表、装箱、整帧 memset。 // 参数先拷到 TLS(可能与 R(0..) 重叠),再写形参并清空其余寄存器。 // 不用 InterpreterExecuteT 的栈上大数组,以免抬高非尾调用的 C 帧。 @@ -396,6 +397,7 @@ struct InterpFrame { size_t saved; CVar *stk; CVar **boxp; + InterpFrame(size_t nslots, bool want_boxes) { saved = g_interp_top; const size_t need = saved + nslots; @@ -428,6 +430,7 @@ struct InterpFrame { } g_interp_top = need; } + ~InterpFrame() { if (boxp) { --g_interp_boxed_frames; @@ -459,433 +462,432 @@ CVar InterpreterExecuteT(State *s, FuncProto *proto, const CVar *args, int arg_c // 所以补位置不需要在热路径上为每个指令先存一份。 try { - // GNU/Clang:token-threading(computed goto)。字节码由 codegen 保证以 RETURN - // 结尾、跳转落在函数内,热路径不再做 pc 越界 / 未知 opcode 检查。 + // GNU/Clang:token-threading(computed goto)。字节码由 codegen 保证以 RETURN + // 结尾、跳转落在函数内,热路径不再做 pc 越界 / 未知 opcode 检查。 #if defined(__GNUC__) #define FAKELUA_OP_LABEL_ADDR(name) &&L_OP_##name, - static const void *const kDispatch[] = {FAKELUA_FOR_EACH_OP(FAKELUA_OP_LABEL_ADDR)}; + static const void *const kDispatch[] = {FAKELUA_FOR_EACH_OP(FAKELUA_OP_LABEL_ADDR)}; #undef FAKELUA_OP_LABEL_ADDR - static_assert(sizeof(kDispatch) / sizeof(kDispatch[0]) == static_cast(Op::COUNT)); - const void *const *const dispatch = kDispatch; -#define VM_DISPATCH_NEXT() \ - do { \ - inst = *ip++; \ - goto *dispatch[static_cast(inst.op)]; \ + static_assert(sizeof(kDispatch) / sizeof(kDispatch[0]) == static_cast(Op::COUNT)); + const void *const *const dispatch = kDispatch; +#define VM_DISPATCH_NEXT() \ + do { \ + inst = *ip++; \ + goto *dispatch[static_cast(inst.op)]; \ } while (0) #define VM_CASE(name) L_OP_##name: - if (!ip) { - return interp_rt::Nil(); - } - VM_DISPATCH_NEXT(); + if (!ip) { + return interp_rt::Nil(); + } + VM_DISPATCH_NEXT(); #else #define VM_DISPATCH_NEXT() goto vm_switch_dispatch #define VM_CASE(name) case Op::name: -vm_switch_dispatch: - if (!ip) { - return interp_rt::Nil(); - } - inst = *ip++; - switch (inst.op) { + vm_switch_dispatch: + if (!ip) { + return interp_rt::Nil(); + } + inst = *ip++; + switch (inst.op) { #endif - VM_CASE(MOVE) { - VM_W(inst.a, VM_R(inst.b)); - VM_DISPATCH_NEXT(); - } - VM_CASE(LOADK) { - VM_W(inst.a, proto->constants[static_cast(inst.sbx)]); - VM_DISPATCH_NEXT(); - } - VM_CASE(LOADNIL) { - VM_W(inst.a, interp_rt::Nil()); - VM_DISPATCH_NEXT(); - } - VM_CASE(LOADBOOL) { - VM_W(inst.a, interp_rt::Bool(inst.b != 0)); - VM_DISPATCH_NEXT(); - } - VM_CASE(GETGLOBAL) { - VM_W(inst.a, LookupGlobal(proto, ConstToName(proto->constants[static_cast(inst.sbx)]))); - VM_DISPATCH_NEXT(); - } - VM_CASE(SETGLOBAL) { - SetGlobal(proto, ConstToName(proto->constants[static_cast(inst.sbx)]), VM_R(inst.a)); - VM_DISPATCH_NEXT(); - } - VM_CASE(GETUPVAL) { - if (!cl || inst.b >= static_cast(cl->upvalue_count) || !cl->upvalues[inst.b]) { + VM_CASE(MOVE) { + VM_W(inst.a, VM_R(inst.b)); + VM_DISPATCH_NEXT(); + } + VM_CASE(LOADK) { + VM_W(inst.a, proto->constants[static_cast(inst.sbx)]); + VM_DISPATCH_NEXT(); + } + VM_CASE(LOADNIL) { VM_W(inst.a, interp_rt::Nil()); - } else { - VM_W(inst.a, *cl->upvalues[inst.b]); + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(SETUPVAL) { - if (cl && inst.b < static_cast(cl->upvalue_count) && cl->upvalues[inst.b]) { - *cl->upvalues[inst.b] = VM_R(inst.a); + VM_CASE(LOADBOOL) { + VM_W(inst.a, interp_rt::Bool(inst.b != 0)); + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(GETTABLE) { - const CVar t = VM_R(inst.b); - const CVar k = VM_R(inst.c); - if (FL_VM_UNLIKELY(t.type_ != interp_rt::kTable)) { - ThrowFakeluaException("attempt to index a non-table value"); + VM_CASE(GETGLOBAL) { + VM_W(inst.a, LookupGlobal(proto, ConstToName(proto->constants[static_cast(inst.sbx)]))); + VM_DISPATCH_NEXT(); } - if (FL_VM_UNLIKELY(k.type_ == interp_rt::kNil)) { - ThrowFakeluaException("table index is nil"); + VM_CASE(SETGLOBAL) { + SetGlobal(proto, ConstToName(proto->constants[static_cast(inst.sbx)]), VM_R(inst.a)); + VM_DISPATCH_NEXT(); } - if (k.type_ == interp_rt::kInt) { - VM_W(inst.a, table::TableHelper::GetTableInt(s, t, k.data_.i)); - } else { - VM_W(inst.a, table::TableHelper::GetTable(s, t, k)); + VM_CASE(GETUPVAL) { + if (!cl || inst.b >= static_cast(cl->upvalue_count) || !cl->upvalues[inst.b]) { + VM_W(inst.a, interp_rt::Nil()); + } else { + VM_W(inst.a, *cl->upvalues[inst.b]); + } + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(SETTABLE) { - interp_rt::SetTable(s, VM_R(inst.a), VM_R(inst.b), VM_R(inst.c)); - VM_DISPATCH_NEXT(); - } - VM_CASE(NEWTABLE) { - VM_W(inst.a, interp_rt::NewTable(s)); - VM_DISPATCH_NEXT(); - } - VM_CASE(NEWBOX) { - if constexpr (!HasBoxes) { - ThrowFakeluaException("interpreter: NEWBOX without boxes"); - } else { - auto *box = static_cast(interp_rt::Alloc(s, sizeof(CVar))); - *box = VM_R(inst.a); - boxp[inst.a] = box; + VM_CASE(SETUPVAL) { + if (cl && inst.b < static_cast(cl->upvalue_count) && cl->upvalues[inst.b]) { + *cl->upvalues[inst.b] = VM_R(inst.a); + } + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(ADD) { - FL_VM_ARITH_INT(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, add, interp_rt::BinAdd); - VM_DISPATCH_NEXT(); - } - VM_CASE(SUB) { - FL_VM_ARITH_INT(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, sub, interp_rt::BinSub); - VM_DISPATCH_NEXT(); - } - VM_CASE(MUL) { - FL_VM_ARITH_INT(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, mul, interp_rt::BinMul); - VM_DISPATCH_NEXT(); - } - VM_CASE(DIV) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinDiv); - VM_DISPATCH_NEXT(); - } - VM_CASE(IDIV) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinIdiv); - VM_DISPATCH_NEXT(); - } - VM_CASE(MOD) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinMod); - VM_DISPATCH_NEXT(); - } - VM_CASE(POW) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinPow); - VM_DISPATCH_NEXT(); - } - VM_CASE(BAND) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinBand); - VM_DISPATCH_NEXT(); - } - VM_CASE(BXOR) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinBxor); - VM_DISPATCH_NEXT(); - } - VM_CASE(BOR) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinBor); - VM_DISPATCH_NEXT(); - } - VM_CASE(SHL) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinShl); - VM_DISPATCH_NEXT(); - } - VM_CASE(SHR) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinShr); - VM_DISPATCH_NEXT(); - } - VM_CASE(CONCAT) { - VM_W(inst.a, interp_rt::BinConcat(s, VM_R(inst.b), VM_R(inst.c))); - VM_DISPATCH_NEXT(); - } - VM_CASE(UNM) { - FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnMinus); - VM_DISPATCH_NEXT(); - } - VM_CASE(NOT) { - FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnNot); - VM_DISPATCH_NEXT(); - } - VM_CASE(LEN) { - FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnLen); - VM_DISPATCH_NEXT(); - } - VM_CASE(BNOT) { - FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnBnot); - VM_DISPATCH_NEXT(); - } - VM_CASE(EQ) { - const CVar &lb = VM_R(inst.b); - const CVar &lc = VM_R(inst.c); - if (FL_VM_LIKELY(lb.type_ == interp_rt::kInt && lc.type_ == interp_rt::kInt)) { - VM_W(inst.a, interp_rt::Bool(lb.data_.i == lc.data_.i)); - } else { - VM_W(inst.a, interp_rt::Bool(interp_rt::IsEqual(lb, lc))); + VM_CASE(GETTABLE) { + const CVar t = VM_R(inst.b); + const CVar k = VM_R(inst.c); + if (FL_VM_UNLIKELY(t.type_ != interp_rt::kTable)) { + ThrowFakeluaException("attempt to index a non-table value"); + } + if (FL_VM_UNLIKELY(k.type_ == interp_rt::kNil)) { + ThrowFakeluaException("table index is nil"); + } + if (k.type_ == interp_rt::kInt) { + VM_W(inst.a, table::TableHelper::GetTableInt(s, t, k.data_.i)); + } else { + VM_W(inst.a, table::TableHelper::GetTable(s, t, k)); + } + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(LT) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::CmpLt); - VM_DISPATCH_NEXT(); - } - VM_CASE(LE) { - FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::CmpLe); - VM_DISPATCH_NEXT(); - } - VM_CASE(TESTJMP) { - if (interp_rt::IsTrue(VM_R(inst.a)) == (inst.b != 0)) { - ip += inst.sbx; + VM_CASE(SETTABLE) { + interp_rt::SetTable(s, VM_R(inst.a), VM_R(inst.b), VM_R(inst.c)); + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(JMP) { - ip += inst.sbx; - VM_DISPATCH_NEXT(); - } - VM_CASE(CALL) { - CVar fn = VM_R(inst.a); - if (fn.type_ != static_cast(VarType::Closure) || !fn.data_.cl) { - ThrowFakeluaException("attempt to call a non-function value"); + VM_CASE(NEWTABLE) { + VM_W(inst.a, interp_rt::NewTable(s)); + VM_DISPATCH_NEXT(); } - CVar call_args[kMaxFunctionInputParams]; - const int nargs = inst.b; - if (nargs < 0 || nargs > static_cast(kMaxFunctionInputParams)) { - ThrowFakeluaException(std::format("interpreter: too many arguments ({}), max is {}", nargs, kMaxFunctionInputParams)); - } - int nflat = 0; - for (int i = 0; i < nargs; ++i) { - CVar av = VM_R(inst.a + 1 + i); - if (i == nargs - 1 && av.type_ == static_cast(VarType::Multi)) { - VarMulti *m = av.data_.m; - const int extra = m ? static_cast(m->GetCount()) : 0; - if (nflat + extra > static_cast(kMaxFunctionInputParams)) { - ThrowFakeluaException(std::format("interpreter: too many arguments ({}), max is {}", nflat + extra, kMaxFunctionInputParams)); - } - for (uint32_t j = 0; j < m->GetCount(); ++j) { - call_args[nflat++] = m->GetVars()[j]; - } + VM_CASE(NEWBOX) { + if constexpr (!HasBoxes) { + ThrowFakeluaException("interpreter: NEWBOX without boxes"); } else { - call_args[nflat++] = interp_rt::UnboxMulti(av, 0); - } - } - CVar ret; - VarClosure *callee = fn.data_.cl; - // 解释器闭包且非变参、实参个数正好等于形参时,直接 InterpreterExecute, - // 跳过 DispatchCallClosure 的再装箱与 LuaLocScope。其余情况(变参、少参/多参、 - // load() 源码闭包、TCC/GCC 闭包)仍走原来的 DispatchCallClosure。 - if (IsInterpClosure(callee) && !callee->is_vararg && - callee->expected_arg_count == nflat && - nflat <= static_cast(kMaxFunctionInputParams)) { - ret = InterpExecuteInContext(s, AsInterpProto(callee->func_ptr), call_args, nflat, callee); - } else { - ret = inter::DispatchCallClosure(s, callee, call_args, nflat, JIT_INTERP); + auto *box = static_cast(interp_rt::Alloc(s, sizeof(CVar))); + *box = VM_R(inst.a); + boxp[inst.a] = box; + } + VM_DISPATCH_NEXT(); } - VM_W(inst.a, inst.c == 0 ? ret : interp_rt::UnboxMulti(ret, 0)); - VM_DISPATCH_NEXT(); - } - VM_CASE(CALLNAME) { - // ConstToName 返回 string_view(interned,不一定带 NUL)。成功路径不再堆分配 - // std::string;报错走 std::format 的 string_view,仅在 CallByNameImpl 的失败路径 - // 才可能分配。 - { - const std::string_view name = ConstToName(proto->constants[static_cast(inst.sbx)]); + VM_CASE(ADD) { + FL_VM_ARITH_INT(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, add, interp_rt::BinAdd); + VM_DISPATCH_NEXT(); + } + VM_CASE(SUB) { + FL_VM_ARITH_INT(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, sub, interp_rt::BinSub); + VM_DISPATCH_NEXT(); + } + VM_CASE(MUL) { + FL_VM_ARITH_INT(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, mul, interp_rt::BinMul); + VM_DISPATCH_NEXT(); + } + VM_CASE(DIV) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinDiv); + VM_DISPATCH_NEXT(); + } + VM_CASE(IDIV) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinIdiv); + VM_DISPATCH_NEXT(); + } + VM_CASE(MOD) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinMod); + VM_DISPATCH_NEXT(); + } + VM_CASE(POW) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinPow); + VM_DISPATCH_NEXT(); + } + VM_CASE(BAND) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinBand); + VM_DISPATCH_NEXT(); + } + VM_CASE(BXOR) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinBxor); + VM_DISPATCH_NEXT(); + } + VM_CASE(BOR) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinBor); + VM_DISPATCH_NEXT(); + } + VM_CASE(SHL) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinShl); + VM_DISPATCH_NEXT(); + } + VM_CASE(SHR) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::BinShr); + VM_DISPATCH_NEXT(); + } + VM_CASE(CONCAT) { + VM_W(inst.a, interp_rt::BinConcat(s, VM_R(inst.b), VM_R(inst.c))); + VM_DISPATCH_NEXT(); + } + VM_CASE(UNM) { + FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnMinus); + VM_DISPATCH_NEXT(); + } + VM_CASE(NOT) { + FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnNot); + VM_DISPATCH_NEXT(); + } + VM_CASE(LEN) { + FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnLen); + VM_DISPATCH_NEXT(); + } + VM_CASE(BNOT) { + FL_VM_UNOP(HasBoxes, stk, boxp, inst.a, inst.b, interp_rt::UnBnot); + VM_DISPATCH_NEXT(); + } + VM_CASE(EQ) { + const CVar &lb = VM_R(inst.b); + const CVar &lc = VM_R(inst.c); + if (FL_VM_LIKELY(lb.type_ == interp_rt::kInt && lc.type_ == interp_rt::kInt)) { + VM_W(inst.a, interp_rt::Bool(lb.data_.i == lc.data_.i)); + } else { + VM_W(inst.a, interp_rt::Bool(interp_rt::IsEqual(lb, lc))); + } + VM_DISPATCH_NEXT(); + } + VM_CASE(LT) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::CmpLt); + VM_DISPATCH_NEXT(); + } + VM_CASE(LE) { + FL_VM_BINOP(HasBoxes, stk, boxp, inst.a, inst.b, inst.c, interp_rt::CmpLe); + VM_DISPATCH_NEXT(); + } + VM_CASE(TESTJMP) { + if (interp_rt::IsTrue(VM_R(inst.a)) == (inst.b != 0)) { + ip += inst.sbx; + } + VM_DISPATCH_NEXT(); + } + VM_CASE(JMP) { + ip += inst.sbx; + VM_DISPATCH_NEXT(); + } + VM_CASE(CALL) { + CVar fn = VM_R(inst.a); + if (fn.type_ != static_cast(VarType::Closure) || !fn.data_.cl) { + ThrowFakeluaException("attempt to call a non-function value"); + } CVar call_args[kMaxFunctionInputParams]; const int nargs = inst.b; if (nargs < 0 || nargs > static_cast(kMaxFunctionInputParams)) { - ThrowFakeluaException(std::format("interpreter: too many arguments ({}) passed for function '{}', max is {}", nargs, name, kMaxFunctionInputParams)); + ThrowFakeluaException(std::format("interpreter: too many arguments ({}), max is {}", nargs, kMaxFunctionInputParams)); } + int nflat = 0; for (int i = 0; i < nargs; ++i) { - call_args[i] = VM_R(inst.a + i); + CVar av = VM_R(inst.a + 1 + i); + if (i == nargs - 1 && av.type_ == static_cast(VarType::Multi)) { + VarMulti *m = av.data_.m; + const int extra = m ? static_cast(m->GetCount()) : 0; + if (nflat + extra > static_cast(kMaxFunctionInputParams)) { + ThrowFakeluaException(std::format("interpreter: too many arguments ({}), max is {}", nflat + extra, kMaxFunctionInputParams)); + } + for (uint32_t j = 0; j < m->GetCount(); ++j) { + call_args[nflat++] = m->GetVars()[j]; + } + } else { + call_args[nflat++] = interp_rt::UnboxMulti(av, 0); + } } CVar ret; - if (!TryInterpNamedCall(s, name, call_args, nargs, ret)) { - ret = CallByNameArgs(s, JIT_INTERP, name, nargs, call_args); + VarClosure *callee = fn.data_.cl; + // 解释器闭包且非变参、实参个数正好等于形参时,直接 InterpreterExecute, + // 跳过 DispatchCallClosure 的再装箱与 LuaLocScope。其余情况(变参、少参/多参、 + // load() 源码闭包、TCC/GCC 闭包)仍走原来的 DispatchCallClosure。 + if (IsInterpClosure(callee) && !callee->is_vararg && callee->expected_arg_count == nflat && nflat <= static_cast(kMaxFunctionInputParams)) { + ret = InterpExecuteInContext(s, AsInterpProto(callee->func_ptr), call_args, nflat, callee); + } else { + ret = inter::DispatchCallClosure(s, callee, call_args, nflat, JIT_INTERP); } VM_W(inst.a, inst.c == 0 ? ret : interp_rt::UnboxMulti(ret, 0)); + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(TAIL) { - if (inst.c == kTailSelfExact) { - // 本函数精确自尾调用:保持 cl,直接重置 ip。 - if (ReuseFrameSelfTail(proto, stk, boxp, ip, inst.a + 1, inst.b)) { - VM_DISPATCH_NEXT(); + VM_CASE(CALLNAME) { + // ConstToName 返回 string_view(interned,不一定带 NUL)。成功路径不再堆分配 + // std::string;报错走 std::format 的 string_view,仅在 CallByNameImpl 的失败路径 + // 才可能分配。 + { + const std::string_view name = ConstToName(proto->constants[static_cast(inst.sbx)]); + CVar call_args[kMaxFunctionInputParams]; + const int nargs = inst.b; + if (nargs < 0 || nargs > static_cast(kMaxFunctionInputParams)) { + ThrowFakeluaException(std::format("interpreter: too many arguments ({}) passed for function '{}', max is {}", nargs, name, kMaxFunctionInputParams)); + } + for (int i = 0; i < nargs; ++i) { + call_args[i] = VM_R(inst.a + i); + } + CVar ret; + if (!TryInterpNamedCall(s, name, call_args, nargs, ret)) { + ret = CallByNameArgs(s, JIT_INTERP, name, nargs, call_args); + } + VM_W(inst.a, inst.c == 0 ? ret : interp_rt::UnboxMulti(ret, 0)); } - return interp_rt::Nil(); - } - CVar ret{}; - const bool cont = InterpDoTail(s, proto, cl, stk, boxp, frame.saved, ip, inst, ret); - if (cont) { VM_DISPATCH_NEXT(); } - return ret; - } - VM_CASE(TAILNAME) { - if (inst.c == kTailSelfExact) { - if (ReuseFrameSelfTail(proto, stk, boxp, ip, inst.a, inst.b)) { + VM_CASE(TAIL) { + if (inst.c == kTailSelfExact) { + // 本函数精确自尾调用:保持 cl,直接重置 ip。 + if (ReuseFrameSelfTail(proto, stk, boxp, ip, inst.a + 1, inst.b)) { + VM_DISPATCH_NEXT(); + } + return interp_rt::Nil(); + } + CVar ret{}; + const bool cont = InterpDoTail(s, proto, cl, stk, boxp, frame.saved, ip, inst, ret); + if (cont) { VM_DISPATCH_NEXT(); } - return interp_rt::Nil(); - } - CVar ret{}; - const bool cont = InterpDoTailName(s, proto, cl, stk, boxp, frame.saved, ip, inst, ret); - if (cont) { - VM_DISPATCH_NEXT(); + return ret; } - return ret; - } - VM_CASE(RETURN) { - CVar ret = VM_R(inst.a); - return inst.b == 0 ? ret : interp_rt::UnboxMulti(ret, 0); - } - VM_CASE(CLOSURE) { - { - FuncProto *child = proto->child_protos[static_cast(inst.sbx)]; - const int nup = static_cast(child->upvalues.size()); - std::vector upbufs(static_cast(nup), nullptr); - for (int i = 0; i < nup; ++i) { - const auto &uv = child->upvalues[static_cast(i)]; - if (uv.in_stack) { - if constexpr (!HasBoxes) { - ThrowFakeluaException("interpreter: captured local without boxes"); - } else { - if (!boxp[uv.idx]) { - auto *box = static_cast(interp_rt::Alloc(s, sizeof(CVar))); - *box = VM_R(uv.idx); - boxp[uv.idx] = box; + VM_CASE(TAILNAME) { + if (inst.c == kTailSelfExact) { + if (ReuseFrameSelfTail(proto, stk, boxp, ip, inst.a, inst.b)) { + VM_DISPATCH_NEXT(); + } + return interp_rt::Nil(); + } + CVar ret{}; + const bool cont = InterpDoTailName(s, proto, cl, stk, boxp, frame.saved, ip, inst, ret); + if (cont) { + VM_DISPATCH_NEXT(); + } + return ret; + } + VM_CASE(RETURN) { + CVar ret = VM_R(inst.a); + return inst.b == 0 ? ret : interp_rt::UnboxMulti(ret, 0); + } + VM_CASE(CLOSURE) { + { + FuncProto *child = proto->child_protos[static_cast(inst.sbx)]; + const int nup = static_cast(child->upvalues.size()); + std::vector upbufs(static_cast(nup), nullptr); + for (int i = 0; i < nup; ++i) { + const auto &uv = child->upvalues[static_cast(i)]; + if (uv.in_stack) { + if constexpr (!HasBoxes) { + ThrowFakeluaException("interpreter: captured local without boxes"); + } else { + if (!boxp[uv.idx]) { + auto *box = static_cast(interp_rt::Alloc(s, sizeof(CVar))); + *box = VM_R(uv.idx); + boxp[uv.idx] = box; + } + upbufs[i] = boxp[uv.idx]; } - upbufs[i] = boxp[uv.idx]; + } else { + upbufs[i] = (cl && uv.idx < static_cast(cl->upvalue_count)) ? cl->upvalues[uv.idx] : nullptr; } - } else { - upbufs[i] = (cl && uv.idx < static_cast(cl->upvalue_count)) ? cl->upvalues[uv.idx] : nullptr; } + VM_W(inst.a, interp_rt::MakeClosure(s, TagInterpProto(child), nup, child->param_count, child->is_vararg, upbufs.empty() ? nullptr : upbufs.data())); } - VM_W(inst.a, interp_rt::MakeClosure(s, TagInterpProto(child), nup, child->param_count, child->is_vararg, upbufs.empty() ? nullptr : upbufs.data())); - } - VM_DISPATCH_NEXT(); - } - VM_CASE(SETLIST) { - interp_rt::TableExpandMulti(s, VM_R(inst.a), inst.sbx, VM_R(inst.c)); - VM_DISPATCH_NEXT(); - } - VM_CASE(TABENT) { - CVar idxv = VM_R(inst.sbx); - uint32_t idx = 0; - if (idxv.type_ == static_cast(VarType::Int) && idxv.data_.i >= 0) { - idx = static_cast(idxv.data_.i); + VM_DISPATCH_NEXT(); } - CVar k, v; - if (!interp_rt::TableEntry(VM_R(inst.a), idx, k, v)) { - k = interp_rt::Nil(); - v = interp_rt::Nil(); + VM_CASE(SETLIST) { + interp_rt::TableExpandMulti(s, VM_R(inst.a), inst.sbx, VM_R(inst.c)); + VM_DISPATCH_NEXT(); } - VM_W(inst.b, k); - VM_W(inst.c, v); - VM_DISPATCH_NEXT(); - } - VM_CASE(TABCOUNT) { - VM_W(inst.a, interp_rt::Int(static_cast(interp_rt::TableEntryCount(VM_R(inst.b))))); - VM_DISPATCH_NEXT(); - } - VM_CASE(UNBOX) { - VM_W(inst.a, interp_rt::UnboxMulti(VM_R(inst.b), inst.c)); - VM_DISPATCH_NEXT(); - } - VM_CASE(MAKEMULTI) { - { - const int n = inst.b; - std::vector vals(static_cast(n)); - for (int i = 0; i < n; ++i) { - vals[static_cast(i)] = VM_R(inst.a + i); + VM_CASE(TABENT) { + CVar idxv = VM_R(inst.sbx); + uint32_t idx = 0; + if (idxv.type_ == static_cast(VarType::Int) && idxv.data_.i >= 0) { + idx = static_cast(idxv.data_.i); + } + CVar k, v; + if (!interp_rt::TableEntry(VM_R(inst.a), idx, k, v)) { + k = interp_rt::Nil(); + v = interp_rt::Nil(); } - VM_W(inst.a, interp_rt::MakeMulti(s, vals.data(), static_cast(n))); + VM_W(inst.b, k); + VM_W(inst.c, v); + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(COMBINE) { - { - const int nprefix = inst.b; - std::vector prefix(static_cast(nprefix)); - for (int i = 0; i < nprefix; ++i) { - prefix[static_cast(i)] = VM_R(inst.a + i); + VM_CASE(TABCOUNT) { + VM_W(inst.a, interp_rt::Int(static_cast(interp_rt::TableEntryCount(VM_R(inst.b))))); + VM_DISPATCH_NEXT(); + } + VM_CASE(UNBOX) { + VM_W(inst.a, interp_rt::UnboxMulti(VM_R(inst.b), inst.c)); + VM_DISPATCH_NEXT(); + } + VM_CASE(MAKEMULTI) { + { + const int n = inst.b; + std::vector vals(static_cast(n)); + for (int i = 0; i < n; ++i) { + vals[static_cast(i)] = VM_R(inst.a + i); + } + VM_W(inst.a, interp_rt::MakeMulti(s, vals.data(), static_cast(n))); } - VM_W(inst.a, interp_rt::CombineMulti(s, prefix.data(), static_cast(nprefix), VM_R(inst.a + nprefix))); + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(THROW) { - const CVar msg = proto->constants[static_cast(inst.sbx)]; - ThrowFakeluaException(std::string(ConstToName(msg))); - } - VM_CASE(FORPREP) { - const int base = inst.a; - CVar ctrl = VM_R(base); - CVar limit = VM_R(base + 1); - CVar step = VM_R(base + 2); - CVar loopv{}; - if (interp_rt::ForPrep(ctrl, limit, step, loopv)) { - stk[base + 3] = loopv; - interp_rt::VmClearBox(boxp, base + 3); - ip += inst.sbx; - } else { - VM_W(base, ctrl); - VM_W(base + 1, limit); - VM_W(base + 2, step); - stk[base + 3] = loopv; - interp_rt::VmClearBox(boxp, base + 3); + VM_CASE(COMBINE) { + { + const int nprefix = inst.b; + std::vector prefix(static_cast(nprefix)); + for (int i = 0; i < nprefix; ++i) { + prefix[static_cast(i)] = VM_R(inst.a + i); + } + VM_W(inst.a, interp_rt::CombineMulti(s, prefix.data(), static_cast(nprefix), VM_R(inst.a + nprefix))); + } + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } - VM_CASE(FORLOOP) { - const int base = inst.a; - const CVar &step = VM_R(base + 2); - if (FL_VM_LIKELY(step.type_ == interp_rt::kInt)) { - const uint64_t count = static_cast(VM_R(base + 1).data_.i); - if (count > 0) { - interp_rt::VmWriteInt(stk, boxp, base + 1, static_cast(count - 1)); - const int64_t newidx = static_cast(static_cast(VM_R(base).data_.i) + static_cast(step.data_.i)); - interp_rt::VmWriteInt(stk, boxp, base, newidx); - stk[base + 3] = stk[base]; + VM_CASE(THROW) { + const CVar msg = proto->constants[static_cast(inst.sbx)]; + ThrowFakeluaException(std::string(ConstToName(msg))); + } + VM_CASE(FORPREP) { + const int base = inst.a; + CVar ctrl = VM_R(base); + CVar limit = VM_R(base + 1); + CVar step = VM_R(base + 2); + CVar loopv{}; + if (interp_rt::ForPrep(ctrl, limit, step, loopv)) { + stk[base + 3] = loopv; interp_rt::VmClearBox(boxp, base + 3); ip += inst.sbx; + } else { + VM_W(base, ctrl); + VM_W(base + 1, limit); + VM_W(base + 2, step); + stk[base + 3] = loopv; + interp_rt::VmClearBox(boxp, base + 3); } VM_DISPATCH_NEXT(); } - CVar ctrl = VM_R(base); - const CVar &limit = VM_R(base + 1); - VM_W(base, interp_rt::BinAdd(ctrl, step)); - if (interp_rt::ForLoopInRange(VM_R(base), limit, step)) { - stk[base + 3] = VM_R(base); - interp_rt::VmClearBox(boxp, base + 3); - ip += inst.sbx; + VM_CASE(FORLOOP) { + const int base = inst.a; + const CVar &step = VM_R(base + 2); + if (FL_VM_LIKELY(step.type_ == interp_rt::kInt)) { + const uint64_t count = static_cast(VM_R(base + 1).data_.i); + if (count > 0) { + interp_rt::VmWriteInt(stk, boxp, base + 1, static_cast(count - 1)); + const int64_t newidx = static_cast(static_cast(VM_R(base).data_.i) + static_cast(step.data_.i)); + interp_rt::VmWriteInt(stk, boxp, base, newidx); + stk[base + 3] = stk[base]; + interp_rt::VmClearBox(boxp, base + 3); + ip += inst.sbx; + } + VM_DISPATCH_NEXT(); + } + CVar ctrl = VM_R(base); + const CVar &limit = VM_R(base + 1); + VM_W(base, interp_rt::BinAdd(ctrl, step)); + if (interp_rt::ForLoopInRange(VM_R(base), limit, step)) { + stk[base + 3] = VM_R(base); + interp_rt::VmClearBox(boxp, base + 3); + ip += inst.sbx; + } + VM_DISPATCH_NEXT(); } - VM_DISPATCH_NEXT(); - } #if !defined(__GNUC__) - default: - ThrowFakeluaException("interpreter: unknown opcode"); + default: + ThrowFakeluaException("interpreter: unknown opcode"); } #endif +} - } catch (FakeluaException &e) { - AnnotateInterpLocation(proto, ip, e); - throw; - } +catch (FakeluaException &e) { + AnnotateInterpLocation(proto, ip, e); + throw; +} #undef VM_DISPATCH_NEXT #undef VM_CASE diff --git a/src/interp/opcode.h b/src/interp/opcode.h index 0d53c5e4..d3bb66b1 100644 --- a/src/interp/opcode.h +++ b/src/interp/opcode.h @@ -14,55 +14,55 @@ inline constexpr int kMaxFunctionRegisters = kMaxFunctionRegisterIndex + 1; // 寄存器字节码指令。布局参考 fakescript 的三地址形式,操作数以寄存器槽为主。 // X-macro 同时驱动 enum 与解释器 computed-goto 跳转表,新增 opcode 必须加在这里。 -#define FAKELUA_FOR_EACH_OP(X) \ - X(MOVE) /* R(A) = R(B) */ \ - X(LOADK) /* R(A) = K(Bx) */ \ - X(LOADNIL) /* R(A) = nil */ \ - X(LOADBOOL) /* R(A) = (B != 0) */ \ - X(GETGLOBAL) /* R(A) = G[Kstr(Bx)] */ \ - X(SETGLOBAL) /* G[Kstr(Bx)] = R(A) */ \ - X(GETUPVAL) /* R(A) = UV(B) */ \ - X(SETUPVAL) /* UV(B) = R(A) */ \ - X(GETTABLE) /* R(A) = R(B)[R(C)] */ \ - X(SETTABLE) /* R(A)[R(B)] = R(C) */ \ - X(NEWTABLE) /* R(A) = {} */ \ - X(NEWBOX) /* boxes[A] = alloc CVar*; *boxes[A] = R(A) */ \ - X(ADD) \ - X(SUB) \ - X(MUL) \ - X(DIV) \ - X(IDIV) \ - X(MOD) \ - X(POW) \ - X(BAND) \ - X(BXOR) \ - X(BOR) \ - X(SHL) \ - X(SHR) \ - X(CONCAT) /* R(A) = R(B) .. R(C) */ \ - X(UNM) /* R(A) = -R(B) */ \ - X(NOT) /* R(A) = not R(B) */ \ - X(LEN) /* R(A) = #R(B) */ \ - X(BNOT) /* R(A) = ~R(B) */ \ - X(EQ) /* R(A) = (R(B) == R(C)) */ \ - X(LT) \ - X(LE) \ - X(TESTJMP) /* if IsTrue(R(A)) == (B!=0) then pc += sbx */ \ - X(JMP) /* pc += sbx */ \ - X(CALL) /* R(A) = call R(A) with B args at R(A+1)..; C=0 keep multi, C=1 unbox */ \ - X(CALLNAME) /* R(A) = CallByName(Kstr(Bx), B args at R(A)..); C as CALL */ \ - X(TAIL) /* tail-call R(A) with B args at R(A+1)..; C=kTailSelfExact: reuse this proto/cl */ \ - X(TAILNAME) /* tail CallByName(Kstr(Bx), B args at R(A)..); C=kTailSelfExact: no name lookup */ \ - X(RETURN) /* return R(A); B=0 keep multi, B=1 single */ \ - X(CLOSURE) /* R(A) = closure(proto Bx) */ \ - X(SETLIST) /* expand multi R(C) into table R(A) starting at int sbx */ \ - X(TABENT) /* R(B), R(C) = table_entry(R(A), R(sbx)) */ \ - X(TABCOUNT) /* R(A) = Int(table_entry_count(R(B))) */ \ - X(UNBOX) /* R(A) = UnboxMulti(R(B), C) */ \ - X(MAKEMULTI) /* R(A) = MakeMulti(R(A) .. R(A+B-1)) */ \ - X(COMBINE) /* R(A) = CombineMulti(prefix R(A)..R(A+B-1), last R(A+B)) */ \ - X(THROW) /* throw string constant K(sbx) */ \ - X(FORPREP) /* int: R(A+1)=count; float: keep limit; skip if empty (pc+=sbx); R(A+3)=R(A) */ \ +#define FAKELUA_FOR_EACH_OP(X) \ + X(MOVE) /* R(A) = R(B) */ \ + X(LOADK) /* R(A) = K(Bx) */ \ + X(LOADNIL) /* R(A) = nil */ \ + X(LOADBOOL) /* R(A) = (B != 0) */ \ + X(GETGLOBAL) /* R(A) = G[Kstr(Bx)] */ \ + X(SETGLOBAL) /* G[Kstr(Bx)] = R(A) */ \ + X(GETUPVAL) /* R(A) = UV(B) */ \ + X(SETUPVAL) /* UV(B) = R(A) */ \ + X(GETTABLE) /* R(A) = R(B)[R(C)] */ \ + X(SETTABLE) /* R(A)[R(B)] = R(C) */ \ + X(NEWTABLE) /* R(A) = {} */ \ + X(NEWBOX) /* boxes[A] = alloc CVar*; *boxes[A] = R(A) */ \ + X(ADD) \ + X(SUB) \ + X(MUL) \ + X(DIV) \ + X(IDIV) \ + X(MOD) \ + X(POW) \ + X(BAND) \ + X(BXOR) \ + X(BOR) \ + X(SHL) \ + X(SHR) \ + X(CONCAT) /* R(A) = R(B) .. R(C) */ \ + X(UNM) /* R(A) = -R(B) */ \ + X(NOT) /* R(A) = not R(B) */ \ + X(LEN) /* R(A) = #R(B) */ \ + X(BNOT) /* R(A) = ~R(B) */ \ + X(EQ) /* R(A) = (R(B) == R(C)) */ \ + X(LT) \ + X(LE) \ + X(TESTJMP) /* if IsTrue(R(A)) == (B!=0) then pc += sbx */ \ + X(JMP) /* pc += sbx */ \ + X(CALL) /* R(A) = call R(A) with B args at R(A+1)..; C=0 keep multi, C=1 unbox */ \ + X(CALLNAME) /* R(A) = CallByName(Kstr(Bx), B args at R(A)..); C as CALL */ \ + X(TAIL) /* tail-call R(A) with B args at R(A+1)..; C=kTailSelfExact: reuse this proto/cl */ \ + X(TAILNAME) /* tail CallByName(Kstr(Bx), B args at R(A)..); C=kTailSelfExact: no name lookup */ \ + X(RETURN) /* return R(A); B=0 keep multi, B=1 single */ \ + X(CLOSURE) /* R(A) = closure(proto Bx) */ \ + X(SETLIST) /* expand multi R(C) into table R(A) starting at int sbx */ \ + X(TABENT) /* R(B), R(C) = table_entry(R(A), R(sbx)) */ \ + X(TABCOUNT) /* R(A) = Int(table_entry_count(R(B))) */ \ + X(UNBOX) /* R(A) = UnboxMulti(R(B), C) */ \ + X(MAKEMULTI) /* R(A) = MakeMulti(R(A) .. R(A+B-1)) */ \ + X(COMBINE) /* R(A) = CombineMulti(prefix R(A)..R(A+B-1), last R(A+B)) */ \ + X(THROW) /* throw string constant K(sbx) */ \ + X(FORPREP) /* int: R(A+1)=count; float: keep limit; skip if empty (pc+=sbx); R(A+3)=R(A) */ \ X(FORLOOP) /* int: if --count>=0: R(A)+=step, R(A+3)=R(A), pc+=sbx; float: add+compare */ // TAIL/TAILNAME 的 C 操作数:精确自尾调用(非变参、编译期已知实参个数、无末参展开)。 @@ -73,7 +73,7 @@ enum class Op : uint8_t { #define FAKELUA_OP_ENUM(name) name, FAKELUA_FOR_EACH_OP(FAKELUA_OP_ENUM) #undef FAKELUA_OP_ENUM - COUNT + COUNT }; struct Inst { @@ -83,10 +83,11 @@ struct Inst { uint8_t c = 0; int32_t sbx = 0; }; + static_assert(sizeof(Inst) == 8, "Inst should be 8 bytes for denser dispatch"); struct UpvalDesc { - bool in_stack = false; // true: enclosing function register; false: enclosing upvalue + bool in_stack = false;// true: enclosing function register; false: enclosing upvalue uint16_t idx = 0; }; diff --git a/src/interp/vm_ops.h b/src/interp/vm_ops.h index fdde1e11..7dd15e42 100644 --- a/src/interp/vm_ops.h +++ b/src/interp/vm_ops.h @@ -411,27 +411,25 @@ FL_VM_INLINE bool ForPrep(CVar &idx, CVar &limit, CVar &step, CVar &loopvar) { #define FL_VM_INT_OP_add(a, b) ((a) + (b)) #define FL_VM_INT_OP_sub(a, b) ((a) - (b)) #define FL_VM_INT_OP_mul(a, b) ((a) * (b)) -#define FL_VM_ARITH_INT(HAS, stk, box, ra, rb, rc, OPNAME, SLOWFN) \ - do { \ - const CVar &fl_rb_ = ::fakelua::interp_rt::VmSlot(stk, box, rb); \ - const CVar &fl_rc_ = ::fakelua::interp_rt::VmSlot(stk, box, rc); \ - if (FL_VM_LIKELY(fl_rb_.type_ == ::fakelua::interp_rt::kInt && fl_rc_.type_ == ::fakelua::interp_rt::kInt)) { \ - ::fakelua::interp_rt::VmWriteInt(stk, box, ra, \ - static_cast(FL_VM_INT_OP_##OPNAME(static_cast(fl_rb_.data_.i), static_cast(fl_rc_.data_.i)))); \ - } else { \ - ::fakelua::interp_rt::VmWrite(stk, box, ra, (SLOWFN)(fl_rb_, fl_rc_)); \ - } \ +#define FL_VM_ARITH_INT(HAS, stk, box, ra, rb, rc, OPNAME, SLOWFN) \ + do { \ + const CVar &fl_rb_ = ::fakelua::interp_rt::VmSlot(stk, box, rb); \ + const CVar &fl_rc_ = ::fakelua::interp_rt::VmSlot(stk, box, rc); \ + if (FL_VM_LIKELY(fl_rb_.type_ == ::fakelua::interp_rt::kInt && fl_rc_.type_ == ::fakelua::interp_rt::kInt)) { \ + ::fakelua::interp_rt::VmWriteInt(stk, box, ra, static_cast(FL_VM_INT_OP_##OPNAME(static_cast(fl_rb_.data_.i), static_cast(fl_rc_.data_.i)))); \ + } else { \ + ::fakelua::interp_rt::VmWrite(stk, box, ra, (SLOWFN) (fl_rb_, fl_rc_)); \ + } \ } while (0) -#define FL_VM_BINOP(HAS, stk, box, ra, rb, rc, FN) \ - do { \ - ::fakelua::interp_rt::VmWrite(stk, box, ra, (FN)(::fakelua::interp_rt::VmSlot(stk, box, rb), \ - ::fakelua::interp_rt::VmSlot(stk, box, rc))); \ +#define FL_VM_BINOP(HAS, stk, box, ra, rb, rc, FN) \ + do { \ + ::fakelua::interp_rt::VmWrite(stk, box, ra, (FN) (::fakelua::interp_rt::VmSlot(stk, box, rb), ::fakelua::interp_rt::VmSlot(stk, box, rc))); \ } while (0) -#define FL_VM_UNOP(HAS, stk, box, ra, rb, FN) \ - do { \ - ::fakelua::interp_rt::VmWrite(stk, box, ra, (FN)(::fakelua::interp_rt::VmSlot(stk, box, rb))); \ +#define FL_VM_UNOP(HAS, stk, box, ra, rb, FN) \ + do { \ + ::fakelua::interp_rt::VmWrite(stk, box, ra, (FN) (::fakelua::interp_rt::VmSlot(stk, box, rb))); \ } while (0) }// namespace fakelua::interp_rt diff --git a/src/jit/code_binding.h b/src/jit/code_binding.h new file mode 100644 index 00000000..730e5c1c --- /dev/null +++ b/src/jit/code_binding.h @@ -0,0 +1,18 @@ +#pragma once +#include "state/const_string.h" +#include "state/state.h" +#include + +namespace fakelua { +inline LuaLoc *BindGeneratedCode(void *symbol, State *s, const std::string &filename) { + if (!symbol) ThrowFakeluaException("JIT image has no binding entry point"); + using Intern = int64_t (*)(void *, const char *, size_t); + using Bind = void *(*) (void *, void *, void *, const char *, Intern); + // Host-owned bytes survive unloading/recompilation of another image. + auto *file = static_cast(s->GetHeap().GetAllocator(true).Alloc(filename.size() + 1)); + std::memcpy(file, filename.c_str(), filename.size() + 1); + return static_cast(reinterpret_cast(symbol)( + s, &s->GetTempHeapSlot(), &s->GetConstHeapSlot(), file, + +[](void *state, const char *data, size_t size) -> int64_t { return static_cast(state)->GetConstString().Alloc(std::string_view(data, size)); })); +} +}// namespace fakelua diff --git a/src/jit/gcc_cache.cpp b/src/jit/gcc_cache.cpp new file mode 100644 index 00000000..b677d60b --- /dev/null +++ b/src/jit/gcc_cache.cpp @@ -0,0 +1,697 @@ +#include "jit/gcc_cache.h" +#include "jit_build_identity.h" +#include "util/file_util.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined(_WIN32) +#define NOMINMAX +#include +#else +#include +#include +#include +#include +#include +#endif + +namespace fakelua { +namespace { +namespace fs = std::filesystem; + +#if defined(_WIN32) +constexpr DWORD kShareAll = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; + +class WinHandle { +public: + HANDLE h = INVALID_HANDLE_VALUE; + WinHandle() = default; + explicit WinHandle(HANDLE handle) : h(handle) {} + ~WinHandle() { reset(); } + WinHandle(const WinHandle &) = delete; + WinHandle &operator=(const WinHandle &) = delete; + WinHandle(WinHandle &&other) noexcept : h(other.h) { other.h = INVALID_HANDLE_VALUE; } + void reset() { + if (h != INVALID_HANDLE_VALUE) CloseHandle(h); + h = INVALID_HANDLE_VALUE; + } + explicit operator bool() const { return h != INVALID_HANDLE_VALUE; } +}; + +// One-pass POSIX unlink: never retries, and does not block on a share-delete open handle. +bool UnlinkPath(const fs::path &p) { + WinHandle handle(CreateFileW(p.wstring().c_str(), DELETE, kShareAll, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr)); + if (!handle) { + const auto err = GetLastError(); + return err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND; + } + struct DispositionEx { + DWORD Flags; + } posix{0x00000001 | 0x00000002};// FILE_DISPOSITION_FLAG_DELETE | POSIX_SEMANTICS + if (SetFileInformationByHandle(handle.h, static_cast(21), &posix, sizeof(posix))) return true; + FILE_DISPOSITION_INFO info{}; + info.DeleteFile = TRUE; + return SetFileInformationByHandle(handle.h, FileDispositionInfo, &info, sizeof(info)); +} + +void RemoveTree(const fs::path &p, std::error_code &ec) { + WIN32_FILE_ATTRIBUTE_DATA attrs{}; + if (!GetFileAttributesExW(p.wstring().c_str(), GetFileExInfoStandard, &attrs)) { + const auto err = GetLastError(); + if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) ec.clear(); + else + ec.assign(err, std::system_category()); + return; + } + if (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + WIN32_FIND_DATAW data{}; + HANDLE find = FindFirstFileW((p / "*").wstring().c_str(), &data); + if (find != INVALID_HANDLE_VALUE) { + do { + if (data.cFileName[0] == L'.' && (data.cFileName[1] == L'\0' || (data.cFileName[1] == L'.' && data.cFileName[2] == L'\0'))) continue; + std::error_code child; + RemoveTree(p / data.cFileName, child); + } while (FindNextFileW(find, &data)); + FindClose(find); + } + } + if (UnlinkPath(p)) { + ec.clear(); + return; + } + const auto err = GetLastError(); + if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) ec.clear(); + else + ec.assign(err, std::system_category()); +} + +std::string ReadBytes(const fs::path &p) { + WinHandle handle(CreateFileW(p.wstring().c_str(), GENERIC_READ, kShareAll, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + if (!handle) throw std::runtime_error("cache read"); + LARGE_INTEGER size{}; + if (!GetFileSizeEx(handle.h, &size) || size.QuadPart < 0) throw std::runtime_error("cache size"); + std::string data(static_cast(size.QuadPart), '\0'); + size_t offset = 0; + while (offset < data.size()) { + DWORD chunk = 0; + const DWORD want = static_cast(std::min(data.size() - offset, size_t(1 << 20))); + if (!ReadFile(handle.h, data.data() + offset, want, &chunk, nullptr) || !chunk) throw std::runtime_error("cache read"); + offset += chunk; + } + return data; +} + +void WriteBytes(const fs::path &p, const std::string &data) { + WinHandle handle(CreateFileW(p.wstring().c_str(), GENERIC_WRITE, kShareAll, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr)); + if (!handle) throw std::runtime_error("cache write"); + size_t offset = 0; + while (offset < data.size()) { + DWORD chunk = 0; + const DWORD want = static_cast(std::min(data.size() - offset, size_t(1 << 20))); + if (!WriteFile(handle.h, data.data() + offset, want, &chunk, nullptr) || !chunk) throw std::runtime_error("cache write"); + offset += chunk; + } + if (!FlushFileBuffers(handle.h)) throw std::runtime_error("cache write"); +} +#else +std::string ReadBytes(const fs::path &p) { + std::ifstream in(p, std::ios::binary | std::ios::ate); + if (!in) throw std::runtime_error("cache read"); + const auto size = in.tellg(); + if (size < 0) throw std::runtime_error("cache size"); + std::string data(static_cast(size), '\0'); + in.seekg(0); + if (!data.empty() && !in.read(data.data(), static_cast(data.size()))) throw std::runtime_error("cache read"); + return data; +} + +void WriteBytes(const fs::path &p, const std::string &data) { + std::ofstream out(p, std::ios::binary | std::ios::trunc); + out.write(data.data(), static_cast(data.size())); + out.close(); + if (!out) throw std::runtime_error("cache write"); +} + +void RemoveTree(const fs::path &p, std::error_code &ec) { fs::remove_all(p, ec); } +#endif + +std::string Digest(const std::string &data) { + std::array hash{}; + if (!SHA256(reinterpret_cast(data.data()), data.size(), hash.data())) throw std::runtime_error("cache digest failed"); + std::ostringstream out; + for (auto c: hash) out << std::hex << std::setw(2) << std::setfill('0') << unsigned(c); + return out.str(); +} + +// libc++ file_clock ticks may use __int128, which has no ostream operator. +template +void AppendInteger(std::ostream &out, T value) { + char buf[80]; + char *p = buf + sizeof(buf); + *--p = '\0'; + const bool negative = value < T{0}; + do { + T digit = value % 10; + if (digit < T{0}) digit = -digit; + *--p = static_cast('0' + static_cast(digit)); + value /= 10; + } while (value != T{0}); + if (negative) *--p = '-'; + out << p; +} + +std::string FileStamp(const fs::path &path) { + std::ostringstream stamp; +#if defined(_WIN32) + // MinGW last_write_time often comes from stat() and is only 1-second + // resolution, so a same-size rewrite in the same second would reuse a + // stale FileDigest. Use the 100ns FILETIME plus volume/file identity. + WIN32_FILE_ATTRIBUTE_DATA info{}; + if (!GetFileAttributesExW(path.wstring().c_str(), GetFileExInfoStandard, &info)) throw std::runtime_error("cache dependency stat"); + const auto size = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 0ull : ((static_cast(info.nFileSizeHigh) << 32) | info.nFileSizeLow); + stamp << size << ':'; + const auto filetime = [](const FILETIME &t) { return (static_cast(t.dwHighDateTime) << 32) | t.dwLowDateTime; }; + AppendInteger(stamp, filetime(info.ftLastWriteTime)); + stamp << ':'; + AppendInteger(stamp, filetime(info.ftCreationTime)); + BY_HANDLE_FILE_INFORMATION ident{}; + HANDLE handle = CreateFileW(path.wstring().c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (handle != INVALID_HANDLE_VALUE) { + if (GetFileInformationByHandle(handle, &ident)) { + stamp << ':' << ident.dwVolumeSerialNumber << ':' << ident.nFileIndexHigh << ':' << ident.nFileIndexLow; + } + CloseHandle(handle); + } +#else + stamp << (fs::is_directory(path) ? 0 : fs::file_size(path)) << ':'; + AppendInteger(stamp, fs::last_write_time(path).time_since_epoch().count()); + + struct stat st{}; + + if (stat(path.c_str(), &st)) throw std::runtime_error("cache dependency stat"); + stamp << ':' << st.st_dev << ':' << st.st_ino << ':' << st.st_ctime; +#if defined(__APPLE__) + stamp << ':' << st.st_ctimespec.tv_nsec; +#else + stamp << ':' << st.st_ctim.tv_nsec; +#endif +#endif + return stamp.str(); +} + +std::string FileDigest(const fs::path &path) { + struct Entry { + std::string stamp, digest; + }; + + static std::mutex mutex; + static std::map known; + std::lock_guard lock(mutex); + const auto stamp = FileStamp(path); + auto &entry = known[path.string()]; + if (entry.stamp != stamp) { + auto digest = Digest(ReadBytes(path)); + if (FileStamp(path) != stamp) throw std::runtime_error("cache changing dependency"); + entry = {stamp, std::move(digest)}; + } + return entry.digest; +} + +fs::path CompilerPath(const std::string &compiler) { + if (fs::path(compiler).has_parent_path()) return fs::absolute(compiler); + const char *env = std::getenv("PATH"); + std::istringstream paths(env ? env : ""); + std::string part; +#if defined(_WIN32) + constexpr char sep = ';'; +#else + constexpr char sep = ':'; +#endif + while (std::getline(paths, part, sep)) { + fs::path p = fs::path(part) / compiler; +#if defined(_WIN32) + if (!p.has_extension()) p += ".exe"; +#endif + if (fs::is_regular_file(p)) return fs::absolute(p); + } + throw std::runtime_error("cache compiler not found"); +} + +// Query only compiler metadata, without a shell or process-global stdio changes. +std::string QueryCompiler(const fs::path &compiler, const char *option) { + std::string output; + std::array buf{}; +#if defined(_WIN32) + SECURITY_ATTRIBUTES sa{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE}; + HANDLE read_pipe = nullptr, write_pipe = nullptr; + if (!CreatePipe(&read_pipe, &write_pipe, &sa, 0)) throw std::runtime_error("cache pipe"); + SetHandleInformation(read_pipe, HANDLE_FLAG_INHERIT, 0); + STARTUPINFOA startup{}; + startup.cb = sizeof(startup); + startup.dwFlags = STARTF_USESTDHANDLES; + startup.hStdOutput = write_pipe; + startup.hStdError = write_pipe; + startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + PROCESS_INFORMATION process{}; + std::string command = "\"" + compiler.string() + "\" " + option; + BOOL ok = CreateProcessA(compiler.string().c_str(), command.data(), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &startup, &process); + CloseHandle(write_pipe); + if (!ok) { + CloseHandle(read_pipe); + throw std::runtime_error("cache compiler query"); + } + DWORD size = 0; + while (ReadFile(read_pipe, buf.data(), static_cast(buf.size()), &size, nullptr) && size) output.append(buf.data(), size); + CloseHandle(read_pipe); + WaitForSingleObject(process.hProcess, INFINITE); + DWORD status = 1; + GetExitCodeProcess(process.hProcess, &status); + CloseHandle(process.hThread); + CloseHandle(process.hProcess); + if (status) throw std::runtime_error("cache compiler query"); +#else + int pipes[2]; + if (pipe(pipes)) throw std::runtime_error("cache pipe"); + fcntl(pipes[0], F_SETFD, FD_CLOEXEC); + fcntl(pipes[1], F_SETFD, FD_CLOEXEC); + auto exe = compiler.string(); + pid_t child = fork(); + if (child == 0) { + dup2(pipes[1], STDOUT_FILENO); + // Metadata switches produce tiny output, never generated code. + close(pipes[0]); + close(pipes[1]); + execl(exe.c_str(), exe.c_str(), option, static_cast(nullptr)); + _exit(127); + } + close(pipes[1]); + if (child < 0) { + close(pipes[0]); + throw std::runtime_error("cache fork"); + } + ssize_t size; + while ((size = read(pipes[0], buf.data(), buf.size())) != 0) { + if (size < 0) { + if (errno == EINTR) continue; + break; + } + output.append(buf.data(), static_cast(size)); + } + close(pipes[0]); + int status = 0; + pid_t waited; + do { + waited = waitpid(child, &status, 0); + } while (waited < 0 && errno == EINTR); + if (waited < 0 || !WIFEXITED(status) || WEXITSTATUS(status)) throw std::runtime_error("cache compiler query"); +#endif + while (!output.empty() && std::isspace(static_cast(output.back()))) output.pop_back(); + return output; +} + +std::vector ToolchainFiles(const fs::path &compiler) { + std::vector files{compiler}; + for (const char *option: {"-print-prog-name=cc1", "-print-prog-name=as", "-print-prog-name=ld", "-print-file-name=specs", "-print-libgcc-file-name", "-print-file-name=crtbeginS.o", + "-print-file-name=crtendS.o", "-print-file-name=crti.o", "-print-file-name=crtn.o"}) { + auto result = QueryCompiler(compiler, option); + if (result.empty()) throw std::runtime_error("cache compiler identity"); + if (fs::is_regular_file(result)) files.push_back(fs::absolute(result)); + else if (std::string_view(option).starts_with("-print-prog-name=") && !fs::path(result).has_parent_path() && result != "cc1") + files.push_back(CompilerPath(result)); + } + return files; +} + +std::string HostIdentity() { + // Avoid hashing a potentially huge debug-enabled host DLL on every process + // startup. CMake hashes source contents and the ABI/build configuration. + return FAKELUA_JIT_BUILD_ID ":" FAKELUA_JIT_BUILD_CONFIGURATION; +} + +std::shared_ptr KeyMutex(const std::string &key) { + static std::mutex guard; + static std::map> locks; + std::lock_guard lock(guard); + // Drop unused keys: long-lived services may compile arbitrarily many files. + for (auto it = locks.begin(); it != locks.end();) { + if (it->second.expired()) it = locks.erase(it); + else + ++it; + } + auto &weak = locks[key]; + auto ret = weak.lock(); + if (!ret) weak = ret = std::make_shared(); + return ret; +} + +// Fixed lock stripes bound synchronization metadata independently of entry count. +// Lock files are never unlinked by eviction; key -> index is the only lock order. +class CacheFileLock { + fs::path path_; + std::shared_ptr mutex_; + std::unique_lock thread_lock_; +#if defined(_WIN32) + HANDLE handle_ = INVALID_HANDLE_VALUE; +#else + int fd_ = -1; +#endif +public: + explicit CacheFileLock(const fs::path &path) : path_(path), mutex_(KeyMutex(path.string())), thread_lock_(*mutex_) { +#if defined(_WIN32) + handle_ = CreateFileW(path.wstring().c_str(), GENERIC_READ | GENERIC_WRITE, kShareAll, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (handle_ == INVALID_HANDLE_VALUE) throw std::runtime_error("cache lock open"); + OVERLAPPED overlap{}; + if (!LockFileEx(handle_, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlap)) { + CloseHandle(handle_); + handle_ = INVALID_HANDLE_VALUE; + throw std::runtime_error("cache lock"); + } +#else + fd_ = open(path.c_str(), O_RDWR | O_CREAT | O_CLOEXEC | O_NOFOLLOW, 0600); + if (fd_ < 0) throw std::runtime_error("cache lock open"); + int status; + do { + status = flock(fd_, LOCK_EX); + } while (status < 0 && errno == EINTR); + if (status < 0) { + close(fd_); + fd_ = -1; + throw std::runtime_error("cache lock"); + } +#endif + } + + ~CacheFileLock() { +#if defined(_WIN32) + if (handle_ != INVALID_HANDLE_VALUE) CloseHandle(handle_); +#else + if (fd_ >= 0) close(fd_); +#endif + } + + bool StillLinked() const { +#if defined(_WIN32) + // Share-delete opens allow an external cleaner to unlink the lock name. + if (handle_ == INVALID_HANDLE_VALUE) return false; + FILE_STANDARD_INFO standard{}; + if (GetFileInformationByHandleEx(handle_, FileStandardInfo, &standard, sizeof(standard)) && (standard.DeletePending || standard.NumberOfLinks == 0)) return false; + BY_HANDLE_FILE_INFORMATION held{}; + if (!GetFileInformationByHandle(handle_, &held)) return false; + WinHandle named(CreateFileW(path_.wstring().c_str(), 0, kShareAll, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); + if (!named) return false; + BY_HANDLE_FILE_INFORMATION info{}; + return GetFileInformationByHandle(named.h, &info) && held.dwVolumeSerialNumber == info.dwVolumeSerialNumber && held.nFileIndexHigh == info.nFileIndexHigh && held.nFileIndexLow == info.nFileIndexLow; +#else + struct stat held{}, named{}; + + return fstat(fd_, &held) == 0 && lstat(path_.c_str(), &named) == 0 && held.st_dev == named.st_dev && held.st_ino == named.st_ino; +#endif + } +}; + +bool IsCacheKey(const std::string &name) { + return name.size() == 64 && name.find_first_not_of("0123456789abcdef") == std::string::npos; +} + +struct CacheEntry { + fs::path path; + uint64_t order = 0; +}; + +std::vector CompleteEntries(const fs::path &root) { + std::vector result; + for (const auto &item: fs::directory_iterator(root)) { + if (!IsCacheKey(item.path().filename().string()) || !fs::is_directory(item.symlink_status())) continue; + if (!fs::is_regular_file(fs::symlink_status(item.path() / "image")) || !fs::is_regular_file(fs::symlink_status(item.path() / "manifest"))) continue; + uint64_t order = 0; + std::ifstream(item.path() / "published") >> order;// pre-FIFO entries are oldest + result.push_back({item.path(), order}); + } + std::sort(result.begin(), result.end(), [](const auto &a, const auto &b) { return a.order != b.order ? a.order < b.order : a.path < b.path; }); + return result; +} + +void TrimEntries(const fs::path &root, size_t limit) { + if (!limit) return;// zero explicitly means unlimited + auto entries = CompleteEntries(root); + auto count = entries.size(); + for (const auto &entry: entries) { + if (count <= limit) break; + std::error_code ec; + RemoveTree(entry.path, ec); + if (!ec) --count;// permission/cleanup races are best effort, never Lua errors + } +} + +fs::path CacheRoot(const StateGCCConfig &cfg) { + fs::path root = cfg.cache_directory; + if (root.empty()) { + root = fs::path(GetTmpDirectory()) / "cache"; +#if !defined(_WIN32) + // System temporary directories may be shared by several accounts. + root /= "uid-" + std::to_string(getuid()); +#endif + } + root = fs::absolute(root); +#if !defined(_WIN32) + // Never execute artifacts in a shared/writable-by-others cache. mkdir with + // 0700 from the outset, and reject symlinks instead of chmod'ing user paths. + fs::create_directories(root.parent_path()); + // A private leaf is insufficient if another account can replace it through + // a writable/foreign-owned ancestor. Resolve platform symlinks (/var on + // macOS), then trust only our account/root and sticky shared temp parents. + for (auto parent = fs::canonical(root.parent_path());; parent = parent.parent_path()) { + struct stat ancestor{}; + + if (stat(parent.c_str(), &ancestor) || (ancestor.st_uid != getuid() && ancestor.st_uid != 0) || ((ancestor.st_mode & 0022) && !(ancestor.st_mode & S_ISVTX))) + throw std::runtime_error("cache parent directory is not trusted"); + if (parent == parent.root_path()) break; + } + if (::mkdir(root.c_str(), 0700) != 0 && errno != EEXIST) throw std::runtime_error("cache mkdir"); + + struct stat st{}; + + if (lstat(root.c_str(), &st) || !S_ISDIR(st.st_mode) || st.st_uid != getuid() || (st.st_mode & 0077)) throw std::runtime_error("cache directory must be private"); +#else + fs::create_directories(root); +#endif + return root; +} + +// GCC makefile dependency syntax: escaped spaces/backslashes and continuations. +std::vector Dependencies(const std::string &text) { + std::vector result; + // -MT flua has no drive-letter colon, even on Windows. + auto colon = text.find(':'); + if (colon == std::string::npos) throw std::runtime_error("cache dependencies"); + std::string token; + for (size_t i = colon + 1; i <= text.size(); ++i) { + char c = i == text.size() ? ' ' : text[i]; + if (c == '\\' && i + 1 < text.size()) { + char next = text[i + 1]; + if (next == '\n') { + ++i; + continue; + } + if (next == '\r' && i + 2 < text.size() && text[i + 2] == '\n') { + i += 2; + continue; + } + if (next == ' ' || next == '\t' || next == '\\' || next == '#' || next == ':') { + token += next; + ++i; + continue; + } + } + if (c == '$' && i + 1 < text.size() && text[i + 1] == '$') ++i; + if (std::isspace(static_cast(c))) { + if (!token.empty()) { + result.emplace_back(token); + token.clear(); + } + } else + token += c; + } + if (result.empty()) throw std::runtime_error("cache empty dependencies"); + return result; +} +}// namespace + +struct GccCache::Impl { + fs::path entry; + fs::path compiler; + size_t max_entries = 256; + std::unique_ptr key_lock; + + CacheFileLock IndexLock() const { + if (!key_lock->StillLinked()) throw std::runtime_error("cache lock removed"); + return CacheFileLock(entry.parent_path() / "index.lock"); + } +}; + +GccCache::~GccCache() = default; + +void GccCache::Release() { + impl_.reset(); +} + +GccCache::GccCache(const StateGCCConfig &cfg, const std::string &compiler, const std::string &code, bool debug) { + // Arbitrary user headers/libraries (and coverage files) cannot be reliably + // fingerprinted as a closed toolchain. Retain the uncached behavior there. +#if !defined(USE_COV) + if (!cfg.enable_cache || !cfg.include_paths.empty() || !cfg.library_paths.empty() || !cfg.libraries.empty()) return; + // Search-path overrides can add a newly shadowing header/library without + // changing any previously recorded dependency. Such open-ended toolchains + // deliberately retain the uncached path. + for (auto name: {"CPATH", "C_INCLUDE_PATH", "LIBRARY_PATH", "COMPILER_PATH", "GCC_EXEC_PREFIX", "CCC_OVERRIDE_OPTIONS", "CLANG_CONFIG_FILE_SYSTEM_DIR", "CLANG_CONFIG_FILE_USER_DIR"}) + if (auto value = std::getenv(name); value && *value) return; + try { + auto root = CacheRoot(cfg); + auto cc = CompilerPath(compiler); + const auto driver_name = fs::canonical(cc).filename().string(); + if (driver_name.find("gcc") == std::string::npos && driver_name.find("clang") == std::string::npos) return; + // Do not persist arbitrary wrapper scripts with hidden inputs. + { + std::ifstream driver(cc, std::ios::binary); + char magic[2]{}; + driver.read(magic, 2); + if (!driver || (magic[0] == '#' && magic[1] == '!')) return; + } + std::ostringstream key; + key << "fakelua-gcc-cache-v1\n" + << HostIdentity() << '\n' + << debug << '\n' + << sizeof(void *) << '\n' + << compiler << '\n' + << cc.string() << '\n' + << FileDigest(cc) + << '\n' + // Apple /usr/bin/clang can be a stable shim for a newly selected + // Xcode toolchain. Version/InstalledDir must be part of the key. + << QueryCompiler(cc, "--version") << '\n'; + // Environment affecting compiler search, preprocessing, linking, SDK and + // reproducibility. Each value is length-delimited to avoid ambiguity. + for (auto name: {"PATH", "CPATH", "C_INCLUDE_PATH", "LIBRARY_PATH", "COMPILER_PATH", "GCC_EXEC_PREFIX", "SDKROOT", "MACOSX_DEPLOYMENT_TARGET", "DEVELOPER_DIR", "SOURCE_DATE_EPOCH", "LANG", + "LC_ALL", "LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH"}) { + const char *value = std::getenv(name); + key << name << ':' << (value ? std::string(value).size() : 0) << ':' << (value ? value : "") << '\n'; + } + if (auto sdk = std::getenv("SDKROOT"); sdk && *sdk && fs::exists(sdk)) key << "SDKROOT-real:" << fs::canonical(sdk).string() << '\n'; + key << fs::current_path().string() << '\n' << code; + auto hash = Digest(key.str()); + auto p = std::make_unique(); + p->entry = root / hash; + p->compiler = cc; + p->max_entries = cfg.cache_max_entries; + p->key_lock = std::make_unique(root / ("key-" + hash.substr(0, 2) + ".lock")); + impl_ = std::move(p); + } catch (...) { /* Caching is an optimization, never a compile requirement. */ + } +#endif +} + +bool GccCache::Restore(const std::string &destination) { + if (!impl_) return false; + try { + auto index_lock = impl_->IndexLock(); + if (!index_lock.StillLinked()) return false; + TrimEntries(impl_->entry.parent_path(), impl_->max_entries); + std::istringstream meta(ReadBytes(impl_->entry / "manifest")); + std::string binary_hash, path, hash; + if (!(meta >> binary_hash) || binary_hash.size() != 64) throw std::runtime_error("cache manifest"); + size_t count = 0; + if (!(meta >> count) || count == 0 || count > 10000) throw std::runtime_error("cache dependency count"); + for (size_t i = 0; i < count; ++i) { + if (!(meta >> std::quoted(path) >> hash)) throw std::runtime_error("cache dependency"); + const auto actual = hash.starts_with("directory:") ? "directory:" + Digest(FileStamp(path)) : FileDigest(path); + if (actual != hash) throw std::runtime_error("cache stale dependency"); + } + std::string bytes = ReadBytes(impl_->entry / "image"); + if (Digest(bytes) != binary_hash) throw std::runtime_error("cache corrupt image"); + // A byte copy, not a hardlink: dlopen deduplicates mapped inodes and would + // otherwise share _S, init flags, Lua locations and module globals. + WriteBytes(destination, bytes); + return true; + } catch (...) { + Invalidate(); + return false; + } +} + +void GccCache::Invalidate() { + if (!impl_) return; + try { + auto index_lock = impl_->IndexLock(); + if (!index_lock.StillLinked()) return; + std::error_code ec; + RemoveTree(impl_->entry, ec); + } catch (...) { /* An externally removed cache is just a miss. */ + } +} + +void GccCache::Publish(const std::string &binary, const std::string &dependencies, const std::string &source) { + if (!impl_) return; + fs::path staging; + try { + auto bytes = ReadBytes(binary); + std::map deps; + auto src = fs::canonical(source); + // The source itself is already hashed as part of the cache key. + for (auto p: Dependencies(ReadBytes(dependencies))) { + p = fs::absolute(p).lexically_normal(); + if (!fs::equivalent(p, src)) deps.emplace(p.string(), FileDigest(p)); + } + for (const auto &p: ToolchainFiles(impl_->compiler)) { + deps[p.string()] = FileDigest(p); + // Also detect added/removed default specs/config/startup files. + const auto parent = p.parent_path(); + deps[parent.string()] = "directory:" + Digest(FileStamp(parent)); + } + if (deps.empty()) return; + std::ostringstream manifest; + manifest << Digest(bytes) << '\n' << deps.size() << '\n'; + for (const auto &[p, h]: deps) manifest << std::quoted(p) << ' ' << h << '\n'; + auto index_lock = impl_->IndexLock(); + if (!index_lock.StillLinked()) return; + const auto root = impl_->entry.parent_path(); + auto entries = CompleteEntries(root); + uint64_t order = entries.empty() ? 1 : entries.back().order + 1; + if (!order) { + // Recover a corrupt/overflowed age without poisoning future writes. + order = 1; + for (const auto &entry: entries) WriteBytes(entry.path / "published", std::to_string(order++)); + } + // Each writer owns its staging directory, even if an external cleaner + // unlinks a lock inode and another process acquires a replacement lock. + staging = impl_->entry.string() + ".tmp." + fs::path(GenerateTmpFilename("", "")).filename().string(); + std::error_code stale; + RemoveTree(impl_->entry.string() + ".tmp", stale);// old-format abandoned staging only + if (!fs::create_directory(staging)) return; + WriteBytes(staging / "image", bytes); + WriteBytes(staging / "manifest", manifest.str()); + WriteBytes(staging / "published", std::to_string(order)); + if (!index_lock.StillLinked() || !impl_->key_lock->StillLinked()) { + RemoveTree(staging, stale); + return; + } + RemoveTree(impl_->entry, stale); + fs::rename(staging, impl_->entry);// only complete, validated images become visible + TrimEntries(root, impl_->max_entries); + } catch (...) { + std::error_code ec; + if (!staging.empty()) RemoveTree(staging, ec); + } +} +}// namespace fakelua diff --git a/src/jit/gcc_cache.h b/src/jit/gcc_cache.h new file mode 100644 index 00000000..37273a2c --- /dev/null +++ b/src/jit/gcc_cache.h @@ -0,0 +1,24 @@ +#pragma once + +#include "fakelua.h" +#include +#include +#include + +namespace fakelua { +// A best-effort, private, content-addressed compiler artifact cache. Never load +// the cache inode itself: generated code contains State-local mutable statics. +class GccCache { +public: + GccCache(const StateGCCConfig &config, const std::string &compiler, const std::string &code, bool debug); + ~GccCache(); + bool Restore(const std::string &destination); + void Publish(const std::string &binary, const std::string &dependencies, const std::string &source); + void Invalidate(); + void Release(); + +private: + struct Impl; + std::unique_ptr impl_; +}; +}// namespace fakelua diff --git a/src/jit/gcc_handle.cpp b/src/jit/gcc_handle.cpp index 0f73b63b..2f19cdaa 100644 --- a/src/jit/gcc_handle.cpp +++ b/src/jit/gcc_handle.cpp @@ -46,6 +46,7 @@ GCCHandle::~GCCHandle() { if (!log_file_.empty()) { std::filesystem::remove(log_file_, ec); } + if (!workspace_.empty()) std::filesystem::remove_all(workspace_, ec); } }// namespace fakelua diff --git a/src/jit/gcc_handle.h b/src/jit/gcc_handle.h index 0a481c31..76274729 100644 --- a/src/jit/gcc_handle.h +++ b/src/jit/gcc_handle.h @@ -10,8 +10,8 @@ struct LuaLoc; class GCCHandle : public JITHandle { public: - GCCHandle(State *s, std::string c_file, std::string so_file, std::string log_file, void *dl_handle) - : s_(s), c_file_(std::move(c_file)), so_file_(std::move(so_file)), log_file_(std::move(log_file)), dl_handle_(dl_handle) { + GCCHandle(State *s, std::string c_file, std::string so_file, std::string log_file, void *dl_handle, std::string workspace = {}) + : s_(s), c_file_(std::move(c_file)), so_file_(std::move(so_file)), log_file_(std::move(log_file)), dl_handle_(dl_handle), workspace_(std::move(workspace)) { } ~GCCHandle() override; @@ -39,6 +39,7 @@ class GCCHandle : public JITHandle { std::string log_file_; void *dl_handle_ = nullptr; LuaLoc *lua_loc_ = nullptr; + std::string workspace_; }; typedef std::shared_ptr GCCHandlePtr; diff --git a/src/jit/gcc_jit.cpp b/src/jit/gcc_jit.cpp index 01caae42..5c8566b8 100644 --- a/src/jit/gcc_jit.cpp +++ b/src/jit/gcc_jit.cpp @@ -1,4 +1,6 @@ #include "jit/gcc_jit.h" +#include "jit/code_binding.h" +#include "jit/gcc_cache.h" #include "jit/gcc_handle.h" #include "state/state.h" #include "util/file_util.h" @@ -9,9 +11,9 @@ #include #include #include +#include #include #include -#include #if defined(_WIN32) #define NOMINMAX @@ -20,6 +22,7 @@ #else #include #include +#include #include #include #endif @@ -83,6 +86,19 @@ GccJitter::GccJitter(State *s) : s_(s) { void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const CompileConfig &cfg) { LOG_INFO(s_, "engine", "GCC JIT compile start: {}, {} functions", pr.file_name, gr.function_names.size()); + + struct Workspace { + std::string path; + bool transferred = false; + bool created = false; + + ~Workspace() { + if (created && !transferred && !path.empty()) { + std::error_code ec; + std::filesystem::remove_all(path, ec); + } + } + } workspace; #if defined(USE_COV) // Coverage builds: put JIT temps under the CMake build tree so .gcno/.gcda are // visible to `lcov --directory build`. Keep random names to avoid collisions. @@ -98,7 +114,17 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil } while (std::ifstream(c_file)); } #else - const std::string c_file = GenerateTmpFilename("fakelua_jit_", ".c"); + // An exclusive private directory prevents temp-file collisions/symlink + // races and ensures every dlopen/LoadLibrary sees a distinct image inode. + workspace.path = GenerateTmpFilename("fakelua_jit_", ".dir"); +#if defined(_WIN32) + if (!std::filesystem::create_directory(workspace.path)) +#else + if (::mkdir(workspace.path.c_str(), 0700) != 0) +#endif + ThrowFakeluaException("GCC compile failed, cannot create private workspace"); + workspace.created = true; + const std::string c_file = (std::filesystem::path(workspace.path) / "module.c").string(); #endif const std::string so_file = c_file.substr(0, c_file.size() - kCExtLen) + #if defined(_WIN32) @@ -139,6 +165,19 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil } #endif + GccCache cache(s_->GetStateConfig().gcc_config, compiler, gr.c_code, cfg.debug_mode); + bool cache_hit = cache.Restore(so_file); + const std::string dep_file = c_file + ".d"; + + struct DependencyCleanup { + const std::string &path; + + ~DependencyCleanup() { + std::error_code ec; + std::filesystem::remove(path, ec); + } + } dependency_cleanup{dep_file}; + const auto opt_flag = cfg.debug_mode ? "-O0" : "-O3"; const std::string jit_type_def = "-DFAKELUA_JIT_TYPE=" + std::to_string(static_cast(JIT_GCC)); @@ -242,77 +281,92 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil #endif }; + auto compile_image = [&]() { #if defined(USE_COV) - // Two-step compile so gcov notes/data are named `.gcno/.gcda` (not - // `.so-.gcno` from one-shot -shared of a .c file). - { - std::vector compile_args{compiler, "-x", "c", "-c"}; + // Two-step compile so gcov notes/data are named `.gcno/.gcda` (not + // `.so-.gcno` from one-shot -shared of a .c file). + { + std::vector compile_args{compiler, "-x", "c", "-c"}; #if !defined(_WIN32) - compile_args.emplace_back("-fPIC"); + compile_args.emplace_back("-fPIC"); #endif - compile_args.emplace_back(opt_flag); - compile_args.emplace_back("-fprofile-arcs"); - compile_args.emplace_back("-ftest-coverage"); - compile_args.emplace_back("-fprofile-update=atomic"); - compile_args.emplace_back(jit_type_def); - append_includes_and_libs(compile_args, false); - compile_args.emplace_back("-o"); - compile_args.emplace_back(obj_file); - compile_args.emplace_back(c_file); - run_gcc(compile_args); - } - { - std::vector link_args{compiler, "-shared"}; + compile_args.emplace_back(opt_flag); + compile_args.emplace_back("-fprofile-arcs"); + compile_args.emplace_back("-ftest-coverage"); + compile_args.emplace_back("-fprofile-update=atomic"); + compile_args.emplace_back(jit_type_def); + append_includes_and_libs(compile_args, false); + compile_args.emplace_back("-o"); + compile_args.emplace_back(obj_file); + compile_args.emplace_back(c_file); + run_gcc(compile_args); + } + { + std::vector link_args{compiler, "-shared"}; #if !defined(_WIN32) - link_args.emplace_back("-fPIC"); + link_args.emplace_back("-fPIC"); #endif #if defined(__APPLE__) - link_args.emplace_back("-undefined"); - link_args.emplace_back("dynamic_lookup"); - link_args.emplace_back("-Wl,-adhoc_codesign"); + link_args.emplace_back("-undefined"); + link_args.emplace_back("dynamic_lookup"); + link_args.emplace_back("-Wl,-adhoc_codesign"); #endif - link_args.emplace_back("--coverage"); - append_includes_and_libs(link_args, true); - link_args.emplace_back("-o"); - link_args.emplace_back(so_file); - link_args.emplace_back(obj_file); - run_gcc(link_args); - } - { - // Object file not needed after link; keep .c/.gcno for lcov. - std::error_code ec; - std::filesystem::remove(obj_file, ec); - } + link_args.emplace_back("--coverage"); + append_includes_and_libs(link_args, true); + link_args.emplace_back("-o"); + link_args.emplace_back(so_file); + link_args.emplace_back(obj_file); + run_gcc(link_args); + } + { + // Object file not needed after link; keep .c/.gcno for lcov. + std::error_code ec; + std::filesystem::remove(obj_file, ec); + } #else - { - std::vector args{compiler, "-x", "c", "-shared"}; + { + std::vector args{compiler, "-x", "c", "-shared"}; #if !defined(_WIN32) - args.emplace_back("-fPIC"); + args.emplace_back("-fPIC"); #endif #if defined(__APPLE__) - // macOS 的 .dylib 需要 -undefined dynamic_lookup 选项, - // 允许运行时由宿主进程提供未解析的符号。 - args.emplace_back("-undefined"); - args.emplace_back("dynamic_lookup"); - args.emplace_back("-Wl,-adhoc_codesign"); + // macOS 的 .dylib 需要 -undefined dynamic_lookup 选项, + // 允许运行时由宿主进程提供未解析的符号。 + args.emplace_back("-undefined"); + args.emplace_back("dynamic_lookup"); + args.emplace_back("-Wl,-adhoc_codesign"); #endif - args.emplace_back(opt_flag); - args.emplace_back(jit_type_def); - append_includes_and_libs(args, true); - args.emplace_back("-o"); - args.emplace_back(so_file); - args.emplace_back(c_file); - run_gcc(args); - } + args.emplace_back("-MD"); + args.emplace_back("-MF"); + args.emplace_back(dep_file); + args.emplace_back("-MT"); + args.emplace_back("flua"); + args.emplace_back(opt_flag); + args.emplace_back(jit_type_def); + append_includes_and_libs(args, true); + args.emplace_back("-o"); + args.emplace_back(so_file); + args.emplace_back(c_file); + run_gcc(args); + } #endif + }; + if (!cache_hit) compile_image(); #if defined(_WIN32) HMODULE module_handle = LoadLibraryA(so_file.c_str()); + if (!module_handle && cache_hit) { + cache.Invalidate(); + cache_hit = false; + compile_image(); + module_handle = LoadLibraryA(so_file.c_str()); + } if (!module_handle) { CleanupOnError(); ThrowFakeluaException(std::format("GCC compile failed, LoadLibrary failed for {}: {}", so_file, WinErrToString(GetLastError()))); } - const auto handle = std::make_shared(s_, c_file, so_file, log_file, module_handle); + const auto handle = std::make_shared(s_, c_file, so_file, log_file, module_handle, workspace.path); + workspace.transferred = true; // Common post-load: register compiled functions + call init. auto dlsym_lambda = [&](const std::string &sym) -> void * { @@ -321,6 +375,12 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil }; #else void *dl_handle = dlopen(so_file.c_str(), RTLD_NOW | RTLD_LOCAL); + if (!dl_handle && cache_hit) { + cache.Invalidate(); + cache_hit = false; + compile_image(); + dl_handle = dlopen(so_file.c_str(), RTLD_NOW | RTLD_LOCAL); + } if (!dl_handle) { // 在高并发多进程测试场景下,macOS 内核 AMFI 代码签名检查偶现缓存同步微延迟,重试以确保平滑加载 for (int retry = 0; retry < 5 && !dl_handle; ++retry) { @@ -332,11 +392,27 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil CleanupOnError(); ThrowFakeluaException(std::format("GCC compile failed, dlopen failed for {}: {}", so_file, dlerror())); } - const auto handle = std::make_shared(s_, c_file, so_file, log_file, dl_handle); + const auto handle = std::make_shared(s_, c_file, so_file, log_file, dl_handle, workspace.path); + workspace.transferred = true; auto dlsym_lambda = [&](const std::string &sym) -> void * { return dlsym(dl_handle, sym.c_str()); }; #endif + // Own this exact image's location before init: init may throw or compile + // another module recursively, so GetLuaLocs().back() is not reliable. + handle->SetLuaLoc(BindGeneratedCode(dlsym_lambda("__fakelua_bind"), s_, pr.file_name)); + // Validate before publication/registration. Runtime init executes separately + // for every private image, including cache hits. + for (const auto &[name, info]: gr.function_names) { + const auto &symbol = info.c_symbol_name.empty() ? name : info.c_symbol_name; + if (!dlsym_lambda(symbol)) { + cache.Invalidate(); + ThrowFakeluaException("GCC image missing function " + symbol); + } + } + if (!cache_hit) cache.Publish(so_file, dep_file, c_file); + cache.Release();// init may call native code which recursively compiles Lua + // ---- Common post-load: register compiled functions + call init ---- LOG_DEBUG(s_, "engine", "GCC: registering {} functions from {}", gr.function_names.size(), so_file); for (const auto &[name, info]: gr.function_names) { @@ -353,9 +429,6 @@ void GccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil if (init_ptr) { State::ConstAllocScope const_alloc(s_); inter::DispatchCall(s_, init_ptr, nullptr, 0, JIT_GCC); - if (!s_->GetLuaLocs(JIT_GCC).empty()) { - handle->SetLuaLoc(s_->GetLuaLocs(JIT_GCC).back()); - } } #if !defined(_WIN32) diff --git a/src/jit/jit_error_boundary.h b/src/jit/jit_error_boundary.h index 63f5f024..18ee8203 100644 --- a/src/jit/jit_error_boundary.h +++ b/src/jit/jit_error_boundary.h @@ -92,24 +92,25 @@ class LuaLocScope { single_ptr_->line = 0; } else if (!locs.empty()) { saved_.reserve(locs.size()); - for (LuaLoc *loc : locs) { + for (LuaLoc *loc: locs) { saved_.emplace_back(loc, loc->line); loc->line = 0; } } } + ~LuaLocScope() { const auto &locs = s_->GetLuaLocs(jit_); if (single_ptr_ != nullptr) { - for (LuaLoc *cur : locs) { + for (LuaLoc *cur: locs) { if (cur == single_ptr_) { single_ptr_->line = single_line_; break; } } } else { - for (const auto &[loc, line] : saved_) { - for (LuaLoc *cur : locs) { + for (const auto &[loc, line]: saved_) { + for (LuaLoc *cur: locs) { if (cur == loc) { loc->line = line; break; @@ -118,6 +119,7 @@ class LuaLocScope { } } } + LuaLocScope(const LuaLocScope &) = delete; LuaLocScope &operator=(const LuaLocScope &) = delete; diff --git a/src/jit/tcc_jit.cpp b/src/jit/tcc_jit.cpp index 3203f88e..7bdc7305 100644 --- a/src/jit/tcc_jit.cpp +++ b/src/jit/tcc_jit.cpp @@ -1,4 +1,5 @@ #include "jit/tcc_jit.h" +#include "jit/code_binding.h" #include "jit/tcc_handle.h" #include "state/state.h" #include "util/logging.h" @@ -23,6 +24,8 @@ void TccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil ThrowFakeluaException(std::format("TCC compile failed, tcc_relocate failed for {}", pr.file_name)); } + handle->SetLuaLoc(BindGeneratedCode(tcc_get_symbol(s, "__fakelua_bind"), s_, pr.file_name)); + LOG_DEBUG(s_, "engine", "TCC: registering {} functions", gr.function_names.size()); for (const auto &[name, info]: gr.function_names) { const std::string &sym = info.c_symbol_name.empty() ? name : info.c_symbol_name; @@ -42,9 +45,6 @@ void TccJitter::Compile(const ParseResult &pr, const GenResult &gr, const Compil if (init_ptr) { State::ConstAllocScope const_alloc(s_); inter::DispatchCall(s_, init_ptr, nullptr, 0, JIT_TCC); - if (!s_->GetLuaLocs(JIT_TCC).empty()) { - handle->SetLuaLoc(s_->GetLuaLocs(JIT_TCC).back()); - } } LOG_INFO(s_, "engine", "TCC JIT compilation finished for {}", pr.file_name); diff --git a/src/jit/vm.cpp b/src/jit/vm.cpp index 3a5a7048..64f821d3 100644 --- a/src/jit/vm.cpp +++ b/src/jit/vm.cpp @@ -161,7 +161,7 @@ static CVar CallByNameImpl(State *state, int jit_type, std::string_view name, in if (flat_count >= static_cast(kMaxFunctionInputParams)) { ThrowFakeluaException(std::format("FakeluaCallByName: too many arguments, max is {}", kMaxFunctionInputParams)); } - flat_args_buf[flat_count++] = (m && m->GetCount() > 0) ? m->GetVars()[0] : (CVar){static_cast(VarType::Nil)}; + flat_args_buf[flat_count++] = (m && m->GetCount() > 0) ? m->GetVars()[0] : (CVar) {static_cast(VarType::Nil)}; } else { if (flat_count >= static_cast(kMaxFunctionInputParams)) { ThrowFakeluaException(std::format("FakeluaCallByName: too many arguments, max is {}", kMaxFunctionInputParams)); @@ -172,7 +172,7 @@ static CVar CallByNameImpl(State *state, int jit_type, std::string_view name, in if (UNLIKELY(is_vararg)) { for (int i = 0; i < fixed_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar){static_cast(VarType::Nil)}; + temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {static_cast(VarType::Nil)}; } const int vararg_count = std::max(0, flat_count - fixed_arg_count); VarMulti *m = VarMulti::AllocTemp(state, vararg_count); @@ -189,7 +189,7 @@ static CVar CallByNameImpl(State *state, int jit_type, std::string_view name, in ThrowFakeluaException(std::format("FakeluaCallByName: function '{}' expects {} argument(s), got {}", name, expected_arg_count, flat_count)); } for (int i = 0; i < expected_arg_count; ++i) { - temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar){static_cast(VarType::Nil)}; + temp_arg_arr[i] = i < flat_count ? flat_args_buf[i] : (CVar) {static_cast(VarType::Nil)}; } } arg_arr = temp_arg_arr; @@ -217,7 +217,7 @@ static CVar CallByNameImpl(State *state, int jit_type, std::string_view name, in if (flat_count >= static_cast(kMaxFunctionInputParams)) { ThrowFakeluaException(std::format("FakeluaCallByName: too many arguments, max is {}", kMaxFunctionInputParams)); } - flat_args_buf[flat_count++] = (m && m->GetCount() > 0) ? m->GetVars()[0] : (CVar){static_cast(VarType::Nil)}; + flat_args_buf[flat_count++] = (m && m->GetCount() > 0) ? m->GetVars()[0] : (CVar) {static_cast(VarType::Nil)}; } else { if (flat_count >= static_cast(kMaxFunctionInputParams)) { ThrowFakeluaException(std::format("FakeluaCallByName: too many arguments, max is {}", kMaxFunctionInputParams)); diff --git a/src/native/container/native_container.cpp b/src/native/container/native_container.cpp index f084e400..54e42cb9 100644 --- a/src/native/container/native_container.cpp +++ b/src/native/container/native_container.cpp @@ -420,9 +420,11 @@ CVar SetValues(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { NativeField &Nth(boost::container::vector &d, size_t i) { return d[i]; } + NativeField &Nth(SmallVec &d, size_t i) { return d[i]; } + NativeField &Nth(boost::container::list &d, size_t i) { auto it = d.begin(); std::advance(it, static_cast(i)); @@ -505,11 +507,13 @@ boost::container::vector &VecOf(NativeObject *self) { if (c->kind != ContainerImpl::Kind::Vector) ThrowFakeluaException("not a vector"); return c->vec; } + SmallVec &SmallOf(NativeObject *self) { auto *c = Require(self); if (c->kind != ContainerImpl::Kind::SmallVector) ThrowFakeluaException("not a small_vector"); return c->small; } + boost::container::list &ListOf(NativeObject *self) { auto *c = Require(self); if (c->kind != ContainerImpl::Kind::List) ThrowFakeluaException("not a list"); @@ -521,25 +525,31 @@ CVar VectorPushBack(NativeObject *self, State *s, CVar *args, int n) { self->MarkModified(); return r; } + CVar VectorPopBack(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { auto r = SeqPopBack(VecOf(self), s); self->MarkModified(); return r; } + CVar VectorFront(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqFront(VecOf(self), s); } + CVar VectorBack(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqBack(VecOf(self), s); } + CVar VectorAt(NativeObject *self, State *s, CVar *args, int n) { return SeqAt(VecOf(self), s, args, n, "vector:at"); } + CVar VectorSet(NativeObject *self, State *s, CVar *args, int n) { auto r = SeqSet(VecOf(self), s, args, n, "vector:set"); self->MarkModified(); return r; } + CVar VectorToTable(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqToTable(VecOf(self), s); } @@ -549,25 +559,31 @@ CVar SmallPushBack(NativeObject *self, State *s, CVar *args, int n) { self->MarkModified(); return r; } + CVar SmallPopBack(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { auto r = SeqPopBack(SmallOf(self), s); self->MarkModified(); return r; } + CVar SmallFront(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqFront(SmallOf(self), s); } + CVar SmallBack(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqBack(SmallOf(self), s); } + CVar SmallAt(NativeObject *self, State *s, CVar *args, int n) { return SeqAt(SmallOf(self), s, args, n, "small_vector:at"); } + CVar SmallSet(NativeObject *self, State *s, CVar *args, int n) { auto r = SeqSet(SmallOf(self), s, args, n, "small_vector:set"); self->MarkModified(); return r; } + CVar SmallToTable(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqToTable(SmallOf(self), s); } @@ -577,41 +593,48 @@ CVar ListPushBack(NativeObject *self, State *s, CVar *args, int n) { self->MarkModified(); return r; } + CVar ListPushFront(NativeObject *self, State *s, CVar *args, int n) { auto r = SeqPushFront(ListOf(self), s, args, n, "list:push_front"); self->MarkModified(); return r; } + CVar ListPopBack(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { auto r = SeqPopBack(ListOf(self), s); self->MarkModified(); return r; } + CVar ListPopFront(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { auto r = SeqPopFront(ListOf(self), s); self->MarkModified(); return r; } + CVar ListFront(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqFront(ListOf(self), s); } + CVar ListBack(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqBack(ListOf(self), s); } + CVar ListAt(NativeObject *self, State *s, CVar *args, int n) { return SeqAt(ListOf(self), s, args, n, "list:at"); } + CVar ListSet(NativeObject *self, State *s, CVar *args, int n) { auto r = SeqSet(ListOf(self), s, args, n, "list:set"); self->MarkModified(); return r; } + CVar ListToTable(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) { return SeqToTable(ListOf(self), s); } -void RegisterSeqBack(NativeObject *nat, NativeMethod push, NativeMethod pop, NativeMethod front, NativeMethod back, NativeMethod at, - NativeMethod set, NativeMethod to_table) { +void RegisterSeqBack(NativeObject *nat, NativeMethod push, NativeMethod pop, NativeMethod front, NativeMethod back, NativeMethod at, NativeMethod set, NativeMethod to_table) { nat->RegisterMethod("push_back", push); nat->RegisterMethod("pop_back", pop); nat->RegisterMethod("front", front); diff --git a/src/native/crypto/native_crypto.cpp b/src/native/crypto/native_crypto.cpp index 9794f1bf..1ce2de19 100644 --- a/src/native/crypto/native_crypto.cpp +++ b/src/native/crypto/native_crypto.cpp @@ -540,8 +540,7 @@ void RegisterCryptoLibraryApi(State *s) { if (OSSL_PROVIDER_load(nullptr, "legacy") == nullptr) { // Most common cause: libcrypto cannot find its provider modules // (ossl-modules/legacy), e.g. a static OpenSSL whose MODULESDIR is wrong. - LOG_WARN(s, - "crypto", + LOG_WARN(s, "crypto", "failed to load the OpenSSL \"legacy\" provider: crypto.rc4 / blowfish / des " "and the matching crypto.encrypt/decrypt algorithms will fail. " "check OPENSSL_MODULES or the OpenSSL modules directory"); diff --git a/src/native/http/native_http.cpp b/src/native/http/native_http.cpp index 04776542..721be213 100644 --- a/src/native/http/native_http.cpp +++ b/src/native/http/native_http.cpp @@ -10,9 +10,9 @@ #include "var/var.h" #include "var/var_multi.h" +#include #include #include -#include #include #include #include @@ -358,9 +358,12 @@ struct HttpResponseData { class HttpClientOp : public std::enable_shared_from_this { public: - HttpClientOp(State *state, NativeObject *nat) : io_(state->GetIoContext()), lua_state_(state), native_obj_(nat) {} + HttpClientOp(State *state, NativeObject *nat) : io_(state->GetIoContext()), lua_state_(state), native_obj_(nat) { + } - ~HttpClientOp() { Close(); } + ~HttpClientOp() { + Close(); + } void Start(std::string method, std::string url, HeaderList headers, std::string body, int timeout_ms, unsigned version, bool tls_verify, std::string tls_ca) { timeout_ms_ = timeout_ms; @@ -433,9 +436,13 @@ class HttpClientOp : public std::enable_shared_from_this { if (!done_) Finish("closed"); } - bool Done() const { return done_ && !pending_; } + bool Done() const { + return done_ && !pending_; + } - void SetCallback(std::string name) { cb_ = std::move(name); } + void SetCallback(std::string name) { + cb_ = std::move(name); + } private: bufferevent *MakeBev() { @@ -464,9 +471,7 @@ class HttpClientOp : public std::enable_shared_from_this { void ArmTimeout() { ClearTimeout(); if (timeout_ms_ <= 0) return; - timeout_ev_ = evtimer_new(io_.Get(), [](evutil_socket_t, short, void *p) { - static_cast(p)->Finish("timeout"); - }, this); + timeout_ev_ = evtimer_new(io_.Get(), [](evutil_socket_t, short, void *p) { static_cast(p)->Finish("timeout"); }, this); timeval tv{}; tv.tv_sec = timeout_ms_ / 1000; tv.tv_usec = (timeout_ms_ % 1000) * 1000; @@ -600,8 +605,7 @@ class HttpClientOp : public std::enable_shared_from_this { class HttpServerConn : public std::enable_shared_from_this { public: - HttpServerConn(native::IoContext &io, evutil_socket_t fd, int conn_id, int timeout_ms, SSL_CTX *ctx) - : io_(io), conn_id_(conn_id), timeout_ms_(timeout_ms) { + HttpServerConn(native::IoContext &io, evutil_socket_t fd, int conn_id, int timeout_ms, SSL_CTX *ctx) : io_(io), conn_id_(conn_id), timeout_ms_(timeout_ms) { if (ctx) { SSL *ssl = SSL_new(ctx); if (ssl) { @@ -609,7 +613,8 @@ class HttpServerConn : public std::enable_shared_from_this { if (!bev_) SSL_free(ssl); } if (bev_) bufferevent_openssl_set_allow_dirty_shutdown(bev_, 1); - else evutil_closesocket(fd); + else + evutil_closesocket(fd); } else { bev_ = bufferevent_socket_new(io_.Get(), fd, BEV_OPT_CLOSE_ON_FREE); if (!bev_) evutil_closesocket(fd); @@ -621,7 +626,9 @@ class HttpServerConn : public std::enable_shared_from_this { ArmTimeout(); } - ~HttpServerConn() { Close(); } + ~HttpServerConn() { + Close(); + } void Start(std::function on_request, std::function on_close) { on_request_ = std::move(on_request); @@ -650,15 +657,15 @@ class HttpServerConn : public std::enable_shared_from_this { if (on_close_) on_close_(conn_id_); } - int ConnId() const { return conn_id_; } + int ConnId() const { + return conn_id_; + } private: void ArmTimeout() { ClearTimeout(); if (timeout_ms_ <= 0) return; - timeout_ev_ = evtimer_new(io_.Get(), [](evutil_socket_t, short, void *p) { - static_cast(p)->Close(); - }, this); + timeout_ev_ = evtimer_new(io_.Get(), [](evutil_socket_t, short, void *p) { static_cast(p)->Close(); }, this); timeval tv{}; tv.tv_sec = timeout_ms_ / 1000; tv.tv_usec = (timeout_ms_ % 1000) * 1000; @@ -736,9 +743,12 @@ class HttpServerConn : public std::enable_shared_from_this { class HttpServer { public: - explicit HttpServer(State *state) : io_(state->GetIoContext()), lua_state_(state) {} + explicit HttpServer(State *state) : io_(state->GetIoContext()), lua_state_(state) { + } - ~HttpServer() { Close(); } + ~HttpServer() { + Close(); + } void Listen(const std::string &ip, uint16_t port, int backlog, int timeout_ms, SSL_CTX *tls_ctx) { timeout_ms_ = timeout_ms; @@ -747,22 +757,29 @@ class HttpServer { if (!net::FillSockaddr(addr, ip, port)) { throw std::runtime_error("invalid listen address"); } - listener_ = evconnlistener_new_bind(io_.Get(), [](evconnlistener *, evutil_socket_t fd, sockaddr *, int, void *p) { - static_cast(p)->OnAccept(fd); - }, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, backlog > 0 ? backlog : -1, reinterpret_cast(&addr), sizeof(addr)); + listener_ = evconnlistener_new_bind( + io_.Get(), [](evconnlistener *, evutil_socket_t fd, sockaddr *, int, void *p) { static_cast(p)->OnAccept(fd); }, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, + backlog > 0 ? backlog : -1, reinterpret_cast(&addr), sizeof(addr)); if (!listener_) throw std::runtime_error("listen failed"); auto fd = evconnlistener_get_fd(listener_); sockaddr_in local{}; socklen_t slen = sizeof(local); if (getsockname(fd, reinterpret_cast(&local), &slen) == 0) bound_port_ = ntohs(local.sin_port); - else bound_port_ = port; + else + bound_port_ = port; } - uint16_t BoundPort() const { return bound_port_; } + uint16_t BoundPort() const { + return bound_port_; + } - void SetDispatch(std::string name) { dispatch_ = std::move(name); } + void SetDispatch(std::string name) { + dispatch_ = std::move(name); + } - void SetNativeObject(NativeObject *nat) { native_obj_ = nat; } + void SetNativeObject(NativeObject *nat) { + native_obj_ = nat; + } void Tick() { io_.Poll(); @@ -773,7 +790,8 @@ class HttpServer { } for (auto it = conns_.begin(); it != conns_.end();) { if (!it->second) it = conns_.erase(it); - else ++it; + else + ++it; } } @@ -810,9 +828,7 @@ class HttpServer { int id = ++next_id_; auto conn = std::make_shared(io_, fd, id, timeout_ms_, ssl_ctx_); conns_[id] = conn; - conn->Start( - [this](int conn_id, HttpRequestData req) { pending_requests_.emplace_back(conn_id, std::move(req)); }, - [this](int conn_id) { conns_.erase(conn_id); }); + conn->Start([this](int conn_id, HttpRequestData req) { pending_requests_.emplace_back(conn_id, std::move(req)); }, [this](int conn_id) { conns_.erase(conn_id); }); } void DispatchRequest(int conn_id, const HttpRequestData &req) { @@ -985,7 +1001,8 @@ static CVar ServerPort(NativeObject *self, State *s, CVar * /*args*/, int /*n*/) return inter::NativeToFakeluaInt(s, srv->BoundPort()); } -static void ParseRequestOpts(State *s, CVar opts, std::string &method, std::string &url, HeaderList &headers, std::string &body, int &timeout_ms, unsigned &version, bool &tls_verify, std::string &tls_ca) { +static void ParseRequestOpts(State *s, CVar opts, std::string &method, std::string &url, HeaderList &headers, std::string &body, int &timeout_ms, unsigned &version, bool &tls_verify, + std::string &tls_ca) { method = "GET"; timeout_ms = 10000; version = 11; @@ -1026,9 +1043,11 @@ static CVar StartClient(State *s, std::string method, std::string url, HeaderLis nat->SetInt("__http_owned__", 1); nat->RegisterMethod("close", ClientClose); RegisterWrapper(s, nat, false); + struct ClientHold { std::shared_ptr ptr; }; + auto *hold = new ClientHold{op}; nat->SetInt("__http_hold__", reinterpret_cast(hold)); nat->SetFinalizer([hold](NativeObject *self) { diff --git a/src/native/json/native_json.cpp b/src/native/json/native_json.cpp index 46123671..7e6d41b1 100644 --- a/src/native/json/native_json.cpp +++ b/src/native/json/native_json.cpp @@ -148,8 +148,7 @@ static ArrayShapeInfo CheckArrayShape(const std::vector &kvs) { } static std::string HoleMessage(const std::string &who, const ArrayShapeInfo &info) { - return std::format("{}: array table has a hole at index {} ({} entries, max index {}); nil in an array is not encodable", - who, info.hole_at, info.count, info.max_idx); + return std::format("{}: array table has a hole at index {} ({} entries, max index {}); nil in an array is not encodable", who, info.hole_at, info.count, info.max_idx); } // Convert Lua CVar to boost::json::value diff --git a/src/native/math/native_math.cpp b/src/native/math/native_math.cpp index b542401f..c4f49cf3 100644 --- a/src/native/math/native_math.cpp +++ b/src/native/math/native_math.cpp @@ -54,8 +54,7 @@ static int CompareNumbers(CVar lhs, double lhs_value, CVar rhs, double rhs_value static double CheckMathNumberArg(const CVar &arg, int argno, const char *fname) { CheckNumberArg(arg, argno, fname); const double value = inter::CVarToNumber(arg, std::numeric_limits::quiet_NaN()); - if ((arg.type_ == static_cast(VarType::String) || arg.type_ == static_cast(VarType::StringId)) && - std::isnan(value)) { + if ((arg.type_ == static_cast(VarType::String) || arg.type_ == static_cast(VarType::StringId)) && std::isnan(value)) { ThrowBadArgument(argno, fname, "number expected"); } return value; @@ -507,8 +506,7 @@ void RegisterMathLibraryApi(State *s) { CheckMathNumberArg(ax, 1, "math.clamp"); CheckMathNumberArg(alo, 2, "math.clamp"); CheckMathNumberArg(ahi, 3, "math.clamp"); - if (ax.type_ == static_cast(VarType::Int) && alo.type_ == static_cast(VarType::Int) && - ahi.type_ == static_cast(VarType::Int)) { + if (ax.type_ == static_cast(VarType::Int) && alo.type_ == static_cast(VarType::Int) && ahi.type_ == static_cast(VarType::Int)) { if (alo.data_.i > ahi.data_.i) { ThrowBadArgument(2, "math.clamp", "low bound must not exceed high bound"); } diff --git a/src/native/mysql/mysql_connection.cpp b/src/native/mysql/mysql_connection.cpp index c03d92a7..c67dc620 100644 --- a/src/native/mysql/mysql_connection.cpp +++ b/src/native/mysql/mysql_connection.cpp @@ -24,7 +24,11 @@ int64_t NowMs() { struct TickDepthGuard { int &depth; - explicit TickDepthGuard(int &d) : depth(d) { ++depth; } + + explicit TickDepthGuard(int &d) : depth(d) { + ++depth; + } + ~TickDepthGuard() { if (depth > 0) --depth; } @@ -142,13 +146,12 @@ void MysqlConnection::ArmWait(int status) { ptv = &tv; } my_socket fd = mysql_get_socket(mysql_); - wait_ev_ = event_new(io_.Get(), fd, ev, [](evutil_socket_t, short what, void *ctx) { - static_cast(ctx)->Continue(WaitReady(what)); - }, this); + wait_ev_ = event_new(io_.Get(), fd, ev, [](evutil_socket_t, short what, void *ctx) { static_cast(ctx)->Continue(WaitReady(what)); }, this); event_add(wait_ev_, ptv); } -void MysqlConnection::Connect(const std::string &host, uint16_t port, const std::string &user, const std::string &password, const std::string &database, int timeout_ms, SslMode ssl, std::string ssl_ca) { +void MysqlConnection::Connect(const std::string &host, uint16_t port, const std::string &user, const std::string &password, const std::string &database, int timeout_ms, SslMode ssl, + std::string ssl_ca) { host_ = host; port_ = port; user_ = user; @@ -704,7 +707,8 @@ void MysqlConnection::Tick() { if (pending_connect_) { pending_connect_ = false; if (!pending_connect_err_.empty()) DispatchConnect(pending_connect_err_.c_str()); - else DispatchConnect(nullptr); + else + DispatchConnect(nullptr); pending_connect_err_.clear(); } @@ -767,11 +771,26 @@ bool MysqlConnection::IsRetryable(MysqlErrorType type) { } } -void MysqlConnection::SetConnectCallback(const ResultCallback &cb) { connect_cb_ = cb; } -void MysqlConnection::SetResultCallback(const ResultCallback &cb) { result_cb_ = cb; } -void MysqlConnection::SetState(::fakelua::State *state) { lua_state_ = state; } -void MysqlConnection::SetNativeObject(::fakelua::NativeObject *obj) { native_obj_ = obj; } -bool MysqlConnection::Connected() const { return ready_; } +void MysqlConnection::SetConnectCallback(const ResultCallback &cb) { + connect_cb_ = cb; +} + +void MysqlConnection::SetResultCallback(const ResultCallback &cb) { + result_cb_ = cb; +} + +void MysqlConnection::SetState(::fakelua::State *state) { + lua_state_ = state; +} + +void MysqlConnection::SetNativeObject(::fakelua::NativeObject *obj) { + native_obj_ = obj; +} + +bool MysqlConnection::Connected() const { + return ready_; +} + void MysqlConnection::MarkConnectedForTest() { ClearWait(); wait_op_ = WaitOp::None; @@ -781,17 +800,25 @@ void MysqlConnection::MarkConnectedForTest() { ready_ = true; close_pending_ = false; } -bool MysqlConnection::Connecting() const { return state_ == ConnState::Connecting || state_ == ConnState::Handshaking; } -int MysqlConnection::TickDepth() const { return tick_depth_; } -bool MysqlConnection::ClosePending() const { return close_pending_; } -void MysqlConnection::RequestClose() { close_pending_ = true; } + +bool MysqlConnection::Connecting() const { + return state_ == ConnState::Connecting || state_ == ConnState::Handshaking; +} + +int MysqlConnection::TickDepth() const { + return tick_depth_; +} + +bool MysqlConnection::ClosePending() const { + return close_pending_; +} + +void MysqlConnection::RequestClose() { + close_pending_ = true; +} + bool MysqlConnection::HasPendingWork() const { - return state_ == ConnState::Querying || - state_ == ConnState::Connecting || - state_ == ConnState::Handshaking || - wait_op_ != WaitOp::None || - pending_result_ || - pending_connect_ || + return state_ == ConnState::Querying || state_ == ConnState::Connecting || state_ == ConnState::Handshaking || wait_op_ != WaitOp::None || pending_result_ || pending_connect_ || !queued_queries_.empty(); } @@ -882,8 +909,7 @@ void MysqlConnection::DispatchCallbackWithResult(const ResultCallback &cb, const std::vector args = BuildCallbackArgs(a0, inter::NativeToFakeluaString(lua_state_, msg), nil, decoded); InvokeCallback(cb, args.data(), static_cast(args.size())); } else if (dispatch_stmt_id_ != 0) { - std::vector args = BuildCallbackArgs(a0, nil, - inter::NativeToFakeluaInt(lua_state_, static_cast(dispatch_stmt_id_)), decoded); + std::vector args = BuildCallbackArgs(a0, nil, inter::NativeToFakeluaInt(lua_state_, static_cast(dispatch_stmt_id_)), decoded); InvokeCallback(cb, args.data(), static_cast(args.size())); } else if (pending_results_.empty()) { std::vector args = BuildCallbackArgs(a0, nil, table::TableHelper::CreateTable(lua_state_), decoded); diff --git a/src/native/mysql/mysql_connection.h b/src/native/mysql/mysql_connection.h index 8cfdd143..7ddfe25f 100644 --- a/src/native/mysql/mysql_connection.h +++ b/src/native/mysql/mysql_connection.h @@ -40,7 +40,6 @@ struct StmtParam { std::string value; }; - // C++ → Lua 异步回调:全局函数名 + 绑定参数。 // fakelua 没有 GC,运行期闭包活在临时 arena 上,顶层 Call 的 State::Reset() 会回收, // 无法安全地跨 tick 持有闭包裸指针。因此异步回调只接受【全局函数名】;脚本需要上下文时 @@ -56,7 +55,9 @@ struct ResultCallback { // 回调就执行哪个引擎的编译产物,与内联闭包自带 func_ptr 的语义一致。 JITType jit = JIT_TCC; - [[nodiscard]] bool Empty() const { return name.empty(); } + [[nodiscard]] bool Empty() const { + return name.empty(); + } }; enum class SslMode { @@ -65,16 +66,7 @@ enum class SslMode { Require, }; -enum class MysqlErrorType { - None = 0, - Connection, - Authentication, - Syntax, - Timeout, - Protocol, - Server, - Unknown -}; +enum class MysqlErrorType { None = 0, Connection, Authentication, Syntax, Timeout, Protocol, Server, Unknown }; struct MysqlError { MysqlErrorType type = MysqlErrorType::None; @@ -101,7 +93,8 @@ class MysqlConnection { MysqlConnection(const MysqlConnection &) = delete; MysqlConnection &operator=(const MysqlConnection &) = delete; - void Connect(const std::string &host, uint16_t port, const std::string &user, const std::string &password, const std::string &database, int timeout_ms = 0, SslMode ssl = SslMode::Disable, std::string ssl_ca = {}); + void Connect(const std::string &host, uint16_t port, const std::string &user, const std::string &password, const std::string &database, int timeout_ms = 0, SslMode ssl = SslMode::Disable, + std::string ssl_ca = {}); void Query(const std::string &sql); void StmtPrepare(const std::string &sql); @@ -150,6 +143,7 @@ class MysqlConnection { std::string sql; ResultCallback cb; }; + std::vector queued_queries_; enum class QueryType { None, Query, StmtPrepare, StmtExecute, Ping }; diff --git a/src/native/mysql/native_mysql.cpp b/src/native/mysql/native_mysql.cpp index 1c3957fb..2befee30 100644 --- a/src/native/mysql/native_mysql.cpp +++ b/src/native/mysql/native_mysql.cpp @@ -43,11 +43,9 @@ static std::string CVarToString(CVar v) { // conn:query(sql, "on_result", tag, ctx_table) // 绑定参数在登记当下序列化成自有字节串随回调暂存,派发时 decode 追加在固定参数后。 // 名称参数非法(闭包/数字/空串等)直接抛错——历史上坏回调会被静默转成空串、永不触发。 -static ResultCallback BuildCallback(State *s, CVar name_v, int name_argno, CVar *args, int bound_first, int n, - const char *fname) { +static ResultCallback BuildCallback(State *s, CVar name_v, int name_argno, CVar *args, int bound_first, int n, const char *fname) { if (name_v.type_ == static_cast(VarType::Closure)) { - ThrowBadArgument(name_argno, fname, - "global callback function name expected; inline closures are not supported for async callbacks, pass a name string plus data args"); + ThrowBadArgument(name_argno, fname, "global callback function name expected; inline closures are not supported for async callbacks, pass a name string plus data args"); } std::string name = CVarToString(name_v); if (name.empty()) { @@ -205,8 +203,10 @@ static CVar MysqlConnect(State *s, CVar *args, int n) { } else if (ssl_var.type_ != static_cast(VarType::Nil)) { std::string mode = CVarToString(ssl_var); if (mode == "require" || mode == "true") ssl = SslMode::Require; - else if (mode == "enable") ssl = SslMode::Enable; - else ssl = SslMode::Disable; + else if (mode == "enable") + ssl = SslMode::Enable; + else + ssl = SslMode::Disable; } CVar ca_var = table::TableHelper::GetTableStrId(s, a0, "ssl_ca"); if (ca_var.type_ != static_cast(VarType::Nil)) ssl_ca = CVarToString(ca_var); diff --git a/src/native/mysql/native_mysql_pool.cpp b/src/native/mysql/native_mysql_pool.cpp index 82c86cff..c674ebb5 100644 --- a/src/native/mysql/native_mysql_pool.cpp +++ b/src/native/mysql/native_mysql_pool.cpp @@ -169,8 +169,10 @@ static CVar PoolCreate(State *s, CVar *args, int n) { } else if (ssl_var.type_ != static_cast(VarType::Nil)) { std::string mode = CVarToString(ssl_var); if (mode == "require" || mode == "true") config.ssl = SslMode::Require; - else if (mode == "enable") config.ssl = SslMode::Enable; - else config.ssl = SslMode::Disable; + else if (mode == "enable") + config.ssl = SslMode::Enable; + else + config.ssl = SslMode::Disable; } CVar ca_var = table::TableHelper::GetTableStrId(s, a0, "ssl_ca"); if (ca_var.type_ != static_cast(VarType::Nil)) config.ssl_ca = CVarToString(ca_var); @@ -276,12 +278,14 @@ static CVar PoolWith(NativeObject *self, State *s, CVar *args, int n) { CVar conn_arg = inter::NativeToFakeluaNativeObject(s, nat); auto *conn = UnwrapConnNative(nat); + // 租约保护:离开作用域时检查。 // 若无在途/排队的异步工作(如同步抛错或未发起 query),立刻归还连接; // 若已有 query 在飞行或排队中,打上 auto_release 标记,由 Tick 在全部 query 结果派发完毕后自动归还。 struct LeaseGuard { NativeObject *nat; MysqlConnection *conn; + ~LeaseGuard() { if (!nat) return; if (!conn || !conn->HasPendingWork()) { @@ -291,6 +295,7 @@ static CVar PoolWith(NativeObject *self, State *s, CVar *args, int n) { } } } lease{nat, conn}; + // fn 是同帧同步调用,直接沿用当前 Lua 调用方的引擎(异常边界随之正确), // 不固定 TCC 标记。 return inter::DispatchCallClosure(s, a0.data_.cl, &conn_arg, 1, s->CurrentJit()); diff --git a/src/native/net/native_net.cpp b/src/native/net/native_net.cpp index 284df449..ac7f230b 100644 --- a/src/native/net/native_net.cpp +++ b/src/native/net/native_net.cpp @@ -50,11 +50,12 @@ struct NetObject { // 泵出——消除"回调里 send 是否生效"的平台相关行为(事件驱动改轮询驱动)。 struct PendingSend { enum class Kind { Server, Client, UdpPeer, UdpTo } kind; - int connid = -1; // Server: 目标连接 + int connid = -1;// Server: 目标连接 std::string data; - std::string ip; // UdpTo: 目标地址 - uint16_t port = 0; // UdpTo: 目标端口 + std::string ip; // UdpTo: 目标地址 + uint16_t port = 0;// UdpTo: 目标端口 }; + std::vector pending_sends; static constexpr size_t kMaxEvents = 1024; diff --git a/src/native/net/net_event.cpp b/src/native/net/net_event.cpp index 0885da14..70beb294 100644 --- a/src/native/net/net_event.cpp +++ b/src/native/net/net_event.cpp @@ -4,9 +4,9 @@ #include "native/tls_util.h" #include "state/state.h" +#include #include #include -#include #include #include @@ -188,11 +188,12 @@ bool TryParseWsFrame(CircularBuffer &buf, std::string &payload, bool &closed, bo class EventConn : public std::enable_shared_from_this { public: EventConn(native::IoContext &io, const NetConfig &cfg, int conn_id, bool from_client, bool tls, EventSink sink) - : io_(io), cfg_(cfg), conn_id_(conn_id), from_client_(from_client), tls_(tls), sink_(std::move(sink)), - recv_buf_(cfg.recv_buf_size), send_buf_(cfg.send_buf_size) { + : io_(io), cfg_(cfg), conn_id_(conn_id), from_client_(from_client), tls_(tls), sink_(std::move(sink)), recv_buf_(cfg.recv_buf_size), send_buf_(cfg.send_buf_size) { } - ~EventConn() { Close(false); } + ~EventConn() { + Close(false); + } void Attach(bufferevent *bev) { bev_ = bev; @@ -231,9 +232,17 @@ class EventConn : public std::enable_shared_from_this { return true; } - [[nodiscard]] bool IsOpen() const { return !closed_ && bev_; } - [[nodiscard]] bool Ready() const { return ready_ && !closed_ && bev_; } - [[nodiscard]] int ConnId() const { return conn_id_; } + [[nodiscard]] bool IsOpen() const { + return !closed_ && bev_; + } + + [[nodiscard]] bool Ready() const { + return ready_ && !closed_ && bev_; + } + + [[nodiscard]] int ConnId() const { + return conn_id_; + } void OnConnected() { if (IsWebSocket(cfg_)) return; @@ -259,9 +268,7 @@ class EventConn : public std::enable_shared_from_this { void SendWsClientHandshake() { ws_key_ = RandomWsKey(); std::string host = cfg_.ws_host.empty() ? (cfg_.ip + ":" + std::to_string(cfg_.port)) : cfg_.ws_host; - std::string req = "GET " + cfg_.ws_path + " HTTP/1.1\r\nHost: " + host + - "\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: " + ws_key_ + - "\r\nSec-WebSocket-Version: 13\r\n"; + std::string req = "GET " + cfg_.ws_path + " HTTP/1.1\r\nHost: " + host + "\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: " + ws_key_ + "\r\nSec-WebSocket-Version: 13\r\n"; if (!cfg_.ws_origin.empty()) req += "Origin: " + cfg_.ws_origin + "\r\n"; req += "\r\n"; bufferevent_write(bev_, req.data(), req.size()); @@ -436,9 +443,9 @@ void TcpServer::Start() { if (config_.tls && !ssl_ctx_) return; sockaddr_in addr{}; if (!FillSockaddr(addr, config_.ip, config_.port)) return; - listener_ = evconnlistener_new_bind(io_.Get(), [](evconnlistener *, evutil_socket_t fd, sockaddr *, int, void *p) { - static_cast(p)->OnAccept(fd); - }, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, config_.backlog > 0 ? config_.backlog : -1, reinterpret_cast(&addr), sizeof(addr)); + listener_ = evconnlistener_new_bind( + io_.Get(), [](evconnlistener *, evutil_socket_t fd, sockaddr *, int, void *p) { static_cast(p)->OnAccept(fd); }, this, LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, + config_.backlog > 0 ? config_.backlog : -1, reinterpret_cast(&addr), sizeof(addr)); } void TcpServer::Stop() { @@ -576,11 +583,12 @@ bool TcpClient::Connected() const { } UdpSocket::UdpSocket(const NetConfig &config, ::fakelua::State *state, bool connected) - : config_(config), connected_(connected), io_(state->GetIoContext()), - recv_buf_(config.recv_buf_size > 0 ? static_cast(config.recv_buf_size) : 65536) { + : config_(config), connected_(connected), io_(state->GetIoContext()), recv_buf_(config.recv_buf_size > 0 ? static_cast(config.recv_buf_size) : 65536) { } -UdpSocket::~UdpSocket() { Stop(); } +UdpSocket::~UdpSocket() { + Stop(); +} bool UdpSocket::Start() { fd_ = static_cast(socket(AF_INET, SOCK_DGRAM, 0)); @@ -643,7 +651,9 @@ void UdpSocket::OnRecv() { EmitEvent(std::move(ev)); } -void UdpSocket::EmitEvent(ConnEvent ev) { events_.push_back(std::move(ev)); } +void UdpSocket::EmitEvent(ConnEvent ev) { + events_.push_back(std::move(ev)); +} void UdpSocket::DrainEventsWith(const std::function &dispatcher) { io_.Poll(); diff --git a/src/native/net/net_event.h b/src/native/net/net_event.h index b8d48aba..0aee7395 100644 --- a/src/native/net/net_event.h +++ b/src/native/net/net_event.h @@ -51,7 +51,9 @@ class TcpServer { bool Send(int conn_id, const char *data, size_t len); bool CloseConnection(int conn_id); - [[nodiscard]] bool Running() const { return listener_ != nullptr; } + [[nodiscard]] bool Running() const { + return listener_ != nullptr; + } private: void EmitEvent(ConnEvent ev); @@ -101,9 +103,17 @@ class UdpSocket { bool Send(const char *data, size_t len); bool SendTo(const char *data, size_t len, const std::string &ip, uint16_t port); - [[nodiscard]] bool Running() const { return open_; } - [[nodiscard]] bool Connected() const { return connected_; } - [[nodiscard]] uint16_t BoundPort() const { return bound_port_; } + [[nodiscard]] bool Running() const { + return open_; + } + + [[nodiscard]] bool Connected() const { + return connected_; + } + + [[nodiscard]] uint16_t BoundPort() const { + return bound_port_; + } private: void OnRecv(); diff --git a/src/native/object/native_object.cpp b/src/native/object/native_object.cpp index 74c4dfb1..76d06016 100644 --- a/src/native/object/native_object.cpp +++ b/src/native/object/native_object.cpp @@ -1041,9 +1041,7 @@ uint64_t NativeObject::ComputeContentHash(const NativeObject *obj) { std::vector *> fields; fields.reserve(obj->impl_->kv.size()); for (const auto &item: obj->impl_->kv) fields.push_back(&item); - std::sort(fields.begin(), fields.end(), [](const auto *a, const auto *b) { - return a->first < b->first; - }); + std::sort(fields.begin(), fields.end(), [](const auto *a, const auto *b) { return a->first < b->first; }); FnvHashU64(h, fields.size()); for (const auto *item: fields) { const auto &key = item->first; @@ -1084,8 +1082,7 @@ uint64_t NativeObject::ComputeContentHash(const NativeObject *obj) { std::vector method_names; method_names.reserve(obj->impl_->methods.size()); for (const auto &item: obj->impl_->methods) method_names.push_back(&item.first); - std::sort(method_names.begin(), method_names.end(), - [](const std::string *a, const std::string *b) { return *a < *b; }); + std::sort(method_names.begin(), method_names.end(), [](const std::string *a, const std::string *b) { return *a < *b; }); FnvHashU64(h, method_names.size()); for (const auto *name: method_names) { FnvHashU64(h, name->size()); @@ -1104,8 +1101,7 @@ bool NativeObject::SelfCheck(const NativeObject *obj, bool recursive) { // 内容哈希比对:若某次写入绕过了对象 API(因此没有 TouchVersion), // 记录下来的哈希会与真实内容不一致。 const uint64_t debug_epoch = g_debug_epoch.load(std::memory_order_relaxed); - if (obj->impl_->debug_hash_epoch != 0 && obj->impl_->debug_hash_epoch == debug_epoch && - obj->impl_->debug_hash != ComputeContentHash(obj)) { + if (obj->impl_->debug_hash_epoch != 0 && obj->impl_->debug_hash_epoch == debug_epoch && obj->impl_->debug_hash != ComputeContentHash(obj)) { return false; } } diff --git a/src/native/object/native_object.h b/src/native/object/native_object.h index b253facf..85c757ff 100644 --- a/src/native/object/native_object.h +++ b/src/native/object/native_object.h @@ -103,6 +103,7 @@ void RefreshSpecKeys(VarTable *tbl, const NativeObject *obj, State *s); // 若 tbl 是尚未建快照的 NativeObject 壳,则 RefreshSpecKeys 一次并标记 snapshot_ready。 // pairs/next(ForEachKV)与 GET_TABLE_ENTRY 计数路径在遍历前调用;非 Native 表为 no-op。 void EnsureNativeSpecSnapshotSlow(VarTable *tbl); + inline void EnsureNativeSpecSnapshot(VarTable *tbl) { if (!tbl || !tbl->spec) return; EnsureNativeSpecSnapshotSlow(tbl); diff --git a/src/native/process/native_process.cpp b/src/native/process/native_process.cpp index d173a1f1..7ebc34f6 100644 --- a/src/native/process/native_process.cpp +++ b/src/native/process/native_process.cpp @@ -172,7 +172,10 @@ struct FileStdio { FileStdio() = default; FileStdio(const FileStdio &) = delete; FileStdio &operator=(const FileStdio &) = delete; - ~FileStdio() { Close(); } + + ~FileStdio() { + Close(); + } void Close() { auto close_one = [](HANDLE &h) { @@ -190,8 +193,7 @@ struct FileStdio { SECURITY_ATTRIBUTES sa{}; sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; - HANDLE h = ::CreateFileW(path, for_read ? GENERIC_READ : GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, - for_read ? OPEN_EXISTING : OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + HANDLE h = ::CreateFileW(path, for_read ? GENERIC_READ : GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, for_read ? OPEN_EXISTING : OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (h == INVALID_HANDLE_VALUE) { ThrowFakeluaException(std::format("process.run: CreateFileW failed ({})", static_cast(::GetLastError()))); } @@ -333,9 +335,8 @@ static CVar ProcessRun(State *s, CVar *args, int n) { env_ptr.reset(); } std::wstring cwd_w = cwd.empty() ? std::wstring() : boost::nowide::widen(cwd); - BOOL ok = ::CreateProcessW(boost::nowide::widen(exe).c_str(), cmd_buf.data(), nullptr, nullptr, TRUE, flags, - env_block.empty() ? nullptr : env_block.data(), - cwd_w.empty() ? nullptr : cwd_w.c_str(), &si, &pi); + BOOL ok = ::CreateProcessW(boost::nowide::widen(exe).c_str(), cmd_buf.data(), nullptr, nullptr, TRUE, flags, env_block.empty() ? nullptr : env_block.data(), + cwd_w.empty() ? nullptr : cwd_w.c_str(), &si, &pi); stdio.Close(); if (!ok) { ThrowFakeluaException(std::format("process.run: failed to spawn '{}': {}", argv[0], static_cast(::GetLastError()))); @@ -352,7 +353,8 @@ static CVar ProcessRun(State *s, CVar *args, int n) { } DWORD code = 0; if (::GetExitCodeProcess(pi.hProcess, &code) && code != STILL_ACTIVE) exit_code = static_cast(code); - else if (timed_out) exit_code = 9; + else if (timed_out) + exit_code = 9; ::CloseHandle(pi.hProcess); ::CloseHandle(pi.hThread); #else @@ -400,8 +402,10 @@ static CVar ProcessRun(State *s, CVar *args, int n) { pid_t r = ::waitpid(pid, &status, WNOHANG); if (r == pid) { if (WIFEXITED(status)) exit_code = WEXITSTATUS(status); - else if (WIFSIGNALED(status)) exit_code = 128 + WTERMSIG(status); - else exit_code = -1; + else if (WIFSIGNALED(status)) + exit_code = 128 + WTERMSIG(status); + else + exit_code = -1; break; } if (r < 0 && errno != EINTR) { @@ -420,7 +424,8 @@ static CVar ProcessRun(State *s, CVar *args, int n) { r = ::waitpid(pid, &status, 0); if (r == pid) { if (WIFEXITED(status)) exit_code = WEXITSTATUS(status); - else if (WIFSIGNALED(status)) exit_code = 128 + WTERMSIG(status); + else if (WIFSIGNALED(status)) + exit_code = 128 + WTERMSIG(status); } break; } diff --git a/src/native/redis/native_redis.cpp b/src/native/redis/native_redis.cpp index c198c671..3d34f428 100644 --- a/src/native/redis/native_redis.cpp +++ b/src/native/redis/native_redis.cpp @@ -8,15 +8,15 @@ #include "util/string_util.h" #include "var/var.h" -#include -#include #include +#include +#include #include #include #include -#include #include +#include #include #include #include @@ -110,7 +110,11 @@ static CVar ReplyToLua(State *s, const redisReply *r, int depth = 0) { struct TickDepthGuard { int &depth; - explicit TickDepthGuard(int &d) : depth(d) { ++depth; } + + explicit TickDepthGuard(int &d) : depth(d) { + ++depth; + } + ~TickDepthGuard() { if (depth > 0) --depth; } @@ -125,9 +129,12 @@ struct CmdOp { class RedisConnection { public: - explicit RedisConnection(State *state) : io_(state->GetIoContext()), lua_state_(state) {} + explicit RedisConnection(State *state) : io_(state->GetIoContext()), lua_state_(state) { + } - ~RedisConnection() { Teardown(); } + ~RedisConnection() { + Teardown(); + } RedisConnection(const RedisConnection &) = delete; RedisConnection &operator=(const RedisConnection &) = delete; @@ -182,11 +189,8 @@ class RedisConnection { // 生命周期订阅上下文;在此之前响亮拒绝,避免堆破坏(FakeLua Phase16 Bug 2)。 // 参数校验先于连接状态检查,未就绪的连接上也能拿到明确报错。 std::string verb = parts.front(); - std::transform(verb.begin(), verb.end(), verb.begin(), [](unsigned char ch) { - return static_cast(std::tolower(ch)); - }); - if (verb == "subscribe" || verb == "psubscribe" || verb == "unsubscribe" || verb == "punsubscribe" || - verb == "ssubscribe" || verb == "sunsubscribe") { + std::transform(verb.begin(), verb.end(), verb.begin(), [](unsigned char ch) { return static_cast(std::tolower(ch)); }); + if (verb == "subscribe" || verb == "psubscribe" || verb == "unsubscribe" || verb == "punsubscribe" || verb == "ssubscribe" || verb == "sunsubscribe") { ThrowFakeluaException("redis: '" + verb + "' is not supported, command() is one-shot RPC and cannot deliver pub/sub pushes"); } if (closed_ || close_pending_ || !ac_ || !ready_) { @@ -239,15 +243,30 @@ class RedisConnection { Teardown(); } - int TickDepth() const { return tick_depth_; } - bool ClosePending() const { return close_pending_; } + int TickDepth() const { + return tick_depth_; + } + + bool ClosePending() const { + return close_pending_; + } + void RequestClose() { closed_ = true; close_pending_ = true; } - void SetConnectCallback(std::string name) { connect_cb_ = std::move(name); } - void SetNativeObject(NativeObject *obj) { native_obj_ = obj; } - bool Closed() const { return closed_; } + + void SetConnectCallback(std::string name) { + connect_cb_ = std::move(name); + } + + void SetNativeObject(NativeObject *obj) { + native_obj_ = obj; + } + + bool Closed() const { + return closed_; + } private: static std::string ConnectErr(const std::string &detail) { diff --git a/src/native/runtime/native_runtime.cpp b/src/native/runtime/native_runtime.cpp index 5f0f8dbd..a9e08313 100644 --- a/src/native/runtime/native_runtime.cpp +++ b/src/native/runtime/native_runtime.cpp @@ -1,9 +1,9 @@ #include "native/runtime/native_runtime.h" +#include "native/http/native_http.h" #include "native/mysql/native_mysql.h" #include "native/native_common.h" #include "native/net/native_net.h" -#include "native/http/native_http.h" #include "native/redis/native_redis.h" #include "native/timer/native_timer.h" #include "var/var.h" diff --git a/src/native/serialize/native_serialize.cpp b/src/native/serialize/native_serialize.cpp index 9887acc2..6fba49f7 100644 --- a/src/native/serialize/native_serialize.cpp +++ b/src/native/serialize/native_serialize.cpp @@ -1,6 +1,6 @@ #include "native/serialize/native_serialize.h" -#include "native/serialize/wire_codec.h" #include "native/native_common.h" +#include "native/serialize/wire_codec.h" #include "native/table/native_table.h" #include "util/logging.h" #include "var/var.h" @@ -74,13 +74,13 @@ struct SerNode { template void serialize(Archive &ar, const unsigned int) { - ar & BOOST_SERIALIZATION_NVP(kind); - ar & BOOST_SERIALIZATION_NVP(b); - ar & BOOST_SERIALIZATION_NVP(i); - ar & BOOST_SERIALIZATION_NVP(f); - ar & BOOST_SERIALIZATION_NVP(s); - ar & BOOST_SERIALIZATION_NVP(keys); - ar & BOOST_SERIALIZATION_NVP(vals); + ar &BOOST_SERIALIZATION_NVP(kind); + ar &BOOST_SERIALIZATION_NVP(b); + ar &BOOST_SERIALIZATION_NVP(i); + ar &BOOST_SERIALIZATION_NVP(f); + ar &BOOST_SERIALIZATION_NVP(s); + ar &BOOST_SERIALIZATION_NVP(keys); + ar &BOOST_SERIALIZATION_NVP(vals); } }; diff --git a/src/native/serialize/wire_codec.cpp b/src/native/serialize/wire_codec.cpp index 6a8d36cd..8c639338 100644 --- a/src/native/serialize/wire_codec.cpp +++ b/src/native/serialize/wire_codec.cpp @@ -297,8 +297,7 @@ CVar DecodeValue(std::string_view in, size_t &pos, DecodeState &state, State *s) // 严格校验:回调绑定参数不允许静默丢字段。任意位置出现不可序列化类型都响亮报错。 void ValidateStrict(CVar v, int depth, std::unordered_set &visited, const char *fname, int argno) { if (!IsSupported(v)) { - ThrowBadArgument(argno, fname, - "callback args must be nil, boolean, number, string or a table thereof (closures and native objects are not allowed)"); + ThrowBadArgument(argno, fname, "callback args must be nil, boolean, number, string or a table thereof (closures and native objects are not allowed)"); } if (v.type_ != static_cast(VarType::Table) || !v.data_.t) return; VarTable *t = v.data_.t; diff --git a/src/native/string/lua_pattern.cpp b/src/native/string/lua_pattern.cpp index 76073a7b..df5d0f18 100644 --- a/src/native/string/lua_pattern.cpp +++ b/src/native/string/lua_pattern.cpp @@ -25,8 +25,7 @@ static inline unsigned char U(char c) { // - '-' 惰性量词零宽合法,从「零次 + suffix」开始试。 class PatternMatcher { public: - PatternMatcher(const char *src, size_t slen, const char *pat, size_t plen) - : src_(src), se_(src + slen), pstart_(pat), pe_(pat + plen) { + PatternMatcher(const char *src, size_t slen, const char *pat, size_t plen) : src_(src), se_(src + slen), pstart_(pat), pe_(pat + plen) { } bool RunAt(size_t start, MatchResult &out) { @@ -78,8 +77,7 @@ class PatternMatcher { } switch (*p) { case '(': { - const char *r = (p + 1 < pe_ && p[1] == ')') ? StartCapture(s, p + 2, kCapPosition) - : StartCapture(s, p + 1, kCapUnfinished); + const char *r = (p + 1 < pe_ && p[1] == ')') ? StartCapture(s, p + 2, kCapPosition) : StartCapture(s, p + 1, kCapUnfinished); --depth_; return r; } @@ -134,7 +132,8 @@ class PatternMatcher { } goto dflt; } - default: dflt: { + default: + dflt: { const char *ep = ClassEnd(p); const bool m = (s < se_) && SingleMatch(s, p); if (!m) { @@ -286,18 +285,41 @@ class PatternMatcher { static bool MatchClass(int c, int cl) { bool res; switch (std::tolower(cl)) { - case 'a': res = std::isalpha(c) != 0; break; - case 'c': res = std::iscntrl(c) != 0; break; - case 'd': res = std::isdigit(c) != 0; break; - case 'g': res = std::isgraph(c) != 0; break; - case 'l': res = std::islower(c) != 0; break; - case 'p': res = std::ispunct(c) != 0; break; - case 's': res = std::isspace(c) != 0; break; - case 'u': res = std::isupper(c) != 0; break; - case 'w': res = std::isalnum(c) != 0; break; - case 'x': res = std::isxdigit(c) != 0; break; - case 'z': res = (c == 0); break;// Lua 5.1/5.5 保留类:零字节 - default: return cl == c; + case 'a': + res = std::isalpha(c) != 0; + break; + case 'c': + res = std::iscntrl(c) != 0; + break; + case 'd': + res = std::isdigit(c) != 0; + break; + case 'g': + res = std::isgraph(c) != 0; + break; + case 'l': + res = std::islower(c) != 0; + break; + case 'p': + res = std::ispunct(c) != 0; + break; + case 's': + res = std::isspace(c) != 0; + break; + case 'u': + res = std::isupper(c) != 0; + break; + case 'w': + res = std::isalnum(c) != 0; + break; + case 'x': + res = std::isxdigit(c) != 0; + break; + case 'z': + res = (c == 0); + break;// Lua 5.1/5.5 保留类:零字节 + default: + return cl == c; } return std::islower(cl) ? res : !res; } diff --git a/src/native/string/lua_pattern.h b/src/native/string/lua_pattern.h index 0d8d367a..e6238acb 100644 --- a/src/native/string/lua_pattern.h +++ b/src/native/string/lua_pattern.h @@ -27,8 +27,8 @@ struct Capture { struct MatchResult { const char *begin = nullptr; - const char *end = nullptr; // 尾后指针(零宽匹配时 end == begin) - int level = 0; // 显式捕获个数 + const char *end = nullptr;// 尾后指针(零宽匹配时 end == begin) + int level = 0; // 显式捕获个数 Capture caps[kMaxCaptures]{}; }; diff --git a/src/native/string/native_string.cpp b/src/native/string/native_string.cpp index df39e57a..0bb538fa 100644 --- a/src/native/string/native_string.cpp +++ b/src/native/string/native_string.cpp @@ -162,23 +162,33 @@ static size_t ParsePackAlignOption(const char *&p, const char *end) { switch (c) { case 'b': case 'B': - case 'x': return 1; + case 'x': + return 1; case 'h': - case 'H': return sizeof(short); + case 'H': + return sizeof(short); case 'l': - case 'L': return sizeof(long); + case 'L': + return sizeof(long); case 'j': - case 'J': return sizeof(int64_t); - case 'T': return sizeof(size_t); - case 'f': return sizeof(float); - case 'd': return sizeof(double); + case 'J': + return sizeof(int64_t); + case 'T': + return sizeof(size_t); + case 'f': + return sizeof(float); + case 'd': + return sizeof(double); case 'i': - case 'I': return static_cast(ParsePackIntegralSize(p, end)); + case 'I': + return static_cast(ParsePackIntegralSize(p, end)); case 'c': - (void)ParsePackDecSize(p, end, 1, kMaxPackBlock, 0); + (void) ParsePackDecSize(p, end, 1, kMaxPackBlock, 0); + return 1; + case 'z': return 1; - case 'z': return 1; - default: ThrowFakeluaException("invalid next option for option 'X'"); + default: + ThrowFakeluaException("invalid next option for option 'X'"); } } @@ -1408,8 +1418,7 @@ void RegisterStringLibraryApi(State *s) { // Lua 模式匹配:在原串上从 init_pos-1 起扫描(位置 1-based 由 BuildFindResult 处理)。 // 非法模式直接抛错(与 Lua 的 malformed pattern 一致)。 lua_pattern::MatchResult m; - if (!lua_pattern::Search(sv.data(), static_cast(len), pat_view.data(), pat_view.size(), - static_cast(init_pos - 1), m)) { + if (!lua_pattern::Search(sv.data(), static_cast(len), pat_view.data(), pat_view.size(), static_cast(init_pos - 1), m)) { return inter::NativeToFakeluaNil(state); } return BuildFindResult(state, sv.data(), m); @@ -1447,8 +1456,7 @@ void RegisterStringLibraryApi(State *s) { lua_pattern::MatchResult m; // Search 内部会处理前导 '^' 的锚定;无 '^' 时从 init 起逐位搜索。 - if (!lua_pattern::Search(sv.data(), static_cast(len), pat_view.data(), pat_view.size(), - static_cast(init_pos - 1), m)) { + if (!lua_pattern::Search(sv.data(), static_cast(len), pat_view.data(), pat_view.size(), static_cast(init_pos - 1), m)) { return inter::NativeToFakeluaNil(state); } if (m.level > 0) { @@ -1531,8 +1539,8 @@ void RegisterStringLibraryApi(State *s) { std::string result; result.reserve(slen); - size_t copied = 0; // 已拷进 result 的原文位置 - size_t src = 0; // 下一次搜索起点 + size_t copied = 0; // 已拷进 result 的原文位置 + size_t src = 0; // 下一次搜索起点 size_t prev_end = std::numeric_limits::max();// 上一次产出匹配的尾后位置 int64_t count = 0; @@ -1573,8 +1581,7 @@ void RegisterStringLibraryApi(State *s) { VarClosure *cl = repl_var.data_.cl; const int call_arg_count = (m.level > 0) ? m.level : 1; if (call_arg_count > static_cast(kMaxFunctionInputParams)) { - ThrowFakeluaException(std::format("string.gsub: too many capture arguments ({}), max is {}", - call_arg_count, kMaxFunctionInputParams)); + ThrowFakeluaException(std::format("string.gsub: too many capture arguments ({}), max is {}", call_arg_count, kMaxFunctionInputParams)); } std::vector call_args(static_cast(call_arg_count)); if (m.level > 0) { @@ -1585,11 +1592,9 @@ void RegisterStringLibraryApi(State *s) { call_args[0] = inter::NativeToFakeluaStringView(state, whole); } // 在发起 gsub 的引擎里同步调用替换函数(与 pool:with 同理)。 - CVar fn_res = (cl->func_ptr != nullptr) - ? inter::DispatchCallClosure(state, cl, call_args.data(), call_arg_count, state->CurrentJit()) - : FlEvalLoadClosure(state, cl, call_arg_count, call_args.data()); - if (fn_res.type_ == static_cast(VarType::Nil) || - (fn_res.type_ == static_cast(VarType::Bool) && !fn_res.data_.b)) { + CVar fn_res = (cl->func_ptr != nullptr) ? inter::DispatchCallClosure(state, cl, call_args.data(), call_arg_count, state->CurrentJit()) + : FlEvalLoadClosure(state, cl, call_arg_count, call_args.data()); + if (fn_res.type_ == static_cast(VarType::Nil) || (fn_res.type_ == static_cast(VarType::Bool) && !fn_res.data_.b)) { keep_original = true; } else if (fn_res.type_ == static_cast(VarType::Bool)) { ThrowFakeluaException("invalid replacement value (a boolean)"); @@ -1612,8 +1617,7 @@ void RegisterStringLibraryApi(State *s) { std::string key(whole); val = table::TableHelper::GetTableStrId(state, repl_var, key.c_str()); } - if (val.type_ == static_cast(VarType::Nil) || - (val.type_ == static_cast(VarType::Bool) && !val.data_.b)) { + if (val.type_ == static_cast(VarType::Nil) || (val.type_ == static_cast(VarType::Bool) && !val.data_.b)) { keep_original = true; } else if (val.type_ == static_cast(VarType::Bool) || val.type_ == static_cast(VarType::Table)) { ThrowFakeluaException("invalid replacement value (a boolean)"); @@ -1973,9 +1977,7 @@ void RegisterStringLibraryApi(State *s) { } // 对齐处理 - auto align_up = [&](size_t item_size) { - result.append(PackPadding(result.size(), pm.align, item_size), '\0'); - }; + auto align_up = [&](size_t item_size) { result.append(PackPadding(result.size(), pm.align, item_size), '\0'); }; if (c == 'i' || c == 'I') { bool is_unsigned = (c == 'I'); @@ -2266,9 +2268,7 @@ void RegisterStringLibraryApi(State *s) { continue; } - auto align_up = [&](size_t item_size) { - pos += PackPadding(pos - init_pos, pm.align, item_size); - }; + auto align_up = [&](size_t item_size) { pos += PackPadding(pos - init_pos, pm.align, item_size); }; auto check_available = [&](size_t need) -> bool { return pos + need <= data_len; }; diff --git a/src/native/yaml/native_yaml.cpp b/src/native/yaml/native_yaml.cpp index 519b8053..c87f01c6 100644 --- a/src/native/yaml/native_yaml.cpp +++ b/src/native/yaml/native_yaml.cpp @@ -148,12 +148,15 @@ static CVar NodeToLua(State *s, const YAML::Node &node, int depth, std::vector &stack; + ~PopGuard() { stack.pop_back(); } } guard{stack}; + if (node.IsSequence()) { CVar tbl = table::TableHelper::CreateTable(s); size_t idx = 1; diff --git a/src/state/const_string.cpp b/src/state/const_string.cpp index f1d6e0d7..c4c945cf 100644 --- a/src/state/const_string.cpp +++ b/src/state/const_string.cpp @@ -15,7 +15,7 @@ int64_t ConstString::Alloc(const std::string_view &str) { auto ptr = static_cast(s_->GetHeap().GetAllocator(true).Alloc(sizeof(VarString) + str.size())); new (ptr) VarString(str); - (void)ptr->Hash(); + (void) ptr->Hash(); auto id = reinterpret_cast(ptr); auto key = ptr->Str(); diff --git a/src/state/fakelua.cpp b/src/state/fakelua.cpp index fa963231..6b074125 100644 --- a/src/state/fakelua.cpp +++ b/src/state/fakelua.cpp @@ -612,12 +612,38 @@ static CVar DispatchCallRaw(void *addr, const CVar *arg_arr, int arg_count, VarC DCASE(3) DCASE(4) DCASE(5) - DCASE(6) DCASE(7) DCASE(8) DCASE(9) DCASE(10) DCASE(11) DCASE(12) DCASE(13) DCASE(14) DCASE(15) DCASE(16) DCASE(17) DCASE(18) DCASE(19) DCASE(20) DCASE(21) DCASE(22) DCASE(23) DCASE(24) - DCASE(25) DCASE(26) DCASE(27) DCASE(28) DCASE(29) DCASE(30) DCASE(31) DCASE(32) + DCASE(6) + DCASE(7) + DCASE(8) + DCASE(9) + DCASE(10) + DCASE(11) + DCASE(12) + DCASE(13) + DCASE(14) + DCASE(15) + DCASE(16) + DCASE(17) + DCASE(18) + DCASE(19) + DCASE(20) + DCASE(21) + DCASE(22) + DCASE(23) + DCASE(24) + DCASE(25) + DCASE(26) + DCASE(27) + DCASE(28) + DCASE(29) + DCASE(30) + DCASE(31) + DCASE(32) #undef DCASE #include "util/dispatch_macro_undef.h" - default : ThrowFakeluaException(std::format("DispatchCall: arg_count {} out of range", arg_count)); + default: + ThrowFakeluaException(std::format("DispatchCall: arg_count {} out of range", arg_count)); } __builtin_unreachable(); } diff --git a/src/state/heap.cpp b/src/state/heap.cpp index 66c8f672..31b444e9 100644 --- a/src/state/heap.cpp +++ b/src/state/heap.cpp @@ -98,4 +98,4 @@ size_t HeapAllocator::Size() const { return current_block_index_ * BLOCK_SIZE + current_block_offset_; } -} // namespace fakelua +}// namespace fakelua diff --git a/src/state/heap.h b/src/state/heap.h index c7c86497..2fb359ed 100644 --- a/src/state/heap.h +++ b/src/state/heap.h @@ -15,7 +15,9 @@ struct FlHeapSlot { class HeapAllocator { public: - HeapAllocator(FlHeapSlot *slot = nullptr) : slot_(slot) {} + HeapAllocator(FlHeapSlot *slot = nullptr) : slot_(slot) { + } + ~HeapAllocator(); // 分配一块连续的临时内存,分配后的指针地址不会变化 @@ -44,7 +46,7 @@ class HeapAllocator { static constexpr size_t BLOCK_SIZE = 1024UL * 1024UL;// 每块内存的大小,默认1MB static_assert(BLOCK_SIZE % alignof(std::max_align_t) == 0, "BLOCK_SIZE must be a multiple of max alignment"); - FlHeapSlot *slot_ = nullptr; // 同步更新给 JIT 的快速分配槽 + FlHeapSlot *slot_ = nullptr; // 同步更新给 JIT 的快速分配槽 std::vector blocks_; // 大块内存的地址 size_t current_block_index_ = 0; // 当前使用的大块内存索引 size_t current_block_offset_ = 0; // 当前使用的大块内存偏移 @@ -54,8 +56,8 @@ class HeapAllocator { // 从大块内存中切出一小块内存使用,释放的时候一口气全释放 class Heap { public: - Heap(FlHeapSlot *temp_slot = nullptr, FlHeapSlot *const_slot = nullptr) - : temp_allocator_(temp_slot), const_allocator_(const_slot) {} + Heap(FlHeapSlot *temp_slot = nullptr, FlHeapSlot *const_slot = nullptr) : temp_allocator_(temp_slot), const_allocator_(const_slot) { + } HeapAllocator &GetAllocator(bool is_const) { if (is_const) { diff --git a/src/state/state.cpp b/src/state/state.cpp index 91962731..1bb8834d 100644 --- a/src/state/state.cpp +++ b/src/state/state.cpp @@ -2,6 +2,7 @@ #include "fakelua.h" #include "jit/tcc_jit.h" #include "native/compress/native_compress.h" +#include "native/container/native_container.h" #include "native/crypto/native_crypto.h" #include "native/csv/native_csv.h" #include "native/event/native_event.h" @@ -17,15 +18,14 @@ #include "native/process/native_process.h" #include "native/protobuf/native_protobuf.h" #include "native/random/native_random.h" -#include "native/container/native_container.h" #include "native/redis/native_redis.h" #include "native/runtime/native_runtime.h" #include "native/serialize/native_serialize.h" #include "native/sqlite/native_sqlite.h" #include "native/timer/native_timer.h" #include "native/toml/native_toml.h" -#include "native/utf8/native_utf8.h" #include "native/url/native_url.h" +#include "native/utf8/native_utf8.h" #include "native/xml/native_xml.h" #include "native/yaml/native_yaml.h" #include "util/logging.h" @@ -50,8 +50,7 @@ void State::SetLogFile(const std::string &path, size_t max_size, size_t max_file } } -State::State(const StateConfig &config) - : config_(config), compiler_(this), heap_(&temp_heap_slot_, &const_heap_slot_), const_string_(this) { +State::State(const StateConfig &config) : config_(config), compiler_(this), heap_(&temp_heap_slot_, &const_heap_slot_), const_string_(this) { utf8_io::Init(); log_level_ = config_.log_level; if (!config_.log_file.empty()) { diff --git a/src/state/state.h b/src/state/state.h index 3b0bfe72..b39ba50a 100644 --- a/src/state/state.h +++ b/src/state/state.h @@ -31,6 +31,7 @@ struct LuaLoc { const char *file = nullptr; int line = 0; }; + static_assert(std::is_standard_layout_v); static_assert(offsetof(LuaLoc, line) == sizeof(const char *)); @@ -120,7 +121,7 @@ class State { } const auto idx = static_cast(jit_type); if (idx < JIT_MAX) { - for (auto *existing : lua_locs_[idx]) { + for (auto *existing: lua_locs_[idx]) { if (existing == loc) { return; } @@ -176,9 +177,11 @@ class State { explicit ConstAllocScope(State *s) : s_(s), prev_(s->InterpConstAlloc()) { s_->SetInterpConstAlloc(true); } + ~ConstAllocScope() { s_->SetInterpConstAlloc(prev_); } + ConstAllocScope(const ConstAllocScope &) = delete; ConstAllocScope &operator=(const ConstAllocScope &) = delete; @@ -207,9 +210,11 @@ class State { explicit JitContextScope(State *s, JITType jit) : s_(s), prev_(s->current_jit_) { s_->current_jit_ = jit; } + ~JitContextScope() { s_->current_jit_ = prev_; } + JitContextScope(const JitContextScope &) = delete; JitContextScope &operator=(const JitContextScope &) = delete; diff --git a/src/util/exception.h b/src/util/exception.h index 8dc57a99..9f8a2e41 100644 --- a/src/util/exception.h +++ b/src/util/exception.h @@ -28,12 +28,15 @@ class FakeluaException : public std::runtime_error { [[nodiscard]] const std::string &Msg() const noexcept { return parts_.msg; } + [[nodiscard]] const std::string &Loc() const noexcept { return parts_.loc; } + [[nodiscard]] const std::string &Stack() const noexcept { return parts_.stack; } + [[nodiscard]] bool HasLoc() const noexcept { return !parts_.loc.empty(); } diff --git a/src/util/file_util.cpp b/src/util/file_util.cpp index b5565c15..a6f186af 100644 --- a/src/util/file_util.cpp +++ b/src/util/file_util.cpp @@ -1,9 +1,9 @@ #include "common.h" #include -#include #include #include +#include #include #if defined(_WIN32) #include @@ -13,7 +13,7 @@ namespace fakelua { -std::string GenerateTmpFilename(const std::string &head, const std::string &tail) { +std::string GetTmpDirectory() { boost::system::error_code ec; auto tmpdir = boost::filesystem::temp_directory_path(ec); if (ec) { @@ -21,7 +21,11 @@ std::string GenerateTmpFilename(const std::string &head, const std::string &tail } tmpdir /= "fakelua"; boost::filesystem::create_directories(tmpdir, ec); + return tmpdir.string(); +} +std::string GenerateTmpFilename(const std::string &head, const std::string &tail) { + const boost::filesystem::path tmpdir(GetTmpDirectory()); static std::atomic seq{0}; #if defined(_WIN32) const int pid = _getpid(); diff --git a/src/util/file_util.h b/src/util/file_util.h index 652b0c44..b774090e 100644 --- a/src/util/file_util.h +++ b/src/util/file_util.h @@ -2,6 +2,9 @@ namespace fakelua { +// Shared by transient JIT workspaces and the default filesystem cache. +std::string GetTmpDirectory(); + std::string GenerateTmpFilename(const std::string &head, const std::string &tail); -} +}// namespace fakelua diff --git a/src/util/number_util.h b/src/util/number_util.h index cdf4e01b..ced34288 100644 --- a/src/util/number_util.h +++ b/src/util/number_util.h @@ -64,8 +64,8 @@ inline int CompareInt64Double(int64_t integer, double floating) { if (std::isnan(floating)) { return 0; } - constexpr double kInt64LowerBound = -9223372036854775808.0; // -2^63 - constexpr double kInt64UpperBound = 9223372036854775808.0; // 2^63 + constexpr double kInt64LowerBound = -9223372036854775808.0;// -2^63 + constexpr double kInt64UpperBound = 9223372036854775808.0; // 2^63 if (floating >= kInt64UpperBound) { return -1; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ad244d54..f8d123f0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -78,6 +78,9 @@ ELSE () ENDIF () add_executable(unit_tests ${TEST_SRC_LIST}) +if (USE_COV) + target_compile_definitions(unit_tests PRIVATE FAKELUA_TEST_NO_GCC_CACHE=1) +endif () # dl is only available on Linux. macOS and Windows don't need it. if (NOT WIN32 AND NOT APPLE) diff --git a/test/test_basic.cpp b/test/test_basic.cpp index d67671b0..41dce4d4 100644 --- a/test/test_basic.cpp +++ b/test/test_basic.cpp @@ -1,11 +1,11 @@ #include #include -#include "fakelua.h" -#include "test_jit.h" #include "compile/compiler.h" #include "compile/preprocessor.h" #include "compile/semantic_analysis.h" +#include "fakelua.h" +#include "test_jit.h" using namespace fakelua; @@ -1420,7 +1420,6 @@ TEST(test_basic, heap_offset_sync_string_keys_and_rehash) { FakeluaDeleteState(s); } - // 寄存器高峰门禁:SemanticAnalysis::CheckRegisterLimits 在 Analyze 中按与 InterpCodegen // 相同的 AllocLocal/AllocReg/FreeTo 寿命规则检查,与 JIT_INTERP 的 Inst::a/b/c(uint8) 对齐。 // 计量 = 高峰并发槽(形参 + AllocLocal + AllocReg 临时)。 @@ -1492,7 +1491,6 @@ TEST(test_basic, reg_limit_many_constants_allowed) { FakeluaDeleteState(s); } - // Exact 256 vs 257 AllocLocal slots (indices 0..255 vs 第 257 次 AllocLocal 失败)。 // 无公开 introspection:靠「256 个 local + return u256」与「257 个 local」相邻证明槽位上限。 TEST(test_basic, reg_limit_exact_256_ok) { @@ -1533,12 +1531,8 @@ TEST(test_basic, reg_limit_exact_257_over_all_gates) { // 不生成字节码、不创建原型/常量串、不注册 VM、不跑 init。 TEST(test_basic, reg_limit_check_without_state) { const char *accepted[] = { - "./basic/test_reg_limit_ok.lua", - "./basic/test_reg_limit_exact_ok.lua", - "./basic/test_reg_limit_many_constants.lua", - "./basic/test_init_load_table.lua", - "./closure/test_closure_nested.lua", - "./basic/test_basic_pairs.lua", + "./basic/test_reg_limit_ok.lua", "./basic/test_reg_limit_exact_ok.lua", "./basic/test_reg_limit_many_constants.lua", + "./basic/test_init_load_table.lua", "./closure/test_closure_nested.lua", "./basic/test_basic_pairs.lua", }; for (const char *file: accepted) { SCOPED_TRACE(file); @@ -1554,9 +1548,9 @@ TEST(test_basic, reg_limit_check_without_state) { ASSERT_NO_THROW(ar = semantic.Analyze(pr, {})); } const char *rejected[] = { - "./basic/test_reg_limit_over.lua", - "./basic/test_reg_limit_exact_over.lua", - "./basic/test_reg_limit_nested_over.lua", + "./basic/test_reg_limit_over.lua", + "./basic/test_reg_limit_exact_over.lua", + "./basic/test_reg_limit_nested_over.lua", }; for (const char *file: rejected) { SCOPED_TRACE(file); @@ -1578,12 +1572,17 @@ TEST(test_basic, reg_limit_check_without_state) { } TEST(test_basic, reg_limit_jit_only_execution) { - struct Case { const char *file; const char *fn; double expected; }; + struct Case { + const char *file; + const char *fn; + double expected; + }; + const Case cases[] = { - {"./basic/test_reg_limit_ok.lua", "test_reg_limit", 27500}, - {"./basic/test_reg_limit_exact_ok.lua", "test_reg_limit_exact", 256}, - {"./basic/test_reg_limit_many_constants.lua", "test_reg_limit_many_constants", 301}, - {"./basic/test_init_load_table.lua", "InitLoadTable.test_init_load_table", 1}, + {"./basic/test_reg_limit_ok.lua", "test_reg_limit", 27500}, + {"./basic/test_reg_limit_exact_ok.lua", "test_reg_limit_exact", 256}, + {"./basic/test_reg_limit_many_constants.lua", "test_reg_limit_many_constants", 301}, + {"./basic/test_init_load_table.lua", "InitLoadTable.test_init_load_table", 1}, }; CompileConfig cfg{}; cfg.disable_jit[JIT_INTERP] = true; diff --git a/test/test_common.cpp b/test/test_common.cpp index 3411c6e1..c054360d 100644 --- a/test/test_common.cpp +++ b/test/test_common.cpp @@ -6,10 +6,10 @@ #include #include +#include #include #include #include -#include namespace { struct UnbufferedStdio { @@ -387,8 +387,7 @@ TEST(common, c_runtime_cov_matches_kCRuntimeHeader) { oss << ifs.rdbuf(); const std::string file_body = oss.str(); const std::string expected = kCRuntimeHeader[0] == '\n' ? std::string(kCRuntimeHeader + 1) : std::string(kCRuntimeHeader); - EXPECT_EQ(file_body, expected) << "Edit kCRuntimeHeader and sync " << kCRuntimeCovPath - << " (or the reverse); Codecov #line mapping depends on it."; + EXPECT_EQ(file_body, expected) << "Edit kCRuntimeHeader and sync " << kCRuntimeCovPath << " (or the reverse); Codecov #line mapping depends on it."; } namespace { @@ -627,7 +626,7 @@ TEST(common, compile_number_key_classify) { auto min_neg = CovMakeUnaryMinus(CovMakeNumberExp(std::to_string(std::numeric_limits::min()))); // May classify as float overflow path; just exercise the branch. - (void)ClassifyConstNumberExp(min_neg, kind, canon, iv, fv); + (void) ClassifyConstNumberExp(min_neg, kind, canon, iv, fv); auto two63 = CovMakeUnaryMinus(CovMakeNumberExp("9223372036854775808.0")); EXPECT_TRUE(ClassifyConstNumberExp(two63, kind, canon, iv, fv)); diff --git a/test/test_exception.cpp b/test/test_exception.cpp index 7b4f989d..5cc15270 100644 --- a/test/test_exception.cpp +++ b/test/test_exception.cpp @@ -5,6 +5,7 @@ #include "util/string_util.h" #include "gtest/gtest.h" #include +#include using namespace fakelua; @@ -1039,11 +1040,12 @@ TEST(exception, undeclared_var_error) { const char *name; int line; }; + const Case cases[] = { - {"./exception/test_undeclared_var_read_error.lua", "unknown_global", 2}, - {"./exception/test_undeclared_var_write_read_error.lua", "flag", 2}, - {"./exception/test_undeclared_field_base_error.lua", "unknown_object", 2}, - {"./exception/test_no_define_lvalue_error.lua", "b", 2}, + {"./exception/test_undeclared_var_read_error.lua", "unknown_global", 2}, + {"./exception/test_undeclared_var_write_read_error.lua", "flag", 2}, + {"./exception/test_undeclared_field_base_error.lua", "unknown_object", 2}, + {"./exception/test_no_define_lvalue_error.lua", "b", 2}, }; for (const auto &c: cases) { SCOPED_TRACE(c.file); @@ -1305,8 +1307,7 @@ TEST(exception, vararg_nested_function) { // 通过 FunctionDef / LocalFunction 捕获外层词法 const 时仍禁止赋值; // 同名 local 遮蔽后允许赋值;块作用域遮蔽结束后外层 const 仍受保护。 TEST(exception, const_assign_through_nested_functions) { - for (const char *file: {"./exception/test_const_assign_funcdef.lua", "./exception/test_const_assign_localfunction.lua", - "./exception/test_const_assign_block_scope.lua"}) { + for (const char *file: {"./exception/test_const_assign_funcdef.lua", "./exception/test_const_assign_localfunction.lua", "./exception/test_const_assign_block_scope.lua"}) { SCOPED_TRACE(file); FakeluaStateGuard sg; auto s = sg.GetState(); @@ -1626,17 +1627,8 @@ TEST(exception, const_table_modify_error) { CompileFile(s, "./exception/test_const_table_modify_error.lua", {}); const char *throw_fns[] = { - "test_modify_const", - "test_sort_const", - "test_sort_single_const", - "test_move_into_const", - "test_move_empty_into_const", - "test_insert_const", - "test_insert_float_pos_const", - "test_remove_const", - "test_remove_oob_valid_const", - "test_modify_packed_const", - "test_copy_const_tbl_to_table_modify", + "test_modify_const", "test_sort_const", "test_sort_single_const", "test_move_into_const", "test_move_empty_into_const", "test_insert_const", + "test_insert_float_pos_const", "test_remove_const", "test_remove_oob_valid_const", "test_modify_packed_const", "test_copy_const_tbl_to_table_modify", }; for (auto jit_type: AllJitTypes()) { @@ -1738,14 +1730,15 @@ TEST(exception, runtime_error_carries_lua_location) { const char *fn; const char *location; }; + const Case cases[] = { - {"arith_on_nil", "test_error_location.lua:6"}, - {"index_non_table", "test_error_location.lua:11"}, - {"call_non_function", "test_error_location.lua:16"}, - // 原生函数里抛的错也要定位到调用它的那条 Lua 语句 - {"native_bad_argument", "test_error_location.lua:22"}, - // 同一条语句中函数调用返回后,后续表达式出错仍必须定位到本语句 - {"error_after_callee_returns", "test_error_location.lua:46"}, + {"arith_on_nil", "test_error_location.lua:6"}, + {"index_non_table", "test_error_location.lua:11"}, + {"call_non_function", "test_error_location.lua:16"}, + // 原生函数里抛的错也要定位到调用它的那条 Lua 语句 + {"native_bad_argument", "test_error_location.lua:22"}, + // 同一条语句中函数调用返回后,后续表达式出错仍必须定位到本语句 + {"error_after_callee_returns", "test_error_location.lua:46"}, }; for (const auto jit_type: AllJitTypes()) { for (const auto &c: cases) { @@ -1853,3 +1846,63 @@ TEST(exception, runtime_error_location_tail_call_deep_interp) { EXPECT_NE(msg.find("test_error_location_tail.lua:6"), std::string::npos) << msg; } } + +TEST(exception, gcc_cache_preserves_each_string_input_debug_filename) { + StateConfig state_cfg; + state_cfg.log_level = LogLevel::Off; + FakeluaStateGuard first(state_cfg), second(state_cfg); + CompileConfig cfg; + DisableAllJit(cfg); + cfg.disable_jit[JIT_GCC] = false; + cfg.debug_mode = false; + const std::string source = "function cached_failure()\n local value=nil\n return value.missing\nend\n"; + Compiler first_compiler(first.GetState()), second_compiler(second.GetState()); + const auto a = first_compiler.CompileString(source, cfg); + const auto b = second_compiler.CompileString(source, cfg); + ASSERT_NE(a.file_name, b.file_name); + for (auto [state, own, other]: {std::tuple{first.GetState(), a.file_name, b.file_name}, std::tuple{second.GetState(), b.file_name, a.file_name}}) { + try { + int64_t result = 0; + Call(state, JIT_GCC, "cached_failure", result); + FAIL() << "expected runtime error"; + } catch (const std::exception &e) { + const std::string message = e.what(); + EXPECT_NE(message.find(own + ":3"), std::string::npos) << message; + EXPECT_EQ(message.find(other), std::string::npos) << message; + } + } +} + +TEST(exception, gcc_cache_failed_init_same_state_retry_all_backends) { + const std::string source = R"( + local text = retry_init_seed() + function retry_init_read() return text end + )"; + for (auto type: {JIT_TCC, JIT_GCC, JIT_INTERP}) { + for (bool debug: {false, true}) { + SCOPED_TRACE(std::format("backend={} debug={}", static_cast(type), debug)); + StateConfig config; + config.log_level = LogLevel::Off; + FakeluaStateGuard state(config); + auto *s = state.GetState(); + bool fail = true; + int calls = 0; + RegisterNativeFunction(s, "retry_init_seed", false, std::function([&](State *) { + ++calls; + if (fail) throw std::runtime_error("expected initialization failure"); + return std::string("ok after retry"); + })); + CompileConfig cfg; + DisableAllJit(cfg); + cfg.disable_jit[type] = false; + cfg.debug_mode = debug; + EXPECT_THROW(CompileString(s, source, cfg), std::exception); + fail = false; + CompileString(s, source, cfg); + std::string value; + Call(s, type, "retry_init_read", value); + EXPECT_EQ(value, "ok after retry"); + EXPECT_EQ(calls, 2); + } + } +} diff --git a/test/test_flua_host.cpp b/test/test_flua_host.cpp index d0c95c12..74d8234f 100644 --- a/test/test_flua_host.cpp +++ b/test/test_flua_host.cpp @@ -128,7 +128,7 @@ std::filesystem::path WriteScript(const std::filesystem::path &dir, const char * return path; } -} // namespace +}// namespace TEST(flua_host, set_exit_code_after_frames) { const auto flua = FluaBinary(); diff --git a/test/test_jitter.cpp b/test/test_jitter.cpp index fbceeecd..c2d9f9d7 100644 --- a/test/test_jitter.cpp +++ b/test/test_jitter.cpp @@ -1,14 +1,27 @@ #include "compile/compiler.h" #include "fakelua.h" +#include "jit/gcc_cache.h" #include "state/const_string.h" #include "state/state.h" +#include "test_jit.h" +#include "util/file_util.h" #include "var/var_multi.h" #include "var/var_string.h" #include "var/var_type.h" #include "gtest/gtest.h" -#include "test_jit.h" +#include #include +#include +#include #include +#include +#if defined(_WIN32) +#define NOMINMAX +#include +#else +#include +#include +#endif using namespace fakelua; @@ -3151,3 +3164,972 @@ TEST(jitter, tcc_libraries_config_path) { EXPECT_THROW(CompileString(s, "function tcc_lib_cov()\n return 3\nend\n", ccfg), std::exception); FakeluaDeleteState(s); } + +namespace { +// MinGW libstdc++ remove_all can livelock in recursive_directory_iterator::__erase +// when a child is open or delete-pending and fs::remove returns failure without an +// error_code (while (!ec) retries the same entry). External cleanup in these tests +// therefore uses a one-pass, share-delete/POSIX unlink that never retries. +void RemoveTreeBestEffort(const std::filesystem::path &path) { +#if defined(_WIN32) + WIN32_FILE_ATTRIBUTE_DATA attrs{}; + if (!GetFileAttributesExW(path.wstring().c_str(), GetFileExInfoStandard, &attrs)) return; + if (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + WIN32_FIND_DATAW data{}; + HANDLE find = FindFirstFileW((path / "*").wstring().c_str(), &data); + if (find != INVALID_HANDLE_VALUE) { + do { + if (data.cFileName[0] == L'.' && (data.cFileName[1] == L'\0' || (data.cFileName[1] == L'.' && data.cFileName[2] == L'\0'))) continue; + RemoveTreeBestEffort(path / data.cFileName); + } while (FindNextFileW(find, &data)); + FindClose(find); + } + } + HANDLE handle = CreateFileW(path.wstring().c_str(), DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + if (handle == INVALID_HANDLE_VALUE) return; + struct DispositionEx { + DWORD Flags; + } posix{0x00000001 | 0x00000002};// FILE_DISPOSITION_FLAG_DELETE | POSIX_SEMANTICS + if (!SetFileInformationByHandle(handle, static_cast(21), &posix, sizeof(posix))) { + FILE_DISPOSITION_INFO info{}; + info.DeleteFile = TRUE; + SetFileInformationByHandle(handle, FileDispositionInfo, &info, sizeof(info)); + } + CloseHandle(handle); +#else + std::error_code ec; + std::filesystem::remove_all(path, ec); +#endif +} + +struct JitCacheTestDir { + std::filesystem::path path = GenerateTmpFilename("jit_cache_test_", ".dir"); + + JitCacheTestDir() { + std::filesystem::create_directory(path); + std::filesystem::permissions(path, std::filesystem::perms::owner_all); + } + + ~JitCacheTestDir() { RemoveTreeBestEffort(path); } + + StateConfig Config() const { + StateConfig cfg; + cfg.log_level = LogLevel::Off; + cfg.gcc_config.cache_directory = path.string(); + return cfg; + } + + std::vector Entries() const { + std::vector entries; + for (const auto &entry: std::filesystem::directory_iterator(path)) + if (entry.is_directory() && std::filesystem::exists(entry.path() / "image")) entries.push_back(entry.path()); + return entries; + } +}; + +CompileConfig CacheCompileConfig() { + CompileConfig cfg; + cfg.debug_mode = false; + DisableAllJit(cfg); + cfg.disable_jit[JIT_GCC] = false; + return cfg; +} + +const std::string kCacheSource = R"( +local text = "a\0\255\"\\\n中文" +local config = { value = 17, name = "shared" } +function cache_value(n) return config.value + n end +function cache_string() return text end +function cache_lookup(k) local t = {name="shared", value=23} return t[k] end +function cache_closure(n) return function(x) return n+x end end +function cache_run(n) local f=cache_closure(n) return f(3) end +)"; + +void CheckCacheState(State *s) { + int64_t n = 0; + Call(s, JIT_GCC, "cache_value", n, 4); + EXPECT_EQ(n, 21); + Call(s, JIT_GCC, "cache_run", n, 9); + EXPECT_EQ(n, 12); + std::string text; + Call(s, JIT_GCC, "cache_string", text); + EXPECT_EQ(text, std::string("a\0\xff\"\\\n中文", 12)); + Call(s, JIT_GCC, "cache_lookup", text, std::string("name")); + EXPECT_EQ(text, "shared"); +} +}// namespace +#if defined(FAKELUA_TEST_NO_GCC_CACHE) +TEST(jitter, gcc_cache_coverage_bypass) { + JitCacheTestDir dir; + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), kCacheSource, CacheCompileConfig()); + CheckCacheState(s.GetState()); + EXPECT_TRUE(dir.Entries().empty()); +} +#else +namespace { +struct CacheTestEnvironment { + struct Value { + std::string name, value; + bool present; + }; + + std::vector values; + + void Set(const char *name, const std::string &value) { + const char *old = std::getenv(name); + values.push_back({name, old ? old : "", old != nullptr}); + SetTestEnv(name, value.c_str()); + } + + ~CacheTestEnvironment() { + for (auto it = values.rbegin(); it != values.rend(); ++it) SetTestEnv(it->name.c_str(), it->present ? it->value.c_str() : nullptr); + } +}; + +struct CacheArtifactFixture { + JitCacheTestDir dir; + std::string source = (dir.path / "input.c").string(); + std::string binary = (dir.path / "output.so").string(); + std::string deps = (dir.path / "input.d").string(); + std::string restored = (dir.path / "restored.so").string(); +#if defined(__APPLE__) + const char *compiler = "clang"; +#else + const char *compiler = "gcc"; +#endif + CacheArtifactFixture() { + std::ofstream(source) << "source"; + std::ofstream(binary) << "test artifact bytes"; + // Use only the basename; the source itself is excluded from dependencies. + std::string escaped; + for (char c: source) { + if (c == ' ' || c == '#' || c == '\\') escaped += '\\'; + if (c == '$') escaped += '$'; + escaped += c; + } + std::ofstream(deps) << "flua: " << escaped << "\n"; + } + + void Publish(const std::string &key, size_t limit) { + auto cfg = dir.Config().gcc_config; + cfg.cache_max_entries = limit; + GccCache cache(cfg, compiler, key, false); + cache.Publish(binary, deps, source); + } + + bool Restore(const std::string &key, size_t limit) { + auto cfg = dir.Config().gcc_config; + cfg.cache_max_entries = limit; + GccCache cache(cfg, compiler, key, false); + return cache.Restore(restored); + } +}; +}// namespace + +TEST(jitter, gcc_cache_default_temp_root_and_custom_override) { + JitCacheTestDir temp; + CacheTestEnvironment env; + for (auto name: {"TMPDIR", "TMP", "TEMP"}) env.Set(name, temp.path.string()); + env.Set("XDG_CACHE_HOME", (temp.path / "unused-xdg").string()); + env.Set("LOCALAPPDATA", (temp.path / "unused-localappdata").string()); + auto root = temp.path / "fakelua" / "cache"; +#if !defined(_WIN32) + root /= "uid-" + std::to_string(getuid()); +#endif + EXPECT_EQ(std::filesystem::path(GetTmpDirectory()), temp.path / "fakelua"); + StateConfig config; + config.log_level = LogLevel::Off; + { + FakeluaStateGuard s(config); + CompileString(s.GetState(), "function cache_default_path() return 37 end", CacheCompileConfig()); + int64_t n = 0; + Call(s.GetState(), JIT_GCC, "cache_default_path", n); + EXPECT_EQ(n, 37); + } + ASSERT_TRUE(std::filesystem::exists(root)); + size_t entries = 0; + for (const auto &item: std::filesystem::directory_iterator(root)) + if (item.is_directory() && std::filesystem::exists(item.path() / "image")) ++entries; + EXPECT_EQ(entries, 1u); +#if !defined(_WIN32) + EXPECT_EQ(std::filesystem::status(root).permissions() & std::filesystem::perms::all, std::filesystem::perms::owner_all); +#endif + EXPECT_FALSE(std::filesystem::exists(temp.path / "unused-xdg")); + EXPECT_FALSE(std::filesystem::exists(temp.path / "unused-localappdata")); + JitCacheTestDir custom; + { + FakeluaStateGuard s(custom.Config()); + CompileString(s.GetState(), "function cache_default_path() return 37 end", CacheCompileConfig()); + } + EXPECT_EQ(custom.Entries().size(), 1u); +} + +TEST(jitter, gcc_cache_fifo_limit_and_hits_do_not_reorder) { + CacheArtifactFixture f; + EXPECT_EQ(StateGCCConfig{}.cache_max_entries, 256u); + f.Publish("first C", 2); + f.Publish("second C", 2); + ASSERT_EQ(f.dir.Entries().size(), 2u); + EXPECT_TRUE(f.Restore("first C", 2));// FIFO, not LRU + f.Publish("third C", 2); + ASSERT_EQ(f.dir.Entries().size(), 2u); + EXPECT_FALSE(f.Restore("first C", 2)); + EXPECT_TRUE(f.Restore("second C", 2)); + EXPECT_TRUE(f.Restore("third C", 2)); + // Lowering the limit takes effect even on the next lookup, not only writes. + EXPECT_TRUE(f.Restore("third C", 1)); + EXPECT_EQ(f.dir.Entries().size(), 1u); + EXPECT_FALSE(f.Restore("second C", 1)); +} + +TEST(jitter, gcc_cache_zero_unlimited_and_unrelated_files_preserved) { + CacheArtifactFixture f; + std::filesystem::create_directory(f.dir.path / "user-data"); + std::ofstream(f.dir.path / "user-data" / "image") << "not a cache entry"; + auto partial = f.dir.path / std::string(64, 'f'); + std::filesystem::create_directory(partial); + std::ofstream(partial / "image") << "partial"; + for (int i = 0; i < 4; ++i) f.Publish("unlimited " + std::to_string(i), 0); + EXPECT_EQ(f.dir.Entries().size(), 6u);// includes intentionally unrelated/partial fixtures + f.Publish("bounded", 1); + EXPECT_EQ(f.dir.Entries().size(), 3u); + EXPECT_TRUE(std::filesystem::exists(f.dir.path / "user-data" / "image")); + EXPECT_TRUE(std::filesystem::exists(partial / "image")); + EXPECT_TRUE(f.Restore("bounded", 1)); +} + +TEST(jitter, gcc_cache_corrupt_fifo_metadata_does_not_poison_writes) { + CacheArtifactFixture f; + f.Publish("old metadata", 2); + auto entry = f.dir.Entries().at(0); + { + std::ofstream out(entry / "published", std::ios::trunc); + out << "18446744073709551615"; + } + f.Publish("after overflow", 2); + EXPECT_EQ(f.dir.Entries().size(), 2u); + EXPECT_TRUE(f.Restore("after overflow", 2)); + { + std::ofstream out(entry / "published", std::ios::trunc); + out << "damaged"; + } + f.Publish("after malformed", 2); + EXPECT_EQ(f.dir.Entries().size(), 2u); + EXPECT_TRUE(f.Restore("after malformed", 2)); +} + +TEST(jitter, gcc_cache_eviction_preserves_loaded_modules) { + JitCacheTestDir dir; + auto config = dir.Config(); + config.gcc_config.cache_max_entries = 1; + FakeluaStateGuard live(config); + CompileString(live.GetState(), kCacheSource, CacheCompileConfig()); + CheckCacheState(live.GetState()); + auto old = dir.Entries().at(0); + { + FakeluaStateGuard other(config); + CompileString(other.GetState(), "function cache_other() return 93 end", CacheCompileConfig()); + CheckCacheState(live.GetState()); + } + EXPECT_FALSE(std::filesystem::exists(old)); + EXPECT_EQ(dir.Entries().size(), 1u); + CheckCacheState(live.GetState()); +} + +TEST(jitter, gcc_cache_fifo_concurrent_publishers_and_readers) { + CacheArtifactFixture f; + std::atomic ready{0}, failures{0}; + std::vector threads; + for (int i = 0; i < 8; ++i) + threads.emplace_back([&, i] { + ++ready; + while (ready.load() != 8) std::this_thread::yield(); + try { + auto cfg = f.dir.Config().gcc_config; + cfg.cache_max_entries = 2; + auto key = "parallel C " + std::to_string(i); + GccCache cache(cfg, f.compiler, key, false); + cache.Publish(f.binary, f.deps, f.source); + // Another publisher may evict us before Restore; either outcome is valid. + if (cache.Restore(f.restored + std::to_string(i))) { + std::ifstream in(f.restored + std::to_string(i)); + std::string bytes; + std::getline(in, bytes); + if (bytes != "test artifact bytes") ++failures; + } + } catch (...) { + ++failures; + } + }); + for (auto &thread: threads) thread.join(); + EXPECT_EQ(failures.load(), 0); + EXPECT_EQ(f.dir.Entries().size(), 2u); + size_t locks = 0; + for (const auto &item: std::filesystem::directory_iterator(f.dir.path)) + if (item.path().extension() == ".lock") ++locks; + EXPECT_LE(locks, 257u); +} +#if !defined(_WIN32) +TEST(jitter, gcc_cache_fifo_process_publishers) { + CacheArtifactFixture f; + std::vector children; + for (int i = 0; i < 4; ++i) { + auto pid = fork(); + ASSERT_GE(pid, 0); + if (!pid) { + f.Publish("process C " + std::to_string(i), 2); + _exit(0); + } + children.push_back(pid); + } + for (auto pid: children) { + int status = 0; + ASSERT_EQ(waitpid(pid, &status, 0), pid); + ASSERT_TRUE(WIFEXITED(status)); + EXPECT_EQ(WEXITSTATUS(status), 0); + } + EXPECT_EQ(f.dir.Entries().size(), 2u); +} + +TEST(jitter, gcc_cache_unlinked_live_lock_does_not_replace_new_publication) { + CacheArtifactFixture f; + auto cfg = f.dir.Config().gcc_config; + int signal[2]; + ASSERT_EQ(pipe(signal), 0); + // Fork before acquiring the parent's in-process mutex; the child represents + // an independently started process with no inherited held synchronization. + auto child = fork(); + ASSERT_GE(child, 0); + if (!child) { + close(signal[1]); + char mark = 0; + if (read(signal[0], &mark, 1) != 1) _exit(2); + close(signal[0]); + f.Publish("unlinked lock C", 2); + _exit(0); + } + close(signal[0]); + { + GccCache original(cfg, f.compiler, "unlinked lock C", false); + for (const auto &item: std::filesystem::directory_iterator(f.dir.path)) + if (item.path().extension() == ".lock") std::filesystem::remove(item.path()); + const char mark = 'x'; + ASSERT_EQ(write(signal[1], &mark, 1), 1); + close(signal[1]); + int status = 0; + ASSERT_EQ(waitpid(child, &status, 0), child); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(WEXITSTATUS(status), 0); + ASSERT_EQ(f.dir.Entries().size(), 1u); + auto image = f.dir.Entries()[0] / "image"; + auto stamp = std::filesystem::last_write_time(image); + EXPECT_FALSE(original.Restore(f.restored)); + original.Publish(f.binary, f.deps, f.source); + EXPECT_EQ(std::filesystem::last_write_time(image), stamp); + } + EXPECT_TRUE(f.Restore("unlinked lock C", 2)); +} + +TEST(jitter, gcc_cache_private_root_and_symlink_rejection) { + CacheArtifactFixture f; + auto config = f.dir.Config().gcc_config; + auto shared = f.dir.path / "shared"; + std::filesystem::create_directory(shared); + std::filesystem::permissions(shared, std::filesystem::perms::all); + config.cache_directory = (shared / "cache").string(); + { + GccCache cache(config, f.compiler, "unsafe parent", false); + cache.Publish(f.binary, f.deps, f.source); + } + EXPECT_FALSE(std::filesystem::exists(shared / "cache")); + auto target = f.dir.path / "target"; + std::filesystem::create_directory(target); + auto link = f.dir.path / "link"; + std::filesystem::create_directory_symlink(target, link); + config.cache_directory = link.string(); + { + GccCache cache(config, f.compiler, "unsafe link", false); + cache.Publish(f.binary, f.deps, f.source); + } + EXPECT_TRUE(std::filesystem::is_empty(target)); +} +#endif +TEST(jitter, gcc_cache_external_cleanup_races_restore_without_partial_copy) { + CacheArtifactFixture f; + const std::string bytes(8 * 1024 * 1024, 'x'); + { + std::ofstream out(f.binary, std::ios::binary | std::ios::trunc); + out << bytes; + } + auto cfg = f.dir.Config().gcc_config; + GccCache cache(cfg, f.compiler, "cleanup racing image read", false); + for (int i = 0; i < 3; ++i) { + cache.Publish(f.binary, f.deps, f.source); + ASSERT_EQ(f.dir.Entries().size(), 1u); + auto entry = f.dir.Entries()[0]; + std::atomic started{false}; + std::thread cleaner([&] { + while (!started.load()) std::this_thread::yield(); + RemoveTreeBestEffort(entry); + }); + started = true; + bool restored = cache.Restore(f.restored); + cleaner.join(); + if (restored) { + std::ifstream in(f.restored, std::ios::binary); + std::string actual((std::istreambuf_iterator(in)), {}); + EXPECT_EQ(actual, bytes); + } + } + cache.Publish(f.binary, f.deps, f.source); + EXPECT_TRUE(cache.Restore(f.restored)); +} + +TEST(jitter, gcc_cache_removed_entry_and_root_are_recoverable) { + CacheArtifactFixture f; + auto cfg = f.dir.Config().gcc_config; + { + GccCache cache(cfg, f.compiler, "removed while compiling", false); + // External cleanup may unlink cache files and, on Windows, share-deleted lock names. + RemoveTreeBestEffort(f.dir.path); + EXPECT_FALSE(cache.Restore(f.restored)); + EXPECT_NO_THROW(cache.Publish(f.binary, f.deps, f.source)); + } + // Restore our compile inputs outside the deleted cache root. + std::filesystem::create_directories(f.dir.path); + std::filesystem::permissions(f.dir.path, std::filesystem::perms::owner_all); + std::ofstream(f.source) << "source"; + std::ofstream(f.binary) << "test artifact bytes"; + std::string escaped; + for (char c: f.source) { + if (c == ' ' || c == '#' || c == '\\') escaped += '\\'; + if (c == '$') escaped += '$'; + escaped += c; + } + std::ofstream(f.deps) << "flua: " << escaped << "\n"; + f.Publish("recreated root", 2); + EXPECT_TRUE(f.Restore("recreated root", 2)); + auto entry = f.dir.Entries().at(0); + std::filesystem::remove_all(entry); + EXPECT_FALSE(f.Restore("recreated root", 2)); + f.Publish("recreated root", 2); + EXPECT_TRUE(f.Restore("recreated root", 2)); +} + +TEST(jitter, gcc_cache_external_cleanup_during_compile_and_live_execution) { + JitCacheTestDir dir; + auto config = dir.Config(); + config.gcc_config.cache_max_entries = 2; + FakeluaStateGuard live(config); + CompileString(live.GetState(), kCacheSource, CacheCompileConfig()); + std::atomic stop{false}; + std::thread cleaner([&] { + while (!stop.load()) { + RemoveTreeBestEffort(dir.path); + std::this_thread::yield(); + } + }); + // Always join even when compilation throws, so failures remain diagnostic. + try { + for (int i = 0; i < 2; ++i) { + FakeluaStateGuard other(config); + CompileString(other.GetState(), kCacheSource + "\nfunction cache_cleanup_extra() return " + std::to_string(i) + " end", CacheCompileConfig()); + CheckCacheState(other.GetState()); + CheckCacheState(live.GetState()); + } + } catch (...) { + stop = true; + cleaner.join(); + throw; + } + stop = true; + cleaner.join(); + CheckCacheState(live.GetState()); + { + FakeluaStateGuard recovered(config); + CompileString(recovered.GetState(), kCacheSource, CacheCompileConfig()); + CheckCacheState(recovered.GetState()); + } + EXPECT_EQ(dir.Entries().size(), 1u); +} + +TEST(jitter, gcc_cache_states_strings_closures_and_lifetime) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + cfg.record_c_code = true; + FakeluaStateGuard first(dir.Config()); + CompileString(first.GetState(), kCacheSource, cfg); + CheckCacheState(first.GetState()); + auto entries = dir.Entries(); + ASSERT_EQ(entries.size(), 1u); + auto stamp = std::filesystem::last_write_time(entries[0] / "image"); + { + FakeluaStateGuard second(dir.Config()); + // Perturb its intern table: no address or allocation-order dependencies. + (void) second.GetState()->GetConstString().Alloc("unrelated interned string"); + CompileString(second.GetState(), kCacheSource, cfg); + CheckCacheState(second.GetState()); + CheckCacheState(first.GetState()); + EXPECT_EQ(GetLastRecordedCCode(first.GetState()), GetLastRecordedCCode(second.GetState())); + } + CheckCacheState(first.GetState()); + CompileString(first.GetState(), kCacheSource, cfg); + CheckCacheState(first.GetState()); + ASSERT_EQ(dir.Entries().size(), 1u); + EXPECT_EQ(stamp, std::filesystem::last_write_time(entries[0] / "image")); +} + +TEST(jitter, gcc_cache_interned_identity_survives_actual_module_unload) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + FakeluaStateGuard state(dir.Config()); + auto *s = state.GetState(); + const std::string producer = R"(function cache_export_id() return "shared\0identity" end)"; + CompileString(s, producer, cfg); + const auto image = dir.Entries().at(0) / "image"; + const auto stamp = std::filesystem::last_write_time(image); + std::weak_ptr old = s->GetVM().GetFunction("cache_export_id").GetHandle(JIT_GCC); + CVar value = inter::DispatchCall(s, s->GetVM().FindFunction("cache_export_id")->GetAddr(JIT_GCC), nullptr, 0, JIT_GCC); + if (value.type_ == static_cast(VarType::Multi)) value = inter::GetMultiCVarElement(value, 0); + ASSERT_EQ(value.type_, static_cast(VarType::StringId)); + const std::string text("shared\0identity", 15); + EXPECT_EQ(value.data_.i, s->GetConstString().Alloc(text)); + CompileString(s, R"(function cache_consume_id(k) + local t={["shared\0identity"]=71, a=1, b=2, c=3, d=4, e=5} + return t[k] + end)", + cfg); + // Replace every producer export (including init): the weak handle proves + // this really unloads the first image, not merely loads another module. + CompileString(s, producer, cfg); + EXPECT_TRUE(old.expired()); + EXPECT_EQ(std::filesystem::last_write_time(image), stamp); + inter::Reset(s); + EXPECT_EQ(s->GetConstString().GetString(value.data_.i), text); + const auto result = inter::DispatchCall(s, s->GetVM().FindFunction("cache_consume_id")->GetAddr(JIT_GCC), &value, 1, JIT_GCC); + EXPECT_EQ(inter::FakeluaToNative(s, result), 71); + CVar reloaded = inter::DispatchCall(s, s->GetVM().FindFunction("cache_export_id")->GetAddr(JIT_GCC), nullptr, 0, JIT_GCC); + if (reloaded.type_ == static_cast(VarType::Multi)) reloaded = inter::GetMultiCVarElement(reloaded, 0); + EXPECT_EQ(reloaded.type_, static_cast(VarType::StringId)); + EXPECT_EQ(reloaded.data_.i, value.data_.i); + EXPECT_EQ(dir.Entries().size(), 2u); +} + +TEST(jitter, gcc_cache_nonnumeric_initializers_are_per_load_and_per_state) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + const std::string source = R"( + local text = cache_load_seed() + local config = {name=text, enabled=true} + function cache_read_seed() return config.name end + )"; + int calls_a = 0, calls_b = 0; + std::string seed_a = "first", seed_b = "second"; + FakeluaStateGuard first(dir.Config()), second(dir.Config()); + RegisterNativeFunction(first.GetState(), "cache_load_seed", false, std::function([&](State *) { + ++calls_a; + return seed_a; + })); + RegisterNativeFunction(second.GetState(), "cache_load_seed", false, std::function([&](State *) { + ++calls_b; + return seed_b; + })); + CompileString(first.GetState(), source, cfg); + ASSERT_EQ(dir.Entries().size(), 1u); + const auto image = dir.Entries()[0] / "image"; + const auto stamp = std::filesystem::last_write_time(image); + CompileString(second.GetState(), source, cfg); + EXPECT_EQ(calls_a, 1); + EXPECT_EQ(calls_b, 1); + std::string result; + for (int i = 0; i < 3; ++i) { + Call(first.GetState(), JIT_GCC, "cache_read_seed", result); + EXPECT_EQ(result, "first"); + Call(second.GetState(), JIT_GCC, "cache_read_seed", result); + EXPECT_EQ(result, "second"); + } + EXPECT_EQ(calls_a, 1); + EXPECT_EQ(calls_b, 1); + std::weak_ptr old = first.GetState()->GetVM().GetFunction("cache_read_seed").GetHandle(JIT_GCC); + seed_a = "reloaded"; + CompileString(first.GetState(), source, cfg); + EXPECT_TRUE(old.expired()); + EXPECT_EQ(calls_a, 2); + EXPECT_EQ(calls_b, 1); + Call(first.GetState(), JIT_GCC, "cache_read_seed", result); + EXPECT_EQ(result, "reloaded"); + Call(second.GetState(), JIT_GCC, "cache_read_seed", result); + EXPECT_EQ(result, "second"); + EXPECT_EQ(dir.Entries().size(), 1u); + EXPECT_EQ(std::filesystem::last_write_time(image), stamp); +} + +TEST(jitter, gcc_cache_failed_runtime_init_does_not_cache_state) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + const std::string source = R"( + local text = cache_load_seed() + function cache_read_seed() return text end + )"; + int calls = 0; + bool fail = true; + FakeluaStateGuard good(dir.Config()); + RegisterNativeFunction(good.GetState(), "cache_load_seed", false, std::function([&](State *) { + ++calls; + if (fail) throw std::runtime_error("expected init failure"); + return std::string("recovered"); + })); + EXPECT_THROW(CompileString(good.GetState(), source, cfg), std::exception); + // Valid machine code may be published before Lua initialization. Retrying + // in the SAME State must unload its old image and remove its Lua location, + // not retain dangling diagnostic pointers or a partially initialized flag. + ASSERT_EQ(dir.Entries().size(), 1u); + const auto image = dir.Entries()[0] / "image"; + const auto stamp = std::filesystem::last_write_time(image); + std::weak_ptr old = good.GetState()->GetVM().GetFunction("cache_read_seed").GetHandle(JIT_GCC); + fail = false; + CompileString(good.GetState(), source, cfg); + EXPECT_TRUE(old.expired()); + EXPECT_EQ(good.GetState()->GetLuaLocs(JIT_GCC).size(), 1u); + std::string result; + Call(good.GetState(), JIT_GCC, "cache_read_seed", result); + EXPECT_EQ(result, "recovered"); + EXPECT_EQ(calls, 2); + EXPECT_EQ(std::filesystem::last_write_time(image), stamp); +} + +TEST(jitter, gcc_cache_recursive_same_key_init_owns_exact_location) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + FakeluaStateGuard state(dir.Config()); + auto *s = state.GetState(); + const std::string source = R"( + local boot = cache_reenter() + function cache_recursive_value() return 11 end + )"; + bool entered = false; + int calls = 0; + std::weak_ptr outer; + RegisterNativeFunction(s, "cache_reenter", false, std::function([&](State *current) { + ++calls; + if (!entered) { + entered = true; + outer = current->GetVM().GetFunction("cache_recursive_value").GetHandle(JIT_GCC); + // The same cache key must already be unlocked. The inner image + // replaces all outer exports while its init is still running. + CompileString(current, source, cfg); + } + return int64_t{0}; + })); + CompileString(s, source, cfg); + EXPECT_EQ(calls, 2); + EXPECT_TRUE(outer.expired()); + EXPECT_EQ(s->GetLuaLocs(JIT_GCC).size(), 1u); + int64_t result = 0; + Call(s, JIT_GCC, "cache_recursive_value", result); + EXPECT_EQ(result, 11); + EXPECT_EQ(dir.Entries().size(), 1u); +} + +TEST(jitter, gcc_cache_bad_manifest_recovery_preserves_live_image) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + FakeluaStateGuard live(dir.Config()); + CompileString(live.GetState(), kCacheSource, cfg); + auto entries = dir.Entries(); + ASSERT_EQ(entries.size(), 1u); + for (const auto &bad_manifest: {std::string("not a manifest"), std::string(64, '0') + "\n10001\n"}) { + { + std::ofstream out(entries[0] / "manifest", std::ios::trunc); + out << bad_manifest; + } + FakeluaStateGuard repaired(dir.Config()); + CompileString(repaired.GetState(), kCacheSource, cfg); + CheckCacheState(repaired.GetState()); + CheckCacheState(live.GetState()); + EXPECT_EQ(dir.Entries().size(), 1u); + } + // Deleting the persistent image must not affect the private mapped copy. + std::filesystem::remove(entries[0] / "image"); + CheckCacheState(live.GetState()); + FakeluaStateGuard repaired(dir.Config()); + CompileString(repaired.GetState(), kCacheSource, cfg); + CheckCacheState(repaired.GetState()); + CheckCacheState(live.GetState()); + EXPECT_EQ(dir.Entries().size(), 1u); +} + +TEST(jitter, gcc_cache_invalidation_corruption_and_partial_entry) { + JitCacheTestDir dir; + auto cfg = CacheCompileConfig(); + auto compile = [&](const std::string &source) { + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), source, cfg); + CheckCacheState(s.GetState()); + }; + compile(kCacheSource); + auto entries = dir.Entries(); + ASSERT_EQ(entries.size(), 1u); + { + std::ofstream corrupt(entries[0] / "image", std::ios::trunc); + corrupt << "broken"; + } + compile(kCacheSource); + EXPECT_GT(std::filesystem::file_size(entries[0] / "image"), 100u); + std::filesystem::remove(entries[0] / "manifest"); + compile(kCacheSource); + ASSERT_TRUE(std::filesystem::exists(entries[0] / "manifest")); + cfg.debug_mode = true; + compile(kCacheSource); + EXPECT_EQ(dir.Entries().size(), 2u); + cfg.debug_mode = false; + compile(kCacheSource + "\nfunction extra() return 81 end\n"); + EXPECT_EQ(dir.Entries().size(), 3u); + auto disabled = dir.Config(); + disabled.gcc_config.enable_cache = false; + FakeluaStateGuard s(disabled); + CompileString(s.GetState(), kCacheSource + "\n-- disabled\n", cfg); + CheckCacheState(s.GetState()); + EXPECT_EQ(dir.Entries().size(), 3u); +} + +TEST(jitter, gcc_cache_unavailable_directory_falls_back) { + JitCacheTestDir dir; + auto config = dir.Config(); + auto file = dir.path / "not_a_directory"; + { + std::ofstream out(file); + out << "x"; + } + config.gcc_config.cache_directory = file.string(); + FakeluaStateGuard s(config); + CompileString(s.GetState(), kCacheSource, CacheCompileConfig()); + CheckCacheState(s.GetState()); + EXPECT_EQ(dir.Entries().size(), 0u); +} + +TEST(jitter, gcc_cache_thread_race_private_mutable_statics) { + JitCacheTestDir dir; + std::atomic ready{0}, failures{0}; + std::vector threads; + for (int i = 0; i < 8; ++i) + threads.emplace_back([&] { + ++ready; + while (ready.load() != 8) std::this_thread::yield(); + try { + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), kCacheSource, CacheCompileConfig()); + for (int j = 0; j < 100; ++j) { + int64_t n = 0; + Call(s.GetState(), JIT_GCC, "cache_run", n, j); + if (n != j + 3) ++failures; + } + } catch (...) { + ++failures; + } + }); + for (auto &thread: threads) thread.join(); + EXPECT_EQ(failures.load(), 0); + EXPECT_EQ(dir.Entries().size(), 1u); +} +#if !defined(_WIN32) +TEST(jitter, gcc_cache_process_race_and_reuse) { + JitCacheTestDir dir; + std::vector children; + for (int i = 0; i < 4; ++i) { + pid_t pid = fork(); + ASSERT_GE(pid, 0); + if (pid == 0) { + try { + int result; + { + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), kCacheSource, CacheCompileConfig()); + int64_t n = 0; + Call(s.GetState(), JIT_GCC, "cache_run", n, 9); + result = n == 12 ? 0 : 2; + } + _exit(result); + } catch (...) { + _exit(3); + } + } + children.push_back(pid); + } + for (auto pid: children) { + int status = 0; + ASSERT_EQ(waitpid(pid, &status, 0), pid); + ASSERT_TRUE(WIFEXITED(status)); + EXPECT_EQ(WEXITSTATUS(status), 0); + } + EXPECT_EQ(dir.Entries().size(), 1u); + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), kCacheSource, CacheCompileConfig()); + CheckCacheState(s.GetState()); +} +#endif + +TEST(jitter, gcc_cache_dependency_and_compiler_invalidation) { + JitCacheTestDir dir; + auto config = dir.Config().gcc_config; + const auto source = (dir.path / "input.c").string(); + const auto header = (dir.path / "header.h").string(); + const auto deps = (dir.path / "input.d").string(); + const auto binary = (dir.path / "output.so").string(); + const auto restored = (dir.path / "restored.so").string(); + { + std::ofstream out(source); + out << "source"; + } + { + std::ofstream out(header); + out << "first header"; + } + auto make_path = [](const std::string &path) { + std::string escaped; + for (char c: path) { + if (c == ' ' || c == '#' || c == '\\') escaped += '\\'; + if (c == '$') escaped += '$'; + escaped += c; + } + return escaped; + }; + { + std::ofstream out(deps); + out << "flua: " << make_path(source) << " " << make_path(header) << "\n"; + } + { + std::ofstream out(binary, std::ios::binary); + out << "compiled bytes"; + } +#if defined(__APPLE__) + const char *compiler = "clang"; +#else + const char *compiler = "gcc"; +#endif +#if !defined(_WIN32) + // Abrupt process exit: leave a held OS lock, partial entry and staging data. + // The next writer must recover without relying on destructor cleanup. + int ready[2]; + ASSERT_EQ(pipe(ready), 0); + auto child = fork(); + ASSERT_GE(child, 0); + if (child == 0) { + close(ready[0]); + try { + GccCache cache(config, compiler, "the generated C", false); + cache.Publish(binary, deps, source); + for (const auto &entry: dir.Entries()) { + std::filesystem::remove(entry / "manifest"); + std::filesystem::create_directory(entry.string() + ".tmp"); + } + const char mark = 'x'; + (void) write(ready[1], &mark, 1); + _exit(0); + } catch (...) { + _exit(3); + } + } + close(ready[1]); + char mark = 0; + ASSERT_EQ(read(ready[0], &mark, 1), 1); + close(ready[0]); + int status = 0; + ASSERT_EQ(waitpid(child, &status, 0), child); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(WEXITSTATUS(status), 0); +#endif + { + GccCache cache(config, compiler, "the generated C", false); + EXPECT_FALSE(cache.Restore(restored)); + cache.Publish(binary, deps, source); + } + ASSERT_EQ(dir.Entries().size(), 1u); + EXPECT_FALSE(std::filesystem::exists(dir.Entries()[0].string() + ".tmp")); + { + GccCache cache(config, compiler, "the generated C", false); + EXPECT_TRUE(cache.Restore(restored)); + } + { + GccCache cache(config, "fakelua_no_such_compiler", "the generated C", false); + EXPECT_FALSE(cache.Restore(restored)); + } + { + std::ofstream out(header); + out << "other header"; + } + { + GccCache cache(config, compiler, "the generated C", false); + EXPECT_FALSE(cache.Restore(restored)); + } + EXPECT_EQ(dir.Entries().size(), 0u); +} + +TEST(jitter, gcc_cache_failed_compile_does_not_publish) { + JitCacheTestDir dir; + + struct RestoreCompiler { + bool present = std::getenv("FAKELUA_CC") != nullptr; + std::string value = present ? std::getenv("FAKELUA_CC") : ""; + + ~RestoreCompiler() { + SetTestEnv("FAKELUA_CC", present ? value.c_str() : nullptr); + } + } restore; + + SetTestEnv("FAKELUA_CC", "fakelua_nonexistent_cache_compiler"); + { + FakeluaStateGuard s(dir.Config()); + EXPECT_THROW(CompileString(s.GetState(), kCacheSource, CacheCompileConfig()), std::exception); + } + EXPECT_EQ(dir.Entries().size(), 0u); + SetTestEnv("FAKELUA_CC", nullptr); + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), kCacheSource, CacheCompileConfig()); + CheckCacheState(s.GetState()); + EXPECT_EQ(dir.Entries().size(), 1u); +} + +#endif// FAKELUA_TEST_NO_GCC_CACHE + +TEST(jitter, gcc_cache_binary_literal_strings_all_backends) { + JitCacheTestDir dir; + std::string source = "function cache_all_bytes() return \""; + std::string expected; + for (int i = 0; i < 256; ++i) { + source += std::format("\\{:03d}", i); + expected += static_cast(i); + } + source += "\" end\nfunction cache_trigraph() return \"" + std::string("?") + "?/\" end\n"; + source += R"(function cache_binary_key(k) local t={["a\0b"]=42} return t[k] end +function cache_binary_static() return cache_binary_key("a\0b") end +function cache_collision(k) + local t={ab=1,bA=2,c=3,d=4,e=5,f=6} + t[k]=t[k]+10 + return t[k] +end +function cache_collisions() return cache_collision("ab")*100+cache_collision("bA") end +)"; + CompileConfig cfg; + cfg.debug_mode = false; + for (int instance = 0; instance < 2; ++instance) { + FakeluaStateGuard s(dir.Config()); + CompileString(s.GetState(), source, cfg); + for (auto type: {JIT_TCC, JIT_GCC, JIT_INTERP}) { + std::string result; + Call(s.GetState(), type, "cache_all_bytes", result); + EXPECT_EQ(result, expected); + Call(s.GetState(), type, "cache_trigraph", result); + EXPECT_EQ(result, std::string("?") + "?/"); + int64_t value = 0; + Call(s.GetState(), type, "cache_binary_key", value, std::string("a\0b", 3)); + EXPECT_EQ(value, 42); + Call(s.GetState(), type, "cache_binary_static", value); + EXPECT_EQ(value, 42); + Call(s.GetState(), type, "cache_collisions", value); + EXPECT_EQ(value, 1112); + } + } +} diff --git a/test/test_log.cpp b/test/test_log.cpp index 39c12d49..a2ac2564 100644 --- a/test/test_log.cpp +++ b/test/test_log.cpp @@ -3,15 +3,15 @@ #include "util/logging.h" #include "gtest/gtest.h" +#include "state/state.h" #include -#include #include #include +#include #include #include #include #include -#include "state/state.h" using namespace fakelua; @@ -744,7 +744,6 @@ TEST(test_log, level_transitions_and_errors) { FakeluaDeleteState(s); } - TEST(test_log, log_file_rotation_small_limit) { // Force RotateLogs by using a tiny max size and writing repeatedly. State *s = FakeluaNewState(); diff --git a/test/test_mysql.cpp b/test/test_mysql.cpp index 115e823b..63eeaa9c 100644 --- a/test/test_mysql.cpp +++ b/test/test_mysql.cpp @@ -8,7 +8,6 @@ using namespace fakelua; using namespace fakelua::mysql; - // MySQL 模块测试 // 测试 1: 连接失败时 pcall 能捕获错误(无需真实 MySQL 服务器) @@ -188,6 +187,7 @@ TEST(test_mysql, bad_port) { EXPECT_EQ(ret, 1); FakeluaDeleteState(s); } + // 回归(P0-1/P0-2/P1-4):连接未就绪时用闭包发起 query, // 连接失败后回调应恰好收到一次错误(曾被静默吞掉或报错丢失)。 TEST(test_mysql, query_closure_callback_exactly_once) { diff --git a/test/test_native.cpp b/test/test_native.cpp index 45434e16..a4842227 100644 --- a/test/test_native.cpp +++ b/test/test_native.cpp @@ -1,7 +1,7 @@ #include "fakelua.h" #include "state/state.h" -#include "var/var_table.h" #include "test_jit.h" +#include "var/var_table.h" #include "var/var_type.h" #include "gtest/gtest.h" #include @@ -75,7 +75,6 @@ TEST(test_native, test_nested_object) { FakeluaDeleteState(s); } - TEST(test_native, test_deep_nesting_wrap_is_light) { auto *s = FakeluaNewState(); auto &mgr = GetNativeObjectManager(s); @@ -387,7 +386,8 @@ TEST(test_native, host_scalar_arg_helpers) { }); CompileConfig cfg; - ASSERT_NO_THROW(CompileString(s, R"(package "HostHelpers" + ASSERT_NO_THROW(CompileString(s, + R"(package "HostHelpers" function defaults() return host_join("p1", 7) end @@ -405,7 +405,9 @@ function false_flag() -- false 是假值,from_int 为 false,返回 NativeBool(from_bool == 0) -> true if host_scalar_roundtrip(0, false) then return 1 else return 0 end end -)""", cfg)); +)" + "", + cfg)); for (auto jit_type: AllJitTypes()) { std::string out; @@ -990,10 +992,8 @@ TEST(test_native_version, self_check_is_cycle_safe) { // SelfCheck 只校验本对象(不做子树递归,也不再维护父子边), // 成环对象图上必须直接返回、不崩不挂起。 - EXPECT_TRUE(NativeObject::SelfCheck(a, true)) - << "SelfCheck 在环状对象图(A->B->A)上未能正常结束或误报"; - EXPECT_TRUE(NativeObject::SelfCheck(b, true)) - << "SelfCheck 在环状对象图(B->A->B)上未能正常结束或误报"; + EXPECT_TRUE(NativeObject::SelfCheck(a, true)) << "SelfCheck 在环状对象图(A->B->A)上未能正常结束或误报"; + EXPECT_TRUE(NativeObject::SelfCheck(b, true)) << "SelfCheck 在环状对象图(B->A->B)上未能正常结束或误报"; // 环中任一对象写入都只改自己的计数,不会波及对方(无传播、无递归风险)。 const uint64_t va = a->GetVersion(); @@ -1083,10 +1083,7 @@ TEST(test_native_version, lua_paths_and_container) { static NativeObject *global_player = nullptr; global_player = mgr.Create(gid, "player", 100); global_player->SetInt("hp", 100); - RegisterNativeFunction(s, "get_player", 0, false, - [](State *state, CVar *, int) -> CVar { - return inter::NativeToFakelua(state, global_player); - }); + RegisterNativeFunction(s, "get_player", 0, false, [](State *state, CVar *, int) -> CVar { return inter::NativeToFakelua(state, global_player); }); CompileConfig config; CompileFile(s, "./native/test_native_get_set.lua", config); @@ -1125,3 +1122,26 @@ TEST(test_native_version, lua_paths_and_container) { FakeluaDeleteState(s); } } + +TEST(native, gcc_cache_binds_native_callbacks_per_state) { + StateConfig config; + config.log_level = LogLevel::Off; + // Same default cache key/code, different native callbacks in each State. + FakeluaStateGuard first(config), second(config); + RegisterNativeFunction(first.GetState(), "cache_host", false, std::function([](State *, int64_t n) { return n + 10; })); + RegisterNativeFunction(second.GetState(), "cache_host", false, std::function([](State *, int64_t n) { return n + 20; })); + CompileConfig cfg; + DisableAllJit(cfg); + cfg.disable_jit[JIT_GCC] = false; + cfg.debug_mode = false; + const std::string source = "function cache_native(n) return cache_host(n) end"; + CompileString(first.GetState(), source, cfg); + CompileString(second.GetState(), source, cfg); + int64_t value = 0; + Call(first.GetState(), JIT_GCC, "cache_native", value, 3); + EXPECT_EQ(value, 13); + Call(second.GetState(), JIT_GCC, "cache_native", value, 3); + EXPECT_EQ(value, 23); + Call(first.GetState(), JIT_GCC, "cache_native", value, 4); + EXPECT_EQ(value, 14); +} diff --git a/test/test_runtime.cpp b/test/test_runtime.cpp index 4493f356..badf1562 100644 --- a/test/test_runtime.cpp +++ b/test/test_runtime.cpp @@ -1,6 +1,9 @@ #include "compile/compiler.h" #include "fakelua.h" +#include "interp/runtime.h" +#include "interp/vm_ops.h" #include "jit/vm.h" +#include "native/table/native_table.h" #include "state/const_string.h" #include "state/heap.h" #include "state/state.h" @@ -12,15 +15,12 @@ #include "var/var_table.h" #include "var/var_type.h" #include "gtest/gtest.h" +#include #include #include +#include #include #include -#include "interp/runtime.h" -#include "interp/vm_ops.h" -#include "native/table/native_table.h" -#include -#include using namespace fakelua; @@ -781,10 +781,10 @@ TEST(runtime, interp_runtime_error_paths) { TEST(runtime, vm_ops_for_and_arith_edges) { using namespace interp_rt; - (void)Nil(); - (void)Bool(true); - (void)Int(1); - (void)Float(1.5); + (void) Nil(); + (void) Bool(true); + (void) Int(1); + (void) Float(1.5); EXPECT_THROW(CheckInt(Bool(true)), std::exception); EXPECT_EQ(BinAdd(Int(1), Int(2)).data_.i, 3); diff --git a/test/test_state.cpp b/test/test_state.cpp index 047b3f93..72551113 100644 --- a/test/test_state.cpp +++ b/test/test_state.cpp @@ -1,10 +1,10 @@ #include "compile/compiler.h" #include "fakelua.h" -#include "test_jit.h" #include "jit/vm.h" #include "jit/vm_function.h" #include "state/const_string.h" #include "state/state.h" +#include "test_jit.h" #include "var/var.h" #include "var/var_string.h" #include "gtest/gtest.h" @@ -446,7 +446,6 @@ TEST(state, heap_alloc_boundary) { ASSERT_EQ(alloc.Size(), 0u); } - TEST(state, cvar_to_integer_edge_cases) { FakeluaStateGuard guard; auto *s = guard.GetState(); diff --git a/test/test_syntax_tree.cpp b/test/test_syntax_tree.cpp index 51b4df43..5d654226 100644 --- a/test/test_syntax_tree.cpp +++ b/test/test_syntax_tree.cpp @@ -1,8 +1,8 @@ #include "compile/compiler.h" -#include "fakelua.h" -#include "gtest/gtest.h" #include "compile/semantic_analysis.h" #include "compile/syntax_tree.h" +#include "fakelua.h" +#include "gtest/gtest.h" #include #include #include @@ -2064,8 +2064,7 @@ TEST(syntax_tree, semantic_native_function_as_value_dotted) { Compiler c(s); // 点号原生库函数:local f = table.concat / pcall(table.concat, t) / { cb = string.format } ExpectNativeValueError(c, "function f()\n local g = table.concat\n return g\nend\n", "native function 'table.concat'"); - ExpectNativeValueError(c, "function f()\n local t = { 1, 2 }\n return pcall(table.concat, t, \"-\")\nend\n", - "native function 'table.concat'"); + ExpectNativeValueError(c, "function f()\n local t = { 1, 2 }\n return pcall(table.concat, t, \"-\")\nend\n", "native function 'table.concat'"); ExpectNativeValueError(c, "function f()\n return { cb = string.format }\nend\n", "native function 'string.format'"); ExpectNativeValueError(c, "function f()\n return math.floor\nend\n", "native function 'math.floor'"); } @@ -2215,7 +2214,7 @@ TEST(syntax_tree, getters_setters_coverage) { EXPECT_EQ(empty->Type(), SyntaxTreeType::Empty); EXPECT_FALSE(empty->Dump(1).empty()); empty->SetLoc(StCovLoc()); - (void)empty->Loc(); + (void) empty->Loc(); auto label = std::make_shared(StCovLoc()); label->SetName("L"); @@ -2352,7 +2351,7 @@ end sa.CollectReturnsForBlock(pr.chunk, returns); EXPECT_GE(returns.size(), 5u); // Descend into function bodies via Function/LocalFunction case (no nested returns collected). - for (const auto &stmt : std::dynamic_pointer_cast(pr.chunk)->Stmts()) { + for (const auto &stmt: std::dynamic_pointer_cast(pr.chunk)->Stmts()) { if (stmt->Type() == SyntaxTreeType::Function || stmt->Type() == SyntaxTreeType::LocalFunction) { sa.CollectReturnsForBlock(stmt, returns); } diff --git a/test/test_util.cpp b/test/test_util.cpp index d13d5576..e05132e3 100644 --- a/test/test_util.cpp +++ b/test/test_util.cpp @@ -1,10 +1,10 @@ #include "fakelua.h" #include "util/common.h" -#include "util/string_util.h" -#include "gtest/gtest.h" #include "util/file_util.h" #include "util/number_util.h" #include "util/os.h" +#include "util/string_util.h" +#include "gtest/gtest.h" #include #include #include @@ -185,3 +185,10 @@ TEST(util, generate_tmp_filename_collision) { std::filesystem::remove(first, ec); std::filesystem::remove(second, ec); } + +TEST(util, temporary_directory_shared_with_generated_filenames) { + const auto dir = std::filesystem::path(GetTmpDirectory()); + EXPECT_EQ(dir.filename(), "fakelua"); + EXPECT_TRUE(std::filesystem::is_directory(dir)); + EXPECT_EQ(std::filesystem::path(GenerateTmpFilename("directory_probe_", ".tmp")).parent_path(), dir); +} diff --git a/test/test_var.cpp b/test/test_var.cpp index a8212361..67b6830f 100644 --- a/test/test_var.cpp +++ b/test/test_var.cpp @@ -1,14 +1,14 @@ #include "fakelua.h" +#include "interp/interpreter.h" +#include "interp/runtime.h" +#include "native/table/native_table.h" #include "state/state.h" #include "var/var.h" #include "var/var_closure.h" -#include "var/var_string.h" #include "var/var_multi.h" -#include "native/table/native_table.h" -#include "gtest/gtest.h" -#include "interp/interpreter.h" -#include "interp/runtime.h" +#include "var/var_string.h" #include "var/var_type.h" +#include "gtest/gtest.h" #include #include #include @@ -371,7 +371,6 @@ TEST(var, test_true_string) { ASSERT_TRUE(empty_str.TestTrue());// empty string is still truthy in Lua } - TEST(var, tostring_hash_equal_table_multi_closure) { const FakeluaStateGuard guard; const auto s = guard.GetState(); diff --git a/tools/benchmark_jit_cache.cpp b/tools/benchmark_jit_cache.cpp new file mode 100644 index 00000000..8a0b109a --- /dev/null +++ b/tools/benchmark_jit_cache.cpp @@ -0,0 +1,80 @@ +// Standalone release harness for GCC JIT compilation and execution timings. +#include "fakelua.h" +#include +#include +#include +using namespace fakelua; +using Clock = std::chrono::steady_clock; + +static double Millis(Clock::time_point a, Clock::time_point b) { + return std::chrono::duration(b - a).count(); +} + +int main(int argc, char **argv) { + const std::string mode = argc > 1 ? argv[1] : "warm"; + const std::string directory = argc > 2 ? argv[2] : ""; + if (mode == "cold" && (directory.empty() || !std::filesystem::path(directory).filename().string().starts_with("fakelua-cache-benchmark-"))) { + std::cerr << "cold mode requires a disposable fakelua-cache-benchmark-* directory\n"; + return 1; + } + StateConfig sc; + sc.log_level = LogLevel::Off; +#ifndef FAKELUA_CACHE_BASELINE + sc.gcc_config.enable_cache = mode != "off"; + sc.gcc_config.cache_directory = directory; +#endif + CompileConfig cc; + DisableAllJit(cc); + cc.disable_jit[JIT_GCC] = false; + cc.debug_mode = false; + const std::string source = R"( +local tag = "same" +function arithmetic(n) + local sum=0 + for i=1,n do sum=sum+i end + return sum +end +function fields(n,key) + local t={a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14,o=15,p=16} + local sum=0 + for i=1,n do sum=sum+t[key] end + return sum +end +function interned_fields(n) + local t={a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10,k=11,l=12,m=13,n=14,o=15,p=16} + local keys={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"} + local sum=0 + for i=1,n do sum=sum+t[keys[((i-1)%16)+1]] end + return sum +end +function strings(n,key) + local sum=0 + for i=1,n do if key==tag then sum=sum+1 end end + return sum +end +)"; + std::cout << "compile_ms arithmetic_ms fields_ms strings_ms interned_fields_ms\n"; + for (int sample = 0; sample < 9; ++sample) { + // Only remove the explicitly supplied benchmark-owned directory. + if (mode == "cold" && !directory.empty()) std::filesystem::remove_all(directory); + FakeluaStateGuard state(sc); + auto *s = state.GetState(); + auto a = Clock::now(); + CompileString(s, source, cc); + auto b = Clock::now(); + int64_t result = 0; + for (int j = 0; j < 1000; ++j) Call(s, JIT_GCC, "arithmetic", result, 100000); + auto c = Clock::now(); + if (result != 5000050000LL) return 2; + for (int j = 0; j < 1000; ++j) Call(s, JIT_GCC, "fields", result, 10000, std::string("p")); + auto d = Clock::now(); + if (result != 160000) return 3; + for (int j = 0; j < 1000; ++j) Call(s, JIT_GCC, "strings", result, 10000, std::string("same")); + auto e = Clock::now(); + if (result != 10000) return 4; + for (int j = 0; j < 1000; ++j) Call(s, JIT_GCC, "interned_fields", result, 10000); + auto f = Clock::now(); + if (result != 85000) return 5; + std::cout << Millis(a, b) << ' ' << Millis(b, c) << ' ' << Millis(c, d) << ' ' << Millis(d, e) << ' ' << Millis(e, f) << '\n'; + } +}