lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Jun 5, 2016 at 4:11 PM, Daurnimator <quae@daurnimator.com> wrote:
> On 6 June 2016 at 00:08, cx0cx2001 <cx0cx2001@aliyun.com> wrote:
>> The lua version used is lua5.1.4
>>
>>
>> 2016-06-05
>> ________________________________
>> cx0cx2001
>> ________________________________
>> 发件人: cx0cx2001
>> 发送时间: 2016-06-05  22:04:41
>> 收件人: lua-l
>> 抄送:
>> 主题: cannot receive LUA_HOOKTAILRET event of tail call afterdebug.sethook
>> <code>
>> function hook(event)
>>     print(event);
>> end
>>
>> function test1()
>> end
>>
>> function test()
>>     return test1();
>> end
>>
>> debug.sethook(hook, "cr");
>> test();
>> </code>
>>
>> The output of the above code is following:
>> call
>> call
>> return
>>
>> The count of return hook is not same as the count of call hook.
>> I found that the return or tailretun event is missing when there is tail
>> call.
>> Because of this bug, it's impossible to collect the function's execute
>> time(between call hook and return hook) of lua code.
>>
>> 2016-06-05
>> ________________________________
>> cx0cx2001
>
>
> Please do not send HTML formatted email; and please bottom post.
>
> In Lua 5.1.5 I get:
> return
> call
> call
> return
> tail return
> return
>
> In Lua 5.3.2 I get:
> return
> call
> tail call
> return
> return
>
> The first `return` in both outputs is debug.sethook itself returning,
> the final `return` in both outputs is the test itself finishing. These
> can be ignored for the sake of your example.
>
> In 5.1 if you count +1 for each "call", and -1 for each "return" or
> "tail return" it is balanced:
> 0
> call --> 1
> call --> 2
> return --> 1
> tail return --> 0
>

Hello,

I increased depth of tail calls:

local function identity(n, result)
    result = result or 0
    if n == 0 then
        return result
    else
        return identity(n - 1, result + 1)
    end
end
debug.sethook(print, "cr")
print(identity(1000))

Results:

$ lua5.1 identity.lua > /log
$ grep -c -w call log
1003
$ grep -c -w return log
1006

$ lua5.2 identity.lua > /log
$ grep -c -w call log
1003
$ grep -c -w return log
6

$ lua5.3 identity.lua > /log
$ grep -c -w call log
1003
$ grep -c -w return log
6

$ luajit identity.lua > /log
$ grep -c -w call log
1002
$ grep -c -w return log
2

Only Lua 5.1 produces balanced results. By the way, how it gets this
information for tail calls without using additional stack space? Does
it store only an integer (depth)?

-- 


Best regards,
Boris Nagaev