Skip to content

Commit

Permalink
Merge pull request #841 from rhendric/for-literal-error
Browse files Browse the repository at this point in the history
add robustness to generated loop code
  • Loading branch information
gkz committed Feb 8, 2016
2 parents 94de1f0 + 4a20718 commit fbc1502
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/ast.js

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

8 changes: 5 additions & 3 deletions src/ast.ls
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ SourceNode::to-string = (...args) ->
if once then [sub, ref <<< {+temp}] else [sub, ref, [ref.value]]

# Compiles to a variable/source pair suitable for looping.
compile-loop-reference: (o, name, ret) ->
compile-loop-reference: (o, name, ret, safe-access) ->
if this instanceof Var and o.scope.check @value
or this instanceof Unary and @op in <[ + - ]> and -1/0 < +@it.value < 1/0
or this instanceof Literal and not @is-complex!
return [@compile o] * 2
code = @compile o, LEVEL_PAREN
code = "(#code)" if safe-access and this not instanceof Var
return [code] * 2
asn = Assign Var(tmp = o.scope.temporary name), this
ret or asn.void = true
[tmp; asn.compile o, if ret then LEVEL_CALL else LEVEL_PAREN]
Expand Down Expand Up @@ -2352,7 +2354,7 @@ class exports.For extends While
else
@item = Var o.scope.temporary \x if @ref
if @item or @object and @own or @let
[svar, srcPart] = @source.compile-loop-reference o, \ref, not @object
[svar, srcPart] = @source.compile-loop-reference o, \ref, not @object, true
svar is srcPart or temps.push svar
else
svar = srcPart = @source.compile o, LEVEL_PAREN
Expand Down
10 changes: 10 additions & 0 deletions test/loop.ls
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,13 @@ eq 1, i
o = { [k, -> v] for let k, v of {a: 1, b: 2} }
eq 1 o.a!
eq 2 o.b!

# Certain literals could result in illegal JavaScript if not carefully
# handled. These are all nonsensical use cases and could just as easily
# be LiveScript syntax errors. The thing to avoid is for them to be JavaScript
# syntax errors; lsc should never produce illegal JavaScript on any input,
# silly or otherwise.
deep-equal [] [0 for x in 42]
deep-equal [] [0 for x in -42]
throws "Cannot read property 'length' of null" -> [0 for x in null]
throws "Cannot read property 'length' of undefined" -> [0 for x in void]

0 comments on commit fbc1502

Please sign in to comment.