Harald Klimach avatar Harald Klimach committed 6f39970

Added a wrapper aorund the lua_insert method. Allow setting the top
of the stack as an entry for the table.

Comments (0)

Files changed (3)

LuaFortran/flu_binding.f90

   public :: flu_createTable
   public :: flu_getField, flu_getGlobal, flu_getTable, flu_getTop
   public :: flu_setGlobal
+  public :: flu_insert
   public :: flu_isFunction, flu_isNumber, flu_isTable
   public :: flu_isNone, flu_isNoneOrNil, flu_isNil
   public :: flu_isBoolean
   end function flu_gettop
 
 
+  subroutine flu_insert(L, index)
+    type(flu_state) :: L
+    integer :: index
+
+    integer(kind=c_int) :: c_index
+
+    c_index = int(index, kind = c_int)
+    call lua_insert(L%state, c_index)
+  end subroutine flu_insert
+
+
   function flu_isBoolean(L, index) result(is_boolean)
     type(flu_State) :: L
     integer         :: index

LuaFortran/lua_fif.f90

       integer(kind=c_int) :: lua_gettop
     end function lua_gettop
 
+    subroutine lua_insert(L, index) bind(c, name="lua_insert")
+      use, intrinsic :: iso_c_binding
+      type(c_ptr), value :: L
+      integer(kind=c_int) :: index
+    end subroutine lua_insert
+
     function lua_isNumber(L, index) bind(c, name="lua_isnumber")
       use, intrinsic :: iso_c_binding
       type(c_ptr), value :: L

source/aot_table_module.f90

   public :: aot_table_top, aot_table_length, aot_table_first, aot_table_push
   public :: aot_table_open, aot_table_close, aot_table_get_val
   public :: aot_table_from_1Darray
+  public :: aot_table_set_val, aot_table_set_top
   public :: aot_get_val
 
   !> This routine provides a way to open a table either as a globally defined
     module procedure get_table_logical
   end interface
 
+  !> Set a value in a table.
+  !!
+  !! The given value will be put at the entry named by key into the table
+  !! provided in thandle.
+  !! Alternatively you can also put the value by position into the table by
+  !! providing the pos argument.
+  !! If both, pos and key are provided, the key will be used.
+  !! Though, both of them are optional, at least one of them has to be provided.
+  interface aot_table_set_val
+    module procedure set_table_real
+    module procedure set_table_double
+    module procedure set_table_integer
+    module procedure set_table_long
+  end interface
+
   !> Get a value from a table.
   !!
   !! First the given key is looked up, if this fails, the value
   end subroutine get_table_string
 
 
+  !===========================================================================!
+
+
+  !> Put the top of the stack into a table.
+  subroutine aot_table_set_top(L, thandle, key, pos)
+    type(flu_State) :: L !< Handle to the Lua script.
+
+    !> Handle to the table to look the value up in.
+    integer, intent(in) :: thandle
+
+    !> Name of the entry to look for.
+    !!
+    !! Key and pos are both optional, however at least one of them has to be
+    !! supplied.
+    !! The key takes precedence over the pos if both are given.
+    character(len=*), intent(in), optional :: key
+
+    !> Position of the entry to look for in the table.
+    !!
+    !! It allows the access to unnamed arrays in the Lua tables.
+    integer, intent(in), optional :: pos
+
+    integer :: indpos
+
+    if (thandle > 0) then
+      if (present(key)) then
+        ! Now put it into the table
+        call flu_setField(L, thandle, trim(key))
+      else
+        ! No key given, try to put the value by position
+        if (present(pos)) then
+          ! First store the current top of the stack for later reference, to
+          ! move the desired position infront of it.
+          indpos = flu_gettop(L)
+          ! First put the index, where to write the value into the table, on the
+          ! stack.
+          call flu_pushInteger(L, pos)
+          ! Now move this position infront of the actual argument, which was
+          ! at the top previously..
+          call flu_insert(L, indpos)
+          ! Get the two entries from the stack into the table.
+          call flu_setTable(L, thandle)
+        end if
+      end if
+    end if
+
+  end subroutine aot_table_set_top
+
+
+  !> Put a single precision real value into a table.
+  subroutine set_table_real(val, L, thandle, key, pos)
+    type(flu_State) :: L !< Handle to the Lua script.
+
+    !> Handle to the table to look the value up in.
+    integer, intent(in) :: thandle
+
+    !> Value of the table entry if it exists.
+    real(kind=single_k), intent(in) :: val
+
+    !> Name of the entry to look for.
+    !!
+    !! Key and pos are both optional, however at least one of them has to be
+    !! supplied.
+    !! The key takes precedence over the pos if both are given.
+    character(len=*), intent(in), optional :: key
+
+    !> Position of the entry to look for in the table.
+    !!
+    !! It allows the access to unnamed arrays in the Lua tables.
+    integer, intent(in), optional :: pos
+
+    if (thandle > 0) then
+      if (present(key)) then
+        ! If there is a key, use that.
+        ! First put the value on the top of the stack
+        call flu_pushNumber(L, val)
+        ! Now put it into the table
+        call flu_setField(L, thandle, trim(key))
+      else
+        ! No key given, try to put the value by position
+        if (present(pos)) then
+          ! First put the index, where to write the value into the table, on the
+          ! stack.
+          call flu_pushInteger(L, pos)
+          ! Now put the actual value on the top of the stack.
+          call flu_pushNumber(L, val)
+          ! Get the two entries from the stack into the table.
+          call flu_setTable(L, thandle)
+        end if
+      end if
+    end if
+
+  end subroutine set_table_real
+
+
+  !> Put a double precision real value into a table.
+  subroutine set_table_double(val, L, thandle, key, pos)
+    type(flu_State) :: L !< Handle to the Lua script.
+
+    !> Handle to the table to look the value up in.
+    integer, intent(in) :: thandle
+
+    !> Value of the table entry if it exists.
+    real(kind=double_k), intent(in) :: val
+
+    !> Name of the entry to look for.
+    !!
+    !! Key and pos are both optional, however at least one of them has to be
+    !! supplied.
+    !! The key takes precedence over the pos if both are given.
+    character(len=*), intent(in), optional :: key
+
+    !> Position of the entry to look for in the table.
+    !!
+    !! It allows the access to unnamed arrays in the Lua tables.
+    integer, intent(in), optional :: pos
+
+    if (thandle > 0) then
+      if (present(key)) then
+        ! If there is a key, use that.
+        ! First put the value on the top of the stack
+        call flu_pushNumber(L, val)
+        ! Now put it into the table
+        call flu_setField(L, thandle, trim(key))
+      else
+        ! No key given, try to put the value by position
+        if (present(pos)) then
+          ! First put the index, where to write the value into the table, on the
+          ! stack.
+          call flu_pushInteger(L, pos)
+          ! Now put the actual value on the top of the stack.
+          call flu_pushNumber(L, val)
+          ! Get the two entries from the stack into the table.
+          call flu_setTable(L, thandle)
+        end if
+      end if
+    end if
+
+  end subroutine set_table_double
+
+
+  !> Put a default integer value into a table.
+  subroutine set_table_integer(val, L, thandle, key, pos)
+    type(flu_State) :: L !< Handle to the Lua script.
+
+    !> Handle to the table to look the value up in.
+    integer, intent(in) :: thandle
+
+    !> Value of the table entry if it exists.
+    integer, intent(in) :: val
+
+    !> Name of the entry to look for.
+    !!
+    !! Key and pos are both optional, however at least one of them has to be
+    !! supplied.
+    !! The key takes precedence over the pos if both are given.
+    character(len=*), intent(in), optional :: key
+
+    !> Position of the entry to look for in the table.
+    !!
+    !! It allows the access to unnamed arrays in the Lua tables.
+    integer, intent(in), optional :: pos
+
+    if (thandle > 0) then
+      if (present(key)) then
+        ! If there is a key, use that.
+        ! First put the value on the top of the stack
+        call flu_pushInteger(L, val)
+        ! Now put it into the table
+        call flu_setField(L, thandle, trim(key))
+      else
+        ! No key given, try to put the value by position
+        if (present(pos)) then
+          ! First put the index, where to write the value into the table, on the
+          ! stack.
+          call flu_pushInteger(L, pos)
+          ! Now put the actual value on the top of the stack.
+          call flu_pushInteger(L, val)
+          ! Get the two entries from the stack into the table.
+          call flu_setTable(L, thandle)
+        end if
+      end if
+    end if
+
+  end subroutine set_table_integer
+
+
+  !> Put a long integer value into a table.
+  subroutine set_table_long(val, L, thandle, key, pos)
+    type(flu_State) :: L !< Handle to the Lua script.
+
+    !> Handle to the table to look the value up in.
+    integer, intent(in) :: thandle
+
+    !> Value of the table entry if it exists.
+    integer(kind=long_k), intent(in) :: val
+
+    !> Name of the entry to look for.
+    !!
+    !! Key and pos are both optional, however at least one of them has to be
+    !! supplied.
+    !! The key takes precedence over the pos if both are given.
+    character(len=*), intent(in), optional :: key
+
+    !> Position of the entry to look for in the table.
+    !!
+    !! It allows the access to unnamed arrays in the Lua tables.
+    integer, intent(in), optional :: pos
+
+    if (thandle > 0) then
+      if (present(key)) then
+        ! If there is a key, use that.
+        ! First put the value on the top of the stack
+        call flu_pushInteger(L, int(val))
+        ! Now put it into the table
+        call flu_setField(L, thandle, trim(key))
+      else
+        ! No key given, try to put the value by position
+        if (present(pos)) then
+          ! First put the index, where to write the value into the table, on the
+          ! stack.
+          call flu_pushInteger(L, pos)
+          ! Now put the actual value on the top of the stack.
+          call flu_pushInteger(L, int(val))
+          ! Get the two entries from the stack into the table.
+          call flu_setTable(L, thandle)
+        end if
+      end if
+    end if
+
+  end subroutine set_table_long
+
+
   !> This subroutine takes a one dimensional array, and puts it as a table
   !! into the Lua context.
   !!
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.