Skip to content

Conversation

@TwitchBronBron
Copy link
Member

@TwitchBronBron TwitchBronBron commented Jan 9, 2024

Adds async/await support to brighterscript.

Leverages the @rokucommunity/promises library to support syncronous-looking flows into promise-drive async flows. For example:

async function getAuthToken()
    try
        username = await getUsernameAsync()
        password = await promptForPassword(username)
        authToken = await getAuthToken(username, password)
        return authToken
    catch e
        print e
    end try
end function

Would transpile into something like:

function getAuthToken()
    scope = {}
    return promises_chain(getUsernameAsync()).then(function(username)
        scope.username = username
        return promptForPassword(scope.username)
    end function).then(function(password)
        scope.password = password
        return getAuthToken(scope.username, scope.password)
    end function).catch(function(e)
        print e
    end function).toPromise()

There are many complex use cases that we won't be covering in this first iteration (like if statements, loops, etc). For now, we will handle the simple case of top-level function statements and try-catch.

Tasks:

  • 1. Add lexer and parser support for async and await. Both should be fully allowed as local variables, function names, etc. But when used in the context of an async function, or an await statement, it will result in new AST.

    • add a new token on FunctionStatement called asyncToken. This is how we will determine if the function is async or not.
    • Create a new AwaitedExpression that will wrap the value to the right.
    • for now, in validation we should flag any AwaitedExpression that are not at the root of a function body. In the future we will add more support for nested expressions, but that gets complicated quick...
  • 2. Integrate the @rokucommunity/promises library into brighterscript in a similar fashion to bslib. The promises library should only be included IF async/await is used. Also, if the project already has an instance of @rokucommunity/promises, then bsc shouldn't include another copy and should instead leverage that one.

  • 3. Write the transpiler logic for async/await.

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