Skip to content

Commit

Permalink
using Object.prototype.hasOwnProperty.call instead of obj.hasOwnPrope…
Browse files Browse the repository at this point in the history
…rty, with an alias, for Rhino and java objects
  • Loading branch information
jashkenas committed Jan 12, 2010
1 parent c19647a commit 8dfbd1a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/coffee_script/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ def compile_root(o={})
# at the top.
def compile_with_declarations(o={})
code = compile_node(o)
return code unless o[:scope].declarations?(self)
write("#{idt}var #{o[:scope].declared_variables.join(', ')};\n#{code}")
code = "#{idt}var #{o[:scope].compiled_assignments};\n#{code}" if o[:scope].assignments?(self)
code = "#{idt}var #{o[:scope].compiled_declarations};\n#{code}" if o[:scope].declarations?(self)
write(code)
end

# Compiles a single expression within the expression list.
Expand Down Expand Up @@ -724,8 +725,9 @@ def compile_node(o)
body = Expressions.wrap(IfNode.new(@filter, body))
end
if @object
o[:scope].top_level_assign("__hasProp", "Object.prototype.hasOwnProperty")
body = Expressions.wrap(IfNode.new(
CallNode.new(ValueNode.new(LiteralNode.wrap(svar), [AccessorNode.new(Value.new('hasOwnProperty'))]), [LiteralNode.wrap(ivar)]),
CallNode.new(ValueNode.new(LiteralNode.wrap("__hasProp"), [AccessorNode.new(Value.new('call'))]), [LiteralNode.wrap(svar), LiteralNode.wrap(ivar)]),
Expressions.wrap(body),
nil,
{:statement => true}
Expand Down
25 changes: 25 additions & 0 deletions lib/coffee_script/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,40 @@ def free_variable
@temp_variable.dup
end

# Ensure that an assignment is made at the top-level scope.
# Takes two strings.
def top_level_assign(name, value)
return @parent.top_level_assign(name, value) if @parent
@variables[name.to_sym] = Value.new(value)
end

def declarations?(body)
!declared_variables.empty? && body == @expressions
end

def assignments?(body)
!assigned_variables.empty? && body == @expressions
end

# Return the list of variables first declared in current scope.
def declared_variables
@variables.select {|k, v| v == :var }.map {|pair| pair[0].to_s }.sort
end

# Return the list of variables that are supposed to be assigned at the top
# of scope.
def assigned_variables
@variables.select {|k, v| v.is_a?(Value) }.sort_by {|pair| pair[0].to_s }
end

def compiled_declarations
declared_variables.join(', ')
end

def compiled_assignments
assigned_variables.map {|name, val| "#{name} = #{val}"}.join(', ')
end

def inspect
"<Scope:#{__id__} #{@variables.inspect}>"
end
Expand Down

0 comments on commit 8dfbd1a

Please sign in to comment.