Harald Klimach avatar Harald Klimach committed 7c45d68

Added a set_table_string routine

Comments (0)

Files changed (3)

LuaFortran/flu_binding.f90

   public :: flu_tolstring, flu_tonumber, flu_toboolean
   public :: flu_pop
   public :: flu_pushinteger, flu_pushnil, flu_pushnumber, flu_pushboolean
-  public :: flu_pushvalue
+  public :: flu_pushstring, flu_pushvalue
 
   public :: flu_copyptr
   public :: flu_register
     call lua_pushboolean(L%state, n_c)
   end subroutine flu_pushboolean
 
+  subroutine flu_pushstring(L, string)
+    type(flu_State) :: L
+    character(len=*), intent(in) :: string
+
+    integer(kind=c_size_t) :: c_len
+    type(c_ptr) :: ret
+
+    c_len = len(string)
+    ret = lua_pushlstring(L%state, string, c_len)
+  end subroutine flu_pushstring
+
   subroutine flu_pushreal(L, n)
     type(flu_State) :: L
     real :: n

LuaFortran/lua_fif.f90

       real(kind=c_double), value :: n
     end subroutine lua_pushnumber
 
+    function lua_pushlstring(L, s, len) bind(c, name="lua_pushlstring")
+      use, intrinsic :: iso_c_binding
+      type(c_ptr), value :: L
+      character(kind=c_char), dimension(*) :: s
+      integer(kind=c_size_t), value :: len
+      type(c_ptr) :: lua_pushlstring
+    end function lua_pushlstring
+
     subroutine lua_pushvalue(L, index) bind(c, name="lua_pushvalue")
       use, intrinsic :: iso_c_binding
       type(c_ptr), value :: L

source/aot_table_module.f90

     module procedure set_table_double
     module procedure set_table_integer
     module procedure set_table_long
+    module procedure set_table_string
     module procedure set_table_logical
   end interface
 
   end subroutine set_table_logical
 
 
+  !> Put a string value into a table.
+  subroutine set_table_string(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 to set in the table.
+    character(len=*), intent(in) :: val
+
+    !> Name of the entry to set.
+    !!
+    !! 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 set 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_pushString(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_pushString(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_string
+
+
   !> 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.