Permalink
Browse files

Add support for delete x.y

  • Loading branch information...
1 parent a9bbd26 commit eb9c6a153ce2561e4958ba12fe734f9b7ec8ea3b @wycats wycats committed Sep 3, 2011
Showing with 43 additions and 1 deletion.
  1. +11 −0 lib/sparta/compilers/compiler.rb
  2. +12 −1 lib/sparta/runtime/object.rb
  3. +11 −0 spec/object_spec.rb
  4. +9 −0 spec/spec_helper.rb
@@ -183,6 +183,17 @@ def visit_ResolveNode(o)
s.push_variable o.value
end
+ def visit_DeleteNode(o)
+ set_line(o)
+ if o.value.is_a?(RKelly::Nodes::ResolveNode)
+ # TODO: deal with delete x
+ elsif o.value.is_a?(RKelly::Nodes::DotAccessorNode)
+ o.value.value.accept(self)
+ g.push_literal o.value.accessor.to_sym
+ g.send :delete_property, 1
+ end
+ end
+
def visit_DotAccessorNode(o)
super
set_line(o)
@@ -37,6 +37,11 @@ def self.with_constructor(constructor)
object
end
+ dynamic_method(:undefined) do |g|
+ g.push_undef
+ g.ret
+ end
+
def function(name, block=name)
block = method(block) if block.is_a?(Symbol)
@@ -56,6 +61,8 @@ def get(name)
value
elsif proto = prototype
proto.get(name)
+ else
+ undefined
end
end
@@ -122,7 +129,11 @@ def initialize(array)
end
def get_index(index)
- @array[index]
+ if index >= @array.size
+ undefined
+ else
+ @array[index]
+ end
end
def to_a
View
@@ -27,11 +27,22 @@
e("x = function() {}; x.prototype = { a: 1 }; 'b' in new x").should == false
end
end
+
+ describe "hasOwnProperty" do
+
+ end
+
+ describe "delete key" do
+ it "should delete the key if it exists on the object" do
+ e("x = { a: 1 }; delete x.a; x.a").should == undefined
+ end
+ end
end
describe "Array Literals" do
it "creates a simple Array" do
e("x = [1,2,3]").to_a.should == [1,2,3]
e("x = [1,2,3]; x[1]").should == 2
+ e("x = [1,2,3]; x[5]").should == undefined
end
end
View
@@ -2,7 +2,16 @@
require "sparta"
require "pp"
+module RSpec::JSHelpers
+ dynamic_method :undefined do |g|
+ g.push_undef
+ g.ret
+ end
+end
+
RSpec.configure do
+ include RSpec::JSHelpers
+
def e(string)
Sparta::Environment.new.evaluate(string)
end

0 comments on commit eb9c6a1

Please sign in to comment.