summaryrefslogtreecommitdiff
path: root/components/external/lua/exlibs/lexamplelib.c
blob: f160ae35d8f6211be31b2d6eaba208bfb66ba125 (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
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
74
75
/**
 * example of adding lua external library
 */

#include "lua.h"
#include "lauxlib.h"

#include "lexlibs.h"

#define VERSION         1

int example_hello(lua_State *L)
{
    rt_kprintf("Hello, Lua on RT-Thead!\n");

    return 0;
}

int example_print(lua_State *L)
{
    int n = lua_gettop(L);
    int i;

    for (i=1; i<=n; i++)
    {
        if (i>1)
            rt_kprintf("\t");

        if (lua_isstring(L,i))
            rt_kprintf("%s",lua_tostring(L,i));
        else if (lua_isnumber(L, i))
            rt_kprintf("%d",lua_tointeger(L,i));
        else if (lua_isnil(L,i))
            rt_kprintf("%s","nil");
        else if (lua_isboolean(L,i))
            rt_kprintf("%s",lua_toboolean(L,i) ? "true" : "false");
        else
            rt_kprintf("%s:%p",luaL_typename(L,i),lua_topointer(L,i));
    }

    rt_kprintf("\n");

    return 0;
}

    
#define MIN_OPT_LEVEL       2
#include "lrodefs.h"

const LUA_REG_TYPE example_map[] =
{
    {LSTRKEY("hello"), LFUNCVAL(example_hello)},
    {LSTRKEY("print"), LFUNCVAL(example_print)},
#if LUA_OPTIMIZE_MEMORY > 0
    {LSTRKEY("version"), LNUMVAL(VERSION)},
#endif
    {LNILKEY, LNILVAL}
};

/**
 * Open exmaple library
 */
LUALIB_API int luaopen_example(lua_State *L)
{
#if LUA_OPTIMIZE_MEMORY > 0
    return 0;
#else
    luaL_register(L, EXLIB_EXAMPLE, example_map);
    lua_pushnumber(L, VERSION);
    lua_setfield(L, -2, "version");
    return 1;
#endif
}