github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/cell.go (about)

     1  package runtime
     2  
     3  // Cell is the data structure that represents a reference to a value.  Whenever
     4  // a value is put into a register that contains a cell, it is put in the cell
     5  // rather than the register itself.  It is used in order to implement upvalues
     6  // in lua. Example:
     7  //
     8  //     local x
     9  //     local function f() return x + 1 end
    10  //     x = 3
    11  //     f()
    12  //
    13  // The variable x is an upvalue in f so when x is set to 3 the upvalue of f must
    14  // be set to 3.  This is achieved by setting the register that contains x to a
    15  // Cell.
    16  type Cell struct {
    17  	ref *Value
    18  }
    19  
    20  // newCell returns a new Cell instance containing the given value.
    21  func newCell(v Value) Cell {
    22  	return Cell{&v}
    23  }
    24  
    25  // get returns the value that the cell c contains.
    26  func (c Cell) get() Value {
    27  	return *c.ref
    28  }
    29  
    30  // set sets the the value contained by c to v.
    31  func (c Cell) set(v Value) {
    32  	*c.ref = v
    33  }