LUA interface giving me trouble with garbage collection patch

106 views
Skip to first unread message

Benjamin Fritz

unread,
Jul 7, 2014, 1:37:17 AM7/7/14
to vim_dev
I want to finish up this patch to fix a crash in Vim:
https://groups.google.com/d/topic/vim_dev/dnN58kO5Vg4/discussion

I changed luaV_setref() to return a value if garbage collection cannot
safely proceed.

But, I do not know how to get that return value back to the code
calling it from eval.c, via set_ref_in_lua(). Can someone please
explain briefly how the function calls in the LUA interface work? I
cannot figure out how to get a return value back from lua_call(), in
the C code.

Yukihiro Nakadaira

unread,
Jul 10, 2014, 12:24:50 AM7/10/14
to vim...@googlegroups.com

Here is sample code.

#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>

/* int add(int x, int y) */
int add(lua_State *L)
{
    int x = lua_tointeger(L, 1);
    int y = lua_tointeger(L, 2);
    /* push result to stack */
    lua_pushinteger(L, x + y);
    /* return number of results */
    return 1;
}

int main()
{
    lua_State *L;
    int r;

    L = luaL_newstate();

    /* r = add(111, 222) */
    lua_pushcclosure(L, add, 0);
    lua_pushinteger(L, 111);
    lua_pushinteger(L, 222);
    lua_call(L, 2 /* nargs */, 1 /* nresults */);
    /* get results */
    r = lua_tointeger(L, -1);
    /* remove results from stack */
    lua_pop(L, 1);

    printf("add(111, 222) => %d\n", r);

    lua_close(L);

    return 0;
}

--
Yukihiro Nakadaira - yukihiro....@gmail.com

Ben Fritz

unread,
Jul 10, 2014, 11:49:03 AM7/10/14
to vim...@googlegroups.com
On Wednesday, July 9, 2014 11:24:50 PM UTC-5, Yukihiro Nakadaira wrote:
>
> Here is sample code.
>
> [edited]
>
>     L = luaL_newstate();
>
>     lua_pushcclosure(L, add, 0);
>     lua_pushinteger(L, 111);
>     lua_pushinteger(L, 222);
>     lua_call(L, 2 /* nargs */, 1 /* nresults */);
>     r = lua_tointeger(L, -1);
>     lua_pop(L, 1);
>     lua_close(L);

Thanks! I'll give this a try later. What's the -1 for in lua_tointeger()?

Is there a good way to test this when I'm done? E.g. are there LUA tests
in Vim's test suite that will exercise the garbage collector? Or maybe a
toy script using the LUA interface I could run myself?

Павлов Николай Александрович

unread,
Jul 10, 2014, 11:53:51 AM7/10/14
to vim...@googlegroups.com, Ben Fritz
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On July 10, 2014 7:49:02 PM GMT+03:00, Ben Fritz <fritzo...@gmail.com> wrote:
>On Wednesday, July 9, 2014 11:24:50 PM UTC-5, Yukihiro Nakadaira wrote:
>>
>> Here is sample code.
>>
>> [edited]
>>
>>     L = luaL_newstate();
>>
>>     lua_pushcclosure(L, add, 0);
>>     lua_pushinteger(L, 111);
>>     lua_pushinteger(L, 222);
>>     lua_call(L, 2 /* nargs */, 1 /* nresults */);
>>     r = lua_tointeger(L, -1);
>>     lua_pop(L, 1);
>>     lua_close(L);
>
>Thanks! I'll give this a try later. What's the -1 for in
>lua_tointeger()?

Almost all lua_* functions operate with stack. -1 is stack index. Like indexes in lua itself 1 means first value in the stack, 2 means second, ..., -1 means last, -2 means last but one, ...

In lua_pop you though specify how many values to pop, not which element to pop.

>
>Is there a good way to test this when I'm done? E.g. are there LUA
>tests
>in Vim's test suite that will exercise the garbage collector? Or maybe
>a
>toy script using the LUA interface I could run myself?

-----BEGIN PGP SIGNATURE-----
Version: APG v1.1.1

iQI1BAEBCgAfBQJTvrb/GBxaeVggPHp5eC52aW1AZ21haWwuY29tPgAKCRCf3UKj
HhHSvmnCD/9oKtyxddAIENOS3Yj+18oqxBAfgNFKjNiToD7tY87rx999YYFevnIc
KhgHaoiqcyw4cmTLWnBduoA1VmtUGt8iwxT9VXoOpw6HfbWqiuaqAsoIKrLIoLZp
H7zcLZIQX763eimUY/PsUgzDVShR73OsjHl8x0PcoYXsh80KcW42Fa5xNoV7O4lr
eqnG5V3OsQ6zcOz+SjUN6J3yHITDOpLGe0YNBuSctPqRtHooNR191rzfylQAJna9
5Mx5r05rXAg++HKF6sb7btf0lWMaUmYNJvTweRaxd6mSywxgwh9M+jZ7yiGXE9r3
thD7Be3Tk7woQT0ZdgkoioH6/efZNovoVf2NB/eOU17w3Dxm0doHfyH/s4xH5Dqq
4m2wJMtwlV2R0SYVYjzSOUXBVRxunUlU9m0K4CV2FUUwabLGaEJvmfegUt25sUlL
pcO1OFaJNLjGyHM96xbMwv1GpjRgUAfSjVbbDHxjeV/GeanemLlp8etYw1CTP9zs
CNNlVMzoHMSvYC8dGO4lqv7fdXNSFzvfUT/lL6i4trxVupgCvZ3oxWX4VUBTHRd4
Zzjr20iVLe4TGJTlSyCScvpMSZGdNiEEBhYhUpZGE20J5On7w5y2XHryrsNn+dOP
ZLNVywtzroQxKaLK79aOf4UDJhH2eKTGpuOiKQCnTk7/TxUXlPadTw==
=UYLM
-----END PGP SIGNATURE-----

Yukihiro Nakadaira

unread,
Jul 11, 2014, 8:52:53 AM7/11/14
to vim...@googlegroups.com
I have no idea.
Reply all
Reply to author
Forward
0 new messages