aboutsummaryrefslogtreecommitdiff
path: root/tests/C/lua_call-jsclosure.c
blob: 98fa7397fb3a7ad84c4a961c1cb8eda0e37b50d5 (plain)
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
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>

int func(lua_State *L) {
    const char *s = lua_tostring(L, lua_upvalueindex(1));
    lua_pushstring(L, s);
    return 1;
}

int main(void) {

    lua_State *L = luaL_newstate();

    luaL_openlibs(L);

    lua_pushstring(L, "upvalue hello !");
    lua_pushcclosure(L, func, 1);

    lua_call(L, 0, 1);

    printf("Lua returned: %s\n", lua_tostring(L, -1));

    lua_close(L);

    return 0;

}