修訂 | f91631b6b2453ee4c39107d3501c2adfe9fb162e (tree) |
---|---|
時間 | 2019-04-25 18:15:24 |
作者 | Nick Papior <nickpapior@gmai...> |
Commiter | Nick Papior |
enh: enabled getting user-data back (c_ptr)
@@ -69,6 +69,7 @@ | ||
69 | 69 | module procedure set_table_long |
70 | 70 | module procedure set_table_string |
71 | 71 | module procedure set_table_logical |
72 | + module procedure set_table_userdata | |
72 | 73 | end interface |
73 | 74 | |
74 | 75 | !> Get a value from the Lua script. |
@@ -892,6 +893,53 @@ | ||
892 | 893 | |
893 | 894 | end subroutine set_table_string |
894 | 895 | |
896 | + !> Put user-data pointer into a table. | |
897 | + subroutine set_table_userdata(val, L, thandle, key, pos) | |
898 | + use, intrinsic :: iso_c_binding, only: c_ptr | |
899 | + | |
900 | + type(flu_State) :: L !! Handle to the Lua script. | |
901 | + | |
902 | + !> Handle to the table to look the value up in. | |
903 | + integer, intent(in) :: thandle | |
904 | + | |
905 | + !> Pointer to set in the table. | |
906 | + type(c_ptr), intent(in) :: val | |
907 | + | |
908 | + !> Name of the entry to set. | |
909 | + !! | |
910 | + !! Key and pos are both optional, however at least one of them has to be | |
911 | + !! supplied. | |
912 | + !! The key takes precedence over the pos if both are given. | |
913 | + character(len=*), intent(in), optional :: key | |
914 | + | |
915 | + !> Position of the entry to set in the table. | |
916 | + !! | |
917 | + !! It allows the access to unnamed arrays in the Lua tables. | |
918 | + integer, intent(in), optional :: pos | |
919 | + | |
920 | + if (thandle > 0) then | |
921 | + if (present(key)) then | |
922 | + ! If there is a key, use that. | |
923 | + ! First put the value on the top of the stack | |
924 | + call flu_pushlightuserdata(L, val) | |
925 | + ! Now put it into the table | |
926 | + call flu_setField(L, thandle, trim(key)) | |
927 | + else | |
928 | + ! No key given, try to put the value by position | |
929 | + if (present(pos)) then | |
930 | + ! First put the index, where to write the value into the table, on the | |
931 | + ! stack. | |
932 | + call flu_pushInteger(L, pos) | |
933 | + ! Now put the actual value on the top of the stack. | |
934 | + call flu_pushlightuserdata(L, val) | |
935 | + ! Get the two entries from the stack into the table. | |
936 | + call flu_setTable(L, thandle) | |
937 | + end if | |
938 | + end if | |
939 | + end if | |
940 | + | |
941 | + end subroutine set_table_userdata | |
942 | + | |
895 | 943 | |
896 | 944 | !> This subroutine takes a one dimensional array, and puts it as a table |
897 | 945 | !! into the Lua context. |