X Tutup
Skip to content

passing nil instead of _G as context for global functions when in ES …#271

Merged
Perryvw merged 3 commits intoTypeScriptToLua:new-functionsfrom
tomblind:new-functions
Oct 25, 2018
Merged

passing nil instead of _G as context for global functions when in ES …#271
Perryvw merged 3 commits intoTypeScriptToLua:new-functionsfrom
tomblind:new-functions

Conversation

@tomblind
Copy link
Copy Markdown
Collaborator

As discussed here: #250 (comment)

this.sourceFile = sourceFile;
this.isModule = tsHelper.isFileModule(sourceFile);
this.isStrict = options.alwaysStrict || options.strict
|| (options.target && options.target > ts.ScriptTarget.ES5);
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.

Is ES5+ always strict? Or (if it's default true) did you forget to check if it is not explicitly set to false.

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.

Ok, I just looked this up in the ecma specs. In ES5, strict mode has to be explicitly enabled (with "use strict" which is what alwaysStrict produces). In ES6 (aka ES2015) and beyond, modules are always strict (I don't believe it can be disabled), but non-modules still require the explicit enabling. I'll update to match that logic.

&& !ts.isElementAccessExpression(node.expression)
&& !tsHelper.getCustomDecorators(type, this.checker).has(DecoratorKind.NoContext)) {
params = this.transpileArguments(node.arguments, ts.createIdentifier("_G"));
params = this.transpileArguments(node.arguments, ts.createIdentifier(this.isStrict ? "nil" : "_G"));
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.

Maybe instead of creating an identifier you could just create a null or undefined keyword? I don't know how big that change would be

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.

The context paramater of transpileArguments is optional and skips adding the initial parameter if omitted (or undefined). I could re-purpose null to explicitly convert to nil but having separate logic for null and undefined always feels icky to me.

I could change transpileArguments to take either an expression or a string as the context parameter, and just pass the "nil" instead of a temp identifier.

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.

I don't mean actual null, I meant something like ts.CreateNullKeyword() or something similar.

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.

actually nvm that won't work here since it is an abstract argument and not a concrete instantiation of one.

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.

Ah - sorry - misunderstood. Yes, that seems like a good idea.

@Perryvw Perryvw merged commit bc64913 into TypeScriptToLua:new-functions Oct 25, 2018
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

X Tutup