Comments (2)
I tried to find a workaround for my iterators, but I noticed it does not only affect coroutines. I think it is a problem with tail calls in the iterator function, maybe the stack gets confused.
This example results in an endless loop:
local get_val = function(i)
if i == 1 then return i, "a" end
if i == 2 then return i, "b" end
return nil
end
local my_next = function(invariant, i)
i = (i or 0) + 1
print("next:", i)
return get_val(i)
end
for k, v in my_next do
print("for:", k, v)
end
Output:
next: 1
for: 1 1
for: 1 1
for: 1 1
... and so on
It looks like the for-loop tries to call the inner function get_val
and not the iterator my_next
. It does not happen if we inline the get_val
or eliminate the tail call by saving its results:
local get_val = function(i)
if i == 1 then return i, "a" end
if i == 2 then return i, "b" end
return nil
end
local my_next = function(invariant, i)
i = (i or 0) + 1
print("next:", i)
local a, b = get_val(i)
return a, b
end
for k, v in my_next do
print("for:", k, v)
end
Works correctly:
next: 1
for: 1 a
next: 2
for: 2 b
next: 3
from gopher-lua.
Thank you again, @xconstruct .
I've fixed the bug in dd0fd53 thanks to your detailed report π
from gopher-lua.
Related Issues (20)
- Function names do not support UTF-8/Unicode
- Virtual machine interfaces need to be advertised
- bug: pcall affecting function upvalues HOT 4
- xpcall with error in error handler returns (nil, nil)
- The parameter passed by xpcall is nil
- different behavior in read function HOT 1
- Expand fopen modes
- different behavior in io.popen function
- Incorrect behavior for string.gmatch
- stuck when xpcall HOT 3
- LNumber.String() use strconv instead of fmt.Sprint HOT 1
- Can't require lua-cjson
- tonumber doesn't handle E notation correctly HOT 1
- γBUGγpanic: runtime error: index out of range [1048577] with length 1048577 HOT 2
- os.execute not behaving correctly on windows
- Result of L.DoString with coercion is not correct
- Extra PC increment in JMP instruction
- Error from parsing from LString to LNumber
- Incorrect code generation for `LOADNIL`
- Memory usage is too high
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gopher-lua.