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

Commits on Nov 9, 2013

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    fa80648 View commit details
  2. Cleanup Kernel#to_enum handling

    meh committed Nov 9, 2013

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    fe77622 View commit details
  3. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    360481a View commit details
Showing with 52 additions and 17 deletions.
  1. +40 −13 opal/core/enumerator.rb
  2. +2 −2 opal/core/kernel.rb
  3. +10 −2 stdlib/native.rb
53 changes: 40 additions & 13 deletions opal/core/enumerator.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
class Enumerator
include Enumerable

def initialize(obj = undefined, method = :each, *args, &block)
def self.for(object, method = :each, *args, &block)
%x{
var obj = #{allocate};
obj.object = object;
obj.size = block;
obj.method = method;
obj.args = args;
return obj;
}

end

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

if @size
@size = Opal.coerce_to @size, Integer, :to_int
end
else
@object = `arguments[0]`
@method = `arguments[1] || "each"`
@args = `$slice.call(arguments, 2)`
@size = nil
@object = obj
@method = method
@args = args
end
end

@@ -25,7 +40,7 @@ def each(&block)
end

def size
Proc === @size ? @size.call : @size
Proc === @size ? @size.call(*@args) : @size
end

def with_index(offset = 0, &block)
@@ -107,15 +122,21 @@ def initialize(&block)

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

def <<(*values)
self.yield(*values)

self
end

alias << yield
end

class Lazy < self
@@ -220,6 +241,10 @@ def drop_while(&block)
}
end

def enum_for(method = :each, *args, &block)
self.class.for(self, method, *args, &block)
end

def find_all(&block)
unless block
raise ArgumentError, 'tried to call lazy select without a block'
@@ -346,6 +371,8 @@ def take_while(&block)
}
end

alias to_enum enum_for

def inspect
"#<#{self.class.name}: #{@enumerator.inspect}>"
end
4 changes: 2 additions & 2 deletions opal/core/kernel.rb
Original file line number Diff line number Diff line change
@@ -114,8 +114,8 @@ def dup
copy
end

def enum_for(method = :each, *args)
Enumerator.new self, method, *args
def enum_for(method = :each, *args, &block)
Enumerator.for(self, method, *args, &block)
end

def equal?(other)
12 changes: 10 additions & 2 deletions stdlib/native.rb
Original file line number Diff line number Diff line change
@@ -425,13 +425,21 @@ class Hash
def initialize(defaults = undefined, &block)
%x{
if (defaults != null) {
if (defaults.constructor == Object) {
if (defaults.constructor === Object) {
var map = self.map,
keys = self.keys;
for (var key in defaults) {
var value = defaults[key];
if (value && value.constructor === Object) {
map[key] = #{Hash.new(`value`)};
}
else {
map[key] = #{Native(`defaults[key]`)};
}
keys.push(key);
map[key] = #{Native(`defaults[key]`)};
}
}
else {