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

     1  local f = load("print('hello')")
     2  f()
     3  --> =hello
     4  
     5  do
     6      local env = {x = 1}
     7      local f = load("x = 2", "chunk", "bt", env)
     8      print(env.x)
     9  --> =1
    10      f()
    11      print(env.x)
    12  --> =2
    13  end
    14  
    15  load("print(...)")(1, 2)
    16  --> =1	2
    17  
    18  -- This loads and executes the given file
    19  loadfile("lua/loadfile.lua.notest")()
    20  --> =loadfile
    21  
    22  loadfile("lua/loadfile.lua.notest", "t", {print=print, ggg = "global"})()
    23  --> =global
    24  
    25  print(pcall(loadfile, "lua/loadfile.lua.notest", 123))
    26  --> ~false\t.*must be a string
    27  
    28  print(loadfile("lua/nonexistent_file"))
    29  --> ~nil\t
    30  
    31  dofile("lua/loadfile.lua.notest")
    32  --> =loadfile
    33  
    34  load(coroutine.wrap(function ()
    35      coroutine.yield("print(")
    36      coroutine.yield("'hello')")
    37  end))()
    38  --> =hello
    39  
    40  load(coroutine.wrap(function ()
    41      coroutine.yield("print(")
    42      coroutine.yield("'abc')")
    43      coroutine.yield("")
    44      coroutine.yield("print(")
    45      coroutine.yield("'xyz')")
    46  end))()
    47  --> =abc
    48  
    49  print(load(function() error("argh") end))
    50  --> ~nil\t.*argh
    51  
    52  print(load(function() return {} end))
    53  --> ~nil\t.*must return a string
    54  
    55  print(pcall(load, {}))
    56  --> ~false\t.*string or function
    57  
    58  print(load("\x00", "", "t"))
    59  --> ~nil\t.*invalid token
    60  
    61  print(load("hi", "", "b"))
    62  --> ~nil\t.*text chunk
    63  
    64  local z = "haha"
    65  load(string.dump(function() print("hi", z) end))()
    66  --> =hi	nil
    67  
    68  print(load("???", "", "t"))
    69  --> ~nil\t.*invalid token