Class: RubyRunJs::JsBaseObject

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/ruby_run_js/objects/js_base_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#initializeJsBaseObject

Returns a new instance of JsBaseObject.



10
11
12
13
14
15
16
17
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 10

def initialize
  @prototype = nil
  @extensible = true
  @value = nil
  # @type [Hash]
  @own = {}
  @_class = 'Object'
end

Instance Attribute Details

#_classObject

Returns the value of attribute _class.



6
7
8
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 6

def _class
  @_class
end

#extensibleObject

Returns the value of attribute extensible.



6
7
8
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 6

def extensible
  @extensible
end

#ownObject

Returns the value of attribute own.



6
7
8
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 6

def own
  @own
end

#prototypeObject

Returns the value of attribute prototype.



6
7
8
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 6

def prototype
  @prototype
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 6

def value
  @value
end

Instance Method Details

#_typeObject



19
20
21
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 19

def _type
  'object'
end

#can_put(prop_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 87

def can_put(prop_name)
  prop_desc = get_own_property(prop_name)
  if prop_desc != undefined
    if is_accessor_descriptor(prop_desc)
      return prop_desc['set'] != undefined
    else
      return prop_desc['writable']
    end
  end
  return extensible if prototype.nil?

  inherited = prototype.get_property(prop_name)

  return extensible if inherited == undefined

  return inherited['set'] != undefined if is_accessor_descriptor(inherited)

  return false unless extensible

  inherited['writable']
end

#default_value(hint = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 128

def default_value(hint = nil)
  order = %w[valueOf toString]
  if hint == 'String' || (hint.nil? && _class == 'Date')
    order = %w[toString valueOf]
  end

  order.each do |method_name|
    func = get(method_name)
    if func != undefined && is_callable(func)
      str = func.call(self, [])

      return str if is_primitive(str)
    end
  end

  raise make_error('TypeError', 'Could not find default value')
end

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

Parameters:

  • (String)
  • (Hash)


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 148

def define_own_property(prop_name, prop_desc, throw = false)
  current = get_own_property(prop_name)

  reject = proc {
    if throw
      raise make_error('TypeError', 'Could not define own property')
    end

    return false
  }

  if current == undefined && !extensible
    reject.call
  end
  if current == undefined && extensible
    if is_data_descriptor(prop_desc) || is_generic_descriptor(prop_desc)
      @own[prop_name] = {
        'value' => prop_desc.fetch('value', undefined),
        'writable' => prop_desc.fetch('writable',false),
        'enumerable' => prop_desc.fetch('enumerable',false),
        'configurable' => prop_desc.fetch('configurable',false)
      }
    else
      @own[prop_name] = {
        'get' => prop_desc.fetch('get', undefined),
        'set' => prop_desc.fetch('set', undefined),
        'enumerable' => prop_desc.fetch('enumerable',false),
        'configurable' => prop_desc.fetch('configurable',false)
      }
    end
    return true
  end

  return true if prop_desc.empty? || prop_desc == current

  unless current['configurable']
    if prop_desc['configurable']
      reject.call
    end
    if prop_desc.key?('enumerable') && prop_desc['enumerable'] != current['enumerable']
      reject.call
    end
  end

  if is_generic_descriptor(prop_desc)

  elsif is_data_descriptor(current) != is_data_descriptor(prop_desc)
    unless current['configurable']
      reject.call
    end
    if is_data_descriptor(current)
      current.delete('value')
      current.delete('writable')
      current['set'] = undefined
      current['get'] = undefined
    else
      current.delete('get')
      current.delete('set')
      current['value'] = undefined
      current['writable'] = false
    end
  elsif is_data_descriptor(current) && is_data_descriptor(prop_desc)
    unless current['configurable']
      if !current['writable'] && prop_desc['writable']
        reject.call
      end
      if !current['writable']
        if prop_desc.key?('value') && prop_desc['value'] != current['value']
          reject.call
        end
      end
    end
  elsif is_accessor_descriptor(current) && is_accessor_descriptor(prop_desc)
    unless current['configurable']
      if prop_desc.key?('set') && prop_desc['set'] != current['set']
        reject.call
      end
      if prop_desc.key?('get') && prop_desc['get'] != current['get']
        reject.call
      end
    end
  end
  prop_desc.each do |k, v|
    current[k] = v
  end
  @own[prop_name] = current
  true
end

#delete(prop_name, throw = false) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 113

def delete(prop_name, throw = false)
  desc = get_own_property(prop_name)
  unless desc != undefined
    return true
  end

  if desc['configurable']
    @own.delete(prop_name)
    return true
  elsif throw
    raise make_error('TypeError', 'Could not delete property')
  end
  false
end

#get(prop_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 23

def get(prop_name)
  prop_desc = get_property(prop_name)

  return undefined if prop_desc == undefined

  if is_data_descriptor(prop_desc)
    return prop_desc['value']
  end

  if prop_desc['get'] == undefined
    return undefined
  end

  prop_desc['get'].call(self, [])
end

#get_itemsObject



249
250
251
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 249

def get_items
  (0...to_uint32(get('length'))).map { |i| get(i.to_s) }
end

#get_own_property(prop_name) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 39

def get_own_property(prop_name)
  if @own.key?(prop_name)
    return @own[prop_name].clone()
  end

  undefined
end

#get_property(prop_name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 47

def get_property(prop_name)
  cand = get_own_property(prop_name)
  result = undefined

  if cand != undefined
    result = cand
  elsif @prototype != nil
    result = @prototype.get_property(prop_name)
  end
  result
end

#has_property(prop_name) ⇒ Object



109
110
111
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 109

def has_property(prop_name)
  get_property(prop_name) != undefined
end

#put(prop_name, value, throw = false) ⇒ Object



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
85
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 59

def put(prop_name, value, throw = false)
  unless can_put(prop_name)
    raise make_error('TypeError', 'Could not define own property') if throw

    return
  end

  ownDesc = get_own_property(prop_name)

  if is_data_descriptor(ownDesc)
    valueDesc = { 'value'=> value }
    define_own_property(prop_name, valueDesc, throw)
    return
  end
  desc = get_property(prop_name)
  if is_accessor_descriptor(desc)
    desc['set'].call(self, [value])
  else
    newDesc = {
      'value' => value,
      'writable' => true,
      'enumerable' => true,
      'configurable' => true
    }
    define_own_property(prop_name, newDesc, throw)
  end
end

#set_items(elements) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ruby_run_js/objects/js_base_object.rb', line 237

def set_items(elements)
  elements.each_with_index do |item, index|
    @own[index.to_s] = {
      'value' => item,
      'writable'=> true,
      'enumerable'=> true,
      'configurable'=> true
    }
  end
  @own['length']['value'] = elements.size.to_f
end