Skip to content

Commit

Permalink
Use hasOwnProperty.call instead of calling the method
Browse files Browse the repository at this point in the history
In IE8 and below calling `hasOwnProperty` on a DOM element or anything
else it doesn't like will blow up, but using `.call` fixes this.
  • Loading branch information
meh committed Jan 23, 2014
1 parent 8d72393 commit a901ef4
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 8 deletions.
8 changes: 3 additions & 5 deletions opal/corelib/hash.rb
Expand Up @@ -3,8 +3,6 @@
class Hash
include Enumerable

`var $hasOwn = {}.hasOwnProperty`

def self.[](*objs)
`$opal.hash.apply(null, objs)`
end
Expand Down Expand Up @@ -66,7 +64,7 @@ def [](key)
%x{
var map = self.map;
if ($hasOwn.call(map, key)) {
if ($opal.hasOwnProperty.call(map, key)) {
return map[key];
}
Expand All @@ -84,7 +82,7 @@ def []=(key, value)
%x{
var map = self.map;
if (!$hasOwn.call(map, key)) {
if (!$opal.hasOwnProperty.call(map, key)) {
self.keys.push(key);
}
Expand Down Expand Up @@ -316,7 +314,7 @@ def flatten(level=undefined)
end

def has_key?(key)
`$hasOwn.call(self.map, key)`
`$opal.hasOwnProperty.call(self.map, key)`
end

def has_value?(value)
Expand Down
2 changes: 1 addition & 1 deletion opal/corelib/kernel.rb
Expand Up @@ -303,7 +303,7 @@ def instance_of?(klass)
end

def instance_variable_defined?(name)
`self.hasOwnProperty(name.substr(1))`
`$opal.hasOwnProperty.call(self, name.substr(1))`
end

def instance_variable_get(name)
Expand Down
296 changes: 296 additions & 0 deletions stdlib/matrix.rb
@@ -0,0 +1,296 @@
module ExceptionForMatrix
class ErrNotRegular < Exception
def initialize
super("Not Regular Matrix")
end
end

class ErrDimensionMismatch < Exception
def initialize(name)
super("#{name} dimension mismatch")
end
end

class ErrOperationNotDefined
def initialize(op, name, on)
super("Operation(#{op}) can't be defined: #{name} op #{on}")
end
end
end

class Matrix
def self.[](*rows)
rows(rows, false)
end

def self.rows(rows, copy = true)
end

def self.columns(columns)
rows(columns, false).transpose
end

def self.empty(rows = 0, columns = 0)
aise ArgumentError, "One size must be 0" if columns != 0 && rows != 0
raise ArgumentError, "Negative size" if columns < 0 || row < 0

new([[]] * rows, columns)
end

def self.build(rows, columns = rows, &block)
rows = rows.to_int
columns = columns.to_int

raise ArgumentError if rows < 0 || columns < 0
return to_enum :build, rows, columns unless block

rows = Array.new(row_count) {|i|
Array.new(columns) {|j|
yield i, j
}
}

new rows, columns
end

def self.diagonal(*values)
new Array.new(values.length) {|j|
Array.new(values.length, 0).tap {|row|
row[j] = values[j]
}
}
end

def self.scalar(n, value)
diagonal(*Array.new(n, value))
end

def self.identity(n)
scalar(n,1)
end

def self.zero(rows, columns = rows)
new Array.new(rows) { Array.new(columns, 0) }, columns
end

def self.row_vector(row)
new [Convert.to_array(row)]
end

def self.column_vector(column)
new [column].transpose, 1
end

def initialize(rows, column_count = rows[-1].size)
@rows = rows
@column_count = columns
end

def [](i, j)
@rows.fetch(i) {
return nil
}[j]
end

alias element []
alias component []

def []=(i, j, v)
@rows[i][j] = v
end

alias set_element []=
alias set_component []=

def row_count
@rows.length
end

alias row_size row_count

attr_reader :column_count
alias column_size column_count

module Convert
def self.to_array(obj, copy = false)
case obj
when Array
copy ? obj.dup : obj

when Vector
obj.to_a

else
obj.to_ary
end
end
end
end

class Vector
def self.[](*array)
new Matrix::Convert.to_array(array, false)
end

def self.elements(array, copy = true)
new Matrix::Convert.to_array(array, copy)
end

def initialize(array)
@elements = array
end

def [](i)
@elements[i]
end

alias element []
alias component []

def []=(i, v)
@elements[i] = v
end

alias set_element []=
alias set_component []=

def size
@elements.size
end

def each(&block)
return enum_for :each unless block

@elements.each(&block)

self
end

def along(enum, other, &block)
raise TypeError, "Integer is not like Vector" if Integer === other

if size != other.size
raise ExceptionForMatrix::ErrDimensionMismatch, :each2, Vector, Vector
end


end

def each2(other, &block)
return enum_for :each2, other unless block

size.times {|i|
yield @elements[i], other[i]
}
end

def collect2(other, &block)
raise TypeError, "Integer is not like Vector" if Integer === other

if size != other.size
raise ExceptionForMatrix::ErrDimensionMismatch, :collect2, Vector, Vector
end

Array.new(size) {|i|
yield @elements[i], other[i]
}
end

def ==(other)
return false unless Vector == other

@elements == other.elements
end

def eql?(other)
return false unless Vector === other

@elements.eql? other.elements
end

def clone
self.class.elements(@elements)
end

def hash
@elements.hash
end

def *(other)
case other
when Numeric
self.class.elements(@elements.map(&:*), false)

when Matrix
Matrix.column_vector(self) * other

when Vector
raise ExceptionForMatrix::ErrOperationNotDefined, :*, Vector, Vector
end
end

def +(other)
case other
when Vector
if size != other.size
raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector
end

self.class.elements collect2(other, &:+), false

when Matrix
Matrix.column_vector(self) + other
end
end

def -(other)
case other
when Vector
if size != other.size
raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector
end

self.class.elements collect2(other, &:-), false

when Matrix
Matrix.column_vector(self) - other
end
end

def /(other)
case other
when Numeric
self.class.elements @elements.collect(&:/), false

when Matrix, Vector
raise ExceptionForMatrix::ErrOperationNotDefined, :*, Vector, other.class
end
end

def inner_product(other)
if size != other.size
raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector
end

p = 0
each2(other) {|v1, v2|
p += v1 * v2.conj
}
p
end

def collect(&block)
return enum_for :collect unless block

self.class.elements @elements.collect(&block), false
end

alias map collect

def magnitude
`Math.sqrt(#{@elements.reduce(0) { |v, e| `v + Math.pow(Math.abs(e), 2)` }})`
end

alias r magnitude
alias norm magnitude
end
4 changes: 2 additions & 2 deletions stdlib/native.rb
Expand Up @@ -149,7 +149,7 @@ def ==(other)
end

def has_key?(name)
`#@native.hasOwnProperty(#{name})`
`$opal.hasOwnProperty.call(#@native, #{name})`
end

alias key? has_key?
Expand Down Expand Up @@ -210,7 +210,7 @@ def respond_to?(name, include_all = false)
end

def respond_to_missing?(name)
`#@native.hasOwnProperty(#{name})`
`$opal.hasOwnProperty.call(#@native, #{name})`
end

def method_missing(mid, *args, &block)
Expand Down

2 comments on commit a901ef4

@elia
Copy link
Member

@elia elia commented on a901ef4 Jan 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everyone fits a matrix impl inside an IE8 fix lol

@meh
Copy link
Member Author

@meh meh commented on a901ef4 Jan 26, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the fuck. How did this happen I don't know.

Please sign in to comment.