lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2019-08-08 10:10 p.m., Luiz Henrique de Figueiredo wrote:
> Why "function values caching" has been removed from Lua 5.4?

See https://github.com/lua/lua/commit/e8c779736f3029df353038352c14c8ab63728811


say that to cratera: (look for "INPUT"/"OUTPUT" markers)

----------------------------------------- INPUT
-- Cratera-specific tests. Run Lua test suite separately.

local t = setmetatable({}, { __tostring=function()return"t"end})
local F = {}
local T = {}
t.t = t
t.tt = t
t[T] = t
t.f = print
t.ff = print
t.g = function(self, a) print(self, a[1]) end
t[F] = print
local _f="f"
local _t="t"

-- print("------ t:[k]()")
-- t:f(1) -- plain old lua
-- t:[_f](2) -- simple string key in register
-- t:[string.char(string.byte("f"))](3,32,33) -- string key from function
-- t:["f".."f"](4) -- string key from concatenation
-- t:["f"..string.sub("afun",2,2)](5,52,53) -- concatenation with function result
-- t:[(string.sub("afun",2,2))](6,62,63) -- function result in parentheses
-- t:[(function()return"f"end)()](7) -- closure in key
-- -- be careful with the ambiguous function call!!!
-- ;(function()return t end)():[(function()return"f"end)()](8) -- closure in object and in key
-- t:[F](9) -- object key

-- standard lua tests (compiler/passthrough)
do
  print("------ standard lua tests (compiler/passthrough)")
  local x
  t["t"]:f(1)
end

print("------ t:[k].f()")
t:t.f(1) -- string identifier
t:[_t].f(2) -- string key in register
t:[string.char(string.byte("t"))].f(3,32,33) -- string key from function
t:["t".."t"].f(4) -- string key from concatenation
t:["t"..string.sub("atable",2,2)].f(5,52,53) -- concatenation with function result
t:[(string.sub("atable",2,2))].f(6,62,63) -- function result in parentheses
t:[(function()return"t"end)()].f(7) -- closure in key
do end(function()return t end)():[(function()return"t"end)()].f(8) -- closure in object and in key, with "end" keyword at the start
-- be careful with the ambiguous function call!!!
;(function()return t end)():[(function()return"t"end)()].f(9) -- closure in object and in key, with semicolon at the start
t:[T].f(10) -- object key
_=(t:[_t].f(11)) -- inside ()

t:[_t].g {12} -- table call
t:[_t].f "13" -- string call


entity = {}

inventory = {get=false, set=false, size=false}
inventory.new=function(size)
  local t = {size=function() return size end}
  function t.set(e, i, o)
    if i <= 0 or i > e:[inventory].size() then error() end
    e[inventory][i] = o
  end
  function t.get(e, i)
    if i <= 0 or i > e:[inventory].size() then error() end
    return e[inventory][i]
  end
  return t
end
inventory.of=function(e) -- helper for passing standalone inventories around
  return {get=function(...)return e:[inventory].get(...)end, set=function(...)return e:[inventory].set(...)end, size=function(...)return e:[inventory].size(...)end}
end

entity[inventory] = inventory.new(5)

entity:[inventory].set(1, "Hello World!")

print(entity:[inventory].get(1))

for i=1, entity:[inventory].size() do
  print(i, entity:[inventory].get(i))
end

local myinv = inventory.of(entity)

for i=1, myinv.size() do
  print("wrapped", i, myinv.get(i))
end
----------------------------------------- OUTPUT
 local t = setmetatable ( { } , { __tostring = function ( ) return "t" end } )
 local F = { }
 local T = { }
 t . t = t
 t . tt = t
 t [ T ] = t
 t . f = print
 t . ff = print
 t . g = function ( self , a ) print ( self , a [ (1) ] ) end
 t [ F ] = print
 local _f = "f"
 local _t = "t"














 do
 print ( "------ standard lua tests (compiler/passthrough)" )
 local x
 t [ "t" ] : f ( (1) )
 end

 print ( "------ t:[k].f()" ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (  t , "t" , "f" , (1) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (  t , ( _t ) , "f" , (2) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (  t , ( string . char ( string . byte ( "t" ) ) ) , "f" , (3) , (32) , (33) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (  t , ( "t" .. "t" ) , "f" , (4) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (  t , ( "t" .. string . sub ( "atable" , (2) , (2) ) ) , "f" , (5) , (52) , (53) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (  t , ( ( string . sub ( "atable" , (2) , (2) ) ) ) , "f" , (6) , (62) , (63) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (
 t , ( ( function ( ) return "t" end ) ( ) ) , "f" , (7) )
 do end ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( ( function ( ) return t end ) ( ) , ( ( function ( ) return "t" end ) ( ) ) , "f" , (8) )

 ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( ( function ( ) return t end ) ( ) , ( ( function ( ) return "t" end ) ( ) ) , "f" , (9) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (
 t , ( T ) , "f" , (10) )
 _ = ( ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( t , ( _t ) , "f" , (11) ) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (

 t , ( _t ) , "g" , { (12) } ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (
 t , ( _t ) , "f" , "13" )


 entity = { }

 inventory = { get = false , set = false , size = false }
 inventory . new = function ( size )
 local t = { size = function ( ) return size end }
 function t . set ( e , i , o )
 if i <= (0) or i > ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( e , ( inventory ) , "size" ) then error ( ) end
 e [ inventory ] [ i ] = o
 end
 function t . get ( e , i )
 if i <= (0) or i > ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( e , ( inventory ) , "size" ) then error ( ) end
 return e [ inventory ] [ i ]
 end
 return t
 end
 inventory . of = function ( e )
 return { get = function ( ... ) return ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( e , ( inventory ) , "get" , ... ) end , set = function ( ... ) return ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( e , ( inventory ) , "set" , ... ) end , size = function ( ... ) return ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( e , ( inventory ) , "size" , ... ) end }
 end

 entity [ inventory ] = inventory . new ( (5) ) ; ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) (

 entity , ( inventory ) , "set" , (1) , "Hello World!" )

 print ( ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( entity , ( inventory ) , "get" , (1) ) )

 for i = (1) , ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( entity , ( inventory ) , "size" ) do  print ( i , ( function(myobj, mytrait, myfunction, ...) return myobj[mytrait][myfunction](myobj, ...) end ) ( entity , ( inventory ) , "get" , i ) )
 end

 local myinv = inventory . of ( entity )

 for i = (1) , myinv . size ( ) do
 print ( "wrapped" , i , myinv . get ( i ) )
 end