On Mon, Jul 7, 2014 at 2:36 PM, Benjamin Fritz <fritzophre...@gmail.com>
wrote:

> 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.


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.nakada...@gmail.com

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui