Skip to content

Commit

Permalink
caught a close call bug with object comprehensions and not specifying…
Browse files Browse the repository at this point in the history
… the value
  • Loading branch information
jashkenas committed Jan 10, 2010
1 parent bb5bf7f commit 24408c7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion documentation/coffee/expressions_comprehension.coffee
@@ -1,3 +1,3 @@
# The first ten global properties. # The first ten global properties.


globals: (name for property, name in window)[0...10] globals: (name for name ino window)[0...10]
9 changes: 5 additions & 4 deletions documentation/js/expressions_comprehension.js
@@ -1,11 +1,12 @@
(function(){ (function(){
var __a, __b, globals, name, property; var __a, __b, globals, name;
// The first ten global properties. // The first ten global properties.
globals = ((function() { globals = ((function() {
__b = []; __a = window; __b = []; __a = window;
for (name=0; name<__a.length; name++) { for (name in __a) {
property = __a[name]; if (__a.hasOwnProperty(name)) {
__b.push(name); __b.push(name);
}
} }
return __b; return __b;
})()).slice(0, 10); })()).slice(0, 10);
Expand Down
20 changes: 11 additions & 9 deletions index.html
Expand Up @@ -927,24 +927,26 @@ <h2>Language Reference</h2>
</p> </p>
<div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> The first ten global properties.</span> <div class='code'><pre class="idle"><span class="Comment"><span class="Comment">#</span> The first ten global properties.</span>


<span class="FunctionName">globals</span><span class="Keyword">:</span> (name <span class="Keyword">for</span> property, name <span class="Keyword">in</span> window)[<span class="Number">0</span>...<span class="Number">10</span>] <span class="FunctionName">globals</span><span class="Keyword">:</span> (name <span class="Keyword">for</span> name <span class="Keyword">ino</span> window)[<span class="Number">0</span>...<span class="Number">10</span>]
</pre><pre class="idle"><span class="Storage">var</span> __a, __b, globals, name, property; </pre><pre class="idle"><span class="Storage">var</span> __a, __b, globals, name;
<span class="Comment"><span class="Comment">//</span> The first ten global properties.</span> <span class="Comment"><span class="Comment">//</span> The first ten global properties.</span>
globals <span class="Keyword">=</span> ((<span class="Storage">function</span>() { globals <span class="Keyword">=</span> ((<span class="Storage">function</span>() {
__b <span class="Keyword">=</span> []; __a <span class="Keyword">=</span> <span class="LibraryClassType">window</span>; __b <span class="Keyword">=</span> []; __a <span class="Keyword">=</span> <span class="LibraryClassType">window</span>;
<span class="Keyword">for</span> (name<span class="Keyword">=</span><span class="Number">0</span>; name<span class="Keyword">&lt;</span>__a.<span class="LibraryConstant">length</span>; name<span class="Keyword">++</span>) { <span class="Keyword">for</span> (name <span class="Keyword">in</span> __a) {
property <span class="Keyword">=</span> __a[name]; <span class="Keyword">if</span> (__a.hasOwnProperty(name)) {
__b.<span class="LibraryFunction">push</span>(name); __b.<span class="LibraryFunction">push</span>(name);
}
} }
<span class="Keyword">return</span> __b; <span class="Keyword">return</span> __b;
})()).<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, <span class="Number">10</span>); })()).<span class="LibraryFunction">slice</span>(<span class="Number">0</span>, <span class="Number">10</span>);
</pre><button onclick='javascript: var __a, __b, globals, name, property; </pre><button onclick='javascript: var __a, __b, globals, name;
// The first ten global properties. // The first ten global properties.
globals = ((function() { globals = ((function() {
__b = []; __a = window; __b = []; __a = window;
for (name=0; name<__a.length; name++) { for (name in __a) {
property = __a[name]; if (__a.hasOwnProperty(name)) {
__b.push(name); __b.push(name);
}
} }
return __b; return __b;
})()).slice(0, 10); })()).slice(0, 10);
Expand Down
8 changes: 4 additions & 4 deletions lib/coffee_script/nodes.rb
Expand Up @@ -643,7 +643,7 @@ def compile_node(o)
range = @source.is_a?(ValueNode) && @source.literal.is_a?(RangeNode) && @source.properties.empty? range = @source.is_a?(ValueNode) && @source.literal.is_a?(RangeNode) && @source.properties.empty?
source = range ? @source.literal : @source source = range ? @source.literal : @source
scope = o[:scope] scope = o[:scope]
scope.find(@name) name_found = @name && scope.find(@name)
index_found = @index && scope.find(@index) index_found = @index && scope.find(@index)
body_dent = idt(1) body_dent = idt(1)
svar = scope.free_variable svar = scope.free_variable
Expand All @@ -658,8 +658,8 @@ def compile_node(o)
index_var = nil index_var = nil
source_part = "#{svar} = #{source.compile(o)};\n#{idt}" source_part = "#{svar} = #{source.compile(o)};\n#{idt}"
for_part = "#{ivar}=0; #{ivar}<#{svar}.length; #{ivar}++" for_part = "#{ivar}=0; #{ivar}<#{svar}.length; #{ivar}++"
for_part = "#{@index} in #{svar}" if @object for_part = "#{ivar} in #{svar}" if @object
var_part = "#{body_dent}#{@name} = #{svar}[#{ivar}];\n" var_part = @name ? "#{body_dent}#{@name} = #{svar}[#{ivar}];\n" : ''
end end
body = @body body = @body
set_result = rvar ? "#{idt}#{rvar} = []; " : idt set_result = rvar ? "#{idt}#{rvar} = []; " : idt
Expand All @@ -680,7 +680,7 @@ def compile_node(o)
end end
if @object if @object
body = Expressions.wrap(IfNode.new( body = Expressions.wrap(IfNode.new(
CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(@index)]), CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(ivar)]),
Expressions.wrap(body), Expressions.wrap(body),
nil, nil,
{:statement => true} {:statement => true}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/execution/test_array_comprehension.coffee
Expand Up @@ -5,7 +5,7 @@ print(results.join(',') is '2,18')




obj: {one: 1, two: 2, three: 3} obj: {one: 1, two: 2, three: 3}
names: key + '!' for key, value ino obj names: key + '!' for key ino obj
odds: key + '!' for key, value ino obj when value % 2 isnt 0 odds: key + '!' for key, value ino obj when value % 2 isnt 0


print(names.join(' ') is "one! two! three!") print(names.join(' ') is "one! two! three!")
Expand Down

0 comments on commit 24408c7

Please sign in to comment.