From a356d492930427a1a42b4d8d4fc7a5bdaeb94e01 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 7 Nov 2025 17:47:33 +0800 Subject: Made YueScript stop lintting defined global variables. --- src/3rdParty/colib/ljson.c | 36 ++++++++++++++++++------------------ src/yuescript/yue_compiler.cpp | 16 ++++++++-------- src/yuescript/yue_compiler.h | 1 + src/yuescript/yuescript.cpp | 1 + 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/3rdParty/colib/ljson.c b/src/3rdParty/colib/ljson.c index e6fabeb..4daba07 100644 --- a/src/3rdParty/colib/ljson.c +++ b/src/3rdParty/colib/ljson.c @@ -200,11 +200,11 @@ typedef struct { int curdepth; // 当前层次 int maxdepth; // 最大层次 int allowcomment; // 是否允许注释 - char errmsg[ERRMSG_SIZE]; // 保存错误消息 + char errmsg[ERRMSG_SIZE]; // 保存错误消息 //>>>jmp_buf jb; // 用于实现从解析中出错直接跳出 } json_parser_t; -static inline void parser_init(json_parser_t *parser, const char *str, size_t size, void *ud, +static inline void parser_init(json_parser_t *parser, const char *str, size_t size, void *ud, int maxdepth, int allowcomment) { membuffer_init(&parser->buff); membuffer_ensure_space(&parser->buff, size); @@ -254,7 +254,7 @@ static const char* parser_error_content(json_parser_t *p) { static inline void parser_add_depth(json_parser_t *p) { p->curdepth++; if (p->curdepth >= p->maxdepth) - parser_throw_error(p, "Too many nested data, max depth is %d, at: %s[:%lu]", p->maxdepth, + parser_throw_error(p, "Too many nested data, max depth is %d, at: %s[:%lu]", p->maxdepth, parser_error_content(p), currpos(p)); } @@ -402,7 +402,7 @@ static inline void parser_process_string(json_parser_t *p) { } else { parser_throw_error(p, "Invalid escape sequence, at: %s[:%lu]", parser_error_content(p), currpos(p)); } - } else if (ch == '"') { + } else if (ch == '"') { break; } else if ((unsigned char)ch < 0x20) { parser_throw_error(p, "Invalid string, at: %s[:%lu]", parser_error_content(p), currpos(p)); @@ -490,7 +490,7 @@ static inline void parser_process_number(json_parser_t *p, char ch) { if (isdouble) { int n = exponent < 0 ? -exponent : exponent; - if (unlikely(n>511)) + if (unlikely(n>511)) n = 511; // inf double p10 = 1.0; double *d; @@ -576,13 +576,13 @@ static void parser_process_value(json_parser_t *p) { parser_skip_whitespaces(p); char ch = get_and_next(p); switch (ch) { - case 'f': + case 'f': parser_process_false(p); break; case 't': parser_process_true(p); break; - case 'n': + case 'n': parser_process_null(p); break; case '"': @@ -609,7 +609,7 @@ static void parser_do_parse(const char *str, size_t size, void *ud, int maxdepth parser_process_value(&p); parser_skip_whitespaces(&p); if (peekchar(&p) != '\0') { - parser_throw_error(&p, "Expect '' but got '%c', at: %s[:%lu]", peekchar(&p), + parser_throw_error(&p, "Expect '' but got '%c', at: %s[:%lu]", peekchar(&p), parser_error_content(&p), currpos(&p)); } parser_free(&p); @@ -625,7 +625,7 @@ typedef struct { int format; // 是否格式化 int empty_as_array; // 空表是否当成数组 int num_as_str; // 数字Key转为字符串 - char errmsg[ERRMSG_SIZE]; // 保存错误消息 + char errmsg[ERRMSG_SIZE]; // 保存错误消息 } json_dumpper_t; // 足够转换数字的缓存大小 @@ -699,7 +699,7 @@ static void dumpper_process_string(json_dumpper_t *d, lua_State *L, int idx) { for (i = 0; i < len; ++i) { ch = (unsigned char)str[i]; esc = char2escape[ch]; - if (likely(!esc)) + if (likely(!esc)) membuffer_putc_unsafe(buff, (char)ch); else { membuffer_putc_unsafe(buff, '\\'); @@ -781,7 +781,7 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) { } else { comma = 1; if (unlikely(d->format)) membuffer_putc(buff, '\n'); - } + } // key ktp = lua_type(L, -2); if (ktp == LUA_TSTRING) { @@ -814,7 +814,7 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) { if (unlikely(d->format && comma)) { membuffer_putc(buff, '\n'); dumpper_add_indent(d, depth-1); - } + } membuffer_putc(buff, '}'); } @@ -873,9 +873,9 @@ static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth) { // 接口 #define DEF_MAX_DEPTH 128 -// 从字符串加载:json.load(str, maxdepth) -> obj +// 从字符串加载:json.decode(str, maxdepth) -> obj // 要求字符串必须以0结尾 -int colibc_json_load(lua_State *L) { +int colibc_json_decode(lua_State *L) { size_t size; const char *str = luaL_checklstring(L, 1, &size); int maxdepth = (int)luaL_optinteger(L, 2, DEF_MAX_DEPTH); @@ -884,8 +884,8 @@ int colibc_json_load(lua_State *L) { return 1; } -// 保存到字符串: json.dump(obj) -> str -int colibc_json_dump(lua_State *L) { +// 保存到字符串: json.encode(obj) -> str +int colibc_json_encode(lua_State *L) { luaL_checkany(L, 1); json_dumpper_t dumpper; membuffer_init(&dumpper.buff); @@ -902,8 +902,8 @@ int colibc_json_dump(lua_State *L) { } static const luaL_Reg lib[] = { - {"load", colibc_json_load}, - {"dump", colibc_json_dump}, + {"decode", colibc_json_decode}, + {"encode", colibc_json_encode}, {NULL, NULL}, }; diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 75f4787..ed0a587 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -78,7 +78,7 @@ static std::unordered_set Metamethods = { "close"s // Lua 5.4 }; -const std::string_view version = "0.29.6"sv; +const std::string_view version = "0.29.7"sv; const std::string_view extension = "yue"sv; class CompileError : public std::logic_error { @@ -258,8 +258,8 @@ public: if (config.lintGlobalVariable) { globals = std::make_unique(); for (const auto& var : _globals) { - auto [name, line, col, accessType] = var.second; - globals->push_back({name, line + _config.lineOffset, col, accessType}); + auto [name, line, col, accessType, defined] = var.second; + globals->push_back({name, line + _config.lineOffset, col, accessType, defined}); } std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB) { if (varA.line < varB.line) { @@ -396,7 +396,7 @@ private: }; std::stack _continueVars; std::list> _codeCache; - std::unordered_map> _globals; + std::unordered_map> _globals; std::ostringstream _buf; std::ostringstream _joinBuf; const std::string _newLine = "\n"; @@ -1604,7 +1604,7 @@ private: if (accessType == AccessType::Read && _funcLevel > 1) { accessType = AccessType::Capture; } - _globals[key] = {str, x->m_begin.m_line, x->m_begin.m_col, accessType}; + _globals[key] = {str, x->m_begin.m_line, x->m_begin.m_col, accessType, isSolidDefined(str)}; } } } @@ -4588,7 +4588,7 @@ private: if (accessType == AccessType::Read && _funcLevel > 1) { accessType = AccessType::Capture; } - _globals[key] = {out.back(), item->m_begin.m_line, item->m_begin.m_col, accessType}; + _globals[key] = {out.back(), item->m_begin.m_line, item->m_begin.m_col, accessType, isSolidDefined(out.back())}; } } break; @@ -9179,7 +9179,7 @@ private: if (_config.lintGlobalVariable && !isLocal(name)) { auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col); if (_globals.find(key) != _globals.end()) { - _globals[key] = {name, pair->name->m_begin.m_line, pair->name->m_begin.m_col, _funcLevel > 1 ? AccessType::Capture : AccessType::Read}; + _globals[key] = {name, pair->name->m_begin.m_line, pair->name->m_begin.m_col, _funcLevel > 1 ? AccessType::Capture : AccessType::Read, isSolidDefined(name)}; } } } @@ -10829,7 +10829,7 @@ private: void transformImportFrom(ImportFrom_t* importNode, str_list& out) { str_list temp; - auto x = importNode; + auto x = importNode->item.get(); auto objVar = singleVariableFrom(importNode->item, AccessType::Read); ast_ptr objAssign; if (objVar.empty()) { diff --git a/src/yuescript/yue_compiler.h b/src/yuescript/yue_compiler.h index aff5978..cdd83e0 100644 --- a/src/yuescript/yue_compiler.h +++ b/src/yuescript/yue_compiler.h @@ -52,6 +52,7 @@ struct GlobalVar { int line; int col; AccessType accessType; + bool defined; }; using GlobalVars = std::vector; diff --git a/src/yuescript/yuescript.cpp b/src/yuescript/yuescript.cpp index 61e3949..2be74be 100644 --- a/src/yuescript/yuescript.cpp +++ b/src/yuescript/yuescript.cpp @@ -242,6 +242,7 @@ static int yuecheck(lua_State* L) { } if (result.globals) { for (const auto& global : *result.globals) { + if (global.defined) continue; lua_createtable(L, 4, 0); lua_pushliteral(L, "global"); lua_rawseti(L, -2, 1); -- cgit v1.2.3-55-g6feb