Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

function load env

  • Loading branch information...
commit c5654f4ea82bed914f9969bf2ef32839ad337c9d 1 parent 2170771
@cloudwu authored
Showing with 29 additions and 4 deletions.
  1. +24 −3 database.lua
  2. +1 −1  load.lua
  3. +4 −0 test.lua
View
27 database.lua
@@ -1,4 +1,6 @@
local c = require "database.c"
+local assert = assert
+local tostring = tostring
local database = {}
@@ -36,7 +38,9 @@ function database.open(loader)
return c.open(loader , posload)
end
-local ucache = setmetatable({} , { __mode = "v" } )
+local weak = { __mode = "v" }
+
+local ucache = setmetatable({} , weak )
local function cache(v, t)
local ret = ucache[v]
@@ -54,12 +58,29 @@ local function cache(v, t)
return ret
end
-function database.get(key)
+local fcache = setmetatable({} , weak)
+
+local function function_cache(v , key , env)
+ local h = tostring(v) .. tostring(env)
+ local ret = fcache[h]
+ if ret then
+ return ret
+ end
+ ret = load( c.expend(v), key , "b", env)
+ fcache[v] = ret
+
+ return ret
+end
+
+function database.get(key , env)
local v , t = c.get(key)
if type(v) ~= "userdata" then
return v
- else
+ elseif env == nil then
return cache(v,t)
+ else
+ assert(t == "function")
+ return function_cache(v,key,env)
end
end
View
2  load.lua
@@ -6,6 +6,6 @@ for i = 1, 100 do
e.A[i] = i * 2
end
-e.A.B = load [[function() return "hello" end]]
+e.A.B = function() return print, "hello" end
return e
View
4 test.lua
@@ -9,6 +9,10 @@ for k,v in pairs(t) do
print(k,v)
end
+local print = print
+
+f = db.get("A.B" , { print = function(...) print("--->",...) end })
+f()("Hello")
for i=1,1000 do
local f = db.get "A.B"
Please sign in to comment.
Something went wrong with that request. Please try again.