ftnlf

Check-in [1be03a2e74]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Commit existing files.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:1be03a2e7440862070b96d97b7b4755212c632d9
User & Date: vadim 2016-01-27 09:07:53
Context
2016-01-27
16:14
Fix for 64bit systems. check-in: 9bf2fbbda3 user: vadim tags: trunk
09:07
Commit existing files. check-in: 1be03a2e74 user: vadim tags: trunk
08:52
initial empty check-in check-in: fadf64900b user: vadim tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added CMakeLists.txt.













































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
cmake_minimum_required(VERSION 2.6)
project(ftnlf Fortran C)

# Prefer static
find_library(LUA_STATIC_LIB NAMES 
	liblua51.a liblua5.1.a liblua-5.1.a liblua.a
	lua51 lua5.1 lua-5.1 lua)

if(LUA_STATIC_LIB)
	message(STATUS "Lua 5.1 found: ${LUA_STATIC_LIB}")
else()
	message(SEND_ERROR "Lua 5.1 not found")
endif()

find_package(Lua51 REQUIRED)

# By default, debug configuration is compiled
if(CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
	set (LFLAGS "-mkl -static-intel")
	if(CMAKE_BUILD_TYPE STREQUAL "Release")
		set (FCFLAGS "-fp-model precise -xHOST -O2") 
	else()
		set (FCFLAGS "-fp-model precise -g -O0 -C -traceback")
	endif()
	set (FCFLAGS90 "-warn -ipo")
elseif(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
	if(MINGW)
#		set (LFLAGS "-static-libgcc -static-libgfortran -Wl,-s")
		set (LFLAGS "-static-libgcc -static-libgfortran")
	else()		
		set (LFLAGS "")
	endif()
	if(CMAKE_BUILD_TYPE STREQUAL "Release")
		set (FCFLAGS "-O2 -march=native -mfpmath=sse")
	else()
		set (FCFLAGS "-g -O0 -fcheck=all -fbacktrace")
	endif()
	set (FCFLAGS90 "-Wall")
endif()

# gfortran does not respect current directory in INCLUDEs unless explicitly told to
include_directories(${LUA_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
add_executable(genconf luaf/genconf.c)
target_link_libraries(genconf ${LUA_LIBRARIES})

add_custom_command(
	OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/luaf_conf.fi
	COMMAND genconf > luaf_conf.fi
	DEPENDS genconf
	)

set (src
	ftnlf_interp.f90
	luaf/luaf.f90
	luaf/luafe.f90
	${CMAKE_CURRENT_BINARY_DIR}/luaf_conf.fi
	)

add_library(ftnlf MODULE ${src})
# Link static
# target_link_libraries(ftnlf ${LUA_STATIC_LIB})
# Link dynamic
target_link_libraries(ftnlf ${LUA_LIBRARIES})
set_target_properties(ftnlf PROPERTIES COMPILE_FLAGS "${FCFLAGS} ${FCFLAGS90}")
set_target_properties(ftnlf PROPERTIES LINK_FLAGS "${LFLAGS}")
set_target_properties(ftnlf PROPERTIES PREFIX "")

# Ad-hoc
set(CMAKE_SHARED_LIBRARY_LINK_Fortran_FLAGS "")
set(CMAKE_Fortran_FLAGS_RELEASE "")

Added ftnlf.txt.



























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Список элементов для последующей интеграции.

0. Interp
0.0. <Сделано>
0.1. (???) Заменить метаметод index на call, проверить. Сделать проверку, что передан один аргумент.

1. Инициализация.
1.1. Инициализация интерпретатора Lua.
1.1.1. Глобальная (модульная) переменная, содержащая состояние интерпретатора.
1.2. Открытие библиотек.
1.3. Открытие модуля, содержащего interp.
1.4. (?) Глобализация interp
1.5. Загрузка файла Lua с БД (NB: имя файла должно быть известно на этот момент!!!)
1.6. Запись необходимых значений (каких?) в реестр Lua.
1.6.1. Выбор материала топлива и оболочки, таблицы для которых будут записаны в реестр (?).

2. Финализация.
2.1. Финализация Lua.

3. Механизм для вызова функции -- универсальная обертка.
3.1. Распаковать аргументы.
3.2. Положить в стек "свойство" (таблицу взять из реестра, имя "свойства" из аргументов).
3.3. Обработать случай скаляра.
3.4. (???) Обработать случай ud, если не будет сделан 0.1.
3.5. Обработать случай функции.
3.6. protected call с диагностикой.

 
 

Added ftnlf_interp.f90.





































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
! Subroutines to use in Lua extensions to Fortran
! Linear interpolation: 
module ftnlf_interp
contains
    ! Interpolation of the 0th and 1st order
    REAL(8) FUNCTION TABINT(LS, KEY,N,X0,X,Y)
        !&P
        !&P   Programmed by:    B.B.Sakharov
        !&P
        !&F   Function:
        !&F      perform interpolation
        !&A
        !&A   Arguments:
        !&A      LS   : Lua state
        !&A      KEY  : type of interpolation
        !&A         KEY=0 - constant interpolation
        !&A         KEY=1 - linear interpolation
        !&A      N    : dimensions of X and Y
        !&A      X0   : argument at which fuction is to be evaluated
        !&A      X    : argument table
        !&A             condition X(i) < X(i+1) is obligatory
        !&A             and not checked by the routine
        !&A      Y    : function table
        !&Z
        use, intrinsic :: iso_c_binding, only: c_ptr
        use :: LuaFE, only: luaFE_check
        implicit none
        type(c_ptr), intent(in) :: LS
        INTEGER(4) :: KEY,N
        REAL(8) :: X0,X,Y
        DIMENSION X(N),Y(N)
        INTEGER I
        DOUBLE PRECISION A

        call luaFE_check(LS, (KEY.LE.1) .AND. (KEY.GE.0), &
            '<<TABINT>> Invalid input parameter KEY')
        call luaFE_check(LS, N .GE. 1, &
            '<<TABINT>> Invalid input parameter N')

        IF (X0.LE.X(1)) THEN
            TABINT=Y(1)
            RETURN
        ENDIF

        IF (X0.GE.X(N)) THEN
            TABINT=Y(N)
            RETURN
        ENDIF

        DO I=1,N-1
            IF ((X0.GE.X(I)) .AND. (X0.LT.X(I+1))) GO TO 10
        ENDDO
    10  CONTINUE

        IF (KEY.EQ.0) THEN
            TABINT=Y(I)
            RETURN
        ELSE
            A=(X0-X(I))/DMAX1((X(I+1)-X(I)),1.D-30)
            TABINT=Y(I)+A*(Y(I+1)-Y(I))
            RETURN
        ENDIF
    END FUNCTION TABINT

    ! Create new interpolation object from table at the top
    function l_newintobj(L) bind(C) result(r)
        use, intrinsic :: iso_c_binding, only: c_int, c_ptr, c_f_pointer
        use :: LuaF
        use :: LuaFE
        implicit none
        type(c_ptr), value, intent(in) :: L
        integer(c_int) :: r
        integer(4) :: ntbl, lentbl, k, i, key
        integer(4), pointer:: itbl(:)
        type(c_ptr) :: ud
        real(8), pointer :: tbl(:)
        real(8) :: x, y
        
        ! Check args
        call luaL_checktype(L, 1, LUA_TTABLE)
        if (lua_gettop(L) == 2) then
            call luaL_checktype(L, 2, LUA_TNUMBER)
            ! Get interpolation key and pop it
            key = INT(lua_tointeger(L, 2), 4)
            call lua_pop(L, 1)
        else
            key = 1
        endif
        
        ! get table length
        ntbl = INT(lua_objlen(L, 1), 4)
        call luaFE_check(L, ntbl > 0 .and. mod(ntbl, 2) == 0, &
            'Invalid interpolation table')
        ntbl = ntbl/2
        lentbl = (1+2*ntbl)
        
        ! Allocate Lua userdata
        ud = lua_newuserdata(L, lentbl*8)
        ! Move table atop
        call lua_insert(L, -2)
        
        ! Associate Fortran table with ptr (integer part only)
        call c_f_pointer(ud, itbl, [2])
        itbl(1) = ntbl
        itbl(2) = key
        ! Disassociate
        itbl => NULL()

        ! Associate Fortran table with ptr
        call c_f_pointer(ud, tbl, [lentbl])
        
        ! Fill the table
        do k = 1, ntbl
            ! Get x
            i = 2*k-1
            call lua_rawgeti(L, -1, i)
            x = luaFE_checkdouble(L, -1, 'x element')
            ! Get y
            i = 2*k
            call lua_rawgeti(L, -2, i)
            y = luaFE_checkdouble(L, -1, 'y element')
            ! Pop x, y
            call lua_pop(L, 2)
            ! Put to table
            tbl(1+k) = x
            tbl(1+ntbl+k) = y
        enddo

        ! Disassociate
        tbl => NULL()
        
        ! Table is not needed anymore
        call lua_pop(L, 1)
        
        ! Set metatable
        call lua_newtable(L)
        call luaFE_register(L, '__index', l_intidx)
        r = lua_setmetatable(L, -2)
        
        ! Return userdata
        r = 1
    end function l_newintobj
    
    ! Interpolation userdata metatable __index method
    function l_intidx(L) bind(C) result(r)
        use, intrinsic :: iso_c_binding, only: c_int, c_ptr, c_f_pointer
        use :: LuaF
        implicit none
        type(c_ptr), value, intent(in) :: L
        integer(c_int) :: r
        integer(4) :: ntbl, lentbl, key
        type(c_ptr) :: ud
        integer(4), pointer:: itbl(:)
        real(8), pointer :: tbl(:)
        real(8) :: x, y
        
        ! Check args, get length
        call luaL_checktype(L, 1, LUA_TUSERDATA)
        call luaL_checktype(L, 2, LUA_TNUMBER)
        ! Get ptr to interpolation data
        ud = lua_touserdata(L, 1)
        ! Get interpolation value
        x = REAL(lua_tonumber(L, 2), 8)
        ! Pop them
        call lua_pop(L, 2)
        
        ! Associate Fortran table with ptr (integer part only)
        call c_f_pointer(ud, itbl, [2])
        ntbl = itbl(1)
        key = itbl(2)
        lentbl = (1+2*ntbl)
        ! Disassociate
        itbl => NULL()

        ! Associate Fortran table with ptr
        call c_f_pointer(ud, tbl, [lentbl])

        ! Interpolate
        y = TABINT(L, key, ntbl, x, tbl(2:1+ntbl), tbl(2+ntbl:lentbl))

        ! Disassociate
        tbl => NULL()
        
        ! Return interpolated value
        call lua_pushnumber(L, y)
        r = 1
    end function l_intidx
    
end module ftnlf_interp

! Register Fortran libraries
function luaopen_ftnlf(L) bind(C) result(r)
    use, intrinsic :: iso_c_binding, only: c_int, c_ptr
    use :: LuaF
    use :: LuaFE
    use :: ftnlf_interp
    implicit none
!DEC$ ATTRIBUTES DLLEXPORT :: luaopen_ftnlf
!GCC$ ATTRIBUTES DLLEXPORT :: luaopen_ftnlf
    type(c_ptr), value, intent(in) :: L
    integer(c_int) :: r

    ! Pop package name
    call lua_pop(L, 1)

    call lua_newtable(L)
    call luaFE_register(L, 'interp', l_newintobj)

    r = 1
end function luaopen_ftnlf

Added intel/c.cmd.





>
>
1
2
call "C:\Program Files\Intel\Composer XE 2011 SP1\bin\ipsxe-comp-vars.bat" ia32 vs2010
ifort /QxHOST /O2 /C /traceback /warn ..\luaf\luaf.f90 ..\luaf\luafe.f90 ..\ftnlf_interp.f90 /dll /exe:ftnlf.dll lua5.1_32.lib 

Added intel/lua5.1.dll.

cannot compute difference between binary files

Added intel/lua5.1_32.lib.

cannot compute difference between binary files

Added intel/luaf_conf.fi.







>
>
>
1
2
3
INTEGER(KIND=C_INT), PARAMETER :: LUA_IDSIZE = 60
INTEGER(KIND=C_INT), PARAMETER :: LUA_MINSTACK = 20
INTEGER(KIND=C_INT), PARAMETER :: LUAL_BUFFERSIZE = 512

Added luaf/genconf.c.

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include "lua.h"

int main(void) {
  printf("%s = %d\n", "INTEGER(KIND=C_INT), PARAMETER :: LUA_IDSIZE",
	 LUA_IDSIZE);
  printf("%s = %d\n", "INTEGER(KIND=C_INT), PARAMETER :: LUA_MINSTACK",
	 LUA_MINSTACK);
  printf("%s = %d\n", "INTEGER(KIND=C_INT), PARAMETER :: LUAL_BUFFERSIZE",
	 LUAL_BUFFERSIZE);
  return 0;
}

Added luaf/luaf.f90.





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
MODULE LUAF_TYPES
    USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT, C_PTR, C_FUNPTR, C_CHAR
    IMPLICIT NONE
    PRIVATE

    INCLUDE 'luaf_conf.fi'

    TYPE, PUBLIC, BIND(C) :: luaL_Reg
        TYPE(C_PTR) :: name
        TYPE(C_FUNPTR) :: func
    END TYPE luaL_Reg

    TYPE, PUBLIC, BIND(C) :: lua_Debug
        INTEGER(KIND=C_INT) :: event
        TYPE(C_PTR) :: name
        TYPE(C_PTR) :: namewhat
        TYPE(C_PTR) :: what
        TYPE(C_PTR) :: source
        INTEGER(KIND=C_INT) :: currentline
        INTEGER(KIND=C_INT) :: nups
        INTEGER(KIND=C_INT) :: linedefined
        INTEGER(KIND=C_INT) :: lastlinedefined
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(LUA_IDSIZE) :: short_src
        ! private part 
    END TYPE lua_Debug

    TYPE, PUBLIC, BIND(C) :: luaL_Buffer
        TYPE(C_PTR) :: p
        INTEGER(KIND=C_INT) :: lvl
        TYPE(C_PTR) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(LUAL_BUFFERSIZE) :: buffer
    END TYPE luaL_Buffer

END MODULE LUAF_TYPES
    
MODULE LUAF
    USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
    USE LUAF_TYPES
    IMPLICIT NONE

    INCLUDE 'luaf_const.fi'
    INCLUDE 'luaf_conf.fi'

    INTERFACE
        INCLUDE 'luaf_int.fi'
    END INTERFACE

CONTAINS

    ! ==================================================================
    ! By-reference interfaces for some functions where pointer interface
    ! is default. We need them since gfortran fails to compile different
    ! subroutines with the same binding label (permitted in Fortran'08).
    ! They have _r suffix and accept C types.

!extern lua_Alloc (lua_getallocf) (lua_State *L, void **ud)
    FUNCTION lua_getallocf_r(L1, ud2)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_LOC
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        TYPE(C_PTR), TARGET, INTENT(INOUT) :: ud2
        TYPE(C_FUNPTR) :: lua_getallocf_r
        TYPE(C_PTR) :: p
        p = C_LOC(ud2)
        lua_getallocf_r = lua_getallocf(L1, p)
    END FUNCTION lua_getallocf_r

!extern void (luaL_register) (lua_State *L, const char *libname,
!                                const luaL_Reg *l)
    SUBROUTINE luaL_register_r(L1, libname2, l3)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_LOC
        USE LUAF_TYPES, ONLY: luaL_Reg
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), TARGET, INTENT(IN) :: libname2
        TYPE(luaL_Reg), DIMENSION(*), INTENT(IN) :: l3
        TYPE(C_PTR) :: p
        p = C_LOC(libname2)
        CALL luaL_register(L1, p, l3)
    END SUBROUTINE luaL_register_r

!extern int (luaL_checkoption) (lua_State *L, int narg, const char *def,
!                                   const char *const lst[])
    FUNCTION luaL_checkoption_r(L1, narg2, def3, lst4)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR, C_LOC
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        INTEGER(KIND=C_INT), INTENT(IN) :: narg2
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), TARGET, INTENT(IN) :: def3
        TYPE(C_PTR), DIMENSION(*), INTENT(IN) :: lst4
        INTEGER(KIND=C_INT) :: luaL_checkoption_r
        TYPE(C_PTR) :: p
        p = C_LOC(def3)
        luaL_checkoption_r = luaL_checkoption(L1, narg2, p, lst4)
    END FUNCTION luaL_checkoption_r

!extern int (luaL_loadfile) (lua_State *L, const char *filename)
    FUNCTION luaL_loadfile_r(L1, file_fname2)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_INT, C_LOC
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), TARGET, INTENT(IN) :: file_fname2
        INTEGER(KIND=C_INT) :: luaL_loadfile_r
        TYPE(C_PTR) :: p
        p = C_LOC(file_fname2)
        luaL_loadfile_r = luaL_loadfile(L1, p)
    END FUNCTION luaL_loadfile_r

    ! ==================================================================
    ! Implemented macros

!#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i))
    FUNCTION lua_upvalueindex(i)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
        IMPLICIT NONE
        INTEGER, INTENT(IN) :: i
        INTEGER(KIND=C_INT) :: lua_upvalueindex
        lua_upvalueindex = LUA_GLOBALSINDEX-INT(i, C_INT)
    END FUNCTION lua_upvalueindex
        
!#define lua_pop(L,n) lua_settop(L, -(n)-1)
    SUBROUTINE lua_pop(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        CALL lua_settop(L, INT(-n-1, C_INT))
    END SUBROUTINE lua_pop

!#define lua_newtable(L) lua_createtable(L, 0, 0)
    SUBROUTINE lua_newtable(L)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CALL lua_createtable(L, 0_C_INT, 0_C_INT)
    END SUBROUTINE lua_newtable

!#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
    SUBROUTINE lua_register(L, n, f)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_FUNPTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: n
        TYPE(C_FUNPTR), INTENT(IN) :: f
        CALL lua_pushcfunction(L, f)
        CALL lua_setglobal(L, n)
    END SUBROUTINE lua_register

!#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0)
    SUBROUTINE lua_pushcfunction(L, f)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        TYPE(C_FUNPTR), INTENT(IN) :: f
        CALL lua_pushcclosure(L, f, 0_C_INT)
    END SUBROUTINE lua_pushcfunction

!#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION)
    FUNCTION lua_isfunction(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_isfunction
        lua_isfunction = lua_type(L, INT(n, C_INT)) == LUA_TFUNCTION
    END FUNCTION lua_isfunction

!#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE)
    FUNCTION lua_istable(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_istable
        lua_istable = lua_type(L, INT(n, C_INT)) == LUA_TTABLE
    END FUNCTION lua_istable

!#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA)
    FUNCTION lua_islightuserdata(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_islightuserdata
        lua_islightuserdata = lua_type(L, INT(n, C_INT)) == LUA_TLIGHTUSERDATA
    END FUNCTION lua_islightuserdata

!#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL)
    FUNCTION lua_isnil(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_isnil
        lua_isnil = lua_type(L, INT(n, C_INT)) == LUA_TNIL
    END FUNCTION lua_isnil

!#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN)
    FUNCTION lua_isboolean(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_isboolean
        lua_isboolean = lua_type(L, INT(n, C_INT)) == LUA_TBOOLEAN
    END FUNCTION lua_isboolean

!#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD)
    FUNCTION lua_isthread(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_isthread
        lua_isthread = lua_type(L, INT(n, C_INT)) == LUA_TTHREAD
    END FUNCTION lua_isthread

!#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE)
    FUNCTION lua_isnone(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_isnone
        lua_isnone = lua_type(L, INT(n, C_INT)) == LUA_TNONE
    END FUNCTION lua_isnone

!#define lua_isnoneornil(L,n) (lua_type(L, (n)) <= 0)
    FUNCTION lua_isnoneornil(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        LOGICAL :: lua_isnoneornil
        lua_isnoneornil = lua_type(L, INT(n, C_INT)) <= 0
    END FUNCTION lua_isnoneornil

! This macro/subroutine is useless in Fortran since we always know
! string length. Use lua_pushstring_f instead.
!#define lua_pushliteral(L,s) lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1)

!#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s))
    SUBROUTINE lua_setglobal(L, s)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s
        CALL lua_setfield(L, LUA_GLOBALSINDEX, s)
    END SUBROUTINE lua_setglobal

!#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s))
    SUBROUTINE lua_getglobal(L, s)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s
        CALL lua_getfield(L, LUA_GLOBALSINDEX, s)
    END SUBROUTINE lua_getglobal

! Macro corresponds to wrapper lua_tostring_f, see below
!#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)

!#define luaL_argcheck(L,cond,numarg,extramsg) ((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
    SUBROUTINE luaL_argcheck(L, cond, numarg, extramsg)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        LOGICAL, INTENT(IN) :: cond
        INTEGER, INTENT(IN) :: numarg
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: extramsg
        INTEGER :: dummy
        IF (cond) THEN
            dummy = luaL_argerror(L, INT(numarg, C_INT), extramsg)
        ENDIF
    END SUBROUTINE luaL_argcheck
    
! Macro corresponds to wrapper lua_checkstring_f, see below
!#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))

! Macro corresponds to wrapper lua_optstring_f, see below
!#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))

!#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
    FUNCTION luaL_checkint(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        INTEGER :: luaL_checkint
        luaL_checkint = INT(luaL_checkinteger(L, INT(n, C_INT)), &
            & KIND(luaL_checkint))
    END FUNCTION luaL_checkint

!#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
    FUNCTION luaL_optint(L, n, d)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_INTPTR_T
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        INTEGER, INTENT(IN) :: d
        INTEGER :: luaL_optint
        luaL_optint = INT(luaL_optinteger(L, INT(n, C_INT), &
            & INT(d, C_INTPTR_T)), KIND(d))
    END FUNCTION luaL_optint

! Assume long = 8
!#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
    FUNCTION luaL_checklong(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_LONG, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        INTEGER(8) :: luaL_checklong
        luaL_checklong = INT(luaL_checkinteger(L, INT(n, C_INT)), &
            & KIND(luaL_checklong))
    END FUNCTION luaL_checklong

!#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
    FUNCTION luaL_optlong(L, n, d)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_LONG, C_INTPTR_T, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        INTEGER(8), INTENT(IN) :: d
        INTEGER(8) :: luaL_optlong
        luaL_optlong = INT(luaL_optinteger(L, INT(n, C_INT), &
            & INT(d, C_INTPTR_T)), KIND(d))
    END FUNCTION luaL_optlong

! Macro corresponds to wrapper lua_typename_f, see below
!#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))

! Two interfaces
!#define luaL_dofile(L,fn) (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
    FUNCTION luaL_dofile(L, fn)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        TYPE(C_PTR), INTENT(IN) :: fn
        INTEGER :: luaL_dofile
        INTEGER :: r
        r = luaL_loadfile(L, fn)
        IF (r == 0) THEN
            r = lua_pcall(L, 0_C_INT, LUA_MULTRET, 0_C_INT)
        END IF
        luaL_dofile = r 
    END FUNCTION luaL_dofile

    FUNCTION luaL_dofile_r(L, fn)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: fn
        INTEGER :: luaL_dofile_r
        INTEGER :: r
        r = luaL_loadfile_r(L, fn)
        IF (r == 0) THEN
            r = lua_pcall(L, 0_C_INT, LUA_MULTRET, 0_C_INT)
        END IF
        luaL_dofile_r = r 
    END FUNCTION luaL_dofile_r

!#define luaL_dostring(L,s) (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
    FUNCTION luaL_dostring(L, s)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s
        INTEGER :: luaL_dostring
        INTEGER :: r
        r = luaL_loadstring(L, s)
        IF (r == 0) THEN
            r = lua_pcall(L, 0_C_INT, LUA_MULTRET, 0_C_INT)
        END IF
        luaL_dostring = r 
    END FUNCTION luaL_dostring

!#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
    SUBROUTINE luaL_getmetatable(L, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: n
        CALL lua_getfield(L, LUA_REGISTRYINDEX, n)
    END SUBROUTINE luaL_getmetatable


    ! These functions assume that C_PTR and INTEGER(C_INTPTR_T)
    ! have the same internal representation and that C character is 1 byte
    ! This might be invalid on some platforms.

!#define luaL_addchar(B,c) ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), (*(B)->p++ = (char)(c)))
    SUBROUTINE luaL_addchar(B, c)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_CHAR, C_INTPTR_T, C_PTR, C_LOC
        USE LUAF_TYPES, ONLY: luaL_Buffer
        IMPLICIT NONE
        TYPE(luaL_Buffer), TARGET, INTENT(INOUT) :: B
        CHARACTER(KIND=C_CHAR, LEN=1), INTENT(IN) :: c
        INTEGER(KIND=C_INTPTR_T) :: ipp, ipbuf, idx
        TYPE(C_PTR) :: dummy

        ! Convert ptrs to integer addresses
        ! Without (1), some versions of gfortran fail w/segfault
        ipbuf = TRANSFER(C_LOC(B%buffer(1)), 0_C_INTPTR_T)
        ipp = TRANSFER(B%p, 0_C_INTPTR_T)
        idx = ipp - ipbuf
        
        IF (idx >= LUAL_BUFFERSIZE) THEN
            dummy = luaL_prepbuffer(B)
            ! Recalculate addresses
            ipbuf = TRANSFER(C_LOC(B%buffer(1)), 0_C_INTPTR_T)
            ipp = TRANSFER(B%p, 0_C_INTPTR_T)
            idx = ipp - ipbuf
        ENDIF
        B%buffer(1+idx) = c
        ! Convert back to ptr
        B%p = TRANSFER(ipp+1, B%p)
    END SUBROUTINE luaL_addchar

!#define luaL_addsize(B,n) ((B)->p += (n))
    SUBROUTINE luaL_addsize(B, n)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INTPTR_T
        USE LUAF_TYPES, ONLY: luaL_Buffer
        IMPLICIT NONE
        TYPE(luaL_Buffer), TARGET, INTENT(INOUT) :: B
        INTEGER, INTENT(IN) :: n
        INTEGER(KIND=C_INTPTR_T) :: ipp

        ! Convert ptr to integer addresses
        ipp = TRANSFER(B%p, 0_C_INTPTR_T)
        ! Convert back to ptr
        B%p = TRANSFER(ipp+INT(n, C_INTPTR_T), B%p)
    END SUBROUTINE luaL_addsize

    ! ==================================================================
    ! Fortran <---> C string conversion functions.
    ! Note that C_CHAR kind is assumed here to be equal to default kind.
    ! This might be invalid on some platforms.

    ! Function to add terminating null char to Fortran string

    ! 1nd variant: default length
    FUNCTION F_C_STR(fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_CHAR, C_NULL_CHAR
        IMPLICIT NONE
        CHARACTER(*), INTENT(IN) :: fstr
        CHARACTER(KIND=C_CHAR, LEN=LEN(fstr)+1) :: F_C_STR

        F_C_STR = fstr // C_NULL_CHAR
    END FUNCTION F_C_STR

    ! 2nd variant: trimmed length
    FUNCTION F_C_STRT(fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_CHAR
        IMPLICIT NONE
        CHARACTER(*), INTENT(IN) :: fstr
        CHARACTER(KIND=C_CHAR, LEN=LEN_TRIM(fstr)+1) :: F_C_STRT

        F_C_STRT = F_C_STR(TRIM(fstr))
    END FUNCTION F_C_STRT

    ! Subroutines to convert C_PTR to Fortran string.
    ! Fortran buffer is specified as INOUT parameter.
    ! Result length is specified as length of the buffer.
    ! Output is truncated if required.

    ! 1st variant:
    ! Optional output parameters:
    ! actual string length determined from null character.
    ! logical flag if result isn't truncated.
    SUBROUTINE C_F_STR(cstr, fstr, lact, stat)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR,&
            & C_NULL_CHAR, C_F_POINTER
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: cstr
        CHARACTER(*), INTENT(INOUT) :: fstr
        INTEGER, OPTIONAL, INTENT(OUT) :: lact
        LOGICAL, OPTIONAL, INTENT(OUT) :: stat
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(:), POINTER :: fptr
        INTEGER :: l, l1

        l = LEN(fstr)
        fstr = ''

        ! Associate
        CALL C_F_POINTER(cstr, fptr, [l+1])

        l1 = 0
        DO WHILE (l1 < l .AND. fptr(l1+1) /= C_NULL_CHAR)
            l1 = l1 + 1
            fstr(l1:l1) = fptr(l1)
        ENDDO

        ! Now l1 <= l
        IF (PRESENT(lact)) THEN
            lact = l1
        ENDIF
        IF (PRESENT(stat)) THEN
            stat = fptr(l1+1) == C_NULL_CHAR
        END IF
    END SUBROUTINE C_F_STR

    ! 2nd variant:
    ! Input parameter:
    ! actual string length.
    ! Optional output parameters:
    ! logical flag if result isn't truncated.
    SUBROUTINE C_F_STRL(cstr, fstr, lact, stat)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_F_POINTER
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: cstr
        CHARACTER(*), INTENT(INOUT) :: fstr
        INTEGER, INTENT(IN) :: lact
        LOGICAL, OPTIONAL, INTENT(OUT) :: stat
        CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(:), POINTER :: fptr
        INTEGER :: l, k

        l = LEN(fstr)
        fstr = ''

        ! Associate
        CALL C_F_POINTER(cstr, fptr, [l+1])

        DO k = 1, MIN(l, lact)
            fstr(k:k) = fptr(k)
        ENDDO

        IF (PRESENT(stat)) THEN
            stat = lact <= l
        END IF
    END SUBROUTINE C_F_STRL

    ! ==================================================================
    ! Fortran convenience wrappers
    ! Functon/macro is wrapped iff:
    ! * it returns (pointer to) C string, or
    ! * length of input (Fortran) string can be used somehow
    ! Output strings are truncated if required, 
    ! actual length and status args are ignored

!extern const char *(lua_typename) (lua_State *L, int tp)
    SUBROUTINE lua_typename_f(L1, tp2, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        INTEGER, INTENT(IN) :: tp2
        CHARACTER(*), INTENT(INOUT) :: fstr
        TYPE(C_PTR) :: cstr

        cstr = lua_typename(L1, tp2)
        CALL C_F_STR(cstr, fstr)
    END SUBROUTINE lua_typename_f

!extern const char *(lua_tolstring) (lua_State *L, int idx, size_t *len)
    FUNCTION lua_tolstring_f(L, idx, len, fstr) RESULT(r)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_SIZE_T, &
            & C_LOC, C_ASSOCIATED
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx
        INTEGER, INTENT(OUT) :: len
        CHARACTER(*), INTENT(INOUT) :: fstr
        LOGICAL :: r
        INTEGER(KIND=C_SIZE_T), TARGET :: ls
        TYPE(C_PTR) :: cstr
        TYPE(C_PTR) :: p

        p = C_LOC(ls)
        cstr = lua_tolstring(L, INT(idx, C_INT), p)
        len = INT(ls, KIND(len))

        r = C_ASSOCIATED(cstr)
        IF (r) THEN
            CALL C_F_STRL(cstr, fstr, len)
        END IF
    END FUNCTION lua_tolstring_f

! Since we always know string length, this wrapper calls lua_pushlstring
!extern void (lua_pushstring) (lua_State *L, const char *s)
    SUBROUTINE lua_pushstring_f(L, s)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_SIZE_T
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(*), INTENT(IN) :: s
        CALL lua_pushlstring(L, s, INT(LEN(s), C_SIZE_T))
    END SUBROUTINE lua_pushstring_f

!extern const char *(luaL_checklstring) (lua_State *L, int numArg,
!                                                          size_t *l)
    SUBROUTINE luaL_checklstring_f(L1, numArg2, l3, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_SIZE_T, C_LOC
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        INTEGER, INTENT(IN) :: numArg2
        INTEGER, INTENT(INOUT) :: l3
        CHARACTER(*), INTENT(INOUT) :: fstr
        INTEGER(KIND=C_SIZE_T), TARGET :: ls
        TYPE(C_PTR) :: cstr
        TYPE(C_PTR) :: p

        p = C_LOC(ls)
        cstr = luaL_checklstring(L1, INT(numArg2, C_INT), p)
        l3 = INT(ls, KIND(l3))
        CALL C_F_STRL(cstr, fstr, l3)
    END SUBROUTINE luaL_checklstring_f

!extern const char *(luaL_optlstring) (lua_State *L, int numArg,
!                                          const char *def, size_t *l)
    SUBROUTINE luaL_optlstring_f(L1, numArg2, def3, l4, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_SIZE_T, C_LOC
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        INTEGER, INTENT(IN) :: numArg2
        CHARACTER(*), INTENT(IN) :: def3
        INTEGER, INTENT(INOUT) :: l4
        CHARACTER(*), INTENT(INOUT) :: fstr
        INTEGER(KIND=C_SIZE_T), TARGET :: ls
        TYPE(C_PTR) :: cstr
        TYPE(C_PTR) :: p

        p = C_LOC(ls)
        cstr = luaL_optlstring(L1, INT(numArg2, C_INT), &
            & F_C_STRT(def3), p)
        l4 = INT(ls, KIND(l4))
        CALL C_F_STRL(cstr, fstr, l4)
    END SUBROUTINE luaL_optlstring_f

!extern const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
!                                                  const char *r)
    SUBROUTINE luaL_gsub_f(L1, s2, p3, r4, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L1
        CHARACTER(*), INTENT(IN) :: s2, p3, r4
        CHARACTER(*), INTENT(INOUT) :: fstr
        TYPE(C_PTR) :: cstr

        cstr = luaL_gsub(L1, F_C_STRT(s2), F_C_STRT(p3), F_C_STRT(r4))
        CALL C_F_STR(cstr, fstr)
    END SUBROUTINE luaL_gsub_f

!extern void (luaL_addstring) (luaL_Buffer *B, const char *s)
! Since we always know string length, this wrapper calls luaL_addlstring
    SUBROUTINE luaL_addstring_f(B, s)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_SIZE_T
        USE LUAF_TYPES, ONLY: luaL_Buffer
        IMPLICIT NONE
        TYPE(luaL_Buffer), INTENT(INOUT) :: B
        CHARACTER(*), INTENT(IN) :: s

        CALL luaL_addlstring(B, s, INT(LEN(s), C_SIZE_T))
    END SUBROUTINE luaL_addstring_f

!#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
    FUNCTION lua_tostring_f(L, idx, fstr) RESULT(r)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, &
            & C_NULL_PTR, C_ASSOCIATED
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx
        CHARACTER(*), INTENT(INOUT) :: fstr
        LOGICAL :: r
        TYPE(C_PTR) :: cstr

        cstr = lua_tolstring(L, INT(idx, C_INT), C_NULL_PTR)

        r = C_ASSOCIATED(cstr)
        IF (r) THEN
            CALL C_F_STR(cstr, fstr)
        END IF
    END FUNCTION lua_tostring_f

!#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
    SUBROUTINE luaL_checkstring_f(L, idx, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_NULL_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx
        CHARACTER(*), INTENT(INOUT) :: fstr
        TYPE(C_PTR) :: cstr

        cstr = luaL_checklstring(L, INT(idx, C_INT), C_NULL_PTR)
        CALL C_F_STR(cstr, fstr)
    END SUBROUTINE luaL_checkstring_f

!#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
    SUBROUTINE luaL_optstring_f(L, n, d, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_NULL_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: n
        CHARACTER(*), INTENT(IN) :: d
        CHARACTER(*), INTENT(INOUT) :: fstr
        TYPE(C_PTR) :: cstr

        cstr = luaL_optlstring(L, INT(n, C_INT), &
            & F_C_STRT(d), C_NULL_PTR)
        CALL C_F_STR(cstr, fstr)
    END SUBROUTINE luaL_optstring_f

!#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
    SUBROUTINE luaL_typename_f(L, i, fstr)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: i
        CHARACTER(*), INTENT(INOUT) :: fstr
        TYPE(C_PTR) :: cstr

        cstr = lua_typename(L, lua_type(L, INT(i, C_INT)))
        CALL C_F_STR(cstr, fstr)
    END SUBROUTINE luaL_typename_f
    
END MODULE LUAF

Added luaf/luaf_const.fi.















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
INTEGER(KIND=C_INT), PARAMETER :: LUA_MULTRET = (-1)
INTEGER(KIND=C_INT), PARAMETER :: LUA_REGISTRYINDEX = (-10000)
INTEGER(KIND=C_INT), PARAMETER :: LUA_ENVIRONINDEX = (-10001)
INTEGER(KIND=C_INT), PARAMETER :: LUA_GLOBALSINDEX = (-10002)
INTEGER(KIND=C_INT), PARAMETER :: LUA_YIELD = 1
INTEGER(KIND=C_INT), PARAMETER :: LUA_ERRRUN = 2
INTEGER(KIND=C_INT), PARAMETER :: LUA_ERRSYNTAX = 3
INTEGER(KIND=C_INT), PARAMETER :: LUA_ERRMEM = 4
INTEGER(KIND=C_INT), PARAMETER :: LUA_ERRERR = 5
INTEGER(KIND=C_INT), PARAMETER :: LUA_TNONE = (-1)
INTEGER(KIND=C_INT), PARAMETER :: LUA_TNIL = 0
INTEGER(KIND=C_INT), PARAMETER :: LUA_TBOOLEAN = 1
INTEGER(KIND=C_INT), PARAMETER :: LUA_TLIGHTUSERDATA = 2
INTEGER(KIND=C_INT), PARAMETER :: LUA_TNUMBER = 3
INTEGER(KIND=C_INT), PARAMETER :: LUA_TSTRING = 4
INTEGER(KIND=C_INT), PARAMETER :: LUA_TTABLE = 5
INTEGER(KIND=C_INT), PARAMETER :: LUA_TFUNCTION = 6
INTEGER(KIND=C_INT), PARAMETER :: LUA_TUSERDATA = 7
INTEGER(KIND=C_INT), PARAMETER :: LUA_TTHREAD = 8
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCSTOP = 0
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCRESTART = 1
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCCOLLECT = 2
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCCOUNT = 3
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCCOUNTB = 4
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCSTEP = 5
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCSETPAUSE = 6
INTEGER(KIND=C_INT), PARAMETER :: LUA_GCSETSTEPMUL = 7
INTEGER(KIND=C_INT), PARAMETER :: LUA_HOOKCALL = 0
INTEGER(KIND=C_INT), PARAMETER :: LUA_HOOKRET = 1
INTEGER(KIND=C_INT), PARAMETER :: LUA_HOOKLINE = 2
INTEGER(KIND=C_INT), PARAMETER :: LUA_HOOKCOUNT = 3
INTEGER(KIND=C_INT), PARAMETER :: LUA_HOOKTAILRET = 4
INTEGER(KIND=C_INT), PARAMETER :: LUA_MASKCALL = (ISHFT(1, LUA_HOOKCALL))
INTEGER(KIND=C_INT), PARAMETER :: LUA_MASKRET = (ISHFT(1, LUA_HOOKRET))
INTEGER(KIND=C_INT), PARAMETER :: LUA_MASKLINE = (ISHFT(1, LUA_HOOKLINE))
INTEGER(KIND=C_INT), PARAMETER :: LUA_MASKCOUNT = (ISHFT(1, LUA_HOOKCOUNT))
INTEGER(KIND=C_INT), PARAMETER :: LUA_ERRFILE = (LUA_ERRERR+1)
INTEGER(KIND=C_INT), PARAMETER :: LUA_NOREF = (-2)
INTEGER(KIND=C_INT), PARAMETER :: LUA_REFNIL = (-1)

Added luaf/luaf_int.fi.





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
!extern lua_State *(lua_newstate) (lua_Alloc f, void *ud)
FUNCTION lua_newstate(f1, ud2) BIND(C, name="lua_newstate")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_FUNPTR, C_PTR
IMPLICIT NONE
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: f1
TYPE(C_PTR), VALUE, INTENT(IN) :: ud2
TYPE(C_PTR) :: lua_newstate
END FUNCTION lua_newstate

!extern void (lua_close) (lua_State *L)
SUBROUTINE lua_close(L1) BIND(C, name="lua_close")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
END SUBROUTINE lua_close

!extern lua_State *(lua_newthread) (lua_State *L)
FUNCTION lua_newthread(L1) BIND(C, name="lua_newthread")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_PTR) :: lua_newthread
END FUNCTION lua_newthread

!extern lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf)
FUNCTION lua_atpanic(L1, panicf2) BIND(C, name="lua_atpanic")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: panicf2
TYPE(C_FUNPTR) :: lua_atpanic
END FUNCTION lua_atpanic

!extern int (lua_gettop) (lua_State *L)
FUNCTION lua_gettop(L1) BIND(C, name="lua_gettop")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: lua_gettop
END FUNCTION lua_gettop

!extern void (lua_settop) (lua_State *L, int idx)
SUBROUTINE lua_settop(L1, idx2) BIND(C, name="lua_settop")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_settop

!extern void (lua_pushvalue) (lua_State *L, int idx)
SUBROUTINE lua_pushvalue(L1, idx2) BIND(C, name="lua_pushvalue")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_pushvalue

!extern void (lua_remove) (lua_State *L, int idx)
SUBROUTINE lua_remove(L1, idx2) BIND(C, name="lua_remove")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_remove

!extern void (lua_insert) (lua_State *L, int idx)
SUBROUTINE lua_insert(L1, idx2) BIND(C, name="lua_insert")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_insert

!extern void (lua_replace) (lua_State *L, int idx)
SUBROUTINE lua_replace(L1, idx2) BIND(C, name="lua_replace")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_replace

!extern int (lua_checkstack) (lua_State *L, int sz)
FUNCTION lua_checkstack(L1, sz2) BIND(C, name="lua_checkstack")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: sz2
INTEGER(KIND=C_INT) :: lua_checkstack
END FUNCTION lua_checkstack

!extern void (lua_xmove) (lua_State *from, lua_State *to, int n)
SUBROUTINE lua_xmove(from1, to2, n3) BIND(C, name="lua_xmove")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: from1
TYPE(C_PTR), VALUE, INTENT(IN) :: to2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
END SUBROUTINE lua_xmove

!extern int (lua_isnumber) (lua_State *L, int idx)
FUNCTION lua_isnumber(L1, idx2) BIND(C, name="lua_isnumber")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_isnumber
END FUNCTION lua_isnumber

!extern int (lua_isstring) (lua_State *L, int idx)
FUNCTION lua_isstring(L1, idx2) BIND(C, name="lua_isstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_isstring
END FUNCTION lua_isstring

!extern int (lua_iscfunction) (lua_State *L, int idx)
FUNCTION lua_iscfunction(L1, idx2) BIND(C, name="lua_iscfunction")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_iscfunction
END FUNCTION lua_iscfunction

!extern int (lua_isuserdata) (lua_State *L, int idx)
FUNCTION lua_isuserdata(L1, idx2) BIND(C, name="lua_isuserdata")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_isuserdata
END FUNCTION lua_isuserdata

!extern int (lua_type) (lua_State *L, int idx)
FUNCTION lua_type(L1, idx2) BIND(C, name="lua_type")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_type
END FUNCTION lua_type

!extern const char *(lua_typename) (lua_State *L, int tp)
FUNCTION lua_typename(L1, tp2) BIND(C, name="lua_typename")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: tp2
TYPE(C_PTR) :: lua_typename
END FUNCTION lua_typename

!extern int (lua_equal) (lua_State *L, int idx1, int idx2)
FUNCTION lua_equal(L1, idx12, idx23) BIND(C, name="lua_equal")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx12
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx23
INTEGER(KIND=C_INT) :: lua_equal
END FUNCTION lua_equal

!extern int (lua_rawequal) (lua_State *L, int idx1, int idx2)
FUNCTION lua_rawequal(L1, idx12, idx23) BIND(C, name="lua_rawequal")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx12
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx23
INTEGER(KIND=C_INT) :: lua_rawequal
END FUNCTION lua_rawequal

!extern int (lua_lessthan) (lua_State *L, int idx1, int idx2)
FUNCTION lua_lessthan(L1, idx12, idx23) BIND(C, name="lua_lessthan")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx12
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx23
INTEGER(KIND=C_INT) :: lua_lessthan
END FUNCTION lua_lessthan

!extern lua_Number (lua_tonumber) (lua_State *L, int idx)
FUNCTION lua_tonumber(L1, idx2) BIND(C, name="lua_tonumber")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_DOUBLE
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
REAL(KIND=C_DOUBLE) :: lua_tonumber
END FUNCTION lua_tonumber

!extern lua_Integer (lua_tointeger) (lua_State *L, int idx)
FUNCTION lua_tointeger(L1, idx2) BIND(C, name="lua_tointeger")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_INTPTR_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INTPTR_T) :: lua_tointeger
END FUNCTION lua_tointeger

!extern int (lua_toboolean) (lua_State *L, int idx)
FUNCTION lua_toboolean(L1, idx2) BIND(C, name="lua_toboolean")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_toboolean
END FUNCTION lua_toboolean

!extern const char *(lua_tolstring) (lua_State *L, int idx, size_t *len)
FUNCTION lua_tolstring(L1, idx2, len3) BIND(C, name="lua_tolstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
TYPE(C_PTR), VALUE, INTENT(IN) :: len3
TYPE(C_PTR) :: lua_tolstring
END FUNCTION lua_tolstring

!extern size_t (lua_objlen) (lua_State *L, int idx)
FUNCTION lua_objlen(L1, idx2) BIND(C, name="lua_objlen")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_SIZE_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_SIZE_T) :: lua_objlen
END FUNCTION lua_objlen

!extern lua_CFunction (lua_tocfunction) (lua_State *L, int idx)
FUNCTION lua_tocfunction(L1, idx2) BIND(C, name="lua_tocfunction")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_FUNPTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
TYPE(C_FUNPTR) :: lua_tocfunction
END FUNCTION lua_tocfunction

!extern void *(lua_touserdata) (lua_State *L, int idx)
FUNCTION lua_touserdata(L1, idx2) BIND(C, name="lua_touserdata")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
TYPE(C_PTR) :: lua_touserdata
END FUNCTION lua_touserdata

!extern lua_State *(lua_tothread) (lua_State *L, int idx)
FUNCTION lua_tothread(L1, idx2) BIND(C, name="lua_tothread")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
TYPE(C_PTR) :: lua_tothread
END FUNCTION lua_tothread

!extern const void *(lua_topointer) (lua_State *L, int idx)
FUNCTION lua_topointer(L1, idx2) BIND(C, name="lua_topointer")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
TYPE(C_PTR) :: lua_topointer
END FUNCTION lua_topointer

!extern void (lua_pushnil) (lua_State *L)
SUBROUTINE lua_pushnil(L1) BIND(C, name="lua_pushnil")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
END SUBROUTINE lua_pushnil

!extern void (lua_pushnumber) (lua_State *L, lua_Number n)
SUBROUTINE lua_pushnumber(L1, n2) BIND(C, name="lua_pushnumber")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_DOUBLE
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
REAL(KIND=C_DOUBLE), VALUE, INTENT(IN) :: n2
END SUBROUTINE lua_pushnumber

!extern void (lua_pushinteger) (lua_State *L, lua_Integer n)
SUBROUTINE lua_pushinteger(L1, n2) BIND(C, name="lua_pushinteger")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INTPTR_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INTPTR_T), VALUE, INTENT(IN) :: n2
END SUBROUTINE lua_pushinteger

!extern void (lua_pushlstring) (lua_State *L, const char *s, size_t l)
SUBROUTINE lua_pushlstring(L1, s2, l3) BIND(C, name="lua_pushlstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_SIZE_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s2
INTEGER(KIND=C_SIZE_T), VALUE, INTENT(IN) :: l3
END SUBROUTINE lua_pushlstring

!extern void (lua_pushstring) (lua_State *L, const char *s)
SUBROUTINE lua_pushstring(L1, s2) BIND(C, name="lua_pushstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s2
END SUBROUTINE lua_pushstring

!extern const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
!                                                      va_list argp)
! Failed to map type va_list

!extern const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...)
! Failed to map variadic function

!extern void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n)
SUBROUTINE lua_pushcclosure(L1, fn2, n3) BIND(C, name="lua_pushcclosure")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: fn2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
END SUBROUTINE lua_pushcclosure

!extern void (lua_pushboolean) (lua_State *L, int b)
SUBROUTINE lua_pushboolean(L1, b2) BIND(C, name="lua_pushboolean")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: b2
END SUBROUTINE lua_pushboolean

!extern void (lua_pushlightuserdata) (lua_State *L, void *p)
SUBROUTINE lua_pushlightuserdata(L1, p2) BIND(C, name="lua_pushlightuserdata")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_PTR), VALUE, INTENT(IN) :: p2
END SUBROUTINE lua_pushlightuserdata

!extern int (lua_pushthread) (lua_State *L)
FUNCTION lua_pushthread(L1) BIND(C, name="lua_pushthread")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: lua_pushthread
END FUNCTION lua_pushthread

!extern void (lua_gettable) (lua_State *L, int idx)
SUBROUTINE lua_gettable(L1, idx2) BIND(C, name="lua_gettable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_gettable

!extern void (lua_getfield) (lua_State *L, int idx, const char *k)
SUBROUTINE lua_getfield(L1, idx2, k3) BIND(C, name="lua_getfield")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: k3
END SUBROUTINE lua_getfield

!extern void (lua_rawget) (lua_State *L, int idx)
SUBROUTINE lua_rawget(L1, idx2) BIND(C, name="lua_rawget")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_rawget

!extern void (lua_rawgeti) (lua_State *L, int idx, int n)
SUBROUTINE lua_rawgeti(L1, idx2, n3) BIND(C, name="lua_rawgeti")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
END SUBROUTINE lua_rawgeti

!extern void (lua_createtable) (lua_State *L, int narr, int nrec)
SUBROUTINE lua_createtable(L1, narr2, nrec3) BIND(C, name="lua_createtable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: narr2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nrec3
END SUBROUTINE lua_createtable

!extern void *(lua_newuserdata) (lua_State *L, size_t sz)
FUNCTION lua_newuserdata(L1, sz2) BIND(C, name="lua_newuserdata")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_SIZE_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_SIZE_T), VALUE, INTENT(IN) :: sz2
TYPE(C_PTR) :: lua_newuserdata
END FUNCTION lua_newuserdata

!extern int (lua_getmetatable) (lua_State *L, int objindex)
FUNCTION lua_getmetatable(L1, objindex2) BIND(C, name="lua_getmetatable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: objindex2
INTEGER(KIND=C_INT) :: lua_getmetatable
END FUNCTION lua_getmetatable

!extern void (lua_getfenv) (lua_State *L, int idx)
SUBROUTINE lua_getfenv(L1, idx2) BIND(C, name="lua_getfenv")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_getfenv

!extern void (lua_settable) (lua_State *L, int idx)
SUBROUTINE lua_settable(L1, idx2) BIND(C, name="lua_settable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_settable

!extern void (lua_setfield) (lua_State *L, int idx, const char *k)
SUBROUTINE lua_setfield(L1, idx2, k3) BIND(C, name="lua_setfield")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: k3
END SUBROUTINE lua_setfield

!extern void (lua_rawset) (lua_State *L, int idx)
SUBROUTINE lua_rawset(L1, idx2) BIND(C, name="lua_rawset")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
END SUBROUTINE lua_rawset

!extern void (lua_rawseti) (lua_State *L, int idx, int n)
SUBROUTINE lua_rawseti(L1, idx2, n3) BIND(C, name="lua_rawseti")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
END SUBROUTINE lua_rawseti

!extern int (lua_setmetatable) (lua_State *L, int objindex)
FUNCTION lua_setmetatable(L1, objindex2) BIND(C, name="lua_setmetatable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: objindex2
INTEGER(KIND=C_INT) :: lua_setmetatable
END FUNCTION lua_setmetatable

!extern int (lua_setfenv) (lua_State *L, int idx)
FUNCTION lua_setfenv(L1, idx2) BIND(C, name="lua_setfenv")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_setfenv
END FUNCTION lua_setfenv

!extern void (lua_call) (lua_State *L, int nargs, int nresults)
SUBROUTINE lua_call(L1, nargs2, nresults3) BIND(C, name="lua_call")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nargs2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nresults3
END SUBROUTINE lua_call

!extern int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc)
FUNCTION lua_pcall(L1, nargs2, nresults3, errfunc4) BIND(C, name="lua_pcall")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nargs2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nresults3
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: errfunc4
INTEGER(KIND=C_INT) :: lua_pcall
END FUNCTION lua_pcall

!extern int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud)
FUNCTION lua_cpcall(L1, func2, ud3) BIND(C, name="lua_cpcall")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: func2
TYPE(C_PTR), VALUE, INTENT(IN) :: ud3
INTEGER(KIND=C_INT) :: lua_cpcall
END FUNCTION lua_cpcall

!extern int (lua_load) (lua_State *L, lua_Reader reader, void *dt,
!                                        const char *chunkname)
FUNCTION lua_load(L1, reader2, dt3, chunkname4) BIND(C, name="lua_load")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_CHAR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: reader2
TYPE(C_PTR), VALUE, INTENT(IN) :: dt3
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: chunkname4
INTEGER(KIND=C_INT) :: lua_load
END FUNCTION lua_load

!extern int (lua_dump) (lua_State *L, lua_Writer writer, void *data)
FUNCTION lua_dump(L1, writer2, data3) BIND(C, name="lua_dump")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: writer2
TYPE(C_PTR), VALUE, INTENT(IN) :: data3
INTEGER(KIND=C_INT) :: lua_dump
END FUNCTION lua_dump

!extern int (lua_yield) (lua_State *L, int nresults)
FUNCTION lua_yield1(L1, nresults2) BIND(C, name="lua_yield")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nresults2
INTEGER(KIND=C_INT) :: lua_yield1
END FUNCTION lua_yield1

!extern int (lua_resume) (lua_State *L, int narg)
FUNCTION lua_resume(L1, narg2) BIND(C, name="lua_resume")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: narg2
INTEGER(KIND=C_INT) :: lua_resume
END FUNCTION lua_resume

!extern int (lua_status) (lua_State *L)
FUNCTION lua_status(L1) BIND(C, name="lua_status")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: lua_status
END FUNCTION lua_status

!extern int (lua_gc) (lua_State *L, int what, int data)
FUNCTION lua_gc(L1, what2, data3) BIND(C, name="lua_gc")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: what2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: data3
INTEGER(KIND=C_INT) :: lua_gc
END FUNCTION lua_gc

!extern int (lua_error) (lua_State *L)
FUNCTION lua_error(L1) BIND(C, name="lua_error")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: lua_error
END FUNCTION lua_error

!extern int (lua_next) (lua_State *L, int idx)
FUNCTION lua_next(L1, idx2) BIND(C, name="lua_next")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
INTEGER(KIND=C_INT) :: lua_next
END FUNCTION lua_next

!extern void (lua_concat) (lua_State *L, int n)
SUBROUTINE lua_concat(L1, n2) BIND(C, name="lua_concat")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n2
END SUBROUTINE lua_concat

!extern lua_Alloc (lua_getallocf) (lua_State *L, void **ud)
FUNCTION lua_getallocf(L1, ud2) BIND(C, name="lua_getallocf")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_PTR), VALUE, INTENT(IN) :: ud2
TYPE(C_FUNPTR) :: lua_getallocf
END FUNCTION lua_getallocf

!extern void lua_setallocf (lua_State *L, lua_Alloc f, void *ud)
SUBROUTINE lua_setallocf(L1, f2, ud3) BIND(C, name="lua_setallocf")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: f2
TYPE(C_PTR), VALUE, INTENT(IN) :: ud3
END SUBROUTINE lua_setallocf

!extern void lua_setlevel (lua_State *from, lua_State *to)
SUBROUTINE lua_setlevel(from1, to2) BIND(C, name="lua_setlevel")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: from1
TYPE(C_PTR), VALUE, INTENT(IN) :: to2
END SUBROUTINE lua_setlevel

!extern int lua_getstack (lua_State *L, int level, lua_Debug *ar)
FUNCTION lua_getstack(L1, level2, ar3) BIND(C, name="lua_getstack")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
USE LUAF_TYPES, ONLY: lua_Debug
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: level2
TYPE(lua_Debug), INTENT(INOUT) :: ar3
INTEGER(KIND=C_INT) :: lua_getstack
END FUNCTION lua_getstack

!extern int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar)
FUNCTION lua_getinfo(L1, what2, ar3) BIND(C, name="lua_getinfo")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_INT
USE LUAF_TYPES, ONLY: lua_Debug
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: what2
TYPE(lua_Debug), INTENT(INOUT) :: ar3
INTEGER(KIND=C_INT) :: lua_getinfo
END FUNCTION lua_getinfo

!extern const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n)
FUNCTION lua_getlocal(L1, ar2, n3) BIND(C, name="lua_getlocal")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
USE LUAF_TYPES, ONLY: lua_Debug
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(lua_Debug), INTENT(IN) :: ar2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
TYPE(C_PTR) :: lua_getlocal
END FUNCTION lua_getlocal

!extern const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n)
FUNCTION lua_setlocal(L1, ar2, n3) BIND(C, name="lua_setlocal")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
USE LUAF_TYPES, ONLY: lua_Debug
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(lua_Debug), INTENT(IN) :: ar2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
TYPE(C_PTR) :: lua_setlocal
END FUNCTION lua_setlocal

!extern const char *lua_getupvalue (lua_State *L, int funcindex, int n)
FUNCTION lua_getupvalue(L1, funcindex2, n3) BIND(C, name="lua_getupvalue")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: funcindex2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
TYPE(C_PTR) :: lua_getupvalue
END FUNCTION lua_getupvalue

!extern const char *lua_setupvalue (lua_State *L, int funcindex, int n)
FUNCTION lua_setupvalue(L1, funcindex2, n3) BIND(C, name="lua_setupvalue")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: funcindex2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: n3
TYPE(C_PTR) :: lua_setupvalue
END FUNCTION lua_setupvalue

!extern int lua_sethook (lua_State *L, lua_Hook func, int mask, int count)
FUNCTION lua_sethook(L1, func2, mask3, count4) BIND(C, name="lua_sethook")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR), VALUE, INTENT(IN) :: func2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: mask3
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: count4
INTEGER(KIND=C_INT) :: lua_sethook
END FUNCTION lua_sethook

!extern lua_Hook lua_gethook (lua_State *L)
FUNCTION lua_gethook(L1) BIND(C, name="lua_gethook")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_FUNPTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_FUNPTR) :: lua_gethook
END FUNCTION lua_gethook

!extern int lua_gethookmask (lua_State *L)
FUNCTION lua_gethookmask(L1) BIND(C, name="lua_gethookmask")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: lua_gethookmask
END FUNCTION lua_gethookmask

!extern int lua_gethookcount (lua_State *L)
FUNCTION lua_gethookcount(L1) BIND(C, name="lua_gethookcount")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: lua_gethookcount
END FUNCTION lua_gethookcount

!extern int (luaopen_base) (lua_State *L)
FUNCTION luaopen_base(L1) BIND(C, name="luaopen_base")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_base
END FUNCTION luaopen_base

!extern int (luaopen_table) (lua_State *L)
FUNCTION luaopen_table(L1) BIND(C, name="luaopen_table")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_table
END FUNCTION luaopen_table

!extern int (luaopen_io) (lua_State *L)
FUNCTION luaopen_io(L1) BIND(C, name="luaopen_io")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_io
END FUNCTION luaopen_io

!extern int (luaopen_os) (lua_State *L)
FUNCTION luaopen_os(L1) BIND(C, name="luaopen_os")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_os
END FUNCTION luaopen_os

!extern int (luaopen_string) (lua_State *L)
FUNCTION luaopen_string(L1) BIND(C, name="luaopen_string")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_string
END FUNCTION luaopen_string

!extern int (luaopen_math) (lua_State *L)
FUNCTION luaopen_math(L1) BIND(C, name="luaopen_math")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_math
END FUNCTION luaopen_math

!extern int (luaopen_debug) (lua_State *L)
FUNCTION luaopen_debug(L1) BIND(C, name="luaopen_debug")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_debug
END FUNCTION luaopen_debug

!extern int (luaopen_package) (lua_State *L)
FUNCTION luaopen_package(L1) BIND(C, name="luaopen_package")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT) :: luaopen_package
END FUNCTION luaopen_package

!extern void (luaL_openlibs) (lua_State *L)
SUBROUTINE luaL_openlibs(L1) BIND(C, name="luaL_openlibs")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
END SUBROUTINE luaL_openlibs

!extern void (luaL_openlib) (lua_State *L, const char *libname,
!                                const luaL_Reg *l, int nup)
SUBROUTINE luaL_openlib(L1, libname2, l3, nup4) BIND(C, name="luaL_openlib")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_INT
USE LUAF_TYPES, ONLY: luaL_Reg
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: libname2
TYPE(luaL_Reg), DIMENSION(*), INTENT(IN) :: l3
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nup4
END SUBROUTINE luaL_openlib

!extern void (luaL_register) (lua_State *L, const char *libname,
!                                const luaL_Reg *l)
SUBROUTINE luaL_register(L1, libname2, l3) BIND(C, name="luaL_register")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
USE LUAF_TYPES, ONLY: luaL_Reg
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_PTR), VALUE, INTENT(IN) :: libname2
TYPE(luaL_Reg), DIMENSION(*), INTENT(IN) :: l3
END SUBROUTINE luaL_register

!extern int (luaL_getmetafield) (lua_State *L, int obj, const char *e)
FUNCTION luaL_getmetafield(L1, obj2, e3) BIND(C, name="luaL_getmetafield")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: obj2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: e3
INTEGER(KIND=C_INT) :: luaL_getmetafield
END FUNCTION luaL_getmetafield

!extern int (luaL_callmeta) (lua_State *L, int obj, const char *e)
FUNCTION luaL_callmeta(L1, obj2, e3) BIND(C, name="luaL_callmeta")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: obj2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: e3
INTEGER(KIND=C_INT) :: luaL_callmeta
END FUNCTION luaL_callmeta

!extern int (luaL_typerror) (lua_State *L, int narg, const char *tname)
FUNCTION luaL_typerror(L1, narg2, tname3) BIND(C, name="luaL_typerror")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: narg2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: tname3
INTEGER(KIND=C_INT) :: luaL_typerror
END FUNCTION luaL_typerror

!extern int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg)
FUNCTION luaL_argerror(L1, numarg2, extramsg3) BIND(C, name="luaL_argerror")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: numarg2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: extramsg3
INTEGER(KIND=C_INT) :: luaL_argerror
END FUNCTION luaL_argerror

!extern const char *(luaL_checklstring) (lua_State *L, int numArg,
!                                                          size_t *l)
FUNCTION luaL_checklstring(L1, numArg2, l3) BIND(C, name="luaL_checklstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: numArg2
TYPE(C_PTR), VALUE, INTENT(IN) :: l3
TYPE(C_PTR) :: luaL_checklstring
END FUNCTION luaL_checklstring

!extern const char *(luaL_optlstring) (lua_State *L, int numArg,
!                                          const char *def, size_t *l)
FUNCTION luaL_optlstring(L1, numArg2, def3, l4) BIND(C, name="luaL_optlstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: numArg2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: def3
TYPE(C_PTR), VALUE, INTENT(IN) :: l4
TYPE(C_PTR) :: luaL_optlstring
END FUNCTION luaL_optlstring

!extern lua_Number (luaL_checknumber) (lua_State *L, int numArg)
FUNCTION luaL_checknumber(L1, numArg2) BIND(C, name="luaL_checknumber")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_DOUBLE
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: numArg2
REAL(KIND=C_DOUBLE) :: luaL_checknumber
END FUNCTION luaL_checknumber

!extern lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def)
FUNCTION luaL_optnumber(L1, nArg2, def3) BIND(C, name="luaL_optnumber")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_DOUBLE
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nArg2
REAL(KIND=C_DOUBLE), VALUE, INTENT(IN) :: def3
REAL(KIND=C_DOUBLE) :: luaL_optnumber
END FUNCTION luaL_optnumber

!extern lua_Integer (luaL_checkinteger) (lua_State *L, int numArg)
FUNCTION luaL_checkinteger(L1, numArg2) BIND(C, name="luaL_checkinteger")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_INTPTR_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: numArg2
INTEGER(KIND=C_INTPTR_T) :: luaL_checkinteger
END FUNCTION luaL_checkinteger

!extern lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
!                                          lua_Integer def)
FUNCTION luaL_optinteger(L1, nArg2, def3) BIND(C, name="luaL_optinteger")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_INTPTR_T
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: nArg2
INTEGER(KIND=C_INTPTR_T), VALUE, INTENT(IN) :: def3
INTEGER(KIND=C_INTPTR_T) :: luaL_optinteger
END FUNCTION luaL_optinteger

!extern void (luaL_checkstack) (lua_State *L, int sz, const char *msg)
SUBROUTINE luaL_checkstack(L1, sz2, msg3) BIND(C, name="luaL_checkstack")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: sz2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: msg3
END SUBROUTINE luaL_checkstack

!extern void (luaL_checktype) (lua_State *L, int narg, int t)
SUBROUTINE luaL_checktype(L1, narg2, t3) BIND(C, name="luaL_checktype")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: narg2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: t3
END SUBROUTINE luaL_checktype

!extern void (luaL_checkany) (lua_State *L, int narg)
SUBROUTINE luaL_checkany(L1, narg2) BIND(C, name="luaL_checkany")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: narg2
END SUBROUTINE luaL_checkany

!extern int (luaL_newmetatable) (lua_State *L, const char *tname)
FUNCTION luaL_newmetatable(L1, tname2) BIND(C, name="luaL_newmetatable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: tname2
INTEGER(KIND=C_INT) :: luaL_newmetatable
END FUNCTION luaL_newmetatable

!extern void *(luaL_checkudata) (lua_State *L, int ud, const char *tname)
FUNCTION luaL_checkudata(L1, ud2, tname3) BIND(C, name="luaL_checkudata")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: ud2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: tname3
TYPE(C_PTR) :: luaL_checkudata
END FUNCTION luaL_checkudata

!extern void (luaL_where) (lua_State *L, int lvl)
SUBROUTINE luaL_where(L1, lvl2) BIND(C, name="luaL_where")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: lvl2
END SUBROUTINE luaL_where

!extern int (luaL_error) (lua_State *L, const char *fmt, ...)
! Failed to map variadic function

!extern int (luaL_checkoption) (lua_State *L, int narg, const char *def,
!                                   const char *const lst[])
FUNCTION luaL_checkoption(L1, narg2, def3, lst4) BIND(C, name="luaL_checkoption")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: narg2
TYPE(C_PTR), VALUE, INTENT(IN) :: def3
TYPE(C_PTR), DIMENSION(*), INTENT(IN) :: lst4
INTEGER(KIND=C_INT) :: luaL_checkoption
END FUNCTION luaL_checkoption

!extern int (luaL_ref) (lua_State *L, int t)
FUNCTION luaL_ref(L1, t2) BIND(C, name="luaL_ref")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: t2
INTEGER(KIND=C_INT) :: luaL_ref
END FUNCTION luaL_ref

!extern void (luaL_unref) (lua_State *L, int t, int ref)
SUBROUTINE luaL_unref(L1, t2, ref3) BIND(C, name="luaL_unref")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: t2
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: ref3
END SUBROUTINE luaL_unref

!extern int (luaL_loadfile) (lua_State *L, const char *filename)
FUNCTION luaL_loadfile(L1, filename2) BIND(C, name="luaL_loadfile")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(C_PTR), VALUE, INTENT(IN) :: filename2
INTEGER(KIND=C_INT) :: luaL_loadfile
END FUNCTION luaL_loadfile

!extern int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
!                                  const char *name)
FUNCTION luaL_loadbuffer(L1, buff2, sz3, name4) BIND(C, name="luaL_loadbuffer")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_SIZE_T, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: buff2
INTEGER(KIND=C_SIZE_T), VALUE, INTENT(IN) :: sz3
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: name4
INTEGER(KIND=C_INT) :: luaL_loadbuffer
END FUNCTION luaL_loadbuffer

!extern int (luaL_loadstring) (lua_State *L, const char *s)
FUNCTION luaL_loadstring(L1, s2) BIND(C, name="luaL_loadstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR, C_INT
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s2
INTEGER(KIND=C_INT) :: luaL_loadstring
END FUNCTION luaL_loadstring

!extern lua_State *(luaL_newstate) (void)
FUNCTION luaL_newstate() BIND(C, name="luaL_newstate")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
IMPLICIT NONE
TYPE(C_PTR) :: luaL_newstate
END FUNCTION luaL_newstate

!extern const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
!                                                  const char *r)
FUNCTION luaL_gsub(L1, s2, p3, r4) BIND(C, name="luaL_gsub")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: p3
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: r4
TYPE(C_PTR) :: luaL_gsub
END FUNCTION luaL_gsub

!extern const char *(luaL_findtable) (lua_State *L, int idx,
!                                         const char *fname, int szhint)
FUNCTION luaL_findtable(L1, idx2, fname3, szhint4) BIND(C, name="luaL_findtable")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_CHAR
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: idx2
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: fname3
INTEGER(KIND=C_INT), VALUE, INTENT(IN) :: szhint4
TYPE(C_PTR) :: luaL_findtable
END FUNCTION luaL_findtable

!extern void (luaL_buffinit) (lua_State *L, luaL_Buffer *B)
SUBROUTINE luaL_buffinit(L1, B2) BIND(C, name="luaL_buffinit")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
USE LUAF_TYPES, ONLY: luaL_Buffer
IMPLICIT NONE
TYPE(C_PTR), VALUE, INTENT(IN) :: L1
TYPE(luaL_Buffer), INTENT(INOUT) :: B2
END SUBROUTINE luaL_buffinit

!extern char *(luaL_prepbuffer) (luaL_Buffer *B)
FUNCTION luaL_prepbuffer(B1) BIND(C, name="luaL_prepbuffer")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
USE LUAF_TYPES, ONLY: luaL_Buffer
IMPLICIT NONE
TYPE(luaL_Buffer), INTENT(INOUT) :: B1
TYPE(C_PTR) :: luaL_prepbuffer
END FUNCTION luaL_prepbuffer

!extern void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l)
SUBROUTINE luaL_addlstring(B1, s2, l3) BIND(C, name="luaL_addlstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_CHAR, C_SIZE_T
USE LUAF_TYPES, ONLY: luaL_Buffer
IMPLICIT NONE
TYPE(luaL_Buffer), INTENT(INOUT) :: B1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s2
INTEGER(KIND=C_SIZE_T), VALUE, INTENT(IN) :: l3
END SUBROUTINE luaL_addlstring

!extern void (luaL_addstring) (luaL_Buffer *B, const char *s)
SUBROUTINE luaL_addstring(B1, s2) BIND(C, name="luaL_addstring")
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_CHAR
USE LUAF_TYPES, ONLY: luaL_Buffer
IMPLICIT NONE
TYPE(luaL_Buffer), INTENT(INOUT) :: B1
CHARACTER(KIND=C_CHAR, LEN=1), DIMENSION(*), INTENT(IN) :: s2
END SUBROUTINE luaL_addstring

!extern void (luaL_addvalue) (luaL_Buffer *B)
SUBROUTINE luaL_addvalue(B1) BIND(C, name="luaL_addvalue")
USE LUAF_TYPES, ONLY: luaL_Buffer
IMPLICIT NONE
TYPE(luaL_Buffer), INTENT(INOUT) :: B1
END SUBROUTINE luaL_addvalue

!extern void (luaL_pushresult) (luaL_Buffer *B)
SUBROUTINE luaL_pushresult(B1) BIND(C, name="luaL_pushresult")
USE LUAF_TYPES, ONLY: luaL_Buffer
IMPLICIT NONE
TYPE(luaL_Buffer), INTENT(INOUT) :: B1
END SUBROUTINE luaL_pushresult

Added luaf/luafe.f90.





































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
MODULE LUAFE
    ! Fortran extensions to Lua API

    USE LUAF
    IMPLICIT NONE

CONTAINS

    SUBROUTINE luaFE_check(L, cond, msg)
        ! Check condition and signal error if false
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        LOGICAL :: cond
        CHARACTER(LEN=*), INTENT(IN) :: msg
        INTEGER(KIND=C_INT) :: dummy

        IF (.NOT. cond) THEN
            call lua_pushstring_f(L, F_C_STR(msg))
            dummy = lua_error(L)
        END IF
    END SUBROUTINE luaFE_check

    SUBROUTINE luaFE_checktype(L, idx, typ, vname)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx, typ
        CHARACTER(LEN=*), INTENT(IN) :: vname
        INTEGER :: typ2
        CHARACTER(10) :: tn1, tn2

        typ2 = INT(lua_type(L, INT(idx, C_INT)), KIND(typ))
        IF (typ /= typ2) THEN
            CALL lua_typename_f(L, typ, tn1)
            CALL lua_typename_f(L, typ2, tn2)
            CALL luaFE_check(L, .false., &
                & 'bad type of '//vname//': '//TRIM(tn1) &
                & //' expected, got '//TRIM(tn2))
        END IF
    END SUBROUTINE luaFE_checktype

    SUBROUTINE luaFE_register(L, n, f)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT, C_FUNPTR, C_FUNLOC
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        INTERFACE
            FUNCTION f(L) BIND(C)
                USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
                IMPLICIT NONE
                TYPE(C_PTR), VALUE, INTENT(IN) :: L
                INTEGER(KIND=C_INT) :: f
            END FUNCTION f
        END INTERFACE

        TYPE(C_FUNPTR) :: fptr
        
        fptr = C_FUNLOC(f)
        CALL lua_pushstring_f(L, n)
        CALL lua_pushcfunction(L, fptr)
        CALL lua_settable(L, -3_C_INT)
    END SUBROUTINE luaFE_register

    FUNCTION luaFE_checkint4(L, idx, n) RESULT(r)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx
        CHARACTER(LEN=*), INTENT(IN) :: n
        INTEGER(4) :: r

        call luaFE_checktype(L, INT(idx, C_INT), LUA_TNUMBER, n)
        r = INT(lua_tointeger(L, INT(idx, C_INT)), KIND(r))
    END FUNCTION luaFE_checkint4

    FUNCTION luaFE_checkdouble(L, idx, n) RESULT(r)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx
        CHARACTER(LEN=*), INTENT(IN) :: n
        REAL(8) :: r

        call luaFE_checktype(L, INT(idx, C_INT), LUA_TNUMBER, n)
        r = REAL(lua_tonumber(L, INT(idx, C_INT)), KIND(r))
    END FUNCTION luaFE_checkdouble

    FUNCTION luaFE_tcheckint4(L, n) RESULT(r)
        ! Return field 'n' of table at the stack top as integer (w/check)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        INTEGER(4) :: r

        call lua_getfield(L, -1_C_INT, F_C_STR(n))
        r = luaFE_checkint4(L, -1_C_INT, 'field '//n)
        call lua_pop(L, 1_C_INT)
    END FUNCTION luaFE_tcheckint4

    FUNCTION luaFE_tcheckdouble(L, n) RESULT(r)
        ! Return field 'n' of table at the stack top as double (w/check)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        REAL(8) :: r

        call lua_getfield(L, -1_C_INT, F_C_STR(n))
        r = luaFE_checkdouble(L, -1_C_INT, 'field '//n)
        call lua_pop(L, 1_C_INT)
    END FUNCTION luaFE_tcheckdouble

    SUBROUTINE luaFE_toptint(L, n, v)
        ! Return field 'n' of table at the stack top as integer (w/default)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        INTEGER(4), INTENT(INOUT) :: v

        call lua_getfield(L, -1_C_INT, F_C_STR(n))
        v = luaL_optint(L, -1_C_INT, v)
        call lua_pop(L, 1)
    END SUBROUTINE luaFE_toptint

    SUBROUTINE luaFE_toptdouble(L, n, v)
        ! Return field 'n' of table at the stack top as double (w/default)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        REAL(8), INTENT(INOUT) :: v

        call lua_getfield(L, -1_C_INT, F_C_STR(n))
        v = luaL_optnumber(L, -1_C_INT, v)
        call lua_pop(L, 1)
    END SUBROUTINE luaFE_toptdouble

    SUBROUTINE luaFE_tsetint(L, n, v)
        ! Set field 'n' of table at the stack top as integer v
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INTPTR_T
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        INTEGER(4), INTENT(IN) :: v

        call lua_pushinteger(L, INT(v, C_INTPTR_T))
        call lua_setfield(L, -2_C_INT, F_C_STR(n))
    END SUBROUTINE luaFE_tsetint

    SUBROUTINE luaFE_tsetdouble(L, n, v)
        ! Set field 'n' of table at the stack top as double v
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INTPTR_T
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        CHARACTER(LEN=*), INTENT(IN) :: n
        REAL(8), INTENT(IN) :: v

        call lua_pushnumber(L, v)
        call lua_setfield(L, -2_C_INT, F_C_STR(n))
    END SUBROUTINE luaFE_tsetdouble

    FUNCTION luaFE_checklud(L, idx)
        USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_INT
        IMPLICIT NONE
        TYPE(C_PTR), INTENT(IN) :: L
        INTEGER, INTENT(IN) :: idx
        TYPE(C_PTR) :: luaFE_checklud

        call luaL_checktype(L, INT(idx, C_INT), LUA_TLIGHTUSERDATA)
        luaFE_checklud = lua_touserdata(L, INT(idx, C_INT))
    END FUNCTION luaFE_checklud


END MODULE LUAFE