Lua 的C接口-解释器源码

  Lua 的C接口-解释器源码

作者:      来源:blog.csdn.net/nightfallrove     发表时间:2008-05-30     浏览次数: <script language="Javascript" src="http://www.yuanma.org/view.php?articleid=3051"></script>3902      字号:    


api.cpp
#include  < dirent.h >
#include 
" main.hpp "

int  lua_getcwd(lua_State *  L)//获取当前工作目录
{
        
char path[MAXPATHLEN];
        bzero(path, MAXPATHLEN);
        
if (lua_gettop(L) != 0 ) //不需要参数
        
{   
                luaL_argerror(L, 
0"no arg expected");
                
return 0;
        }
   
        
if ( !getcwd(path, MAXPATHLEN) )
        
{   
                luaL_error(L, 
"getcwd error %d, %s", errno, strerror(errno));
                
return 0;
        }
   
        lua_pushlstring(L, path, strlen(path));//将返回值压栈
        
return 1;//返回返回值个数
}


int  lua_dir(lua_State *  L)//取得目录下元素
{
        
const char* path = luaL_checkstring(L, 1); 
        DIR
* dir = opendir(path);
        
if ( !dir )
        
{   
                lua_pushnil(L);
                lua_pushstring(L, strerror(errno));
                
return 2;
        }
   
        
int i = 1;
        
struct dirent *ent;
        lua_newtable(L);//把所有元素放到一个table中,以数组返回
        
while( ent = readdir(dir) )
        
{   
                lua_pushnumber(L, i
++);
                lua_pushstring(L, ent
->d_name);
                lua_settable(L, 
-3);
        }
   
        closedir(dir);
        
return 1;
}


void  register_api(lua_State *  L)//注册api
{
        lua_register(L, 
"getcwd", lua_getcwd);//脚本中可以使用getcwd调用lua_getcwd
        lua_register(L, 
"dir", lua_dir);
        
const luaL_Reg mylib[] = 
        
{   
                
{"getcwd", lua_getcwd},
                
{"dir", lua_dir},
                
{NULL, NULL},
        }
;
        luaL_register(L, 
"tlib", mylib);//注册一个名为tlib的模块,tlib.getcwd()
}


void  create_table(lua_State *  L)//创建一个table
{
        lua_newtable(L);
        lua_pushnumber(L, 
123);
        lua_setfield(L, 
-2"id");
        lua_pushcfunction(L, lua_getcwd);
        lua_setfield(L, 
-2"fun");
        lua_setglobal(L, 
"tb");
}
init.lua
function  __init__()
        print(
" __init__ ok " )
        return 
1 ;
end
Makefile
CPPFLAGS=-Wall -g -O0 -I /usr/local/include/lua51/
LIB=-L/usr/local/lib/lua51/ -llua -lreadline
CC=g++

SRC=main.cpp api.cpp

OBJ=${SRC:%.cpp=%.o}

all: depend main

depend:
        @$(CC) -MM $(SRC)  > .depend
-include .depend

main: $(OBJ)
        $(CC) $(OBJ) $(CPPFLAGS)  $(LIB) -o $@

clean:
        -rm -rf *.o main .depend

 

以上代码在freebsd 6.2  gcc 3.4.6 lua 5.1.2下编译通过。

[1] [2]

责任编辑 webmaster

 
<script type="text/javascript"> </script> <script style="display: none;" type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值