如何处理lua的循环引用?

314 views
Skip to first unread message

John

unread,
Mar 1, 2010, 9:58:30 PM3/1/10
to Lua中国用户组
local function B()
A()
end

local function A()
B()
end

Chunlin Zhang

unread,
Mar 2, 2010, 12:10:10 AM3/2/10
to lua...@googlegroups.com
没弄懂你的问题是什么,你是说如何防止?

你写的 local 的会出错,还不会 stack overflow,如果是全局的就会出现 stack overflow 了.

2010/3/2 John <ich...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "Lua中国用户组" group.
> To post to this group, send email to lua...@googlegroups.com.
> To unsubscribe from this group, send email to lua_cn+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/lua_cn?hl=en.
>
>

John

unread,
Mar 2, 2010, 2:05:03 AM3/2/10
to Lua中国用户组
不好意思,我的表达不够清晰明了。

我不是防止这个循环引用,而是需要用这个特性。但是发现local的function不能,而global的function却可以(当然上面的代码不
加条件break的话会stack overflow),想知道其中的原委。

Chunlin Zhang

unread,
Mar 2, 2010, 2:38:37 AM3/2/10
to lua...@googlegroups.com
这个还是一个特性啊?呵呵,不太理解

对于 local 会出错,我的看法是 local 变量的作用域是在它声明之后的,所以在你的代码里

local function B()
A()
end

这里的 A 会是 nil,因为 local A 的声明在后面.

如果翻译成c应该会更清楚,局部和全局的操作是不一样的,全局的用的是
lua_setglobal/lua_getglobal,而局部用的应该是栈里面的变量操作(或者其他操作,我不是很确定)

2010/3/2 John <ich...@gmail.com>:

VirusCamp

unread,
Mar 2, 2010, 8:19:20 PM3/2/10
to Lua中国用户组
你原来写的实际上是

local function B()
_G["A"]()
end

local function A()
B()
end

如果要求调用的都是local 的函数,应该这样写

local A

Chunlin Zhang

unread,
Mar 2, 2010, 8:29:11 PM3/2/10
to Lua中国用户组

On Mar 3, 9:19 am, VirusCamp <virusc...@gmail.com> wrote:
> 如果要求调用的都是local 的函数,应该这样写
>
> local A
>
> local function B()
> A()
> end
>
> local function A()
> B()
> end

哦,这个先声明的我倒是没想起来.
现在想想 John 要的可能是用在某些情况下的循环递归吧

Linker

unread,
Mar 3, 2010, 12:22:03 AM3/3/10
to lua...@googlegroups.com
先定义两个local变量.


2010/3/3 Chunlin Zhang <zhangc...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Lua中国用户组" group.
To post to this group, send email to lua...@googlegroups.com.
To unsubscribe from this group, send email to lua_cn+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/lua_cn?hl=en.




--
Regards,
Linker Lin
linker...@gmail.com

John

unread,
Mar 3, 2010, 12:33:12 AM3/3/10
to Lua中国用户组
我原来的意图大概如下
local function start()
-- do sth
end()
end

local function end()
-- do sth
start()
end

start()

我知道start和end定义为global function的话,会被放在一个全局的表(_G)里,lua会自己去查找,不像C++那样调用之前需
要看到定义。
可是对local function的机制就不甚了解了,下面这种方法(forward declaration)在lua里不能通过:

local A

local function B()
A()
end

local function A()
B()
end

A() -- error: attempt to call upvalue 'A' (a nil value)

在B里调用A时,A的值还是nil,而不是function。
不过A怎么成了upvalue了呢?

> 现在想想 John 要的可能是用在某些情况下的循环递归吧

VirusCamp

unread,
Mar 3, 2010, 1:04:13 AM3/3/10
to Lua中国用户组
--额,我写错了,应该这样写,我原来那样是重复定义了A
local A

local function B()
A()
end

A=function()
B()
end

A()

> > 现在想想 John 要的可能是用在某些情况下的循环递归吧- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Chunlin Zhang

unread,
Mar 3, 2010, 3:53:14 AM3/3/10
to lua...@googlegroups.com
2010/3/3 John <ich...@gmail.com>:

> 可是对local function的机制就不甚了解了,下面这种方法(forward declaration)在lua里不能通过:
>
> local A
>
> local function B()
>        A()
> end
>
> local function A()
>        B()
> end
>
> A()  -- error: attempt to call upvalue 'A' (a nil value)
>
> 在B里调用A时,A的值还是nil,而不是function。
> 不过A怎么成了upvalue了呢?
local 的变量,应该有可能是栈里的值,也有可能是 upvalue 吧,至于都分别对应哪些情况还没研究过.
>
>
>
>> 现在想想 John 要的可能是用在某些情况下的循环递归吧

John

unread,
Mar 4, 2010, 10:18:27 PM3/4/10
to Lua中国用户组

On Mar 3, 2:04 pm, VirusCamp <virusc...@gmail.com> wrote:
> --额,我写错了,应该这样写,我原来那样是重复定义了A
> local A
>
> local function B()
> A()
> end
>
> A=function()
> B()
> end
>
> A()
>
> On 3月3日, 下午1时33分, John <ich...@gmail.com> wrote:
>
>


这个方法不错~
怎么当时就没有想到lua可以这样呢,看来还得多多熟悉......
多谢指教了!

John

unread,
Mar 28, 2010, 3:01:51 AM3/28/10
to Lua中文用户组

重读了pil,在6章第2节讲解非全局函数时已经对此作了解答:

local A, B

function A()
B()
end

function B()
A()
end


的确是多看一些书,少提一个问题。

Reply all
Reply to author
Forward
0 new messages