-
-
Notifications
You must be signed in to change notification settings - Fork 184
Generator and iteration improvements #872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Perryvw
merged 6 commits into
TypeScriptToLua:master
from
ark120202:generator-and-iteration-improvements
May 23, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
269a43d
Generator and iteration improvements
ark120202 96546c2
Pass input vararg count to unpack
ark120202 3d9ccde
Fix .next() parameter not being passed
ark120202 28fa434
Merge remote-tracking branch 'upstream/master' into generator-and-ite…
ark120202 4b152c1
Methods are missing `FunctionExpressionFlags.Declaration`
ark120202 55e92ca
Add changelog
ark120202 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| interface GeneratorIterator { | ||
| ____coroutine: LuaThread; | ||
| [Symbol.iterator](): GeneratorIterator; | ||
| next: typeof __TS__GeneratorNext; | ||
| } | ||
|
|
||
| function __TS__GeneratorIterator(this: GeneratorIterator) { | ||
| return this; | ||
| } | ||
|
|
||
| function __TS__GeneratorNext(this: GeneratorIterator, ...args: Vararg<any[]>) { | ||
| const co = this.____coroutine; | ||
| if (coroutine.status(co) === "dead") return { done: true }; | ||
|
|
||
| const [status, value] = coroutine.resume(co, ...args); | ||
| if (!status) throw value; | ||
|
|
||
| return { value, done: coroutine.status(co) === "dead" }; | ||
| } | ||
|
|
||
| function __TS__Generator(this: void, fn: (this: void, ...args: any[]) => any) { | ||
| return function(this: void, ...args: Vararg<any[]>): GeneratorIterator { | ||
| const argsLength = select("#", ...args); | ||
| return { | ||
| // Using explicit this there, since we don't pass arguments after the first nil and context is likely to be nil | ||
| ____coroutine: coroutine.create(() => fn((unpack || table.unpack)(args, 1, argsLength))), | ||
| [Symbol.iterator]: __TS__GeneratorIterator, | ||
| next: __TS__GeneratorNext, | ||
| }; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,32 @@ | ||
| function __TS__Iterator<T>(this: void, iterable: Iterable<T>): (this: void) => T { | ||
| if (iterable[Symbol.iterator]) { | ||
| /** @tupleReturn */ | ||
| function __TS__IteratorGeneratorStep(this: GeneratorIterator): [true, any] | [] { | ||
| const co = this.____coroutine; | ||
|
|
||
| const [status, value] = coroutine.resume(co); | ||
| if (!status) throw value; | ||
|
|
||
| if (coroutine.status(co) === "dead") return []; | ||
| return [true, value]; | ||
| } | ||
|
|
||
| /** @tupleReturn */ | ||
| function __TS__IteratorIteratorStep<T>(this: Iterator<T>): [true, T] | [] { | ||
| const result = this.next(); | ||
| if (result.done) return []; | ||
| return [true, result.value]; | ||
| } | ||
|
|
||
| /** @tupleReturn */ | ||
| function __TS__Iterator<T>( | ||
| this: void, | ||
| iterable: Iterable<T> | GeneratorIterator | readonly T[] | ||
| ): [(...args: any[]) => [any, T] | [], ...any[]] { | ||
| if ("____coroutine" in iterable) { | ||
| return [__TS__IteratorGeneratorStep, iterable]; | ||
| } else if (iterable[Symbol.iterator]) { | ||
| const iterator = iterable[Symbol.iterator](); | ||
| return () => { | ||
| const result = iterator.next(); | ||
| if (!result.done) { | ||
| return result.value; | ||
| } else { | ||
| return undefined; | ||
| } | ||
| }; | ||
| return [__TS__IteratorIteratorStep, iterator]; | ||
| } else { | ||
| let i = 0; | ||
| return () => { | ||
| i += 1; | ||
| return iterable[i]; | ||
| }; | ||
| return ipairs(iterable as readonly T[]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** @noSelfInFile */ | ||
|
|
||
| interface LuaThread { | ||
| readonly __internal__: unique symbol; | ||
| } | ||
|
|
||
| declare namespace coroutine { | ||
| function create(f: (...args: any[]) => any): LuaThread; | ||
|
|
||
| /** @tupleReturn */ | ||
| function resume(co: LuaThread, ...val: any[]): [true, ...any[]] | [false, string]; | ||
|
|
||
| function status(co: LuaThread): "running" | "suspended" | "normal" | "dead"; | ||
|
|
||
| function wrap(f: (...args: any[]) => any): /** @tupleReturn */ (...args: any[]) => any[]; | ||
|
|
||
| /** @tupleReturn */ | ||
| function yield(...args: any[]): any[]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need these lualib declarations now we included lua-types as dependency?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lua-types is used only in benchmark project, not in lualib. It should be possible to use it in the future, but it probably would need some code changes, since some things are declared differently