Skip to content

Commit bb6ddf1

Browse files
committed
more check about access
1 parent 2decf0a commit bb6ddf1

File tree

15 files changed

+97
-16
lines changed

15 files changed

+97
-16
lines changed

locale/en-us/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ PARSER_LOCAL_LIMIT =
323323
'Only 200 active local variables and upvalues can be existed at the same time.'
324324
PARSER_VARIABLE_NOT_DECLARED =
325325
'Variable `{name}` is not declared. (Use `global *` to allow undefined variables, or declare it with `global {name}`)'
326+
PARSER_ENV_IS_GLOBAL =
327+
'_ENV is global when accessing variable `{name}`.'
326328
PARSER_ASSIGN_CONST_GLOBAL =
327329
'Cannot assign to const global variable `{name}`.'
328330
PARSER_LUADOC_MISS_CLASS_NAME =

locale/es-419/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ PARSER_LOCAL_LIMIT =
317317
'Solo 200 variables locales activas y valores anteriores pueden existir al mismo tiempo.'
318318
PARSER_VARIABLE_NOT_DECLARED =
319319
'Variable `{name}` no está declarada. (Use `global *` para permitir variables indefinidas, o declárela con `global {name}`)'
320+
PARSER_ENV_IS_GLOBAL =
321+
'_ENV es global al acceder a la variable `{name}`.'
320322
PARSER_ASSIGN_CONST_GLOBAL =
321323
'No se puede asignar a la variable global constante `{name}`.'
322324
PARSER_LUADOC_MISS_CLASS_NAME =

locale/ja-jp/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ PARSER_LOCAL_LIMIT =
319319
'同時に存在できるローカル変数とアップバリューの数は200個までです。'
320320
PARSER_VARIABLE_NOT_DECLARED =
321321
'変数 `{name}` は宣言されていません。(`global *` を使用して未定義の変数を許可するか、`global {name}` で宣言してください)'
322+
PARSER_ENV_IS_GLOBAL =
323+
'変数 `{name}` にアクセスする際、_ENV がグローバル変数です。'
322324
PARSER_ASSIGN_CONST_GLOBAL =
323325
'定数グローバル変数 `{name}` に代入できません。'
324326
PARSER_LUADOC_MISS_CLASS_NAME =

locale/pt-br/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ PARSER_LOCAL_LIMIT = -- TODO: need translate!
319319
'Only 200 active local variables and upvalues can be existed at the same time.'
320320
PARSER_VARIABLE_NOT_DECLARED =
321321
'Variável `{name}` não foi declarada. (Use `global *` para permitir variáveis indefinidas, ou declare com `global {name}`)'
322+
PARSER_ENV_IS_GLOBAL =
323+
'_ENV é global ao acessar a variável `{name}`.'
322324
PARSER_ASSIGN_CONST_GLOBAL =
323325
'Não é possível atribuir à variável global constante `{name}`.'
324326
PARSER_LUADOC_MISS_CLASS_NAME =

locale/zh-cn/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ PARSER_LOCAL_LIMIT =
323323
'只能同时存在200个活跃的局部变量与上值。'
324324
PARSER_VARIABLE_NOT_DECLARED =
325325
'变量 `{name}` 未声明。(使用 `global *` 允许未定义变量,或使用 `global {name}` 声明)'
326+
PARSER_ENV_IS_GLOBAL =
327+
'访问变量 `{name}` 时 _ENV 是全局变量。'
326328
PARSER_ASSIGN_CONST_GLOBAL =
327329
'无法对常量全局变量 `{name}` 赋值。'
328330
PARSER_LUADOC_MISS_CLASS_NAME =

locale/zh-tw/script.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ PARSER_LOCAL_LIMIT =
319319
'只能同時存在200個活躍的區域變數與上值。'
320320
PARSER_VARIABLE_NOT_DECLARED =
321321
'變數 `{name}` 未宣告。(使用 `global *` 允許未定義變數,或使用 `global {name}` 宣告)'
322+
PARSER_ENV_IS_GLOBAL =
323+
'存取變數 `{name}` 時 _ENV 是全域變數。'
322324
PARSER_ASSIGN_CONST_GLOBAL =
323325
'無法對常數全域變數 `{name}` 賦值。'
324326
PARSER_LUADOC_MISS_CLASS_NAME =

script/core/diagnostics/global-in-nil-env.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ return function (uri, callback)
1010

1111
local function check(source)
1212
local node = source.node
13+
if not node then
14+
return
15+
end
1316
if node.tag == '_ENV' then
1417
return
1518
end

script/core/diagnostics/undefined-env-child.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ return function (uri, callback)
2525
end
2626

2727
guide.eachSourceType(state.ast, 'getglobal', function (source)
28+
if not source.node then
29+
return
30+
end
2831
if source.node.tag == '_ENV' then
2932
return
3033
end

script/parser/compile.lua

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,16 @@ local function getVariable(name, pos)
805805
-- Find global in this chunk (globals don't have effect time, just find the last one)
806806
local globals = chunk.globals
807807
if globals then
808-
for n = #globals, 1, -1 do
808+
for n = 1, #globals do
809809
local glob = globals[n]
810-
if glob[1] == name then
811-
resGlobal = glob
810+
if glob.effect > pos then
812811
break
813812
end
813+
if glob[1] == name then
814+
if not resGlobal or resGlobal.effect < glob.effect then
815+
resGlobal = glob
816+
end
817+
end
814818
end
815819
end
816820

@@ -865,6 +869,9 @@ local function linkGlobalToEnv(node, var)
865869
type = 'VARIABLE_NOT_DECLARED',
866870
start = node.start,
867871
finish = node.finish,
872+
info = {
873+
name = node[1],
874+
}
868875
}
869876
end
870877
end
@@ -873,13 +880,13 @@ local function linkGlobalToEnv(node, var)
873880
local env
874881
if State.version == 'Lua 5.5' then
875882
env = getVariable(State.ENVMode, node.start)
876-
if env and env.type == 'global' then
883+
if env and env.type == 'setglobal' then
877884
pushError {
878-
type = 'RUNTIME_ERROR',
885+
type = 'ENV_IS_GLOBAL',
879886
start = node.start,
880887
finish = node.finish,
881888
info = {
882-
message = '_ENV is global when accessing variable'
889+
name = node[1],
883890
}
884891
}
885892
end
@@ -906,6 +913,7 @@ end
906913
local function createGlobalDeclare(obj, attrs)
907914
obj.type = 'setglobal'
908915
obj.declare = true
916+
obj.effect = obj.finish
909917

910918
if attrs then
911919
obj.attrs = attrs
@@ -3188,18 +3196,18 @@ local function parseMultiVars(n1, parser, isLocal)
31883196
end
31893197
end
31903198

3191-
if isLocal then
3192-
local effect = lastValue and lastValue.finish or lastVar.finish
3193-
n1.effect = effect
3194-
if n2 then
3195-
n2.effect = effect
3196-
end
3197-
if nrest then
3198-
for i = 1, #nrest do
3199-
nrest[i].effect = effect
3200-
end
3199+
local effect = lastValue and lastValue.finish or lastVar.finish
3200+
n1.effect = effect
3201+
if n2 then
3202+
n2.effect = effect
3203+
end
3204+
if nrest then
3205+
for i = 1, #nrest do
3206+
nrest[i].effect = effect
32013207
end
3208+
end
32023209

3210+
do
32033211
-- Lua 5.4: only one <close> attribute allowed across a local declaration
32043212
-- Lua 5.5: multiple <close> are allowed
32053213
if State.version == 'Lua 5.4' then

script/vm/compiler.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,6 +1714,9 @@ local compilerSwitch = util.switch()
17141714
if vm.bindAs(source) then
17151715
return
17161716
end
1717+
if not source.node then
1718+
return
1719+
end
17171720
if source.node[1] ~= '_ENV' then
17181721
return
17191722
end

0 commit comments

Comments
 (0)