Skip to content

Custom Iterators#316

Merged
Perryvw merged 3 commits intoTypeScriptToLua:masterfrom
tomblind:lua_iter_pr
Jan 3, 2019
Merged

Custom Iterators#316
Perryvw merged 3 commits intoTypeScriptToLua:masterfrom
tomblind:lua_iter_pr

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

This adds support for using ES6 Iterables in for...of loops. It also adds a custom decorator 'LuaIterator' to indicate a declared function is actually a proper lua iterator and should be used directly in the transpiled for...in loop.

This feature required a lot of additions and changes:

  • New lib function '__TS__Iterator' which is used in place of pairs to iterate Iterables
  • New lib 'Symbol' for well-known symbol support (currently only has Symbol.iterator' but more can be added in the future)
  • Updated Map and Set libs to use custom iterators
  • Updated lib import logic to ensure that dependencies of libs are included before the lib itself
  • Forced Symbol lib to be included first in bundle, as Map and Set depend on it
  • Support for methods declared with computed names
  • Fixed a bug in transpileElementCall that wasn't caught by tests (and updated tests)

Something to note about LuaIterator is that it can be combined TupleReturn:

/** @luaIterator */
/** @tupleReturn */
declare function luaIter(): Iterable<[string, string]>;
for (let [a, b] of luaIter()) {}
for a, b in luaIter() do
end

But if a function is declared this way, the variable(s) in the for...of loop MUST be destructured or an error will be thrown:

for (let a of luaIter()) {} // Error - 'a' is not destructured

This is because there is no straight forward way to represent this in lua. A solution could be to wrap the iterator call in another custom lib function that bundles the results, but this is pretty tricky and I suspect most of the time this error will be hit by accident when the intention was to use the destructured version.

return `[${this.transpileExpression(identifier.expression)}]`;
}

if (identifier.text === "Symbol") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also trigger on myClass.Symbol = 3, which I don't think is something we want.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it doesn't break anything - it just pulls in the lib when it isn't needed. But I'll see if we can prevent that somehow.

- luaLibFeatureSet returned to being a Set
- added lua lib dependency table
- moved lua lib stuff to its own file
- fixed logic for importing Symbol lib so it isn't accidentally imported when it shouldn't be
- reworked property name transpiling to properly handle numeric/string literals and computed names in all places
- updated Map and Set to take a generic Iterable in their constructors
@Perryvw Perryvw merged commit c880ef6 into TypeScriptToLua:master Jan 3, 2019
@tomblind tomblind deleted the lua_iter_pr branch June 9, 2019 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants