Skip to content

Commit 178426b

Browse files
code-health: rawset/rawget for global variables
This patch fixes the following bug: if strict mode is enabled and there is no `_G[_TARANTOOL]`, require fails with "variable '_TARANTOOL' not declared". It also fixes several luacheck warnings about using undeclared global. Part of tarantool/tarantool#7726
1 parent 605ef94 commit 178426b

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
### Fixed
1010

1111
- Fixed compatibility with LuaJIT.
12+
- Fixed compatibility with LuaJIT if strict mode is on.
1213

1314
## [3.1.0] - 2020-10-02
1415

checks.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local _qualifiers_cache = {
1919
}
2020

2121
local function is_tarantool()
22-
return _G['_TARANTOOL'] ~= nil
22+
return rawget(_G, '_TARANTOOL') ~= nil
2323
end
2424

2525
--- Check that string (or substring) starts with given string
@@ -113,7 +113,7 @@ local function check_string_type(value, expected_type)
113113
return true
114114
end
115115

116-
local checker = _G.checkers[typ]
116+
local checker = rawget(_G, 'checkers')[typ]
117117
if type(checker) == 'function' and checker(value) == true then
118118
return true
119119
end
@@ -156,7 +156,7 @@ local function check_table_type(tbl, expected_fields)
156156
return nil, string.format(efmt, '%s'..keyname_fmt(expected_key), '%s')
157157
end
158158

159-
if _G._checks_v2_compatible and value == nil then
159+
if rawget(_G, '_checks_v2_compatible') and value == nil then
160160
value = {}
161161
tbl[expected_key] = value
162162
end
@@ -232,7 +232,7 @@ local function checks(...)
232232
error(err, level)
233233
end
234234

235-
if _G._checks_v2_compatible and value == nil then
235+
if rawget(_G, '_checks_v2_compatible') and value == nil then
236236
value = {}
237237
debug.setlocal(level, i, value)
238238
end
@@ -253,9 +253,13 @@ local function checks(...)
253253
end
254254
end
255255

256-
_G.checks = checks
257-
_G.checkers = rawget(_G, 'checkers') or {}
258-
_G._checks_v2_compatible = rawget(_G, '_checks_v2_compatible') or false
256+
rawset(_G, 'checks', checks)
257+
258+
local checkers = rawget(_G, 'checkers') or {}
259+
rawset(_G, 'checkers', checkers)
260+
261+
local _checks_v2_compatible = rawget(_G, '_checks_v2_compatible') or false
262+
rawset(_G, '_checks_v2_compatible', _checks_v2_compatible)
259263

260264
local ffi = require('ffi')
261265
function checkers.uint64(arg)

0 commit comments

Comments
 (0)