# aot_reference_for Function

## public function aot_reference_for(L, thandle, key, pos) result(ref)

Get a reference for the entry defined by thandle, key and pos, or the current top entry in the stack.

The reference can be used to refer to a given object in the Lua table by storing a reference to it in the LUA_REGISTRYINDEX table.

The object can then be put onto the stack again by using this reference.

### Arguments

Type IntentOptional AttributesName
type(flu_State) :: L

Handle to the Lua script

integer, intent(in), optional :: thandle

Handle to the table containing the object to get a reference for.

character(len=*), intent(in), optional :: key

Name of the object to look up, if thandle is not present, this is a global definition.

If neither thandle nor key is provided, a reference to the top of the stack is returned.

integer, intent(in), optional :: pos

Positional index of the object inside thandle to get the reference for. If thandle is not provided, this argument is ignored.

If a key is provided, that takes precedent over pos.

## Source Code

  function aot_reference_for(L, thandle, key, pos) result(ref)
type(flu_State) :: L !! Handle to the Lua script

!> Handle to the table containing the object to get a reference for.
integer, intent(in), optional :: thandle

!> Name of the object to look up, if thandle is not present, this is
!! a global definition.
!!
!! If neither thandle nor key is provided, a reference to the top of
!! the stack is returned.
character(len=*), intent(in), optional :: key

!> Positional index of the object inside thandle to get the reference
!! for. If thandle is not provided, this argument is ignored.
!!
!! If a key is provided, that takes precedent over pos.
integer, intent(in), optional :: pos

integer :: toptype
integer :: ref

if (present(thandle)) then
call aot_table_push(L=L, thandle=thandle, &
&                 key=key, pos=pos      )
else if (present(key)) then
toptype = flu_getglobal(L, key)
end if

ref = fluL_ref(L, LUA_REGISTRYINDEX)
end function aot_reference_for