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: 889c69bed2ae
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e735812cb6f3
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Oct 26, 2013

  1. Copy the full SHA
    e3df0a0 View commit details
  2. Keep all helpers under Opal::

    meh committed Oct 26, 2013
    Copy the full SHA
    9136c99 View commit details
  3. Copy the full SHA
    e735812 View commit details
Showing with 37 additions and 28 deletions.
  1. +11 −11 corelib/array.rb
  2. +21 −7 corelib/opal.rb
  3. +0 −4 corelib/string.rb
  4. +5 −6 stdlib/json.rb
22 changes: 11 additions & 11 deletions corelib/array.rb
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ def self.new(size = nil, obj = nil, &block)
return size.to_ary
end

size = Opal::Type.coerce_to size, Integer, :to_int
size = Opal.coerce_to size, Integer, :to_int

if `size < 0`
raise ArgumentError, "negative array size"
@@ -66,7 +66,7 @@ def &(other)
if Array === other
other = other.to_a
else
other = Opal::Type.coerce_to other, Array, :to_ary
other = Opal.coerce_to other, Array, :to_ary
end

%x{
@@ -99,7 +99,7 @@ def *(other)
raise TypeError, "no implicit conversion of #{other.class} into Integer"
end

other = Opal::Type.coerce_to other, Integer, :to_int
other = Opal.coerce_to other, Integer, :to_int

if `other < 0`
raise ArgumentError, "negative argument"
@@ -120,7 +120,7 @@ def +(other)
if Array === other
other = other.to_a
else
other = Opal::Type.coerce_to other, Array, :to_ary
other = Opal.coerce_to other, Array, :to_ary
end

`self.concat(other)`
@@ -130,7 +130,7 @@ def -(other)
if Array === other
other = other.to_a
else
other = Opal::Type.coerce_to other, Array, :to_ary
other = Opal.coerce_to other, Array, :to_ary
end

return [] if `self.length === 0`
@@ -347,7 +347,7 @@ def cycle(n = nil, &block)
end
end
else
cycles = Opal::Type.coerce_to n, Integer, :to_int
cycles = Opal.coerce_to n, Integer, :to_int

unless Integer === cycles
raise TypeError, "can't convert #{n.class} into Integer (#{n.class}#to_int gives #{cycles.class}"
@@ -594,22 +594,22 @@ def fill(*args, &block)
if Range === one
raise TypeError, "length invalid with range" if two

left = Opal::Type.coerce_to one.begin, Integer, :to_int
left = Opal.coerce_to one.begin, Integer, :to_int
`left += #@length` if `left < 0`
raise RangeError, "#{one.inspect} out of range" if `left < 0`

right = Opal::Type.coerce_to one.end, Integer, :to_int
right = Opal.coerce_to one.end, Integer, :to_int
`right += #@length` if `right < 0`
`right += 1` unless one.exclude_end?

return self if `right <= left`
elsif one
left = Opal::Type.coerce_to one, Integer, :to_int
left = Opal.coerce_to one, Integer, :to_int
`left += #@length` if `left < 0`
left = 0 if `left < 0`

if two
right = Opal::Type.coerce_to two, Integer, :to_int
right = Opal.coerce_to two, Integer, :to_int

return self if `right == 0`

@@ -1231,7 +1231,7 @@ def transpose
if Array === row
row = row.to_a
else
row = Opal::Type.coerce_to row, Array, :to_ary
row = Opal.coerce_to row, Array, :to_ary
end

max ||= `row.length`
28 changes: 21 additions & 7 deletions corelib/opal.rb
Original file line number Diff line number Diff line change
@@ -45,15 +45,29 @@
RUBY_RELEASE_DATE = '2013-08-13'

module Opal
module Type
def self.coerce_to(object, type, method)
return object if type === object
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
unless object.respond_to? method
raise TypeError, "no implicit conversion of #{object.class} into #{type}"
end

object.__send__ method
end

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

object.__send__ method
def self.falsy?(value)
if value
false
else
true
end
end
end
4 changes: 0 additions & 4 deletions corelib/string.rb
Original file line number Diff line number Diff line change
@@ -281,10 +281,6 @@ def end_with?(*suffixes)
alias eql? ==
alias equal? ===

def getbyte(idx)
`self.charCodeAt(idx)`
end

def gsub(pattern, replace = undefined, &block)
if String === pattern || pattern.respond_to?(:to_str)
pattern = /#{Regexp.escape(pattern.to_str)}/
11 changes: 5 additions & 6 deletions stdlib/json.rb
Original file line number Diff line number Diff line change
@@ -66,19 +66,18 @@ def self.[](value, options = {})
end

def self.parse(source, options = {})
options[:object_class] = Hash unless options.has_key? :object_class
options[:array_class] = Array unless options.has_key? :array_class

`to_opal(json_parse(source), #{options.to_n});`
from_object(`json_parse(source)`, options)
end

def self.parse!(source, options = {})
parse(source, options)
end

# Raw js object => opal object
def self.from_object(js_object)
options = { :object_class => Hash, :array_class => Array }
def self.from_object(js_object, options = {})
options[:object_class] ||= Hash
options[:array_class] ||= Array

`to_opal(js_object, #{options.to_n})`
end