module RubyRunJs::Helper

Public Instance Methods

check_object(obj) click to toggle source
# File lib/ruby_run_js/helper.rb, line 50
def check_object(obj)
  if obj.js_type == :Undefined || obj.js_type == :Null
    raise make_error('TypeError', 'undefined or null can\'t be converted to object')
  end
end
get_member(obj, prop, builtin) click to toggle source
# File lib/ruby_run_js/helper.rb, line 56
def get_member(obj, prop, builtin)
  type = obj.js_type
  if is_primitive(obj)
    case type
    when :String
      if prop.js_type == :Number && prop.finite?
        index = prop.to_i
        if index == prop && index >= 0 && index < obj.length
          return obj[index]
        end
      end

      s_prop = to_string(prop)
      if s_prop == 'length'
        obj.length.to_f
      elsif s_prop =~ /^\d+$/
        index = s_prop.to_i
        if index >= 0 && index < obj.length
          return obj[index]
        end
      end

      return builtin.string_prototype.get(s_prop)
    when :Number
      return builtin.number_prototype.get(to_string(prop))
    when :Boolean
      return builtin.boolean_prototype.get(to_string(prop))
    when :Null
      raise make_error('TypeError', "Cannot read property '#{prop}' of null")
    when :Undefined
      raise make_error('TypeError', "Cannot read property '#{prop}' of undefined")
    end
  end
  obj.get(to_string(prop))
end
get_member_dot(obj, prop, builtin) click to toggle source
# File lib/ruby_run_js/helper.rb, line 92
def get_member_dot(obj, prop, builtin)
  if is_primitive(obj)
    case obj.js_type
    when :String
      if prop == 'length'
        obj.length.to_f
      elsif prop =~ /^\d+$/
        index = prop.to_i
        if index >= 0 && index < obj.length
          return obj[index]
        end
      end
      return builtin.string_prototype.get(prop)
    when :Number
      return builtin.number_prototype.get(to_string(prop))
    when :Boolean
      return builtin.boolean_prototype.get(to_string(prop))
    when :Null
      raise make_error('TypeError', "Cannot read property '#{prop}' of null")
    when :Undefined
      raise make_error('TypeError', "Cannot read property '#{prop}' of undefined")
    end
  end
  obj.get(prop)
end
is_accessor_descriptor(desc) click to toggle source
# File lib/ruby_run_js/helper.rb, line 19
def is_accessor_descriptor(desc)
  desc && desc != undefined && (desc.key?('get') || desc.key?('set'))
end
is_callable(func) click to toggle source
# File lib/ruby_run_js/helper.rb, line 27
def is_callable(func)
  func.respond_to? :call
end
is_data_descriptor(desc) click to toggle source
# File lib/ruby_run_js/helper.rb, line 15
def is_data_descriptor(desc)
  desc && desc != undefined && (desc.key?('value') || desc.key?('writable'))
end
is_generic_descriptor(desc) click to toggle source
# File lib/ruby_run_js/helper.rb, line 23
def is_generic_descriptor(desc)
  desc && desc != undefined && !(is_data_descriptor(desc) || is_accessor_descriptor(desc))
end
is_primitive(value) click to toggle source
# File lib/ruby_run_js/helper.rb, line 31
def is_primitive(value)
  [:Undefined, :Null, :Boolean, :Number, :String].include?(value.js_type)
end
make_error(error_type, message = 'no info', throw_value = nil) click to toggle source
# File lib/ruby_run_js/helper.rb, line 11
def make_error(error_type, message = 'no info', throw_value = nil)
  JsException.new(error_type, message, throw_value)
end
strict_equality(a, b) click to toggle source
# File lib/ruby_run_js/helper.rb, line 35
def strict_equality(a, b)
  type = a.js_type
  if type != b.js_type
    return false
  end
  case type
  when :Undefined, :Null
    return true
  when :Boolean, :String, :Number
    return a == b
  else
    return a.equal?(b)
  end
end