aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2025-11-07 17:47:33 +0800
committerLi Jin <dragon-fly@qq.com>2025-11-07 17:47:33 +0800
commita356d492930427a1a42b4d8d4fc7a5bdaeb94e01 (patch)
treea5c177b71b8f1e108fe390ef436f803d3eea4ae3
parentcd8a5fb1d22ad08ce292c8d399d99d63b8fb7724 (diff)
downloadyuescript-a356d492930427a1a42b4d8d4fc7a5bdaeb94e01.tar.gz
yuescript-a356d492930427a1a42b4d8d4fc7a5bdaeb94e01.tar.bz2
yuescript-a356d492930427a1a42b4d8d4fc7a5bdaeb94e01.zip
Made YueScript stop lintting defined global variables.v0.29.7
-rw-r--r--src/3rdParty/colib/ljson.c36
-rw-r--r--src/yuescript/yue_compiler.cpp16
-rw-r--r--src/yuescript/yue_compiler.h1
-rw-r--r--src/yuescript/yuescript.cpp1
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 {
200 int curdepth; // 当前层次 200 int curdepth; // 当前层次
201 int maxdepth; // 最大层次 201 int maxdepth; // 最大层次
202 int allowcomment; // 是否允许注释 202 int allowcomment; // 是否允许注释
203 char errmsg[ERRMSG_SIZE]; // 保存错误消息 203 char errmsg[ERRMSG_SIZE]; // 保存错误消息
204 //>>>jmp_buf jb; // 用于实现从解析中出错直接跳出 204 //>>>jmp_buf jb; // 用于实现从解析中出错直接跳出
205} json_parser_t; 205} json_parser_t;
206 206
207static inline void parser_init(json_parser_t *parser, const char *str, size_t size, void *ud, 207static inline void parser_init(json_parser_t *parser, const char *str, size_t size, void *ud,
208 int maxdepth, int allowcomment) { 208 int maxdepth, int allowcomment) {
209 membuffer_init(&parser->buff); 209 membuffer_init(&parser->buff);
210 membuffer_ensure_space(&parser->buff, size); 210 membuffer_ensure_space(&parser->buff, size);
@@ -254,7 +254,7 @@ static const char* parser_error_content(json_parser_t *p) {
254static inline void parser_add_depth(json_parser_t *p) { 254static inline void parser_add_depth(json_parser_t *p) {
255 p->curdepth++; 255 p->curdepth++;
256 if (p->curdepth >= p->maxdepth) 256 if (p->curdepth >= p->maxdepth)
257 parser_throw_error(p, "Too many nested data, max depth is %d, at: %s[:%lu]", p->maxdepth, 257 parser_throw_error(p, "Too many nested data, max depth is %d, at: %s[:%lu]", p->maxdepth,
258 parser_error_content(p), currpos(p)); 258 parser_error_content(p), currpos(p));
259} 259}
260 260
@@ -402,7 +402,7 @@ static inline void parser_process_string(json_parser_t *p) {
402 } else { 402 } else {
403 parser_throw_error(p, "Invalid escape sequence, at: %s[:%lu]", parser_error_content(p), currpos(p)); 403 parser_throw_error(p, "Invalid escape sequence, at: %s[:%lu]", parser_error_content(p), currpos(p));
404 } 404 }
405 } else if (ch == '"') { 405 } else if (ch == '"') {
406 break; 406 break;
407 } else if ((unsigned char)ch < 0x20) { 407 } else if ((unsigned char)ch < 0x20) {
408 parser_throw_error(p, "Invalid string, at: %s[:%lu]", parser_error_content(p), currpos(p)); 408 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) {
490 490
491 if (isdouble) { 491 if (isdouble) {
492 int n = exponent < 0 ? -exponent : exponent; 492 int n = exponent < 0 ? -exponent : exponent;
493 if (unlikely(n>511)) 493 if (unlikely(n>511))
494 n = 511; // inf 494 n = 511; // inf
495 double p10 = 1.0; 495 double p10 = 1.0;
496 double *d; 496 double *d;
@@ -576,13 +576,13 @@ static void parser_process_value(json_parser_t *p) {
576 parser_skip_whitespaces(p); 576 parser_skip_whitespaces(p);
577 char ch = get_and_next(p); 577 char ch = get_and_next(p);
578 switch (ch) { 578 switch (ch) {
579 case 'f': 579 case 'f':
580 parser_process_false(p); 580 parser_process_false(p);
581 break; 581 break;
582 case 't': 582 case 't':
583 parser_process_true(p); 583 parser_process_true(p);
584 break; 584 break;
585 case 'n': 585 case 'n':
586 parser_process_null(p); 586 parser_process_null(p);
587 break; 587 break;
588 case '"': 588 case '"':
@@ -609,7 +609,7 @@ static void parser_do_parse(const char *str, size_t size, void *ud, int maxdepth
609 parser_process_value(&p); 609 parser_process_value(&p);
610 parser_skip_whitespaces(&p); 610 parser_skip_whitespaces(&p);
611 if (peekchar(&p) != '\0') { 611 if (peekchar(&p) != '\0') {
612 parser_throw_error(&p, "Expect '<eof>' but got '%c', at: %s[:%lu]", peekchar(&p), 612 parser_throw_error(&p, "Expect '<eof>' but got '%c', at: %s[:%lu]", peekchar(&p),
613 parser_error_content(&p), currpos(&p)); 613 parser_error_content(&p), currpos(&p));
614 } 614 }
615 parser_free(&p); 615 parser_free(&p);
@@ -625,7 +625,7 @@ typedef struct {
625 int format; // 是否格式化 625 int format; // 是否格式化
626 int empty_as_array; // 空表是否当成数组 626 int empty_as_array; // 空表是否当成数组
627 int num_as_str; // 数字Key转为字符串 627 int num_as_str; // 数字Key转为字符串
628 char errmsg[ERRMSG_SIZE]; // 保存错误消息 628 char errmsg[ERRMSG_SIZE]; // 保存错误消息
629} json_dumpper_t; 629} json_dumpper_t;
630 630
631// 足够转换数字的缓存大小 631// 足够转换数字的缓存大小
@@ -699,7 +699,7 @@ static void dumpper_process_string(json_dumpper_t *d, lua_State *L, int idx) {
699 for (i = 0; i < len; ++i) { 699 for (i = 0; i < len; ++i) {
700 ch = (unsigned char)str[i]; 700 ch = (unsigned char)str[i];
701 esc = char2escape[ch]; 701 esc = char2escape[ch];
702 if (likely(!esc)) 702 if (likely(!esc))
703 membuffer_putc_unsafe(buff, (char)ch); 703 membuffer_putc_unsafe(buff, (char)ch);
704 else { 704 else {
705 membuffer_putc_unsafe(buff, '\\'); 705 membuffer_putc_unsafe(buff, '\\');
@@ -781,7 +781,7 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) {
781 } else { 781 } else {
782 comma = 1; 782 comma = 1;
783 if (unlikely(d->format)) membuffer_putc(buff, '\n'); 783 if (unlikely(d->format)) membuffer_putc(buff, '\n');
784 } 784 }
785 // key 785 // key
786 ktp = lua_type(L, -2); 786 ktp = lua_type(L, -2);
787 if (ktp == LUA_TSTRING) { 787 if (ktp == LUA_TSTRING) {
@@ -814,7 +814,7 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) {
814 if (unlikely(d->format && comma)) { 814 if (unlikely(d->format && comma)) {
815 membuffer_putc(buff, '\n'); 815 membuffer_putc(buff, '\n');
816 dumpper_add_indent(d, depth-1); 816 dumpper_add_indent(d, depth-1);
817 } 817 }
818 membuffer_putc(buff, '}'); 818 membuffer_putc(buff, '}');
819} 819}
820 820
@@ -873,9 +873,9 @@ static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth) {
873// 接口 873// 接口
874#define DEF_MAX_DEPTH 128 874#define DEF_MAX_DEPTH 128
875 875
876// 从字符串加载:json.load(str, maxdepth) -> obj 876// 从字符串加载:json.decode(str, maxdepth) -> obj
877// 要求字符串必须以0结尾 877// 要求字符串必须以0结尾
878int colibc_json_load(lua_State *L) { 878int colibc_json_decode(lua_State *L) {
879 size_t size; 879 size_t size;
880 const char *str = luaL_checklstring(L, 1, &size); 880 const char *str = luaL_checklstring(L, 1, &size);
881 int maxdepth = (int)luaL_optinteger(L, 2, DEF_MAX_DEPTH); 881 int maxdepth = (int)luaL_optinteger(L, 2, DEF_MAX_DEPTH);
@@ -884,8 +884,8 @@ int colibc_json_load(lua_State *L) {
884 return 1; 884 return 1;
885} 885}
886 886
887// 保存到字符串: json.dump(obj) -> str 887// 保存到字符串: json.encode(obj) -> str
888int colibc_json_dump(lua_State *L) { 888int colibc_json_encode(lua_State *L) {
889 luaL_checkany(L, 1); 889 luaL_checkany(L, 1);
890 json_dumpper_t dumpper; 890 json_dumpper_t dumpper;
891 membuffer_init(&dumpper.buff); 891 membuffer_init(&dumpper.buff);
@@ -902,8 +902,8 @@ int colibc_json_dump(lua_State *L) {
902} 902}
903 903
904static const luaL_Reg lib[] = { 904static const luaL_Reg lib[] = {
905 {"load", colibc_json_load}, 905 {"decode", colibc_json_decode},
906 {"dump", colibc_json_dump}, 906 {"encode", colibc_json_encode},
907 {NULL, NULL}, 907 {NULL, NULL},
908}; 908};
909 909
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<std::string> Metamethods = {
78 "close"s // Lua 5.4 78 "close"s // Lua 5.4
79}; 79};
80 80
81const std::string_view version = "0.29.6"sv; 81const std::string_view version = "0.29.7"sv;
82const std::string_view extension = "yue"sv; 82const std::string_view extension = "yue"sv;
83 83
84class CompileError : public std::logic_error { 84class CompileError : public std::logic_error {
@@ -258,8 +258,8 @@ public:
258 if (config.lintGlobalVariable) { 258 if (config.lintGlobalVariable) {
259 globals = std::make_unique<GlobalVars>(); 259 globals = std::make_unique<GlobalVars>();
260 for (const auto& var : _globals) { 260 for (const auto& var : _globals) {
261 auto [name, line, col, accessType] = var.second; 261 auto [name, line, col, accessType, defined] = var.second;
262 globals->push_back({name, line + _config.lineOffset, col, accessType}); 262 globals->push_back({name, line + _config.lineOffset, col, accessType, defined});
263 } 263 }
264 std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB) { 264 std::sort(globals->begin(), globals->end(), [](const GlobalVar& varA, const GlobalVar& varB) {
265 if (varA.line < varB.line) { 265 if (varA.line < varB.line) {
@@ -396,7 +396,7 @@ private:
396 }; 396 };
397 std::stack<ContinueVar> _continueVars; 397 std::stack<ContinueVar> _continueVars;
398 std::list<std::unique_ptr<input>> _codeCache; 398 std::list<std::unique_ptr<input>> _codeCache;
399 std::unordered_map<std::string, std::tuple<std::string, int, int, AccessType>> _globals; 399 std::unordered_map<std::string, std::tuple<std::string, int, int, AccessType, bool>> _globals;
400 std::ostringstream _buf; 400 std::ostringstream _buf;
401 std::ostringstream _joinBuf; 401 std::ostringstream _joinBuf;
402 const std::string _newLine = "\n"; 402 const std::string _newLine = "\n";
@@ -1604,7 +1604,7 @@ private:
1604 if (accessType == AccessType::Read && _funcLevel > 1) { 1604 if (accessType == AccessType::Read && _funcLevel > 1) {
1605 accessType = AccessType::Capture; 1605 accessType = AccessType::Capture;
1606 } 1606 }
1607 _globals[key] = {str, x->m_begin.m_line, x->m_begin.m_col, accessType}; 1607 _globals[key] = {str, x->m_begin.m_line, x->m_begin.m_col, accessType, isSolidDefined(str)};
1608 } 1608 }
1609 } 1609 }
1610 } 1610 }
@@ -4588,7 +4588,7 @@ private:
4588 if (accessType == AccessType::Read && _funcLevel > 1) { 4588 if (accessType == AccessType::Read && _funcLevel > 1) {
4589 accessType = AccessType::Capture; 4589 accessType = AccessType::Capture;
4590 } 4590 }
4591 _globals[key] = {out.back(), item->m_begin.m_line, item->m_begin.m_col, accessType}; 4591 _globals[key] = {out.back(), item->m_begin.m_line, item->m_begin.m_col, accessType, isSolidDefined(out.back())};
4592 } 4592 }
4593 } 4593 }
4594 break; 4594 break;
@@ -9179,7 +9179,7 @@ private:
9179 if (_config.lintGlobalVariable && !isLocal(name)) { 9179 if (_config.lintGlobalVariable && !isLocal(name)) {
9180 auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col); 9180 auto key = name + ':' + std::to_string(pair->name->m_begin.m_line) + ':' + std::to_string(pair->name->m_begin.m_col);
9181 if (_globals.find(key) != _globals.end()) { 9181 if (_globals.find(key) != _globals.end()) {
9182 _globals[key] = {name, pair->name->m_begin.m_line, pair->name->m_begin.m_col, _funcLevel > 1 ? AccessType::Capture : AccessType::Read}; 9182 _globals[key] = {name, pair->name->m_begin.m_line, pair->name->m_begin.m_col, _funcLevel > 1 ? AccessType::Capture : AccessType::Read, isSolidDefined(name)};
9183 } 9183 }
9184 } 9184 }
9185 } 9185 }
@@ -10829,7 +10829,7 @@ private:
10829 10829
10830 void transformImportFrom(ImportFrom_t* importNode, str_list& out) { 10830 void transformImportFrom(ImportFrom_t* importNode, str_list& out) {
10831 str_list temp; 10831 str_list temp;
10832 auto x = importNode; 10832 auto x = importNode->item.get();
10833 auto objVar = singleVariableFrom(importNode->item, AccessType::Read); 10833 auto objVar = singleVariableFrom(importNode->item, AccessType::Read);
10834 ast_ptr<false, ExpListAssign_t> objAssign; 10834 ast_ptr<false, ExpListAssign_t> objAssign;
10835 if (objVar.empty()) { 10835 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 {
52 int line; 52 int line;
53 int col; 53 int col;
54 AccessType accessType; 54 AccessType accessType;
55 bool defined;
55}; 56};
56 57
57using GlobalVars = std::vector<GlobalVar>; 58using GlobalVars = std::vector<GlobalVar>;
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) {
242 } 242 }
243 if (result.globals) { 243 if (result.globals) {
244 for (const auto& global : *result.globals) { 244 for (const auto& global : *result.globals) {
245 if (global.defined) continue;
245 lua_createtable(L, 4, 0); 246 lua_createtable(L, 4, 0);
246 lua_pushliteral(L, "global"); 247 lua_pushliteral(L, "global");
247 lua_rawseti(L, -2, 1); 248 lua_rawseti(L, -2, 1);