TimothyQiu's Blog

Happy coding

一道笔试题

Lua 学习笔记:零

timothyqiu posted @ 2011年3月07日 12:43 in 学习笔记 with tags c/c++ Lua , 2268 阅读

其实本来用过一点点Lua,不过都是需要什么功能就慢慢找 Reference Manual,这回毕业设计终于要用 Lua 写完整的东西了,于是准备把 PIL 慢慢看一遍。

首先是最简单的 Lua 为 C/C++ 程序变量赋值,类似史前的 INI 配置文件。

width = 640
height = 480

这样的赋值即设置全局变量,本质上就是在全局表中添加字段。

在 C/C++ 中,Lua 其实并不是直接去改变变量的值,而是宿主程序通过「读取脚本中设置的全局变量到栈、类型检查、从栈上取值」几步去主动查询。

int w, h;
if (luaL_loadfile(L, fname) || // 读取文件,将内容作为一个函数压栈
    lua_pcall(L, 0, 0, 0))     // 执行栈顶函数,0个参数、0个返回值、无出错处理函数(出错时直接把错误信息压栈)
    error();

lua_getglobal(L, "width");     // 将全局变量 width 压栈
lua_getglobal(L, "height");    // 将全局变量 height 压栈
if (!lua_isnumber(L, -2))      // 自顶向下第二个元素是否为数字
    error();
if (!lua_isnumber(L, -1))      // 自顶向下第一个元素是否为数字
    error();
w = lua_tointeger(L, -2);      // 自顶向下第二个元素转为整型返回
h = lua_tointeger(L, -1);      // 自顶向下第一个元素转为整型返回

读取表的字段的操作也是类似,只不过细节上比较麻烦,有点让我想起在汇编里调戏各种寄存器:

score = { chinese=80, english=85 }
int chinese, english;
if (luaL_loadfile(L, fname) || lua_pcall(L, 0, 0, 0))
    error();

lua_getglobal(L, "score");       // 全局变量 score 压栈

lua_pushstring(L, "chinese");    // 字符串 math 压栈
lua_gettable(L, -2);             // 以自顶向下第二个元素为表、第一个元素为索引取值,弹栈,将该值压栈
if (!lua_isnumber(L, -1))        // 栈顶元素是否为数字
    error();
chinese = lua_tointeger(L, -2);
lua_pop(L, 1);                   // 弹出一个元素 (此时栈顶为 score 变量)

lua_getfield(L, -1, "english");  // Lua5.1开始提供该函数简化七八两行
if (!lua_isnumber(L, -1))
    error();
english = lua_tointeger(L, -2);
lua_pop(L, 1);                   // 如果就此结束,这一行弹不弹都无所谓了

前面说过,设置全局变量本质就是在全局表中添加字段,所以 lua_getglobal 函数本质是从全局表中读取字段。没错,lua_getglobal 本身就是一个宏:

#define lua_getglobal(L,s)  lua_getfield(L, LUA_GLOBALSINDEX, s)

LUA_GLOBALSINDEX 指明的就是全局表的索引。

于是基本上今天就看了这么多……

TO BE CONTINUED...

seo service UK 说:
May 18, 2024 05:07:19 PM

Hi there would you mind letting me know which hosting company you’re utilizing?..I’ve loaded your blog in 3 different web browsers and I must say this blog loads a lot faster then most.I’m starting a blog soon but have no coding knowledge so I wanted toget advice from someone with experience. Any help would be..greatly appreciated!

먹튀폴리스보증업체 说:
Jun 08, 2024 02:17:46 PM

Oh my goodness! an amazing article dude. Thank you Nonetheless I’m experiencing concern with ur rss . Don?t know why Unable to subscribe to it. Is there anybody getting equivalent rss problem? Anyone who is aware of kindly respond. Thnkx

바카라사이트 说:
Jun 08, 2024 02:19:29 PM

I’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road. Cheers

메이저놀이터모음 说:
Jun 08, 2024 02:20:44 PM

Prevention is key, which is why we offer maintenance plans. We help you stay ahead of potential hazards and costly repairs with regular cleaning and inspections.

토토사이트순위 说:
Jun 08, 2024 02:21:27 PM

You could definitely see your skills within the paintings you write. The arena hopes for more passionate writers like you who are not afraid to say how they believe. Always follow your heart.

안전놀이터 说:
Jun 08, 2024 02:22:09 PM

My spouse and I absolutely love your blog and find a lot of your post’s to be exactly what I’m looking for. Do you offer guest writers to write content in your case? I wouldn’t mind writing a post or elaborating on a few of the subjects you write with regards to here. Again, awesome web site!

먹튀검증사이트 说:
Jun 08, 2024 02:22:57 PM

Hi, i feel that i noticed you visited my website so i got here to ?go back the prefer?.I’m trying to to find things to enhance my website!I guess its ok to use some of your ideas!!

토토사이트조회 说:
Jun 08, 2024 02:23:53 PM

I think one of your commercials triggered my web browser to resize, you may well want to put that on your blacklist.

메이저놀이터모음 说:
Jun 08, 2024 02:25:53 PM

This may be the proper weblog for desires to find out about this topic. You know so much its virtually challenging to argue along (not too I really would want…HaHa). You definitely put a new spin for a topic thats been revealed for some time. Excellent stuff, just great!

토토사이트추천 说:
Jun 08, 2024 02:26:26 PM

Thanks, I have all the files, is there any way that the geometry and mesh can be connected again? Now, I can import to a new .wbpj each file independently, so if I need to remesh my model, I need to change the geometry and start meshing from scratch.

안전놀이터 说:
Jun 08, 2024 02:27:53 PM

I’m truly enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Great work!

카지노사이트 说:
Jun 08, 2024 02:28:38 PM

This may be the proper weblog for desires to find out about this topic. You know so much its virtually challenging to argue along (not too I really would want…HaHa). You definitely put a new spin for a topic thats been revealed for some time. Excellent stuff, just great!

메이저놀이터 说:
Jun 08, 2024 02:28:46 PM

Great work! That is the kind of info that should be shared across the internet. Shame on Google for now not positioning this submit upper! Come on over and visit my web site . Thanks =)

토토매거진 说:
Jun 08, 2024 02:29:37 PM

Great work! That is the kind of info that should be shared across the internet. Shame on Google for now not positioning this submit upper! Come on over and visit my web site . Thanks =)

토토하이 说:
Jun 08, 2024 02:30:05 PM

I think one of your commercials triggered my web browser to resize, you may well want to put that on your blacklist.

안전놀이터 说:
Jun 08, 2024 02:30:13 PM

This is really likewise an incredibly beneficial placing most of us severely encountered shopping as a result of. It truly is faraway from each and every day we have now possibility to think about something. 

토토탐험대 说:
Jun 08, 2024 02:31:00 PM

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.

แนะนำ เว็บไซต์บาคาร่ 说:
Jun 08, 2024 02:31:22 PM

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking

먹튀검증업체 说:
Jun 08, 2024 02:37:20 PM

Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better. The post is written in very a good manner and it contains many useful information for me. Thank you very much and will look for more postings from you.

스포츠토토 说:
Jun 08, 2024 02:38:36 PM

This may be the proper weblog for desires to find out about this topic. You know so much its virtually challenging to argue along (not too I really would want…HaHa). You definitely put a new spin for a topic thats been revealed for some time. Excellent stuff, just great!

토토경비대 说:
Jun 08, 2024 02:39:39 PM

Goodness! Such an astounding and supportive post this is. I outrageously cherish it. It's so great thus amazing. I am simply flabbergasted. I trust that you keep on doing your work like this later on moreover.

먹튀검증 说:
Jun 08, 2024 02:40:41 PM

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking

먹튀지키미 说:
Jun 08, 2024 02:42:03 PM

It a fabulous blog post As i looked at caused by have the application. Advise everything that Need be to find expectation on potential future you certainly will persist designed for writing a great wonderful blog post.

바카라사이트 说:
Jun 08, 2024 02:44:11 PM

I truly contented to come all over your special data. I like this web page.

카지노사이트 说:
Jun 08, 2024 02:46:02 PM

If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content.

토토사이트 说:
Jun 08, 2024 02:47:05 PM

If more people that write articles really concerned themselves with writing great content like you, more readers would be interested in their writings. Thank you for caring about your content.

안전놀이터 说:
Jun 08, 2024 02:48:14 PM

The author is red hot about acquiring wooden furniture on the web and his examination about best wooden furniture has comprehended the plan of this article.

먹튀검증 说:
Jun 08, 2024 02:48:46 PM

I’m truly enjoying the design and layout of your site. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a developer to create your theme? Great work!

토토사이트 说:
Jun 08, 2024 02:49:14 PM

I was suggested this web site through my cousin. I’m not certain whether this put up is written through him as no one else recognize such exact approximately my difficulty. You’re wonderful! Thank you!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter