Skip to content

Commit

Permalink
Separate wrapper classes in corelib
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Jan 14, 2014
1 parent bf3fe80 commit 1475b2c
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 191 deletions.
112 changes: 0 additions & 112 deletions opal/corelib/array.rb
Expand Up @@ -6,21 +6,6 @@ class Array
# Mark all javascript arrays as being valid ruby arrays
`def._isArray = true`

def self.inherited(klass)
replace = Class.new(Array::Wrapper)

%x{
klass._proto = replace._proto;
klass._proto._klass = klass;
klass._alloc = replace._alloc;
klass.__parent = #{Array::Wrapper};
klass.$allocate = replace.$allocate;
klass.$new = replace.$new;
klass["$[]"] = replace["$[]"];
}
end

def self.[](*objects)
objects
end
Expand Down Expand Up @@ -1549,100 +1534,3 @@ def zip(*others, &block)
}
end
end

class Array::Wrapper
def self.allocate(array = [])
obj = super()
`obj.literal = array`
obj
end

def self.new(*args, &block)
obj = allocate
obj.initialize(*args, &block)
obj
end

def self.[](*objects)
allocate(objects)
end

def initialize(*args, &block)
@literal = Array.new(*args, &block)
end

def method_missing(*args, &block)
result = @literal.__send__(*args, &block)

if `result === #@literal`
self
else
result
end
end

def initialize_copy(other)
@literal = `other.literal`.clone
end

def respond_to?(name, *)
super || @literal.respond_to?(name)
end

def ==(other)
@literal == other
end

def eql?(other)
@literal.eql?(other)
end

def to_a
@literal
end

def to_ary
self
end

def inspect
@literal.inspect
end

# wrapped results
def *(other)
%x{
var result = #{@literal * other};
if (result._isArray) {
return #{self.class.allocate(`result`)}
}
else {
return result;
}
}
end

def [](index, length = undefined)
%x{
var result = #{@literal.slice(index, length)};
if (result._isArray && (index._isRange || length !== undefined)) {
return #{self.class.allocate(`result`)}
}
else {
return result;
}
}
end

alias slice []

def uniq
self.class.allocate(@literal.uniq)
end

def flatten(level = undefined)
self.class.allocate(@literal.flatten(level))
end
end
113 changes: 113 additions & 0 deletions opal/corelib/array_wrapper.rb
@@ -0,0 +1,113 @@
class Array
def self.inherited(klass)
replace = Class.new(Array::Wrapper)

%x{
klass._proto = replace._proto;
klass._proto._klass = klass;
klass._alloc = replace._alloc;
klass.__parent = #{Array::Wrapper};
klass.$allocate = replace.$allocate;
klass.$new = replace.$new;
klass["$[]"] = replace["$[]"];
}
end
end

class Array::Wrapper
def self.allocate(array = [])
obj = super()
`obj.literal = array`
obj
end

def self.new(*args, &block)
obj = allocate
obj.initialize(*args, &block)
obj
end

def self.[](*objects)
allocate(objects)
end

def initialize(*args, &block)
@literal = Array.new(*args, &block)
end

def method_missing(*args, &block)
result = @literal.__send__(*args, &block)

if `result === #@literal`
self
else
result
end
end

def initialize_copy(other)
@literal = `other.literal`.clone
end

def respond_to?(name, *)
super || @literal.respond_to?(name)
end

def ==(other)
@literal == other
end

def eql?(other)
@literal.eql?(other)
end

def to_a
@literal
end

def to_ary
self
end

def inspect
@literal.inspect
end

# wrapped results
def *(other)
%x{
var result = #{@literal * other};
if (result._isArray) {
return #{self.class.allocate(`result`)}
}
else {
return result;
}
}
end

def [](index, length = undefined)
%x{
var result = #{@literal.slice(index, length)};
if (result._isArray && (index._isRange || length !== undefined)) {
return #{self.class.allocate(`result`)}
}
else {
return result;
}
}
end

alias slice []

def uniq
self.class.allocate(@literal.uniq)
end

def flatten(level = undefined)
self.class.allocate(@literal.flatten(level))
end
end
79 changes: 0 additions & 79 deletions opal/corelib/string.rb
Expand Up @@ -5,20 +5,6 @@ class String

`def._isString = true`

def self.inherited(klass)
replace = Class.new(String::Wrapper)

%x{
klass._proto = replace._proto;
klass._proto._klass = klass;
klass._alloc = replace._alloc;
klass.__parent = #{String::Wrapper};
klass.$allocate = replace.$allocate;
klass.$new = replace.$new;
}
end

def self.try_convert(what)
what.to_str
rescue
Expand Down Expand Up @@ -1158,68 +1144,3 @@ def frozen?
end

Symbol = String

class String::Wrapper
def self.allocate(string = "")
obj = super()
`obj.literal = string`
obj
end

def self.new(*args, &block)
obj = allocate
obj.initialize(*args, &block)
obj
end

def self.[](*objects)
allocate(objects)
end

def initialize(string = '')
@literal = string
end

def method_missing(*args, &block)
result = @literal.__send__(*args, &block)

if `result._isString != null`
if `result == #@literal`
self
else
self.class.allocate(result)
end
else
result
end
end

def initialize_copy(other)
@literal = `other.literal`.clone
end

def respond_to?(name, *)
super || @literal.respond_to?(name)
end

def ==(other)
@literal == other
end

alias eql? ==
alias === ==

def to_s
@literal
end

def to_str
self
end

def inspect
@literal.inspect
end

# unwrapped results
end

0 comments on commit 1475b2c

Please sign in to comment.