Commits

Harald Klimach committed 7c190ed

Added aotus convenience functions around lua_dump related features

Comments (0)

Files changed (2)

source/aotus_module.f90

 
   public :: aot_get_val
   public :: open_config_file, close_config
-  public :: open_config_chunk
+  public :: open_config_chunk, open_config_buffer
+  public :: aot_file_to_buffer
 
   ! Entities inherited from aot_top_module, published here to
   ! allow most functionality by "use aotus_module".
   end subroutine open_config_chunk
 
 
+  !> Subroutine to load and execute a script given in a buffer
+  !! (might be bytecode).
+  subroutine open_config_buffer(L, buffer, ErrCode, ErrString)
+    type(flu_State) :: L !< Handle to the Lua script
+
+    !> String with Lua code to load.
+    character, intent(in) :: buffer(:)
+
+    !> Error code returned by Lua during loading or executing the file.
+    !!
+    !! This optional parameter might be used to react on errors in the calling
+    !! side. If neither ErrCode nor ErrString are given, this subroutine will
+    !! stop the program execution and print the error message from Lua to the
+    !! stdout.
+    integer, intent(out), optional :: ErrCode
+
+    !> Obtained error description from the Lua stack.
+    !!
+    !! This optional argument holds the Lua error message in case somehting
+    !! went wrong. It can be used to provide some feedback to the user in the
+    !! calling routine. If neither ErrCode nor ErrString are provided,
+    !! open_config() will print the error message and stop program execution.
+    character(len=*), intent(out), optional :: ErrString
+
+    integer :: err
+
+    if (.not.flu_isopen(L)) L = fluL_newstate()
+
+    err = fluL_loadbuffer(L, buffer)
+
+    call aot_err_handler(L, err, 'Cannot load buffer:', ErrString, ErrCode)
+
+    if (err == 0) then
+      call fluL_openlibs(L)
+
+      err = flu_pcall(L, 0, 0, 0)
+
+      call aot_err_handler(L, err, 'Cannot run buffer:', ErrString, ErrCode)
+    end if
+
+  end subroutine open_config_buffer
+
+
   !> Close an opened Lua script again.
   subroutine close_config(L)
     type(flu_State) :: L !< Handle to the Lua script to close.
 
   end subroutine get_config_string
 
+
+  !> Subroutine to load a script from a file and put it into a character buffer.
+  subroutine aot_file_to_buffer(filename, buffer, ErrCode, ErrString)
+    !> Name of file to load the Lua code from
+    character(len=*), intent(in) :: filename
+
+    !> Buffer to store the script in the given file in
+    character, pointer :: buffer(:)
+
+    !> Error code returned by Lua during loading or executing the file.
+    !!
+    !! This optional parameter might be used to react on errors in the calling
+    !! side. If neither ErrCode nor ErrString are given, this subroutine will
+    !! stop the program execution and print the error message from Lua to the
+    !! stdout.
+    integer, intent(out), optional :: ErrCode
+
+    !> Obtained error description from the Lua stack.
+    !!
+    !! This optional argument holds the Lua error message in case somehting
+    !! went wrong. It can be used to provide some feedback to the user in the
+    !! calling routine. If neither ErrCode nor ErrString are provided,
+    !! open_config() will print the error message and stop program execution.
+    character(len=*), intent(out), optional :: ErrString
+
+    type(flu_State) :: L
+    integer :: err
+    integer :: buflen
+
+    L = fluL_newstate()
+
+    err = fluL_loadfile(L, filename)
+    call aot_err_handler(L, err, 'Cannot load configuration file:', ErrString, &
+      &                  ErrCode)
+
+    if (err == 0) then
+
+      call flu_dump(L = L, buf = buffer, length = buflen, iError = err)
+
+      if (err /= 0) then
+        if (present(ErrCode)) then
+           ErrCode = err
+           if (present(ErrString)) then
+             ErrString = 'Error while dumping the Lua script into a buffer!'
+           end if
+        else
+          write(*,*) 'Error while dumping the Lua script into a buffer!'
+          write(*,*) 'STOPPING'
+          STOP
+        end if
+      end if
+
+    end if
+
+    call close_config(L)
+
+  end subroutine aot_file_to_buffer
+
+
 end module aotus_module
 
 !> \page Aotus

utests/aot_buffer_test.f90

 program aot_buffer_test
-  use flu_binding, only: flu_State, fluL_newstate, fluL_loadfile, flu_dump, &
-    &                    fluL_loadbuffer, flu_pcall, flu_pop
+  use flu_binding, only: flu_State, fluL_newstate, fluL_loadfile, flu_dump
   use aotus_module, only: aot_get_val, aot_err_handler, &
+    &                     open_config_buffer, close_config, &
     &                     aoterr_Fatal, aoterr_NonExistent, aoterr_WrongType
 
   implicit none
     write(*,*) '  : success'
   end if
 
-  ! Pop the script itself from the stack
-  call flu_pop(L)
+  ! Completely close the state to reopen it with the buffered code.
+  call close_config(L)
 
   write(*,*) 'Reading the script back from a buffer'
-  err = fluL_loadbuffer(L, scriptbuffer)
-  call aot_err_handler(L, err, 'Cannot load script from buffer again:')
-
-  write(*,*) 'Executing the script'
-  err = flu_pcall(L, 0, 0, 0)
-  call aot_err_handler(L, err, 'Cannot run script from buffer:')
+  call open_config_buffer(L, scriptbuffer)
 
   ! Testing for global INTEGER
   write(*,*) ' * reading a global integer'
   end if
   ! -------------------------------- !
 
+  call close_config(L)
+
   if (passed) then
     write(*,*) 'PASSED'
   else
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.