Skip to content

Commit a1c1fbb

Browse files
committed
Improve globals
1 parent 6c82e8b commit a1c1fbb

File tree

1 file changed

+36
-251
lines changed

1 file changed

+36
-251
lines changed

server/script/vm/getGlobals.lua

Lines changed: 36 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -7,134 +7,39 @@ local rbxlibs = require 'library.rbxlibs'
77
local rojo = require 'library.rojo'
88
local util = require 'utility'
99
local vm = require 'vm.vm'
10-
local ws = require 'workspace'
1110

12-
local function getGlobalsOfFile(uri)
13-
if not files.exists(uri) then
14-
return {}
15-
end
16-
local cache = files.getCache(uri)
17-
if cache.globals then
18-
return cache.globals
19-
end
11+
function vm.getGlobals(key, uri, onlySet)
2012
local globals = {}
21-
cache.globals = globals
22-
local ast = files.getAst(uri)
23-
if not ast then
24-
return globals
25-
end
26-
tracy.ZoneBeginN 'getGlobalsOfFile'
27-
local results = guide.findGlobals(ast.ast)
28-
local subscribe = ws.getCache 'globalSubscribe'
29-
subscribe[uri] = {}
30-
local mark = {}
31-
if not globals['*'] then
32-
globals['*'] = {}
33-
end
34-
for _, res in ipairs(results) do
35-
if mark[res] then
36-
goto CONTINUE
37-
end
38-
mark[res] = true
39-
local name = guide.getSimpleName(res)
40-
if name then
41-
if not globals[name] then
42-
globals[name] = {}
43-
subscribe[uri][#subscribe[uri]+1] = name
44-
end
45-
globals[name][#globals[name]+1] = res
46-
globals['*'][#globals['*']+1] = res
47-
end
48-
::CONTINUE::
49-
end
50-
tracy.ZoneEnd()
51-
return globals
52-
end
53-
54-
local function getGlobalSetsOfFile(uri)
55-
if not files.exists(uri) then
56-
return {}
57-
end
58-
local cache = files.getCache(uri)
59-
if cache.globalSets then
60-
return cache.globalSets
61-
end
62-
local globals = {}
63-
cache.globalSets = globals
64-
local ast = files.getAst(uri)
65-
if not ast then
66-
return globals
67-
end
68-
tracy.ZoneBeginN 'getGlobalSetsOfFile'
69-
local results = guide.findGlobals(ast.ast)
70-
local subscribe = ws.getCache 'globalSetsSubscribe'
71-
subscribe[uri] = {}
72-
local mark = {}
73-
if not globals['*'] then
74-
globals['*'] = {}
75-
end
76-
for _, res in ipairs(results) do
77-
if mark[res] then
78-
goto CONTINUE
79-
end
80-
mark[res] = true
81-
if vm.isSet(res) then
82-
local name = guide.getSimpleName(res)
83-
if name then
84-
if not globals[name] then
85-
globals[name] = {}
86-
subscribe[uri][#subscribe[uri]+1] = name
87-
end
88-
globals[name][#globals[name]+1] = res
89-
globals['*'][#globals['*']+1] = res
90-
end
91-
end
92-
::CONTINUE::
93-
end
94-
tracy.ZoneEnd()
95-
return globals
96-
end
97-
98-
local function insertLibrary(results, name, uri)
13+
local scriptCache = vm.getCache 'globalDummy'
9914
for _, lib in pairs(rbxlibs.global) do
100-
if name == "*" or lib.name == name then
15+
if key == "*" or lib.name == key then
10116
if lib.name == "script" and uri then
102-
local script = util.shallowCopy(lib)
103-
local scriptValue = rojo.Scripts[uri]
104-
if scriptValue then
105-
script.value = scriptValue
106-
else
107-
script.value[1] = rojo:scriptClass(uri)
17+
if not scriptCache[uri] then
18+
local script = util.shallowCopy(lib)
19+
local scriptValue = rojo.Scripts[uri]
20+
if scriptValue then
21+
script.value = scriptValue
22+
else
23+
script.value[1] = rojo:scriptClass(uri)
24+
end
25+
scriptCache[uri] = script
10826
end
109-
results[#results+1] = script
27+
globals[#globals+1] = scriptCache[uri]
11028
else
111-
results[#results+1] = lib
29+
globals[#globals+1] = lib
11230
end
11331
end
11432
end
11533
if uri:match("%.spec%.lua$") or uri:match("%.spec%/init%.lua$") then
11634
for _, lib in pairs(defaultlibs.testez) do
117-
if name == "*" or lib.name == name then
118-
results[#results+1] = lib
35+
if key == "*" or lib.name == key then
36+
globals[#globals+1] = lib
11937
end
12038
end
12139
end
122-
end
123-
124-
local function getGlobals(name, uri)
125-
tracy.ZoneBeginN 'getGlobals #2'
126-
local results = {}
127-
local n = 0
128-
local globals = getGlobalsOfFile(uri)[name]
129-
if globals then
130-
for j = 1, #globals do
131-
n = n + 1
132-
results[n] = globals[j]
133-
end
134-
end
13540
local dummyCache = vm.getCache 'globalDummy'
136-
for key in pairs(config.config.diagnostics.globals) do
137-
if name == '*' or name == key then
41+
for name in pairs(config.config.diagnostics.globals) do
42+
if key == '*' or name == key then
13843
if not dummyCache[key] then
13944
dummyCache[key] = {
14045
type = 'dummy',
@@ -143,153 +48,33 @@ local function getGlobals(name, uri)
14348
[1] = key
14449
}
14550
end
146-
n = n + 1
147-
results[n] = dummyCache[key]
51+
globals[#globals+1] = dummyCache[key]
14852
end
14953
end
150-
insertLibrary(results, name, uri)
151-
tracy.ZoneEnd()
152-
return results
153-
end
154-
155-
local function getGlobalSets(name, uri)
156-
tracy.ZoneBeginN 'getGlobalSets #2'
157-
local results = {}
158-
local n = 0
159-
local globals = getGlobalSetsOfFile(uri)[name]
160-
if globals then
161-
for j = 1, #globals do
162-
n = n + 1
163-
results[n] = globals[j]
164-
end
165-
end
166-
local dummyCache = vm.getCache 'globalDummy'
167-
for key in pairs(config.config.diagnostics.globals) do
168-
if name == '*' or name == key then
169-
if not dummyCache[key] then
170-
dummyCache[key] = {
171-
type = 'dummy',
172-
start = 0,
173-
finish = 0,
174-
[1] = key
175-
}
176-
end
177-
n = n + 1
178-
results[n] = dummyCache[key]
179-
end
54+
if not uri or not files.exists(uri) then
55+
return globals
18056
end
181-
insertLibrary(results, name, uri)
182-
tracy.ZoneEnd()
183-
return results
184-
end
185-
186-
local function fastGetAnyGlobals()
187-
local results = {}
188-
local mark = {}
189-
for uri in files.eachFile() do
190-
--local globalSets = getGlobalsOfFile(uri)
191-
--for destName, sources in util.sortPairs(globalSets) do
192-
-- if not mark[destName] then
193-
-- mark[destName] = true
194-
-- results[#results+1] = sources[1]
195-
-- end
196-
--end
197-
local globals = getGlobalsOfFile(uri)
198-
for destName, sources in util.sortPairs(globals) do
199-
if not mark[destName] then
200-
mark[destName] = true
201-
results[#results+1] = sources[1]
202-
end
203-
end
57+
local ast = files.getAst(uri)
58+
if not ast then
59+
return globals
20460
end
205-
return results
206-
end
207-
208-
local function fastGetAnyGlobalSets()
209-
local results = {}
61+
local fileGlobals = guide.findGlobals(ast.ast)
21062
local mark = {}
211-
for uri in files.eachFile() do
212-
local globals = getGlobalSetsOfFile(uri)
213-
for destName, sources in util.sortPairs(globals) do
214-
if not mark[destName] then
215-
mark[destName] = true
216-
results[#results+1] = sources[1]
217-
end
63+
for _, res in ipairs(fileGlobals) do
64+
if mark[res] then
65+
goto CONTINUE
21866
end
219-
end
220-
return results
221-
end
222-
223-
local function checkNeedUpdate()
224-
local getGlobalCache = ws.getCache 'getGlobals'
225-
local getGlobalSetsCache = ws.getCache 'getGlobalSets'
226-
local needUpdateGlobals = ws.getCache 'needUpdateGlobals'
227-
local uris = {}
228-
for uri in pairs(needUpdateGlobals) do
229-
uris[#uris+1] = uri
230-
end
231-
for _, uri in ipairs(uris) do
232-
if needUpdateGlobals[uri] then
233-
needUpdateGlobals[uri] = nil
234-
if files.exists(uri) then
235-
for name in pairs(getGlobalsOfFile(uri)) do
236-
getGlobalCache[name] = nil
237-
end
238-
for name in pairs(getGlobalSetsOfFile(uri)) do
239-
getGlobalSetsCache[name] = nil
240-
end
67+
if not onlySet or vm.isSet(res) then
68+
mark[res] = true
69+
if key == "*" or guide.getSimpleName(res) == key then
70+
globals[#globals+1] = res
24171
end
24272
end
73+
::CONTINUE::
24374
end
244-
end
245-
246-
function vm.getGlobals(key, uri)
247-
checkNeedUpdate()
248-
local cache = ws.getCache('getGlobals')[key .. uri]
249-
if cache ~= nil then
250-
return cache
251-
end
252-
cache = getGlobals(key, uri)
253-
ws.getCache('getGlobals')[key .. uri] = cache
254-
return cache
75+
return globals
25576
end
25677

25778
function vm.getGlobalSets(key, uri)
258-
checkNeedUpdate()
259-
local cache = ws.getCache('getGlobalSets')[key .. uri]
260-
if cache ~= nil then
261-
return cache
262-
end
263-
tracy.ZoneBeginN('getGlobalSets')
264-
cache = getGlobalSets(key, uri)
265-
ws.getCache('getGlobalSets')[key .. uri] = cache
266-
tracy.ZoneEnd()
267-
return cache
268-
end
269-
270-
files.watch(function (ev, uri)
271-
if ev == 'update' then
272-
local globalSubscribe = ws.getCache 'globalSubscribe'
273-
local globalSetsSubscribe = ws.getCache 'globalSetsSubscribe'
274-
local getGlobalCache = ws.getCache 'getGlobals'
275-
local getGlobalSetsCache = ws.getCache 'getGlobalSets'
276-
local needUpdateGlobals = ws.getCache 'needUpdateGlobals'
277-
uri = files.asKey(uri)
278-
if globalSubscribe[uri] then
279-
for _, name in ipairs(globalSubscribe[uri]) do
280-
getGlobalCache[name .. uri] = nil
281-
getGlobalCache['*' .. uri] = nil
282-
end
283-
end
284-
if globalSetsSubscribe[uri] then
285-
for _, name in ipairs(globalSetsSubscribe[uri]) do
286-
getGlobalSetsCache[name .. uri] = nil
287-
getGlobalSetsCache['*' .. uri] = nil
288-
end
289-
end
290-
needUpdateGlobals[uri] = true
291-
elseif ev == 'create' then
292-
getGlobalsOfFile(uri)
293-
getGlobalSetsOfFile(uri)
294-
end
295-
end)
79+
return vm.getGlobals(key, uri, true)
80+
end

0 commit comments

Comments
 (0)