Skip to content

Commit

Permalink
Updated Lua API.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed Feb 26, 2024
1 parent 62b077c commit 42aa854
Show file tree
Hide file tree
Showing 5 changed files with 1,869 additions and 115 deletions.
63 changes: 59 additions & 4 deletions src/dm_lua.f90
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ end function dm_lua_callback
public :: dm_lua_table
public :: dm_lua_table_size
public :: dm_lua_to
public :: dm_lua_to_int32
public :: dm_lua_to_int64
public :: dm_lua_to_logical
public :: dm_lua_to_real32
public :: dm_lua_to_real64
public :: dm_lua_to_string
public :: dm_lua_unescape
public :: dm_lua_version

Expand Down Expand Up @@ -205,12 +211,12 @@ function dm_lua_escape(str) result(res)
end do
end function dm_lua_escape

integer function dm_lua_eval(lua, cmd) result(rc)
integer function dm_lua_eval(lua, command) result(rc)
!! Executes Lua command passed in character string `cmd`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
character(len=*), intent(in) :: cmd !! Lua command to evaluate.
type(lua_state_type), intent(inout) :: lua !! Lua type.
character(len=*), intent(in) :: command !! Lua command to evaluate.

rc = dm_lua_error(lual_dostring(lua%ptr, cmd))
rc = dm_lua_error(lual_dostring(lua%ptr, command))
end function dm_lua_eval

integer function dm_lua_exec(lua, file_path) result(rc)
Expand Down Expand Up @@ -338,6 +344,55 @@ integer function dm_lua_table_size(lua) result(n)
n = int(lua_rawlen(lua%ptr, -1), kind=i4)
end function dm_lua_table_size

integer(kind=i4) function dm_lua_to_int32(lua, idx) result(value)
!! Returns 4-byte integer from Lua stack at position `idx`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
integer, intent(in) :: idx !! Stack index.

value = int(lua_tointeger(lua%ptr, idx), kind=i4)
end function dm_lua_to_int32

integer(kind=i8) function dm_lua_to_int64(lua, idx) result(value)
!! Returns 8-byte integer from Lua stack at position `idx`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
integer, intent(in) :: idx !! Stack index.

value = lua_tointeger(lua%ptr, idx)
end function dm_lua_to_int64

logical function dm_lua_to_logical(lua, idx) result(value)
!! Returns 8-byte integer from Lua stack at position `idx`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
integer, intent(in) :: idx !! Stack index.

value = lua_toboolean(lua%ptr, idx)
end function dm_lua_to_logical

real(kind=r4) function dm_lua_to_real32(lua, idx) result(value)
!! Returns 4-byte real from Lua stack at position `idx`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
integer, intent(in) :: idx !! Stack index.

value = real(lua_tonumber(lua%ptr, idx), kind=r4)
end function dm_lua_to_real32

real(kind=r8) function dm_lua_to_real64(lua, idx) result(value)
!! Returns 8-byte real from Lua stack at position `idx`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
integer, intent(in) :: idx !! Stack index.

value = lua_tonumber(lua%ptr, idx)
end function dm_lua_to_real64

function dm_lua_to_string(lua, idx) result(value)
!! Returns allocatable character string from Lua stack at position `idx`.
type(lua_state_type), intent(inout) :: lua !! Lua type.
integer, intent(in) :: idx !! Stack index.
character(len=:), allocatable :: value !! String value.

value = lua_tostring(lua%ptr, idx)
end function dm_lua_to_string

function dm_lua_unescape(str) result(res)
!! Unescapes passed character string by replacing each occurance of
!! `\\` with `\`.
Expand Down
2 changes: 1 addition & 1 deletion src/dm_lua_api.f90
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ integer function dm_lua_api_register(lua, add_errors, add_levels, add_procedures
!! in module `dm_lua_geocom`.
!!
!! This function returns `E_INVALID` if the Lua interpreter has not been
!! initialised, or `E_LUA` if the export failed.
!! initialised, or `E_LUA` if the registration failed.
use :: dm_log
type(lua_state_type), intent(inout) :: lua !! Lua state type.
logical, intent(in), optional :: add_errors !! Export error codes.
Expand Down
Loading

0 comments on commit 42aa854

Please sign in to comment.