具有upvalues的奇怪的LuaLanes行为

时间:2014-08-18 20:33:10

标签: lua lua-lanes

我有这个代码,其中在[LuaLanes] [1]中执行的函数似乎忽略了设置upvalue,所以我想知道这是否是正常行为,我使用它是否错误。

local lanes = require("lanes"):configure()

--A getter/setter module with an upvalue (inlined)
local Common = {}

local internal_variable = {test = 1}

function Common.get_variable()
  print("Using "..tostring(Common).." - Getting var ("..tostring(internal_variable)..") test is "..internal_variable.test)
  return internal_variable
end

function Common.set_variable(new_var)
  print("Using "..tostring(Common).." - Setting var ("..tostring(new_var)..") test is "..new_var.test)
  internal_variable = new_var
  print("Using "..tostring(Common).." - Set     var ("..tostring(internal_variable)..") test is "..internal_variable.test)
end

print("Common ("..tostring(Common)..") initialized")

local function main()
  print("Entering main... Common is "..tostring(Common))
  local var = Common.get_variable()
  print("Inside main var is "..tostring(var))
  local new_var = {test = 2}
  Common.set_variable(new_var)
  local verify_var = Common.get_variable()
  print("Inside main new var is "..tostring(verify_var))
  assert(new_var == verify_var)
end

print("Starting program... Common is "..tostring(Common))

local result
--result = main()
result = lanes.gen("*",main)()[1]

使用Lanes运行时,断言失败,但运行正常(正如预期)只运行main()

使用main()输出:

Common (table: 00000000006E9DD0) initialized
Starting program... Common is table: 00000000006E9DD0
Entering main... Common is table: 00000000006E9DD0
Using table: 00000000006E9DD0 - Getting var (table: 00000000006E9E20) test is 1
Inside main var is table: 00000000006E9E20
Using table: 00000000006E9DD0 - Setting var (table: 00000000006EA140) test is 2
Using table: 00000000006E9DD0 - Set     var (table: 00000000006EA140) test is 2
Using table: 00000000006E9DD0 - Getting var (table: 00000000006EA140) test is 2
Inside main new var is table: 00000000006EA140

使用lanes.gen("*",main)()[1]

输出
Common (table: 0000000000719480) initialized
Starting program... Common is table: 0000000000719480
Entering main... Common is table: 0000000000740800
Using table: 0000000000740800 - Getting var (table: 00000000007408A0) test is 1
Inside main var is table: 00000000007408A0
Using table: 0000000000740800 - Setting var (table: 0000000000740A80) test is 2
Using table: 0000000000740800 - Set     var (table: 0000000000740A80) test is 2
Using table: 0000000000740800 - Getting var (table: 00000000007408A0) test is 1
Inside main new var is table: 00000000007408A0
F:\dev\Env\Lua\lua51-lua-5.1.4-x64\bin\lua.exe: ...\LanesBehaviourTest\LanesBehaviour\src\main\main.lua:29: assertion failed!
stack traceback:
    [C]: ?
    [C]: ?
    ...\LanesBehaviourTest\LanesBehaviour\src\main\main.lua:36: in main chunk
    [C]: ?

为什么get_variable()即使在设置另一个表之后也会返回相同的表?有什么想法吗?

PS:另外,可能是相关的,为什么在车道内运行时Common会发生变化?是否完全复制到新的Lua州?

  [1]: https://github.com/LuaLanes/lanes

0 个答案:

没有答案
相关问题