module RubyRunJs::JsObjectMethods

Public Class Methods

constructor(builtin, this, value) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 11
def constructor(builtin, this, value)
  if value == null || value == undefined
    return constructor_new(builtin, this, value)
  end
  to_object(value, builtin)
end
constructor_create(builtin, this, obj, properties) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 58
def constructor_create(builtin, this, obj, properties)
  if obj.js_type != :Object && obj.js_type != :Null
    raise make_error('TypeError', 'Object.create called on non-object prototype')
  end
  result = JsObject.new(obj == null ? nil : obj)
  unless properties == undefined
    constructor_defineProperties(builtin, this, result, properties)
  end
  result
end
constructor_defineProperties(builtin, this, obj, properties) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 77
def constructor_defineProperties(builtin, this, obj, properties)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.defineProperties called on non-object')
  end
  # @type [JsObject]
  props = to_object(properties, builtin)
  props.own.each do |k, v|
    unless v['enumerable']
      next
    end
    desc = toPropertyDescriptor(v)
    obj.define_own_property(k, desc, true)
  end
  obj
end
constructor_defineProperty(builtin, this, obj, prop_name, attributes) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 69
def constructor_defineProperty(builtin, this, obj, prop_name, attributes)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.defineProperty called on non-object')
  end
  obj.define_own_property(to_string(prop_name), toPropertyDescriptor(attributes), true)
  obj
end
constructor_freeze(builtin, this, obj) click to toggle source

@param [JsObject]

# File lib/ruby_run_js/object_methods/js_object.rb, line 106
def constructor_freeze(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.freeze called on non-object')
  end
  obj.own.each_value do |v|
    v['configurable'] = false
    if is_data_descriptor(v)
      v['writable'] = false
    end
  end
  obj.extensible = false
  obj
end
constructor_getOwnPropertyDescriptor(builtin, this, obj, prop_name) click to toggle source

@param [JsObject]

# File lib/ruby_run_js/object_methods/js_object.rb, line 38
def constructor_getOwnPropertyDescriptor(builtin, this, obj, prop_name)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.getOwnPropertyDescriptor called on non-object')
  end
  prop_name = to_string(prop_name)
  desc = obj.get_own_property(prop_name)
  fromPropertyDescriptor(builtin, desc)
end
constructor_getOwnPropertyNames(builtin, this, obj) click to toggle source

@param [JsObject] @return [JsArray]

# File lib/ruby_run_js/object_methods/js_object.rb, line 49
def constructor_getOwnPropertyNames(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.getOwnPropertyNames called on non-object')
  end
  array = builtin.new_array()
  array.set_items(obj.own.keys)
  array
end
constructor_getPrototypeOf(builtin, this, obj) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 30
def constructor_getPrototypeOf(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.getPrototypeOf called on non-object')
  end
  obj.prototype
end
constructor_isExtensible(builtin, this, obj) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 151
def constructor_isExtensible(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.isExtensible called on non-object')
  end
  obj.extensible
end
constructor_isFrozen(builtin, this, obj) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 139
def constructor_isFrozen(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.isFrozen called on non-object')
  end
  return false if obj.extensible
  obj.own.each_value do |v|
    return false if v['configurable']
    return false if is_data_descriptor(v) && v['writable']
  end
  true
end
constructor_isSealed(builtin, this, obj) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 128
def constructor_isSealed(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.isSealed called on non-object')
  end
  return false if obj.extensible
  obj.own.each_value do |v|
    return false if v['configurable']
  end
  true
end
constructor_keys(builtin, this, obj) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 158
def constructor_keys(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.keys called on non-object')
  end
  array = builtin.new_array()
  array.set_items(obj.own.keys.filter { |k| obj.own[k]['enumerable'] })
  array
end
constructor_new(builtin, this, value) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 18
def constructor_new(builtin, this, value)
  if value != undefined
    case value.js_type
    when :Object
      return value
    when :String, :Boolean, :Number
      return to_object(value, builtin)
    end
  end
  builtin.new_object
end
constructor_preventExtensions(builtin, this, obj) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 120
def constructor_preventExtensions(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.preventExtensions called on non-object')
  end
  obj.extensible = false
  obj
end
constructor_seal(builtin, this, obj) click to toggle source

@param [JsObject]

# File lib/ruby_run_js/object_methods/js_object.rb, line 94
def constructor_seal(builtin, this, obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.seal called on non-object')
  end
  obj.own.each_value do |v|
    v['configurable'] = false
  end
  obj.extensible = false
  obj
end
prototype_hasOwnProperty(builtin, this, prop_name) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 184
def prototype_hasOwnProperty(builtin, this, prop_name)
  o = to_object(this, builtin)
  o.get_own_property(to_string(prop_name)) != undefined
end
prototype_isPrototypeOf(builtin, this, obj) click to toggle source

@param [JsObject] @param [JsObject]

# File lib/ruby_run_js/object_methods/js_object.rb, line 191
def prototype_isPrototypeOf(builtin, this, obj)
  return false if obj.js_type != :Object
  o = to_object(this, builtin)
  loop do
    obj = obj.prototype
    return false if obj.nil? || obj == null
    return true if obj == o
  end
end
prototype_propertyIsEnumerable(builtin, this, prop_name) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 201
def prototype_propertyIsEnumerable(builtin, this, prop_name)
  o = to_object(this, builtin)
  desc = o.get_own_property(to_string(prop_name))
  return false if desc == undefined
  desc['enumerable']
end
prototype_toLocaleString(builtin, this) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 175
def prototype_toLocaleString(builtin, this)
  o = to_object(this, builtin)
  toString = o.get('toString')
  unless is_callable(toString)
    raise make_error('TypeError', 'toString of this is not callcable')
  end
  toString.call(this)
end
prototype_toString(builtin, this) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 167
def prototype_toString(builtin, this)
  "[object #{this.js_class}]"
end
prototype_valueOf(builtin, this) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 171
def prototype_valueOf(builtin, this)
  to_object(this, builtin)
end

Private Class Methods

fromPropertyDescriptor(builtin, desc) click to toggle source
# File lib/ruby_run_js/object_methods/js_object.rb, line 210
def fromPropertyDescriptor(builtin, desc)
  return undefined if desc == undefined

  obj = builtin.new_object()
  define = proc do |prop_name|
    obj.define_own_property(prop_name,{
      'value' => desc[prop_name],
      'writable' => true,
      'enumerable' => true,
      'configurable' => true
    },false)
  end
  if is_data_descriptor(desc)
    define.call('value')
    define.call('writable')
  else
    define.call('get')
    define.call('set')
  end
  define.call('enumerable')
  define.call('configurable')
end
toPropertyDescriptor(obj) click to toggle source

@param [JsObject]

# File lib/ruby_run_js/object_methods/js_object.rb, line 234
def toPropertyDescriptor(obj)
  if obj.js_type != :Object
    raise make_error('TypeError', 'Object.toPropertyDescriptor called on non-object')
  end

  desc = {}

  %w(enumerable configurable writable value get set).each do |prop_name|
    if obj.has_property(prop_name)
      value = obj.get(prop_name)
      if %w(enumerable configurable writable).include?(prop_name)
        desc[prop_name] = to_boolean(value)
      elsif prop_name == 'value'
        desc[prop_name] = value
      else
        if value != undefined && !is_callable(value)
          raise make_error('TypeError', "#{prop_name} of the object is not callable")
        else
          desc[prop_name] = value
        end
      end
    end
  end

  if (desc.key?('get') || desc.key?('set')) &&
    (desc.key?('value') || desc.key?('writable'))
    raise make_error('TypeError', 'the property descriptor cannot be data descriptor and accessor descriptor')
  end
  desc
end