Skip to content

Commit a41acbc

Browse files
committed
global supports and tests
1 parent 51846ea commit a41acbc

File tree

2 files changed

+174
-4
lines changed

2 files changed

+174
-4
lines changed

script/parser/compile.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,13 +2910,12 @@ local function bindValue(n, v, index, lastValue, isLocal, isSet)
29102910
if n.isGlobalDecl then
29112911
-- duplicate initialization check (only for global declarations with value)
29122912
local info = gi.names[nameKey]
2913-
if n.value then
2913+
if v then
29142914
if info and info.initialized then
29152915
pushError {
29162916
type = 'GLOBAL_DUPLICATE_INIT',
29172917
start = n.start,
29182918
finish = n.finish,
2919-
info = { name = nameKey },
29202919
}
29212920
else
29222921
if info then
@@ -2933,7 +2932,6 @@ local function bindValue(n, v, index, lastValue, isLocal, isSet)
29332932
type = 'UNDEFINED_IN_GLOBAL_SCOPE',
29342933
start = n.start,
29352934
finish = n.finish,
2936-
info = { name = nameKey },
29372935
}
29382936
end
29392937
local isConst = (info and info.const) or gi.allConst
@@ -2942,7 +2940,6 @@ local function bindValue(n, v, index, lastValue, isLocal, isSet)
29422940
type = 'ASSIGN_CONST_GLOBAL',
29432941
start = n.start,
29442942
finish = n.finish,
2945-
info = { name = nameKey },
29462943
}
29472944
end
29482945
end
@@ -3226,6 +3223,21 @@ local function parseGlobal()
32263223
name.vstart = func.start
32273224
name.range = func.finish
32283225
func.parent = name
3226+
name.isGlobalDecl = true
3227+
local gi = State.globalInfo
3228+
gi.active = true
3229+
local nameKey = name[1]
3230+
local info = gi.names[nameKey]
3231+
if info and info.initialized then
3232+
pushError {
3233+
type = 'GLOBAL_DUPLICATE_INIT',
3234+
start = name.start,
3235+
finish = name.finish,
3236+
}
3237+
else
3238+
gi.names[nameKey] = gi.names[nameKey] or { declared = true, const = false, initialized = false }
3239+
gi.names[nameKey].initialized = true
3240+
end
32293241
pushActionIntoCurrentChunk(name)
32303242
return name
32313243
else

test/parser_test/syntax_check.lua

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,161 @@ TestWith 'LuaJIT' [[
809809
local function f(x, goto, y) end
810810
]]
811811
(nil)
812+
813+
Version = 'Lua 5.5'
814+
815+
TEST [[
816+
global <!<close>!> x
817+
]]
818+
{
819+
type = 'GLOBAL_CLOSE_ATTRIBUTE',
820+
}
821+
822+
TEST [[
823+
global x = 1
824+
global <!x!> = 2
825+
]]
826+
{
827+
type = 'GLOBAL_DUPLICATE_INIT',
828+
}
829+
830+
-- Duplicate global definitions: multiple initializations of the same name
831+
TEST [[
832+
global x = 1
833+
global function <!x!>() end
834+
]]
835+
{
836+
type = 'GLOBAL_DUPLICATE_INIT',
837+
}
838+
839+
TEST [[
840+
global function f() end
841+
global function <!f!>() end
842+
]]
843+
{
844+
type = 'GLOBAL_DUPLICATE_INIT',
845+
}
846+
847+
TEST [[
848+
global <const> x = 1
849+
global <!x!> = 2
850+
]]
851+
{
852+
type = 'GLOBAL_DUPLICATE_INIT',
853+
}
854+
855+
TEST [[
856+
global x = 1
857+
global x
858+
global <!x!> = 2
859+
]]
860+
{
861+
type = 'GLOBAL_DUPLICATE_INIT',
862+
}
863+
864+
TEST [[
865+
global x = 1
866+
do
867+
global <!x!> = 2
868+
end
869+
]]
870+
{
871+
type = 'GLOBAL_DUPLICATE_INIT',
872+
}
873+
874+
TEST [[
875+
global <const> x = 1
876+
<!x!> = 2
877+
]]
878+
{
879+
type = 'ASSIGN_CONST_GLOBAL',
880+
}
881+
882+
TEST [[
883+
global <const> *
884+
<!x!> = 1
885+
]]
886+
{
887+
type = 'ASSIGN_CONST_GLOBAL',
888+
}
889+
890+
TEST [[
891+
global x
892+
<!y!> = 1
893+
]]
894+
{
895+
type = 'UNDEFINED_IN_GLOBAL_SCOPE',
896+
}
897+
898+
TEST [[
899+
global *
900+
x = 1
901+
]]
902+
(nil)
903+
904+
TEST [[
905+
global function f() end
906+
]]
907+
(nil)
908+
909+
TEST [[
910+
global x, y = 1, 2
911+
]]
912+
(nil)
913+
914+
TEST [[
915+
global <const> x = 1
916+
]]
917+
(nil)
918+
919+
-- Shadowing rules (Lua 5.5): local variables shadow globals within lexical scope
920+
921+
TEST [[
922+
global x
923+
local x = 1
924+
x = 2
925+
]]
926+
(nil)
927+
928+
TEST [[
929+
global x
930+
do
931+
local x = 1
932+
x = 2
933+
end
934+
]]
935+
(nil)
936+
937+
TEST [[
938+
global <const> x = 1
939+
local x = 2
940+
x = 3
941+
]]
942+
(nil)
943+
944+
TEST [[
945+
global <const> *
946+
local x = 1
947+
x = 2
948+
]]
949+
(nil)
950+
951+
TEST [[
952+
global x
953+
function f()
954+
local x
955+
x = 1
956+
end
957+
]]
958+
(nil)
959+
960+
-- Still error when assigning to truly undefined name under strict global scope
961+
TEST [[
962+
global x
963+
do
964+
<!y!> = 1
965+
end
966+
]]
967+
{
968+
type = 'UNDEFINED_IN_GLOBAL_SCOPE',
969+
}

0 commit comments

Comments
 (0)