-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtst_tbl_ptr.f90
73 lines (58 loc) · 1.4 KB
/
tst_tbl_ptr.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
program main
use flook
implicit none
character(*), parameter :: fortran_static_lua = '&
--[[ Print out hello statement directly, and immediately when &
Using the LUA interface &
--]] &
print("tbl-ptr-test") &
flook = {} &
'
! the lua embedded state
type(luaState) :: lua
type(luaTbl) :: tbl
integer :: i
real :: array(10)
real, pointer :: array_p(:)
logical :: fail
character(len=16) :: dtype
fail = .false.
! Open new lua state.
call lua_init(lua)
call lua_run(lua,code = fortran_static_lua)
! Initialize the array
do i = 1, size(array)
array(i) = i
end do
! create a table
tbl = lua_table(lua,'flook')
! define pointer in table `flook`
! Since a pointer does not have *bounds* per-see, we will
! store it.
! So
! flook.type == 'r4'
! flook.size == size(array) ! in correct dimensions
call lua_set_ptr(tbl, array)
! get pointer
call lua_get_ptr(tbl, array_p)
call lua_get(tbl, 'type', dtype)
! Check we have the same data!
if ( dtype /= 'r4' ) then
fail = .true.
end if
if ( size(array) /= size(array_p) ) then
fail = .true.
end if
do i = 1, size(array)
if ( abs(array(i) - array_p(i)) > 0.0000001 ) then
fail = .true.
end if
end do
! Print fail or success
if ( fail ) then
print *, 'FAIL'
else
print *, 'SUCCESS'
end if
call lua_close(lua)
end program main