Skip to content

Commit

Permalink
reduce unnecessary var this$ = this
Browse files Browse the repository at this point in the history
With 9f96f95, all `(.0)` and `(+ 1)`-like functions became bound
functions, introducing extra `var this$ = this` declarations despite not
being needed in many cases. This commit walks that side effect back by
optimizing all bound functions to only introduce a `this$` variable if
`this` is used somewhere inside the function.
  • Loading branch information
rhendric committed Jun 27, 2018
1 parent f08cc0f commit dcbbcb5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
16 changes: 13 additions & 3 deletions lib/ast.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/command.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/ast.ls
Expand Up @@ -2005,7 +2005,8 @@ class exports.Fun extends Node
body.lines.push Return Literal \this$
else if sscope.fun?bound
then @bound = that
else sscope.assign \this$ \this
else if @uses-this!
then sscope.assign \this$ \this
if @statement
name or @carp 'nameless function declaration'
pscope is o.block.scope or @carp 'misplaced function declaration'
Expand Down Expand Up @@ -2090,6 +2091,11 @@ class exports.Fun extends Node
names.pop!
sn(null, ...names)

uses-this: ->
Node::traverse-children.call this, ->
| it instanceof Literal and it.value is \this => true
| it instanceof Fun and it.bound and it.uses-this! => true

#### Class
class exports.Class extends Node
({@title, @sup, @mixins, body}) -> @fun = Fun [] body
Expand Down
4 changes: 4 additions & 0 deletions test/operator.ls
Expand Up @@ -825,6 +825,10 @@ o =
eq 3, @y
o.f!

# but don't overdo it with the `this` binding
ok (LiveScript.compile '(.0)' {+bare,-header} .index-of \this) < 0
ok (LiveScript.compile '(+ 1)' {+bare,-header} .index-of \this) < 0

# Unary ops as functions
ok (not) false
ok (!).call(null, false)
Expand Down

0 comments on commit dcbbcb5

Please sign in to comment.