Lua与C的基础交互

//
//  testLua.cpp
//  testLuaCall
//
//  Created by ljw on 2018/9/26.
//  Copyright © 2018年 x. All rights reserved.
//

#pragma once

#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
#include "lua_bridge.h"
/*
 #define LUA_TNIL        0
 #define LUA_TBOOLEAN        1
 #define LUA_TLIGHTUSERDATA    2
 #define LUA_TNUMBER        3
 #define LUA_TSTRING        4
 #define LUA_TTABLE        5
 #define LUA_TFUNCTION        6
 #define LUA_TUSERDATA        7
 #define LUA_TTHREAD        8
 */

void dump(lua_State* L)
{
    int top = lua_gettop(L);
    int type = LUA_TNONE;
    char typestr[20];
    for (int i = top; i != 0; i--)
    {
        type = lua_type(L, i); // 获取type
        switch (type) {
            case LUA_TNIL:
                snprintf(typestr, 20, "nil"); break;
            case LUA_TBOOLEAN:
                snprintf(typestr, 20, "boolean"); break;
            case LUA_TLIGHTUSERDATA:
                snprintf(typestr, 20, "light-userdata"); break;
            case LUA_TNUMBER:
                snprintf(typestr, 20, "number"); break;
            case LUA_TSTRING:
                snprintf(typestr, 20, "string"); break;
            case LUA_TTABLE:
                snprintf(typestr, 20, "table"); break;
            case LUA_TFUNCTION:
                snprintf(typestr, 20, "function"); break;
            case LUA_TUSERDATA:
                snprintf(typestr, 20, "userdta"); break;
            case LUA_TTHREAD:
                snprintf(typestr, 20, "thread"); break;
            default:
                snprintf(typestr, 20, "unknow"); break;
                break;
        }
        printf("[Stack Index:%d]%s\t: %s \n", i, typestr, lua_tostring(L, i));
    }
}

void clearStack(lua_State* L)
{
    lua_pop(L, lua_gettop(L));
}

void doPushStack(lua_State* L)
{
    clearStack(L);
    lua_pushstring(L, "this is test string in stack 1");//栈index = 1
    lua_pushnumber(L, 1); // 栈index = 2
    lua_pushboolean(L, 1); // 栈index = 3
    lua_pop(L, 1); // 从栈顶(最大栈Index)推出一个元素
    dump(L);
    
    clearStack(L);
    dump(L);
}

void doAdd(lua_State* L)
{
    clearStack(L);
    if (luaL_dostring(L, "add = function(x, y) return x + y end"))
    {
        return;
    }
    lua_getglobal(L, "add"); //栈Index = 1
    dump(L);
    lua_pushnumber(L, 1); //栈Index = 2
    lua_pushnumber(L, 2); //栈Index = 3
    //lua_pushnumber(L, 3); //参数个数异常测试
    dump(L);
    lua_pcall(L, 2, 1, 0); //pcall 为执行栈Index 为 1 的方法 第二个参数为z传入 n 个参数 第三个参数为返回 n 个参数 执行完成之后将pop出 (1(方法栈) + 所有参数个数)个栈内元素 并将返回值入栈 如果参数个数不正确 将返回attempt to call a string value
    dump(L);
    
    clearStack(L);
}

void doDumpTableSimple(lua_State* L)
{
    clearStack(L);
    if (luaL_dostring(L, "testTable = { a = 123, b = 456 , c = 'ccc'}"))
    {
        return;
    }
    lua_getglobal(L, "testTable");
    int idx = lua_gettop(L);
    printf("%d\n", idx);
    lua_pushnil(L);
    while (1)
    {
        if (!lua_next(L, idx))
            break;
        //dump(L);
        printf("[key]:%s, [value]:%s\n", lua_tostring(L, -1), lua_tostring(L, -2));
        lua_pop(L, 1);
        //dump(L);
    }
    //dump(L);
}

void doGetTableValue(lua_State* L)
{
        clearStack(L);
    
    if (luaL_dostring(L, "testTable = { a = 123, b = 456 , c = 'ccc'}"))
    {
        return;
    }
    
    lua_getglobal(L, "testTable"); //栈Index 1
    lua_pushstring(L, "a"); //栈Index 2
    lua_pushstring(L, "b"); //栈Index 3
    lua_pushstring(L, "c"); //栈Index 4
    dump(L);
    lua_gettable(L, 1); //GetTable方法 在栈尾为table的情况下 获取栈顶所在元素为Key值的元素 并替换该Key元素为Table对应的Value
    dump(L);
}


int cMethod(lua_State* L)
{
    int i = 0;
    int sum = 0;
    //检索所有的number值
    int NOS = lua_gettop(L);
    while (++i <= NOS) {
        if (lua_isnumber(L, i))
            sum += lua_tointeger(L, i);
        else
            printf("Index %d is not a number!\n", i);
    }
    clearStack(L); //清除栈
    lua_pushnumber(L, sum); //返回值
    return 1;
}

void doCallCMethod(lua_State* L)
{
    clearStack(L);
    lua_register(L, "cMethod", cMethod);
    if (luaL_dostring(L, "print(cMethod(1, 2, 3, 4, 5, saf) or 'failed')"))
    {
        return;
    }
    dump(L);
}


void testLuaStack(lua_State* L)
{
    clearStack(L);
    for (int i = 0; i != 10; ++i) {
        lua_pushnumber(L, i);
    }
    dump(L);
    
    printf("%d\n", lua_tointeger(L, 1)); //1
    printf("%d\n", lua_tointeger(L, 2)); //2
    printf("%d\n", lua_tointeger(L, 3)); //3
    printf("%d\n", lua_tointeger(L, -1)); //10
    printf("%d\n", lua_tointeger(L, -2)); //9
    printf("%d\n", lua_tointeger(L, -3)); //8
    /*
     *  总结 元素入栈时  所有已入栈的元素Index + 1
     *  Indexu最大的为h栈顶
     *  获取的传入Index为正数时, 顺序为栈顶-栈尾 10-1
     *  获取的传入Index为负数时,顺序为栈尾-栈顶 1-10
     *
     */
}

void doTest(lua_State* L)
{
    //doPushStack(L);
    //doAdd(L);
    //doGetTableValue(L);
    //doDumpTableSimple(L);
    //doCallCMethod(L);
    testLuaStack(L);
}

猜你喜欢

转载自blog.csdn.net/qq_29094161/article/details/82857407