前言
lua初体验, 用lua-5.3.3做了一次试验.
记录
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
};
#define LUA_FILE_NAME "test.lua"
int __cdecl IF_Test(lua_State* L)
{
const char* pParam1 = NULL;
double fParam2 =
0;
double fParam3 =
0;
double fRc = -
1;
int iRc =
1;
printf(
">> IF_Test\r\n");
pParam1 = luaL_checkstring(L,
1);
fParam2 = luaL_checknumber(L,
2);
fParam3 = luaL_checknumber(L,
3);
if (
0 ==
strcmp(
"add", pParam1)) {
fRc = fParam2 + fParam3;
}
else if (
0 ==
strcmp(
"sub", pParam1)) {
fRc = fParam2 - fParam3;
}
lua_pushnumber(L, fRc);
return LUA_YIELD;
}
int main(
int argc,
char* argv[])
{
lua_State* L = NULL;
int iLuaErr =
0;
L = luaL_newstate();
if (NULL != L) {
luaL_openlibs(L);
lua_register(L,
"IF_Test", IF_Test);
iLuaErr = luaL_dofile(L, LUA_FILE_NAME);
if (LUA_OK != iLuaErr) {
printf(
"%s\r\n", luaL_checkstring(L, -
1));
}
lua_close(L);
}
system(
"pause");
return 0;
}
io.write(
">> test.lua\r\n");
addRc = IF_Test(
"add",
100,
200)
print(
"IF_Test(\"add\", 100, 200) = ", addRc)
subRc = IF_Test(
"sub",
100,
200)
print(
"IF_Test(\"sub\", 100, 200) = ", subRc)
io.write(
"<< test.lua\r\n")
run result
>> test
.lua
>> IF_Test
IF_Test(
"add",
100,
200) =
300.0
>> IF_Test
IF_Test(
"sub",
100,
200) = -
100.0
<< test
.lua
请按任意键继续. . .
转载请注明原文地址: https://ju.6miu.com/read-1138588.html