jas43 avatar jas43 committed bbc2708

flu binding to lua_pushcclosure.

I tried to keep the same kind of interface as in lua_pushcclosure, where
a function is passed in as an argument (which then has to be converted to
a function pointer which is interoperable with C).

Comments (0)

Files changed (2)

LuaFortran/flu_binding.f90

     module procedure flu_pushdouble
   end interface flu_pushnumber
 
+  ! Interoperable interface required for a function that is callable from Lua.
+  abstract interface
+    function lua_Function(s) result(val) bind(c)
+      use, intrinsic :: iso_c_binding
+      integer(c_int) :: val 
+      type(c_ptr), value :: s
+    end function lua_Function
+  end interface
+
 contains
 
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     bool = (lua_toBoolean(L%state, c_index) == 1)
   end function flu_toBoolean
 
+  subroutine flu_pushcclosure(L, fn, n)
+    type(flu_State), value :: L 
+    procedure(lua_Function) :: fn
+    integer :: n 
+
+    integer(c_int) :: c_n
+    type(c_funptr) :: c_fn
+
+    c_n = n 
+    c_fn = c_funloc(fn)
+
+    call lua_pushcclosure(L%state, c_fn, c_n)
+
+  end subroutine flu_pushcclosure
+
 
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

LuaFortran/lua_fif.f90

       integer(kind=c_int) :: lua_type
     end function lua_type
 
+    subroutine lua_pushcclosure(L, c_fn, n) bind(c, name="lua_pushcclosure")
+      use, intrinsic :: iso_c_binding
+      type(c_ptr), value :: L
+      type(c_funptr), value :: c_fn
+      integer(c_int), value :: n
+    end subroutine lua_pushcclosure
+
   end interface
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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.