github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/debuglib/lua/upvalues.lua (about)

     1  local function perr(...)
     2      local ok, err = pcall(...)
     3      if not ok then
     4          print(err)
     5      end
     6  end
     7  
     8  -- getupvalue tests
     9  do
    10      local function outer(x, y, z)
    11          return function()
    12              return x, y, z
    13          end
    14      end
    15  
    16      local inner = outer("hello", 1, false)
    17  
    18      for i = 1, 4 do
    19          print(debug.getupvalue(inner, i))
    20      end
    21  
    22      --> =x	hello
    23      --> =y	1
    24      --> =z	false
    25      --> =
    26  
    27      function perr(...)
    28          local ok, err = pcall(...)
    29          if not ok then
    30              print(err)
    31          end
    32      end
    33  
    34      perr(debug.getupvalue, inner)
    35      --> ~.*: 2 arguments needed
    36  
    37      perr(debug.getupvalue, 1, 1)
    38      --> ~.*: #1 must be a lua function
    39  
    40      perr(debug.getupvalue, inner, "a")
    41      --> ~.*: #2 must be an integer
    42  end
    43  
    44  -- setupvalue tests
    45  do
    46      local function outer(x, y, z)
    47          return function()
    48              return x, y, z
    49          end
    50      end
    51  
    52      local inner = outer("hello", 1, false)
    53  
    54      print(debug.setupvalue(inner, 1, "bye"))
    55      --> =x
    56      print(debug.setupvalue(inner, 2, 42))
    57      --> =y
    58      print(debug.setupvalue(inner, 3, true))
    59      --> =z
    60      print(debug.setupvalue(inner, 4, "non-existent"))
    61      --> =
    62  
    63      print(inner())
    64      --> =bye	42	true
    65  
    66      perr(debug.setupvalue, inner, 1)
    67      --> ~.*: 3 arguments needed
    68  
    69      perr(debug.setupvalue, {}, 1, 2)
    70      --> ~.*: #1 must be a lua function
    71  
    72      perr(debug.setupvalue, inner, "x", 3)
    73      --> ~.*: #2 must be an integer
    74  end
    75  
    76  -- upvaluejoin tests
    77  do
    78      local function maker(...)
    79          local function outer(x, y, z)
    80              return function()
    81                  return x, y, z
    82              end
    83          end
    84          return outer(...)
    85      end
    86  
    87      local f1 = maker(1, 2, 3)
    88      local f2 = maker("a", "b", "c")
    89  
    90      debug.upvaluejoin(f1, 1, f2, 2)
    91      debug.upvaluejoin(f2, 1, f1, 2)
    92  
    93      print(f1())
    94      --> =b	2	3
    95  
    96      print(f2())
    97      --> =2	b	c
    98  
    99      debug.setupvalue(f1, 1, "x")
   100      debug.setupvalue(f1, 2, "x")
   101      debug.setupvalue(f1, 3, "x")
   102  
   103      print(f1())
   104      --> =x	x	x
   105  
   106      print(f2())
   107      --> =x	x	c
   108  
   109      perr(debug.upvaluejoin, f1, 1, f2)
   110      --> ~.*: 4 arguments needed
   111  
   112      perr(debug.upvaluejoin, "x", 1, f2, 2)
   113      --> ~.*: #1 must be a lua function
   114  
   115      perr(debug.upvaluejoin, f1, "x", f2, 2)
   116      --> ~.*: #2 must be an integer
   117  
   118      perr(debug.upvaluejoin, f1, 1, "x", 2)
   119      --> ~.*: #3 must be a lua function
   120  
   121      perr(debug.upvaluejoin, f1, 1, f2, "x")
   122      --> ~.*: #4 must be an integer
   123  
   124      perr(debug.upvaluejoin, f1, 1, f2, 4)
   125      --> ~.*: Invalid upvalue index
   126  end
   127  
   128  -- upvalueid tests
   129  do
   130      local f1, f2
   131      local function outer(x, y, z)
   132          local a, b = 1, 2
   133          f1 = function ()
   134              return x, y, a
   135          end
   136          f2 = function()
   137              return a, b, x
   138          end
   139      end
   140      outer()
   141  
   142      local id = debug.upvalueid
   143  
   144      print(id(f1, 1) == id(f2, 3))
   145      --> =true
   146  
   147      print(id(f1, 2) == id(f2, 3))
   148      --> =false
   149  
   150      print(id(f1, 3) == id(f2, 1))
   151      --> =true
   152  
   153      perr(id, f1)
   154      --> ~.*: 2 arguments needed
   155  
   156      perr(id, print, 2)
   157      --> ~.*: #1 must be a lua function
   158  
   159      perr(id, f1, nil)
   160      --> ~.*: #2 must be an integer
   161  
   162      print(id(f1, 0))
   163      --> =nil
   164  
   165      print(id(f1, 4))
   166      --> =nil
   167  end