Class: RubyRunJs::JsArray

Inherits:
JsBaseObject show all
Defined in:
lib/ruby_run_js/objects/js_array.rb

Instance Attribute Summary

Attributes inherited from JsBaseObject

#_class, #extensible, #own, #prototype, #value

Instance Method Summary collapse

Methods inherited from JsBaseObject

#_type, #can_put, #default_value, #delete, #get, #get_items, #get_own_property, #get_property, #has_property, #put, #set_items

Methods included from Helper

#check_object, #get_member, #get_member_dot, #is_accessor_descriptor, #is_callable, #is_data_descriptor, #is_generic_descriptor, #is_primitive, #make_error, #strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Constructor Details

#initialize(length, prototype) ⇒ JsArray

Returns a new instance of JsArray.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ruby_run_js/objects/js_array.rb', line 7

def initialize(length, prototype)
  super()
  @prototype = prototype
  @own['length'] = {
    'value' => length.to_f,
    'writable' => true,
    'enumerable' => false,
    'configurable' => false
  }
  @_class = 'Array'
end

Instance Method Details

#define_own_property(prop_name, prop_desc, throw = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_run_js/objects/js_array.rb', line 19

def define_own_property(prop_name, prop_desc, throw = false)
  reject = proc {
    if throw
      raise make_error('TypeError', 'Could not define own property for array')
    end
    return false
  }
  oldLenDesc = get_own_property('length')
  oldLen = oldLenDesc['value']
  if prop_name == 'length'
    unless prop_desc.key?('value')
      return super
    end
    newLenDesc = prop_desc.clone
    newLen = to_uint32(prop_desc['value'])
    if newLen != to_number(prop_desc['value'])
      raise make_error('RangeError', 'Invalid range')
    end
    newLenDesc['value'] = newLen.to_f
    if newLen >= oldLen
      return super(prop_name, newLenDesc, throw)
    end
    unless oldLenDesc['writable']
      reject.call
    end
    if !newLenDesc.key?('writable') || newLenDesc['writable']
      newWritable = true
    else
      newWritable = false
      newLenDesc['writable'] = true
    end
    succeeded = super('length', newLenDesc, throw)
    return false unless succeeded
    while newLen < oldLen
      oldLen -= 1
      deleteSucceeded = delete(to_string(oldLen),false)
      unless deleteSucceeded
        newLenDesc['value'] = oldLen + 1
        unless newWritable
          newLenDesc['writable'] = false
        end
        super('length',newLenDesc,false)
        reject.call
      end
    end
    unless newWritable
      super('length',{'writable' => false}, false)
    end
    return true
  elsif prop_name == to_string(to_uint32(prop_name)) && to_uint32(prop_name) != 2**32 - 1
    index = to_uint32(prop_name)
    if index >= oldLen && !oldLenDesc['writable']
      reject.call
    end
    unless super(prop_name, prop_desc, false)
      reject.call
    end
    if index >= oldLen
      oldLenDesc['value'] = (index + 1).to_f
      super('length', oldLenDesc, false)
    end
    return true
  else
    return super
  end
end