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: 3039f576f30c
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5b857670d6ad
Choose a head ref
  • 5 commits
  • 6 files changed
  • 1 contributor

Commits on Nov 13, 2013

  1. Move Opal helpers into their own file

    This avoids some issue with precedence.
    meh committed Nov 13, 2013
    Copy the full SHA
    4cec78c View commit details
  2. Copy the full SHA
    2e400ce View commit details
  3. Escape spaces in Regexp.escape

    meh committed Nov 13, 2013
    Copy the full SHA
    deb8b91 View commit details
  4. Copy the full SHA
    b510ca4 View commit details
  5. Copy the full SHA
    5b85767 View commit details
Showing with 124 additions and 120 deletions.
  1. +83 −0 opal/core/helpers.rb
  2. +2 −8 opal/core/regexp.rb
  3. +33 −24 opal/core/string.rb
  4. +1 −84 opal/opal.rb
  5. +2 −3 spec/filters/bugs/string.rb
  6. +3 −1 tasks/mspec.rake
83 changes: 83 additions & 0 deletions opal/core/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module Opal
def self.coerce_to(object, type, method)
return object if type === object

unless object.respond_to? method
raise TypeError, "no implicit conversion of #{object.class} into #{type}"
end

object.__send__ method
end

def self.coerce_to!(object, type, method)
coerced = coerce_to(object, type, method)

unless type === coerced
raise TypeError, "can't convert #{object.class} into #{type} (#{object.class}##{method} gives #{coerced.class}"
end

coerced
end

def self.try_convert(object, type, method)
return object if type === object

if object.respond_to? method
object.__send__ method
end
end

def self.compare(a, b)
compare = a <=> b

if `compare === nil`
raise ArgumentError, "comparison of #{a.class.name} with #{b.class.name} failed"
end

compare
end

def self.fits_fixnum!(value)
# since we have Fixnum#size as 32 bit, this is based on the int limits
if `value > 2147483648`
raise RangeError, "bignum too big to convert into `long'"
end
end

def self.fits_array!(value)
# this is the computed ARY_MAX_SIZE for 32 bit
if `value >= 536870910`
raise ArgumentError, "argument too big"
end
end

def self.truthy?(value)
if value
true
else
false
end
end

def self.falsy?(value)
if value
false
else
true
end
end

def self.destructure(args)
%x{
if (args.length == 1) {
return args[0];
}
else if (args._isArray) {
return args;
}
else {
return $slice.call(args);
}
}
end
end
10 changes: 2 additions & 8 deletions opal/core/regexp.rb
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ class Regexp
`def._isRegexp = true`

def self.escape(string)
`string.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\^\\$\\|]/g, '\\\\$&')`
`string.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\^\\$\\| ]/g, '\\\\$&')`
end

def self.union(*parts)
@@ -36,13 +36,7 @@ def =~(string)
return
end

if `string._isString == null`
unless string.respond_to? :to_str
raise TypeError, "no implicit conversion of #{string.class} into String"
end

string = string.to_str
end
string = Opal.coerce_to(string, String, :to_str).to_s

%x{
var re = self;
57 changes: 33 additions & 24 deletions opal/core/string.rb
Original file line number Diff line number Diff line change
@@ -323,44 +323,53 @@ def include?(other)
end

def index(what, offset = nil)
%x{
if (!(what._isString || what._isRegexp)) {
#{raise TypeError, "type mismatch: #{what.class} given"};
}
if String === what
what = what.to_s
elsif what.respond_to? :to_str
what = what.to_str.to_s
elsif not Regexp === what
raise TypeError, "type mismatch: #{what.class} given"
end

var result = -1;
result = -1

if offset
offset = Opal.coerce_to offset, Integer, :to_int

%x{
var size = self.length;
if (offset !== nil) {
if (offset < 0) {
offset = offset + self.length;
offset = offset + size;
}
if (offset > self.length) {
if (offset > size) {
return nil;
}
}

if (what._isRegexp) {
result = #{(what =~ `self.substr(offset)`) || -1}
}
else {
result = self.substr(offset).indexOf(what);
}
if Regexp === what
result = (what =~ `self.substr(offset)`) || -1
else
result = `self.substr(offset).indexOf(what)`
end

%x{
if (result !== -1) {
result += offset;
}
}
else {
if (what._isRegexp) {
result = #{(what =~ self) || -1}
}
else {
result = self.indexOf(what);
}
}
else
if Regexp === what
result = (what =~ self) || -1
else
result = `self.indexOf(what)`
end
end

return result === -1 ? nil : result;
}
unless `result === -1`
result
end
end

def inspect
85 changes: 1 addition & 84 deletions opal/opal.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'core/runtime'
require 'core/helpers'
require 'core/module'
require 'core/class'
require 'core/basic_object'
@@ -48,87 +49,3 @@
RUBY_VERSION = '2.0.0'
RUBY_ENGINE_VERSION = '0.5.2'
RUBY_RELEASE_DATE = '2013-11-11'

module Opal
def self.coerce_to(object, type, method)
return object if type === object

unless object.respond_to? method
raise TypeError, "no implicit conversion of #{object.class} into #{type}"
end

object.__send__ method
end

def self.coerce_to!(object, type, method)
coerced = coerce_to(object, type, method)

unless type === coerced
raise TypeError, "can't convert #{object.class} into #{type} (#{object.class}##{method} gives #{coerced.class}"
end

coerced
end

def self.try_convert(object, type, method)
return object if type === object

if object.respond_to? method
object.__send__ method
end
end

def self.compare(a, b)
compare = a <=> b

if `compare === nil`
raise ArgumentError, "comparison of #{a.class.name} with #{b.class.name} failed"
end

compare
end

def self.fits_fixnum!(value)
# since we have Fixnum#size as 32 bit, this is based on the int limits
if `value > 2147483648`
raise RangeError, "bignum too big to convert into `long'"
end
end

def self.fits_array!(value)
# this is the computed ARY_MAX_SIZE for 32 bit
if `value >= 536870910`
raise ArgumentError, "argument too big"
end
end

def self.truthy?(value)
if value
true
else
false
end
end

def self.falsy?(value)
if value
false
else
true
end
end

def self.destructure(args)
%x{
if (args.length == 1) {
return args[0];
}
else if (args._isArray) {
return args;
}
else {
return $slice.call(args);
}
}
end
end
5 changes: 2 additions & 3 deletions spec/filters/bugs/string.rb
Original file line number Diff line number Diff line change
@@ -8,12 +8,11 @@
fails "String#size returns the length of self"
fails "String#length returns the length of self"

fails "String#index with Regexp converts start_offset to an integer via to_int"
# we need regexp rewriting for these
fails "String#index with Regexp supports \\G which matches at the given start offset"
fails "String#index with Regexp starts the search at the given offset"
fails "String#index with Regexp returns the index of the first match of regexp"
fails "String#index calls #to_int to convert the second argument"
fails "String#index calls #to_str to convert the first argument"

fails "String#index raises a TypeError if passed a Symbol"

fails "String#intern special cases +(binary) and -(binary)"
4 changes: 3 additions & 1 deletion tasks/mspec.rake
Original file line number Diff line number Diff line change
@@ -102,7 +102,9 @@ class RunSpec
specs << (file.nil? ? Dir.glob("#{Dir.pwd}/spec/**/*_spec.{rb,opal}") : file)

# add any rubyspecs we want to run (defined in spec/rubyspecs)
specs.push File.read('spec/rubyspecs').split("\n").reject(&:empty?)
specs.push File.read('spec/rubyspecs').split("\n").reject {|line|
line.empty? || line.start_with?('#')
}

specs.flatten
end