Skip to content

Commit

Permalink
Fixed more type-related issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
interkosmos committed May 25, 2023
1 parent cf6d16d commit 17763b2
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 116 deletions.
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
.POSIX:

CC = gcc
FC = gfortran
AR = ar
CFLAGS = -Wall `pkg-config --cflags lua-5.3`
FFLAGS = -Wall `pkg-config --cflags lua-5.3`
ARFLAGS = rcs
LDFLAGS = `pkg-config --libs-only-L lua-5.3`
LDLIBS = `pkg-config --libs-only-l lua-5.3`
TARGET = libfortran-lua53.a
TYPES = types

FIBONACCI = examples/fibonacci/fibonacci
LIBRARY = examples/library/fortran.so
Expand All @@ -15,19 +18,24 @@ TABLE = examples/table/table

.PHONY: all clean examples

all: $(TARGET)
all: $(TARGET) $(TYPES)

test: $(TYPES)

examples: $(FIBONACCI) $(LIBRARY) $(STRING) $(TABLE)

$(TARGET):
$(FC) $(FFLAGS) -fPIC -c src/lua.f90
$(AR) $(ARFLAGS) $(TARGET) lua.o

$(TYPES):
$(CC) $(CFLAGS) -o $(TYPES) test/types.c $(LDFLAGS)

$(FIBONACCI): $(TARGET)
$(FC) $(FFLAGS) $(LDFLAGS) -o $(FIBONACCI) examples/fibonacci/fibonacci.f90 $(TARGET) $(LDLIBS)

$(LIBRARY): $(TARGET)
$(FC) $(FFLAGS) $(LDFLAGS) -fPIC -shared -o $(LIBRARY) examples/library/fortran.f90 $(TARGET)
$(FC) $(FFLAGS) $(LDFLAGS) -shared -fPIC -o $(LIBRARY) examples/library/fortran.f90 $(TARGET)

$(STRING): $(TARGET)
$(FC) $(FFLAGS) $(LDFLAGS) -o $(STRING) examples/string/string.f90 $(TARGET) $(LDLIBS)
Expand All @@ -39,6 +47,7 @@ clean:
if [ `ls -1 *.mod 2>/dev/null | wc -l` -gt 0 ]; then rm *.mod; fi
if [ `ls -1 *.o 2>/dev/null | wc -l` -gt 0 ]; then rm *.o; fi
if [ -e $(TARGET) ]; then rm $(TARGET); fi
if [ -e $(TYPES) ]; then rm $(TYPES); fi
if [ -e $(FIBONACCI) ]; then rm $(FIBONACCI); fi
if [ -e $(LIBRARY) ]; then rm $(LIBRARY); fi
if [ -e $(STRING) ]; then rm $(STRING); fi
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# fortran-lua53
A collection of ISO C binding interfaces to Lua 5.3 for Fortran 2003, to call

A collection of ISO C binding interfaces to Lua 5.3 for Fortran 2008, to call
Lua from Fortran and vice versa.

Similar projects:
Expand All @@ -10,6 +11,7 @@ Similar projects:
* [luaf](https://bitbucket.org/vadimz/luaf/): Selected bindings to Lua 5.1 (MIT).

## Build

Install Lua 5.3 with development headers. On FreeBSD, run:

```
Expand Down Expand Up @@ -47,6 +49,7 @@ Link your Fortran applications against `libfortran-lua53.a`, and
as well.

## Example

The following basic example shows how to call the Lua function `hello()` in
`script.lua` from Fortran.

Expand Down Expand Up @@ -97,6 +100,7 @@ $ gfortran -o example example.f90 libfortran-lua53.a /usr/local/lib/liblua-5.3.a
```

## Further Examples

Additional examples can be found in `examples/`.

* **fibonacci:** calls a recursive Lua routine loaded from file.
Expand All @@ -105,6 +109,7 @@ Additional examples can be found in `examples/`.
* **table:** reads values from a Lua table.

## fpm

You can add *fortran-lua53* as an [fpm](https://github.com/fortran-lang/fpm)
dependency:

Expand All @@ -113,7 +118,24 @@ dependency:
fortran-lua53 = { git = "https://github.com/interkosmos/fortran-lua53.git" }
```

## Compatibility

The integer and float types used by Lua internally depend on the targeted
platform. The program `test/types.c` outputs the types:

```
$ make test
$ ./types
lua_integer.: c_long_long
lua_number..: c_double
lua_kcontext: c_intptr_t
```

You may have to alter `lua_integer`, `lua_number`, and `lua_kcontext` in
`src/lua.f90` accordingly.

## Coverage

| Function Name | Fortran Interface Name | Bound | Wrapper |
|-------------------------|-------------------------|-------|---------|
| `luaL_addchar` | | | |
Expand Down Expand Up @@ -282,4 +304,5 @@ fortran-lua53 = { git = "https://github.com/interkosmos/fortran-lua53.git" }
| `lua_yieldk` | | | |

## Licence

ISC
11 changes: 5 additions & 6 deletions examples/fibonacci/fibonacci.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ program main
character(len=*), parameter :: FILE_NAME = 'fibonacci.lua'

type(c_ptr) :: l
integer :: nargs
integer :: nresults
integer :: rc
integer :: r1, r2
integer :: x
integer :: nargs, nresults
integer :: rc, x
logical :: file_exists

integer(kind=lua_integer) :: r1, r2

inquire (file=FILE_NAME, exist=file_exists)
if (.not. file_exists) stop 'Error: Lua file not found'

Expand All @@ -29,7 +28,7 @@ program main
rc = lua_getglobal(l, 'fib')

if (lua_isfunction(l, -1) == 1) then
call lua_pushinteger(l, x)
call lua_pushinteger(l, int(x, kind=lua_integer))
rc = lua_pcall(l, nargs, nresults, 0)

r1 = lua_tointeger(l, -1)
Expand Down
2 changes: 1 addition & 1 deletion examples/table/table.f90
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ program main
print '("table length: ", i0)', lua_rawlen(l, -1)

do i = 1, 3
rc = lua_rawgeti(l, -1, i)
rc = lua_rawgeti(l, -1, int(i, kind=lua_integer))

if (lua_isnumber(l, -1) == 1) then
print '("i: ", i4)', lua_tointeger(l, -1)
Expand Down
Loading

0 comments on commit 17763b2

Please sign in to comment.