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

Commits on Nov 12, 2013

  1. Verified

    This commit was signed with the committer’s verified signature.
    lukekarrys Luke Karrys
    Copy the full SHA
    67499d3 View commit details
  2. Verified

    This commit was signed with the committer’s verified signature.
    lukekarrys Luke Karrys
    Copy the full SHA
    761c1d5 View commit details
Showing with 69 additions and 75 deletions.
  1. +5 −63 opal/core/hash.rb
  2. +1 −12 opal/core/range.rb
  3. +63 −0 opal/core/runtime.js
68 changes: 5 additions & 63 deletions opal/core/hash.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,10 @@
class Hash
include Enumerable

%x{
var $hash = Opal.hash = function() {
if (arguments.length == 1 && arguments[0]._klass == Hash) {
return arguments[0];
}
var hash = new Hash._alloc,
keys = [],
assocs = {};
hash.map = assocs;
hash.keys = keys;
if (arguments.length == 1 && arguments[0]._isArray) {
var args = arguments[0];
for (var i = 0, length = args.length; i < length; i++) {
var key = args[i][0], obj = args[i][1];
if (assocs[key] == null) {
keys.push(key);
}
assocs[key] = obj;
}
}
else {
for (var i = 0, length = arguments.length; i < length; i++) {
var key = arguments[i],
obj = arguments[++i];
if (assocs[key] == null) {
keys.push(key);
}
assocs[key] = obj;
}
}
return hash;
};
}

# hash2 is a faster creator for hashes that just use symbols and
# strings as keys. The map and keys array can be constructed at
# compile time, so they are just added here by the constructor
# function
%x{
var $hash2 = Opal.hash2 = function(keys, map) {
var hash = new Hash._alloc;
hash.keys = keys;
hash.map = map;
return hash;
};
}

`var $hasOwn = {}.hasOwnProperty`

def self.[](*objs)
`$hash.apply(null, objs)`
`$opal.hash.apply(null, objs)`
end

def self.allocate
@@ -450,7 +392,7 @@ def inspect

def invert
%x{
var result = $hash(), keys = self.keys, map = self.map,
var result = $opal.hash(), keys = self.keys, map = self.map,
keys2 = result.keys, map2 = result.map;
for (var i = 0, length = keys.length; i < length; i++) {
@@ -507,7 +449,7 @@ def length
def merge(other, &block)
%x{
var keys = self.keys, map = self.map,
result = $hash(), keys2 = result.keys, map2 = result.map;
result = $opal.hash(), keys2 = result.keys, map2 = result.map;
for (var i = 0, length = keys.length; i < length; i++) {
var key = keys[i];
@@ -602,7 +544,7 @@ def reject(&block)

%x{
var keys = self.keys, map = self.map,
result = $hash(), map2 = result.map, keys2 = result.keys;
result = $opal.hash(), map2 = result.map, keys2 = result.keys;
for (var i = 0, length = keys.length; i < length; i++) {
var key = keys[i], obj = map[key], value;
@@ -640,7 +582,7 @@ def select(&block)

%x{
var keys = self.keys, map = self.map,
result = $hash(), map2 = result.map, keys2 = result.keys;
result = $opal.hash(), map2 = result.map, keys2 = result.keys;
for (var i = 0, length = keys.length; i < length; i++) {
var key = keys[i], obj = map[key], value;
13 changes: 1 addition & 12 deletions opal/core/range.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
class Range
include Enumerable

%x{
Range._proto._isRange = true;
Opal.range = function(first, last, exc) {
var range = new Range._alloc;
range.begin = first;
range.end = last;
range.exclude = exc;
return range;
};
}
`Range._proto._isRange = true;`

attr_reader :begin, :end

63 changes: 63 additions & 0 deletions opal/core/runtime.js
Original file line number Diff line number Diff line change
@@ -735,6 +735,69 @@
}
}

Opal.hash = function() {
if (arguments.length == 1 && arguments[0]._klass == Opal.Hash) {
return arguments[0];
}

var hash = new Opal.Hash._alloc,
keys = [],
assocs = {};

hash.map = assocs;
hash.keys = keys;

if (arguments.length == 1 && arguments[0]._isArray) {
var args = arguments[0];

for (var i = 0, length = args.length; i < length; i++) {
var key = args[i][0], obj = args[i][1];

if (assocs[key] == null) {
keys.push(key);
}

assocs[key] = obj;
}
}
else {
for (var i = 0, length = arguments.length; i < length; i++) {
var key = arguments[i],
obj = arguments[++i];

if (assocs[key] == null) {
keys.push(key);
}

assocs[key] = obj;
}
}

return hash;
};

// hash2 is a faster creator for hashes that just use symbols and
// strings as keys. The map and keys array can be constructed at
// compile time, so they are just added here by the constructor
// function
Opal.hash2 = function(keys, map) {
var hash = new Opal.Hash._alloc;

hash.keys = keys;
hash.map = map;

return hash;
};

Opal.range = function(first, last, exc) {
var range = new Opal.Range._alloc;
range.begin = first;
range.end = last;
range.exclude = exc;

return range;
};

// Initialization
// --------------