Skip to content

Console function calls#454

Merged
Perryvw merged 5 commits intoTypeScriptToLua:masterfrom
hazzard993:console-function-calls
Feb 27, 2019
Merged

Console function calls#454
Perryvw merged 5 commits intoTypeScriptToLua:masterfrom
hazzard993:console-function-calls

Conversation

@hazzard993
Copy link
Copy Markdown
Contributor

Continued from #443 as a partial solution for #439.

console.log("Hello");               // print("Hello")
console.log("Hello %s", "there");   // prints "Hello there" in Lua
console.log("Hello", "There");      // print("Hello", "There")
console.assert(true);               // assert(true)
console.assert(true, "message");    // assert(true, "message")
console.assert(false, "Hello %s", "there"); // Fails with "Hello there" as a message
console.trace();                    // Prints a stack trace in Lua
console.trace("message");           // Prints a stack trace with "message" at the top in Lua
console.trace("Hello %s", "there"); // Prints a stack trace with "Hello there" at the top in Lua

For the case below, the code doesn't work.

console.log("foo%%bar", "blah"); // Prints foo%bar instead of foo%%bar    blah

I looked into implementing these functions into LuaLibs since what happens is figured out at runtime, however something like:

function __TSTL__ConsoleLog(...args) {
    print(...args);
}

Transpiles to lua with unpack. Which means the solution won't work for all lua targets.

This is only a partial implementation of console.log, console.assert and console.trace since the full solution would likely be implemented in a LuaLib.

Submitting this PR just to show what I have and see if it is acceptable for now.

Copy link
Copy Markdown
Member

@Perryvw Perryvw left a comment

Choose a reason for hiding this comment

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

This looks good! I have some superficial comments regarding formatting and readability, but overall I think this is a good first iteration. There are some areas that could be improved, like the lualib stuff you mentioned (lualib would also allow us to deep-print tables which might be nice), but I would be happy merging the current functionality as is and then iterating on it in a next PR.

);
case "trace":
if (expression.arguments.length > 0) {
if (this.isStringFormatTemplate(expression.arguments[0])) {
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.

Might as well merge these two if statements into one with a && (JS short-circuits if the left hand side fails, so it functions exactly the same)

tstl.createTableIndexExpression(
tstl.createIdentifier("debug"),
tstl.createStringLiteral("traceback")),
[tstl.createCallExpression(
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 formatting is a little confusing, maybe adding some intermediate variables would help.

@Perryvw Perryvw merged commit daa904f into TypeScriptToLua:master Feb 27, 2019
@hazzard993 hazzard993 deleted the console-function-calls branch July 21, 2019 11:14
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