Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: afce13596ab0
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ee53ea8d388e
Choose a head ref
  • 2 commits
  • 11 files changed
  • 2 contributors

Commits on Sep 10, 2015

  1. Copy the full SHA
    78016aa View commit details
  2. Merge pull request #1099 from iliabylich/copy-singleton-method-on-obj…

    …ect-cloning
    
    Copy singleton methods on object cloning.
    meh committed Sep 10, 2015
    Copy the full SHA
    ee53ea8 View commit details
3 changes: 1 addition & 2 deletions opal/corelib/array.rb
Original file line number Diff line number Diff line change
@@ -555,6 +555,7 @@ def clear

def clone
copy = []
copy.copy_singleton_methods(self)
copy.initialize_clone(self)
copy
end
@@ -776,8 +777,6 @@ def drop(number)
}
end

alias dup clone

def each(&block)
return enum_for(:each){self.size} unless block_given?

8 changes: 8 additions & 0 deletions opal/corelib/boolean.rb
Original file line number Diff line number Diff line change
@@ -35,6 +35,14 @@ def ==(other)
def to_s
`(self == true) ? 'true' : 'false'`
end

def dup
raise TypeError, "can't dup #{self.class}"
end

def clone
raise TypeError, "can't clone #{self.class}"
end
end

TrueClass = Boolean
24 changes: 24 additions & 0 deletions opal/corelib/kernel.rb
Original file line number Diff line number Diff line change
@@ -108,10 +108,34 @@ def copy_instance_variables(other)
}
end

def copy_singleton_methods(other)
%x{
var name;
if (other.hasOwnProperty('$$meta')) {
var other_singleton_class_proto = Opal.get_singleton_class(other).$$proto;
var self_singleton_class_proto = Opal.get_singleton_class(self).$$proto;
for (name in other_singleton_class_proto) {
if (name.charAt(0) === '$' && other_singleton_class_proto.hasOwnProperty(name)) {
self_singleton_class_proto[name] = other_singleton_class_proto[name];
}
}
}
for (name in other) {
if (name.charAt(0) === '$' && name.charAt(1) !== '$' && other.hasOwnProperty(name)) {
self[name] = other[name];
}
}
}
end

def clone
copy = self.class.allocate

copy.copy_instance_variables(self)
copy.copy_singleton_methods(self)
copy.initialize_clone(self)

copy
6 changes: 5 additions & 1 deletion opal/corelib/nil.rb
Original file line number Diff line number Diff line change
@@ -20,7 +20,11 @@ def ==(other)
end

def dup
raise TypeError
raise TypeError, "can't dup #{self.class}"
end

def clone
raise TypeError, "can't clone #{self.class}"
end

def inspect
8 changes: 8 additions & 0 deletions opal/corelib/numeric.rb
Original file line number Diff line number Diff line change
@@ -178,4 +178,12 @@ def positive?
def negative?
self < 0
end

def dup
raise TypeError, "can't dup #{self.class}"
end

def clone
raise TypeError, "can't clone #{self.class}"
end
end
3 changes: 1 addition & 2 deletions opal/corelib/string.rb
Original file line number Diff line number Diff line change
@@ -313,6 +313,7 @@ def chr

def clone
copy = `self.slice()`
copy.copy_singleton_methods(self)
copy.initialize_clone(self)
copy
end
@@ -349,8 +350,6 @@ def delete(*sets)
}
end

alias dup clone

def downcase
`self.toLowerCase()`
end
1 change: 0 additions & 1 deletion spec/filters/bugs/array.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
opal_filter "Array" do
fails "Array#clone copies singleton methods"
fails "Array#combination when no block is given returned Enumerator size returns 0 when the number of combinations is < 0"
fails "Array#combination when no block is given returned Enumerator size returns the binomial coeficient between the array size the number of combinations"
fails "Array#flatten performs respond_to? and method_missing-aware checks when coercing elements to array"
16 changes: 0 additions & 16 deletions spec/filters/bugs/kernel.rb
Original file line number Diff line number Diff line change
@@ -14,29 +14,13 @@
fails "Kernel#block_given? is a private method"
fails "Kernel#class returns the class of the object"
fails "Kernel#clone copies modules included in the singleton class"
fails "Kernel#clone copies singleton methods"
fails "Kernel#clone preserves tainted state from the original"
fails "Kernel#clone preserves untrusted state from the original"
fails "Kernel#clone raises TypeError when called on nil"
fails "Kernel#clone raises a TypeError for FalseClass"
fails "Kernel#clone raises a TypeError for Fixnum"
fails "Kernel#clone raises a TypeError for NilClass"
fails "Kernel#clone raises a TypeError for Symbol"
fails "Kernel#clone raises a TypeError for TrueClass"
fails "Kernel#define_singleton_method defines a new singleton method for objects"
fails "Kernel#define_singleton_method raises a TypeError when the given method is no Method/Proc"
fails "Kernel#define_singleton_method when given an UnboundMethod adds the new method to the methods list"
fails "Kernel#define_singleton_method when given an UnboundMethod correctly calls the new method"
fails "Kernel#define_singleton_method when given an UnboundMethod defines any Child class method from any Parent's class methods"
fails "Kernel#define_singleton_method when given an UnboundMethod will raise when attempting to define an object's singleton method from another object's singleton method"
fails "Kernel#dup does not copy constants defined in the singleton class"
fails "Kernel#dup does not copy frozen state from the original"
fails "Kernel#dup preserves tainted state from the original"
fails "Kernel#dup preserves untrusted state from the original"
fails "Kernel#dup raises a TypeError for FalseClass"
fails "Kernel#dup raises a TypeError for Fixnum"
fails "Kernel#dup raises a TypeError for Symbol"
fails "Kernel#dup raises a TypeError for TrueClass"
fails "Kernel#eval allows a binding to be captured inside an eval"
fails "Kernel#eval allows creating a new class in a binding created by #eval"
fails "Kernel#eval allows creating a new class in a binding"
2 changes: 0 additions & 2 deletions spec/filters/bugs/string.rb
Original file line number Diff line number Diff line change
@@ -5,8 +5,6 @@
fails "String#bytes yields each byte to a block if one is given, returning self"
fails "String#byteslice with Range calls to_int on range arguments"
fails "String#clone copies modules included in the singleton class"
fails "String#clone copies singleton methods"
fails "String#clone does not modify the original string when changing cloned string"
fails "String#dump includes .force_encoding(name) if the encoding isn't ASCII compatible"
fails "String#dump returns a string with # not escaped when followed by any other character"
fails "String#dump returns a string with \" and \\ escaped with a backslash"
7 changes: 7 additions & 0 deletions spec/filters/unsupported/kernel.rb
Original file line number Diff line number Diff line change
@@ -19,4 +19,11 @@
fails "Kernel.Float raises a TypeError if #to_f returns an Integer"
fails "Kernel.Integer calls to_i on Rationals"
fails "Kernel.Integer returns a Fixnum or Bignum object"
fails "Kernel#clone preserves tainted state from the original"
fails "Kernel#clone preserves untrusted state from the original"
fails "Kernel#clone raises a TypeError for Symbol"
fails "Kernel#dup does not copy frozen state from the original"
fails "Kernel#dup preserves tainted state from the original"
fails "Kernel#dup preserves untrusted state from the original"
fails "Kernel#dup raises a TypeError for Symbol"
end
1 change: 1 addition & 0 deletions spec/filters/unsupported/string.rb
Original file line number Diff line number Diff line change
@@ -178,6 +178,7 @@
fails "String#chop! removes the final newline"
fails "String#chop! returns nil when called on an empty string"
fails "String#chop! returns self if modifications were made"
fails "String#clone does not modify the original string when changing cloned string" # string can't be modified
fails "String#concat concatenates the given argument to self and returns self"
fails "String#concat converts the given argument to a String using to_str"
fails "String#concat raises a RuntimeError when self is frozen"