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

Commits on Nov 8, 2013

  1. Add Enumerator#size

    meh committed Nov 8, 2013
    Copy the full SHA
    da34b56 View commit details
  2. Copy the full SHA
    78af294 View commit details
  3. Copy the full SHA
    b619097 View commit details
Showing with 53 additions and 24 deletions.
  1. +50 −24 opal/core/enumerator.rb
  2. +3 −0 opal/core/numeric.rb
74 changes: 50 additions & 24 deletions opal/core/enumerator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
class Enumerator
include Enumerable

def initialize(obj = nil, method = :each, *args, &block)
def initialize(obj = undefined, method = :each, *args, &block)
if block
@block = block
@size = obj
@object = Generator.new(&block)
@method = :each
else
if `obj === undefined`
raise ArgumentError, "wrong number of arguments (0 for 1+)"
end

@size = nil
@object = obj
@method = method
@args = args
@@ -14,11 +21,11 @@ def initialize(obj = nil, method = :each, *args, &block)
def each(&block)
return enum_for :each unless block_given?

if @block
Yielder.new(self, @block).each(&block)
else
@object.__send__(@method, *@args, &block)
end
@object.__send__(@method, *@args, &block)
end

def size
Proc === @size ? @size.call : @size
end

def next
@@ -35,30 +42,29 @@ def rewind
self
end

class Yielder
def initialize(enumerator, block)
@enumerator = enumerator
@block = block
end
def inspect
"#<Enumerator: #{@object.inspect}:#{@method}>"
end

def yield(*values)
%x{
if ($opal.$yieldX(#@to, values) === $breaker) {
throw $breaker;
}
}
class Generator
include Enumerable

self
end
def initialize(&block)
raise LocalJumpError, 'no block given' unless block

alias << yield
@block = block
end

def each(&block)
@to = block
def each(*args, &block)
yielder = Yielder.new(&block)

%x{
try {
#@block(self)
args.unshift(#{yielder});
if ($opal.$yieldX(#@block, args) === $breaker) {
return $breaker.$v;
}
}
catch (e) {
if (e === $breaker) {
@@ -69,6 +75,26 @@ def each(&block)
}
}
}

self
end
end

class Yielder
def initialize(&block)
@block = block
end

def yield(*values)
%x{
if ($opal.$yieldX(#@block, values) === $breaker) {
throw $breaker;
}
}

self
end

alias << yield
end
end
3 changes: 3 additions & 0 deletions opal/core/numeric.rb
Original file line number Diff line number Diff line change
@@ -440,4 +440,7 @@ class Float < Numeric
def self.===(other)
`!!(other._isNumber && (other % 1) != 0)`
end

INFINITY = `Infinity`
NAN = `NaN`
end