summaryrefslogtreecommitdiff
path: root/server/src/vm/chain.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/vm/chain.lua')
-rw-r--r--server/src/vm/chain.lua49
1 files changed, 49 insertions, 0 deletions
diff --git a/server/src/vm/chain.lua b/server/src/vm/chain.lua
new file mode 100644
index 00000000..23b38a75
--- /dev/null
+++ b/server/src/vm/chain.lua
@@ -0,0 +1,49 @@
+local valueMgr = require 'vm.value'
+local sourceMgr = require 'vm.source'
+
+local mt = {}
+mt.__index = mt
+mt.type = 'chain'
+
+mt.min = 100
+mt.max = 100
+mt.count = 0
+
+function mt:clearCache()
+ if self.count <= self.max then
+ return
+ end
+ local n = 0
+ for uri, value in pairs(self.cache) do
+ local ok = value:eachInfo(function ()
+ return true
+ end)
+ if ok then
+ n = n + 1
+ else
+ self.cache[uri] = nil
+ end
+ end
+ self.count = n
+ self.max = self.count * 2
+ if self.max < self.min then
+ self.max = self.min
+ end
+end
+
+function mt:get(uri)
+ if not self.cache[uri] then
+ self.count = self.count + 1
+ self:clearCache()
+ self.cache[uri] = valueMgr.create('any', sourceMgr.dummy())
+ self.cache[uri]:markGlobal()
+ self.cache[uri].uri = uri
+ end
+ return self.cache[uri]
+end
+
+return function ()
+ return setmetatable({
+ cache = {},
+ }, mt)
+end