一个自动更新VC++项目版本号的Lua脚本

某个项目经常需要在改完代码之后修改版本号,大概有7、8个文件需要一个个改动然后check in。我发现这个操作有个特点就是所有的版本号都是一样的,而且需要修改的字符串非常有规律,就是类似查找1, 6, 0, 16替换成1, 6, 0, 17这样,同时还有1,6,0,16替换成1,6,0,17(区别是逗号后面没有空格)。这种分析查找替换用Lua来做再简单不过了,故此写了这个脚本。

继续阅读“一个自动更新VC++项目版本号的Lua脚本”

Lua Wiki 部分翻译 — Lua Source

Lua Source

The section of the wiki allows anyone to document, explain, post questions, or make comments on the Lua source code. You may link to [1] or paste the code in question. Note: Please indicate which version of the source (e.g. 5.1.1) you are discussing so there will be no confusion upon future releases of Lua. Preferably, discussions should be on or updated to the latest release of Lua.

代码注释

概述
通过阅读Lua论文[2]以及Lua架构文档[The Lua Architecture]从不同角度了解Lua实现。
关于Lua虚拟机指令集有一个文档[A No-Frills Introduction to Lua 5.1 VM Instructions].
[Yueliang]项目(一个Lua虚拟机,移植c代码为Lua代码)有大量添加过注释的源代码,可以用来帮助理解相关的C语言代码。

模块结构

Lua源代码模块概要如下:

这些模块实现了应用功能:

  • ldebug.c – 调试接口。包括可以获取debug hooks的函数(lua_sethook, lua_gethook, lua_gethookcount),获取运行时栈信息的函数(lua_getstack / lua_getlocal / lua_setlocal),检查字节码的函数(luaG_checkopenop / luaG_checkcode),以及产生某些错误的函数(luaG_typeerror / luaG_concaterror / luaG_aritherror / luaG_ordererror / luaG_errormsg / luaG_runerror)
  • lzio.c – 一个通用的缓冲输入流接口。
  • lmem.c – 内存管理接口。实现了luaM_realloc / luaM_growaux_,这两个函数包装了内存分配函数。
  • lgc.c – 增量垃圾收集器(内存管理)。

以下模块实现了基本数据类型:

  • lstate.c – 全局状态。包括打开及关闭Lua states的函数(lua_newstate/lua_close)以及线程相关函数(luaE_newthread / luaE_freethread)。
  • lobject.c – Lua对象上的一些通用函数,包括数据类型<->字符串转换,原始相等测试(luaO_rawequalObj)和log base 2函数(luaO_log2)。
  • lstring.c – 字符串表(保存所有Lua处理过的字符串)
  • lfunc.c – 操作原型(prototypes)和闭包(closures)的辅助函数。
  • ltable.c – Lua表(哈希)

下面模块处理解析以及代码生成任务:

  • lcode.c – Lua代码生成器,被lparser.c使用。
  • llex.c – 词汇分析器,被lparser.c使用。
  • lparser.c – Lua分析器。
  • lundump.c – 载入编译好的Lua组块(chunks)。实现luaU_undump函数,可以载入预编译组块。也提供了luaU_header函数(luaU_undump内部使用)来解析函数头。
  • ldump.c – 保存编译好的组块。实现luaU_dump函数,可以转存(dump)一个函数对象为编译好的组块字符串。

下面模块处理Lua字节码的运行:

  • lopcodes.c – Lua虚拟机操作码。(通过luaP_opnames and luaP_opmodes表)定义关于所有操作码的名字和信息。
  • lvm.c – Lua virtual machine. Executes bytecodes (luaV_execute). Also exposes a few functions used by lapi.c (e.g. luaV_concat).
  • lvm.c – Lua虚拟机。运行字节码(luaV_execute),也暴露出一些函数给lapi.c使用(比如luaV_concat)。
  • ldo.c – Lua堆栈和调用结构。处理了函数调用(luaD_call / luaD_pcall),增长栈、协程处理……
  • ltm.c – tag方法。实现了从对象中获取元方法。

下面模块实现了标准库:

  • lbaselib.c – (base functions)
  • lstrlib.c – string
  • ltablib.c – table
  • lmathlib.c – math
  • loslib.c – os
  • liolib.c – io
  • loadlib.c – package
  • ldblib.c – debug

下面模块定义了C API:

  • lapi.c – Lua API,实现了大量Lua C API(如lua_* 函数)。
  • lauxlib.c – 定义了Lua_L*函数。
  • linit.c – 实现了luaL_openlibs函数,在C语言中载入以上模块。

下面模块实现了Lua和Luac程序:

  • lua.c – Lua独立解释器
  • print.c – 定义了PrintFunction?函数来打印函数中的字节码(被luac.c "-l"选项使用)。
  • luac.c – Lua编译器,保存字节码到文件,也可以列出字节码。

(5.1.3)

代码规范

外部符号的前缀指明了它从哪个模块来的:

luaA_ – lapi.c
luaB_ – lbaselib.c
luaC_ – lgc.c
luaD_ – ldo.c
luaE_ – lstate.c
luaF_ – lfunc.c
luaG_ – ldebug.c
luaH_ – ltable.c
luaI_ – lauxlib.c
luaK_ – lcode.c
luaL_ – lauxlib.c/h, linit.c (public functions)
luaM_ – lmem.c
luaO_ – lobject.c
luaP_ – lopcodes.c
luaS_ – lstring.c
luaT_ – ltm.c
luaU_ – lundump.c
luaV_ – lvm.c
luaX_ – llex.c
luaY_ – lparser.c
luaZ_ – lzio.c
lua_ – lapi.c/h + luaconf.h, debug.c
luai_ – luaconf.h
luaopen_ – luaconf.h + libraries (lbaselib.c, ldblib.c, liolib.c, lmathlib.c,
loadlib.c, loslib.c, lstrlib.c, ltablib.c)
(5.1.3)

src/Makefile

In src/Makefile (5.1.1), the mingw target is unusual in that it only builds lua (not luac). A mingw-cygwin target could be added too. See mingw notes in BuildingLua for resolution.

In src/luaconf.h (5.1.1), LUA_PATH_DEFAULT refers to both LUA_LDIR and LUA_CDIR, but LUA_CPATH_DEFAULT refers only to the LUA_CDIR of these. RiciLake suggested this might be a security decision, where C modules require more trust than Lua modules.

src/luaconf.h

In src/luaconf.h (5.1.1), there is a LUA_CDIR"loadall.dll", which is discussed in [3] [4].

src/lgc.h and lgc.c (garbage collector)

可以参考GarbageCollection以及EmergencyGarbageCollector,来了解垃圾收集的部分。

src/ltable.h and ltable.c (tables)

See LuaSourceTable

src/lmathlib.c

This is described some in BindingCodeToLua.

Note: the line "#define lmathlib_c" (and similar lines in the other libraries) exist only for conditionals in luaconf.h (noted by lhf).

Comments on this Page

Notes on the purpose of this page: Some users have said the Lua source code should be better documented and particularly, as done here, to allow anyone to assist in that. This is also related to a suggestion for a more wiki-like approach to commenting on each function in the Lua API as done on the below sites. This would probably involve a new section of the wiki, possibly named LuaDoc?, DocLua?, or individual pages like DocLuaTostring? if there’s enough content.

I strongly disagree that a wiki is the right medium to do this for several reasons, the main one being that it won’t scale well. I suggest checking in a pristine copy of the Lua upstream source to a source control repository, and allowing people to check in comments directly onto the source code (e.g. you can put a comment on a specific function, struct member, etc.). This way there is some hope of carrying the comments (by merging) to future Lua releases. —JohnBelmonte

— This was following some discussion with Rici, Lhf, and Steinwookie. I think the understanding is that this documentation would not necessarily be the usual "doxygen"-like format on a function/parameter/line level and cluttering every function. Rather, Lhf suggested this documentation could be more in a literate programming format. This is not entirely unusual. For example, there are books explaining the linux kernel sources, and in Lua there are things like the No-frills Intro to Lua 5.1 VM. There was also a feeling that the chance this documentation, or anything, would get into the core is fairly low, or at least there is no current commitment to that.–DavidManura

None of the arguments seem related to wiki being the wrong medium for this. What will happen with Lua 6, 7, 8, etc. when the set of files and their content changes? –John

— Much of the same concerns apply to LuaPowerPatches. The wiki can be more convenient/open. The Lua source is strongly controlled and a slowly moving target (e.g. very few patches against a small Lua core). Questions on the source can be posted here as well. Keeping the wiki content in sync with different Lua versions affects most pages on the wiki–and that has been a problem, but the solution is a simple convention of wiki content noting which version of Lua it applies to. —DavidManura

See Also

How to create c extension for lua and pass complex structure step by step

You could download the project from http://groups.google.com/group/lua5/web/luautil.rar

At first, create a windows dll application. The IDE I used is VC2008.

I suggest you download and install the "Lua for windows" from luaforge.net, it contains most useful packages for Lua windows development. You could copy "include" and "lib" directory under the installation directory to your project.

继续阅读“How to create c extension for lua and pass complex structure step by step”

Google的Go编程语言使用初探

Go编程语言是Google中一些大牛(尤其是有着plan9前科的大牛们)如Rob Pike,Ken Thomason这两位赫赫有名的程序高手、技术作家。

很多人认为Go编程语言有点像是C语言与Python的混血,在Golang主页上也清楚写着Go的祖先有C,有Pascal/Modula(也是C++的祖先)/Oberon,还有CSP这个语言,另外很多基础代码也来自Plan 9操作系统。

我是在一个Ubuntu9.10的虚拟机上试用的Go,大家可以跟着我的脚步一探Go的究竟。

1,准备

安装Go之前需要安装mercurial,这是Go的版本控制工具,可以直接通过ubuntu的安装程序搜索添加。

然后为当前用户定义下面几个bash变量。在ubuntu下敲入cd $HOME,一般是/home/username,然后敲vim .bashrc,修改.bashrc

在这个文件中添加如下(我的cpu是intel,所以是GOARCH是386,具体可参考golang.org说明):

GOROOT="$HOME/Go"

export GOROOT

GOOS=linux

export GOOS

GOARCH=386

export GOARCH

GOBIN="$HOME/bin"

export GOBIN

在帮助里写GOBIN是可选的,但我试了,必须有。

在.bashrc最后还要加上这一行,保证能正确编译安装。

PATH=$PATH:$GOBIN

2,下载源代码编译

在你的用户目录下运行这个命令

hg clone -r release https://go.googlecode.com/hg/ $GOROOT

这样在你的用户目录下应该有个Go目录了。

确保你的ubuntu安装了gcc(可以在软件包管理中添加build-essential)

进入Go\src

然后敲./all.bash

如果前面没有问题,编译应该可以正确完成。编译后可以进入$HOME/bin查看是不是有6g 6l 8g 8l这样的可执行文件。

image

3,试用Go

一般x86机器是使用8开头的命令,如8g进行编译,8l进行链接,6开头的是给AMD cpu使用,如果编译安装没错,那么$HOME/username/bin这个目录应该已经在PATH中(可以通过echo $PATH确认)。

这时候应该hello world了,编辑一个hello.go如下:

package main
import "fmt"

func main() { 
    fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
}

运行8g hello.go编译,正常情况下什么都不提示就执行完了,但是ls可以发现多了个hello.8文件

运行8l hello.8链接,会产生一个8.out.

运行./8.out应该打出如下消息:

image

然后可以进入Go的目录中(因为我设置的有问题,下载Go的目录名是hg,但是不影响什么),有个test目录,进去test目录,运行./run命令。

image

image

也可以单独编译某个代码运行,如果正确的话,不会有任何提示信息输出。

OK,基本上Go编程语言的大致试用情况就是这样,真正要学Go编程语言的朋友,还是需要花时间来读文档,写写代码不断练习的。