1.
c代码中通过lua_push 把数据压入堆栈,lua调用c函数得到数据。luaL_check是对lua_to的封装,从堆栈中获取lua代码中函数调用的数据。
static int lread(lua_State *L) { struct socket * s = lua_touserdata(L,1); if (s == NULL || s->listen_fd < 0) { return luaL_error(L, "start socket first"); } size_t sz = 0; const char * welcome = luaL_checklstring(L,2,&sz); int fd = test(s, welcome,sz); if (fd >= 0) { char buffer[BUFFER_SIZE]; int rd = recv(fd, buffer, BUFFER_SIZE, 0); if (rd <= 0) { s->closed = 1; lua_pushboolean(L, 0); return 1; } lua_pushlstring(L, buffer, rd); return 1; } return 0; }
static int lstart(lua_State *L) { const char * addr = luaL_checkstring(L,1); int port = luaL_checkinteger(L,2); struct socket * s = lua_newuserdata(L, sizeof(*s)); s->listen_fd = -1; s->fd = -1; s->closed = 0; int lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int reuse = 1; setsockopt(s->listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int)); struct sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(addr); service.sin_port = htons(port); if (bind(lfd, (const struct sockaddr *)&service, sizeof(service)) < 0) { closesocket(lfd); printf("bind() failed"); exit(1); } if (listen(lfd, 1) < 0) { printf("listen(): Error"); exit(1); } s->listen_fd = lfd; return 1; }
function ldebug.start(host) local ip = (host and host.ip) or "127.0.0.1" local port = (host and host.port) or 6789 socks_fd = csock.start(ip , port) end function readline() local ret = split() if ret then return ret end local data = csock.read(socks_fd, socks_prompt) if data then socks_buffer = socks_buffer .. data return split() end return data end
2.stack
lua_pushnumber( L, 211 );
lua_pushnumber( L, 2222 );
lua_newtable( L );
执行代码,跟踪L的top,发现top的地址是增加的。
(StkId) top = 0x00007f9c63d01730
(StkId) top = 0x00007f9c63d01740
(StkId) top = 0x00007f9c63d01750
(StkId) top = 0x00007f9c63d01760
所以
那么当执行完lua_newtable之后栈上有三个元素,大致就是这样:
table
222
211
现在211是第一个元素,index为1,不过LUA也可以用负数来表示,那么现在他是多少?
index -index value
3 -1 table
2 -2 222
1 -3 211
http://blog.csdn.net/djvc/article/details/62135945
http://www.cnblogs.com/ringofthec/archive/2010/11/05/luaClosure.html
http://www.cnblogs.com/dwjaissk/p/3477062.html
http://www.benmutou.com/archives/52
3.quick
http://cocos2d-lua.org/download/3-6-4.md
https://github.com/u0u0/Quick-Cocos2dx-Community
https://github.com/shenzhan168/BubbleGame
old:
https://github.com/ever-changing/quickcocos2dx
17 ls /Users/temp/Downloads/quick-cocos2d-x
18 export
19 export QUICK_COCOS2DX_ROOT=/Users/temp/Downloads/quick-cocos2d-x
20 echo $(QUICK_COCOS2DX_ROOT)
21 $(QUICK_COCOS2DX_ROOT)
22 echo $(QUICK_COCOS2DX_ROOT)
23 cd /Users/temp/Downloads/quick-cocos2d-x/bin
24 ./create_project.sh -o landscape com.Reventon.Game.testGame
25 ./create_project.sh
26 ./create_project.sh -h
27 ./create_project.sh -r landscape com.Reventon.Game.testGame
28 ./create_project.sh -r landscape -p com.Reventon.Game.testGame
29 ls
30 testgame/run-mac.sh
31 testgame/run-mac.sh
32 testgame/run-mac.sh
33 testgame/run-mac.sh
ui.newTTFLabelMenuItem
https://github.com/search?utf8=%E2%9C%93&q=newTTFLabelMenuItem&type=Code
lua调用c/c++时,c/c++语言的函数参数一般只有char *,float,int等基础类型,没有字典类型。
lua代码的函数参数可以是table,function,所以一般会封装一个函数调用c/c++,这样lua代码调用lua函数传递table,lua函数内部调用c/c++。
function ldebug.start(host) local ip = (host and host.ip) or "127.0.0.1" local port = (host and host.port) or 6789 socks_fd = csock.start(ip , port) end ldebug.start { ip = "127.0.0.1", port = 6789 }
static int lstart(lua_State *L) { const char * addr = luaL_checkstring(L,1); int port = luaL_checkinteger(L,2); struct socket * s = lua_newuserdata(L, sizeof(*s)); s->listen_fd = -1; s->fd = -1; s->closed = 0; int lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int reuse = 1; setsockopt(s->listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int)); struct sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr(addr); service.sin_port = htons(port); if (bind(lfd, (const struct sockaddr *)&service, sizeof(service)) < 0) { closesocket(lfd); printf("bind() failed"); exit(1); } if (listen(lfd, 1) < 0) { printf("listen(): Error"); exit(1); } s->listen_fd = lfd; return 1; }
--[[-- 使用 TTF 字体创建文字显示对象,并返回 CCLabelTTF 对象。 可用参数: - text: 要显示的文本 - font: 字体名,如果是非系统自带的 TTF 字体,那么指定为字体文件名 - size: 文字尺寸,因为是 TTF 字体,所以可以任意指定尺寸 - color: 文字颜色(可选),用 ccc3() 指定,默认为白色 - align: 文字的水平对齐方式(可选) - valign: 文字的垂直对齐方式(可选),仅在指定了 dimensions 参数时有效 - dimensions: 文字显示对象的尺寸(可选),使用 CCSize() 指定 - x, y: 坐标(可选) align 和 valign 参数可用的值: - ui.TEXT_ALIGN_LEFT 左对齐 - ui.TEXT_ALIGN_CENTER 水平居中对齐 - ui.TEXT_ALIGN_RIGHT 右对齐 - ui.TEXT_VALIGN_TOP 垂直顶部对齐 - ui.TEXT_VALIGN_CENTER 垂直居中对齐 - ui.TEXT_VALIGN_BOTTOM 垂直底部对齐 ~~~ lua -- 创建一个居中对齐的文字显示对象 local label = ui.newTTFLabel({ text = "Hello, World", font = "Marker Felt", size = 64, align = ui.TEXT_ALIGN_CENTER -- 文字内部居中对齐 }) -- 左对齐,并且多行文字顶部对齐 local label = ui.newTTFLabel({ text = "Hello, World 您好,世界", font = "Arial", size = 64, color = ccc3(255, 0, 0), -- 使用纯红色 align = ui.TEXT_ALIGN_LEFT, valign = ui.TEXT_VALIGN_TOP, dimensions = CCSize(400, 200) }) ~~~ @param table params 参数表格对象 @return CCLabelTTF CCLabelTTF对象 ]] function ui.newTTFLabel(params) assert(type(params) == "table", "[framework.ui] newTTFLabel() invalid params") local text = tostring(params.text) local font = params.font or ui.DEFAULT_TTF_FONT local size = params.size or ui.DEFAULT_TTF_FONT_SIZE local color = params.color or display.COLOR_WHITE local textAlign = params.align or ui.TEXT_ALIGN_LEFT local textValign = params.valign or ui.TEXT_VALIGN_CENTER local x, y = params.x, params.y local dimensions = params.dimensions assert(type(size) == "number", "[framework.ui] newTTFLabel() invalid params.size") local label if dimensions then label = CCLabelTTF:create(text, font, size, dimensions, textAlign, textValign) else label = CCLabelTTF:create(text, font, size) end if label then label:setColor(color) function label:realign(x, y) if textAlign == ui.TEXT_ALIGN_LEFT then label:setPosition(math.round(x + label:getContentSize().width / 2), y) elseif textAlign == ui.TEXT_ALIGN_RIGHT then label:setPosition(x - math.round(label:getContentSize().width / 2), y) else label:setPosition(x, y) end end if x and y then label:realign(x, y) end end return label end