美文网首页
#Lua#Pushing elements

#Lua#Pushing elements

作者: KomalZheng | 来源:发表于2017-03-28 20:31 被阅读112次

    此篇介绍 C API提供的向The Stack中压入元素的操作函数

    lua_pushnil 压入nil类型数据
    lua_pushboolean 压入boolean类型数据
    lua_pushnumber 压入double类型数据
    lua_pushinteger 压入整型数据
    lua_pushunsigned 压入无符号整型数据
    lua_pushlstring 压入字符串类型数据,同时需要传入这个字符串长度
    lua_pushstring 压入字符串类型数据

    void lua_pushnil (lua_State *L);
    void lua_pushboolean (lua_State *L, int bool);
    void lua_pushnumber (lua_State *L, lua_Number n);
    void lua_pushinteger (lua_State *L, lua_Integer n);
    void lua_pushunsigned (lua_State *L, lua_Unsigned n);
    void lua_pushlstring (lua_State *L, const char *s, size_t len);
    void lua_pushstring (lua_State *L, const char *s);
    

    注意事项:
    lua_Number类型是Lua中的number类型,默认是double类型,在某些安装环境中,会设置为float类型甚至更长的整形

    每次向The Stack中压入元素时,检查其是否拥有足够的空间。

    int lua_checkstack (lua_State *L, int sz);
    

    测试并打印


    1. lua_pushnil

    函数原型:

    void lua_pushnil(lua_State *L);
    

    测试:

    lua_pushnil(L);
    dump_stack(L);
    

    输出:

    ---- { dump_stack ----
    nil    
    ---- dump_stack } ----
    

    2. lua_pushboolean

    函数原型:

    void lua_pushboolean(lua_State *L, int bool)
    

    测试:

    lua_pushboolean(L, 1);
    lua_pushboolean(L, 0);
    lua_pushboolean(L, 3);
    lua_pushboolean(L, -3);
    dump_stack(L);
    

    输出:

    ---- { dump_stack ----
    true    false    true    true    
    ---- dump_stack } ----
    

    3. lua_pushnumber

    函数原型

    void lua_pushnumber(lua_State *L, lua_Number n);
    

    其中 lua_Number 即为 double

    测试:

    lua_pushnumber(L, 0);
    lua_pushnumber(L, 1);
    lua_pushnumber(L, 2.0);
    lua_pushnumber(L, -0);
    lua_pushnumber(L, -0.5);
    lua_pushnumber(L, -1);
    lua_pushnumber(L, -2.5);
    lua_pushnumber(L, 9223372036854775807);
    

    输出:

    ---- { dump_stack ----
    0    1    2    0    -0.5    -1    -2.5    9.22337e+18    
    ---- dump_stack } ----
    

    4. lua_pushinteger

    函数原型

    void lua_pushinteger(lua_State *L, lua_Integer n);
    

    其中 lua_Integer 即为 long long

    测试:

    lua_pushinteger(L, 0);
    lua_pushinteger(L, 1);
    lua_pushinteger(L, 2.0);
    lua_pushinteger(L, -0);
    lua_pushinteger(L, -0.6);
    lua_pushinteger(L, -1);
    lua_pushinteger(L, -2.5);
    lua_pushinteger(L, 9223372036854775807);
    

    输出:

    ---- { dump_stack ----
    0    1    2    0    0    -1    -2    9.22337e+18    
    ---- dump_stack } ----
    

    5. lua_pushlstring

    函数原型:

    void lua_pushlstring(lua_State *L, const char *s, size_t len);
    

    测试:

    const char * s = "Hello world";
    lua_pushlstring(L, s, strlen(s));
    

    输出:

    ---- { dump_stack ----
    'Hello world'    
    ---- dump_stack } ----
    

    6. lua_pushstring

    函数原型:

    void lua_pushstring(lua_State *L, const char *s);
    

    测试:

    const char * s = "Hello world\0";
    lua_pushstring(L, s);
    

    输出:

    ---- { dump_stack ----
    'Hello world'    
    ---- dump_stack } ----
    

    相关文章

      网友评论

          本文标题:#Lua#Pushing elements

          本文链接:https://www.haomeiwen.com/subject/qqksottx.html