Skip to content

Generator functions - #384

Merged
tomblind merged 16 commits into
TypeScriptToLua:masterfrom
andreiradu:yeld_statement
Feb 10, 2019
Merged

Generator functions#384
tomblind merged 16 commits into
TypeScriptToLua:masterfrom
andreiradu:yeld_statement

Conversation

@andreiradu

@andreiradu andreiradu commented Feb 8, 2019

Copy link
Copy Markdown
Contributor

One particular issue is that calling iterator.next(42) transpiles to iterator:next(42), which in turn means that the corresponding coroutine.yield() will return iterator, 42. I've made it so let a = yield() transpiles to local _, a = coroutine.yield() to account for that.

function* seq() {
    let a = yield 1;
    return a;
}
const gen = seq();

will transpile to:

seq = function()
    local __co = coroutine.create(function()
        local a = coroutine.yield(1);
        return a;
    end);
    __it = {next = function(___, ...)
        local __err, __value = coroutine.resume(__co, ...);
        if not __err then
            error(__value);
        end
        return {done = coroutine.status(__co) == "dead", value = __value};
    end};
    __it[Symbol.iterator] = function()
        return __it;
    end;
    return __it;
end;
local gen = seq();

Comment thread src/LuaTransformer.ts Outdated
tstl.createTableIndexExpression(itIdentifier, symbolIterator),
tstl.createFunctionExpression(tstl.createBlock([tstl.createReturnStatement([itIdentifier])]))),
tstl.createReturnStatement([itIdentifier])]
);

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.

Could you refactor this entire if block to transformGeneratorFunctionDeclaration()? This is quite hard to read, some intermediate variables wouldn't hurt. It would also be nice if you could try to stick a little closer to how the rest of the code and long expressions are formatted. (see this.transformFunctionBody at line 1078 for example)

@tomblind tomblind left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Overall, a great feature PR - mostly just needs some changes for code consistency. Also, I think it would be good to have a test using a for...of loop, since that's the most likely use-case.

Comment thread src/LuaTransformer.ts Outdated
Comment thread src/LuaAST.ts Outdated
Comment thread src/LuaTransformer.ts Outdated
Comment thread src/LuaTransformer.ts
Comment thread src/TSHelper.ts Outdated
Comment thread src/TSHelper.ts Outdated
Comment thread test/unit/functions.spec.ts Outdated
Comment thread src/LuaTransformer.ts
Comment thread src/LuaTransformer.ts
tstl.createTableIndexExpression(tstl.createIdentifier("coroutine"), tstl.createStringLiteral("yield")),
expression.expression?[this.transformExpression(expression.expression)]:[], expression);
}
public transformReturn(statement: ts.ReturnStatement): tstl.Statement {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above

@tomblind
tomblind merged commit ed87cb6 into TypeScriptToLua:master Feb 10, 2019
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.

3 participants