Module: RubyRunJs::JsArrayMethods
- Extended by:
- Helper
- Defined in:
- lib/ruby_run_js/object_methods/js_array.rb
Class Method Summary
collapse
-
._internal_sort(a, b, cmpfn) ⇒ Object
-
._move_item(array, from, to) ⇒ Object
-
._to_array(obj, builtin) ⇒ Object
-
.constructor(builtin, this, *args) ⇒ Object
-
.constructor_isArray(builtin, this, arg) ⇒ Object
-
.constructor_new(builtin, this, *args) ⇒ Object
-
.prototype_concat(builtin, this, *args) ⇒ Object
-
.prototype_every(builtin, this, callbackfn, thisArg) ⇒ Object
-
.prototype_filter(builtin, this, callbackfn, thisArg) ⇒ Object
-
.prototype_forEach(builtin, this, callbackfn, thisArg) ⇒ Object
-
.prototype_indexOf(builtin, this, searchElement, *fromIndex) ⇒ Object
-
.prototype_join(builtin, this, separator) ⇒ Object
-
.prototype_lastIndexOf(builtin, this, searchElement, *fromIndex) ⇒ Object
-
.prototype_map(builtin, this, callbackfn, thisArg) ⇒ Object
-
.prototype_pop(builtin, this) ⇒ Object
-
.prototype_push(builtin, this, *args) ⇒ Object
-
.prototype_reduce(builtin, this, callbackfn, *initialValue) ⇒ Object
-
.prototype_reduceRight(builtin, this, callbackfn, *initialValue) ⇒ Object
-
.prototype_reverse(builtin, this) ⇒ Object
-
.prototype_shift(builtin, this) ⇒ Object
-
.prototype_slice(builtin, this, i_start, i_end) ⇒ Object
-
.prototype_some(builtin, this, callbackfn, thisArg) ⇒ Object
-
.prototype_sort(builtin, this, comparefn) ⇒ Object
-
.prototype_splice(builtin, this, start, deleteCount, *args) ⇒ Object
-
.prototype_toLocaleString(builtin, this) ⇒ Object
-
.prototype_toString(builtin, this) ⇒ Object
-
.prototype_unshift(builtin, this, *args) ⇒ Object
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
#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32
Class Method Details
._internal_sort(a, b, cmpfn) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 180
def _internal_sort(a, b, cmpfn)
if a.nil?
return b.nil? ? 0 : 1
end
if b.nil?
return a.nil? ? 0 : -1
end
if a == undefined
return b == undefined ? 0 : 1
end
if b == undefined
return a == undefined ? 0 : -1
end
if cmpfn != undefined
unless is_callable(cmpfn)
raise make_error('TypeError', 'the compare function used by Array.sort is not callable')
end
return cmpfn.call(undefined, [a, b])
end
to_string(a) <=> to_string(b)
end
|
._move_item(array, from, to) ⇒ Object
59
60
61
62
63
64
65
66
67
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 59
def _move_item(array, from, to)
from_s = from.to_s
to_s = to.to_s
if array.has_property(from_s)
array.put(to_s, array.get(from_s), true)
else
array.delete(to_s, true)
end
end
|
._to_array(obj, builtin) ⇒ Object
69
70
71
72
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 69
def _to_array(obj, builtin)
array = to_object(obj, builtin)
return array, to_uint32(array.get('length'))
end
|
.constructor(builtin, this, *args) ⇒ Object
9
10
11
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 9
def constructor(builtin, this, *args)
constructor_new(builtin, this, *args)
end
|
.constructor_isArray(builtin, this, arg) ⇒ Object
26
27
28
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 26
def constructor_isArray(builtin, this, arg)
arg.js_type == :Object && arg.js_class == 'Array'
end
|
.constructor_new(builtin, this, *args) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 13
def constructor_new(builtin, this, *args)
if args.length == 1 && args[0].js_type == :Number
if to_uint32(args[0]) == args[0]
return builtin.new_array(to_uint32(args[0]))
else
raise make_error('RangeError', 'the length of Array is not integer')
end
end
array = builtin.new_array
array.set_items(args)
array
end
|
.prototype_concat(builtin, this, *args) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 74
def prototype_concat(builtin, this, *args)
array = to_object(this, builtin)
result = []
([array] + args).each do |item|
if item.js_type == :Object && item.js_class == 'Array'
(0...item.get('length')).each do |i|
i_str = i.to_s
if item.has_property(i_str)
result.append(item.get(i_str))
end
end
else
result.append(item)
end
end
res_array = builtin.new_array
res_array.set_items(result)
res_array
end
|
.prototype_every(builtin, this, callbackfn, thisArg) ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 306
def prototype_every(builtin, this, callbackfn, thisArg)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
len.times do |k|
if array.has_property(k.to_s)
unless to_boolean(callbackfn.call(thisArg, [array.get(k.to_s), k.to_f, array]))
return false
end
end
end
true
end
|
.prototype_filter(builtin, this, callbackfn, thisArg) ⇒ Object
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 364
def prototype_filter(builtin, this, callbackfn, thisArg)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
res = []
len.times do |k|
if array.has_property(k.to_s)
kValue = array.get(k.to_s)
if to_boolean(callbackfn.call(thisArg, [kValue, k.to_f, array]))
res << kValue
end
end
end
new_array = builtin.new_array
new_array.set_items(res)
new_array
end
|
.prototype_forEach(builtin, this, callbackfn, thisArg) ⇒ Object
336
337
338
339
340
341
342
343
344
345
346
347
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 336
def prototype_forEach(builtin, this, callbackfn, thisArg)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
len.times do |k|
if array.has_property(k.to_s)
callbackfn.call(thisArg, [array.get(k.to_s), k.to_f, array])
end
end
undefined
end
|
.prototype_indexOf(builtin, this, searchElement, *fromIndex) ⇒ Object
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 263
def prototype_indexOf(builtin, this, searchElement, *fromIndex)
array, len = _to_array(this, builtin)
return -1.0 if len == 0
fromIndex = fromIndex.length > 0 ? to_integer(fromIndex[0]) : 0
if fromIndex >= len
return -1.0
elsif fromIndex >= 0
k = fromIndex
else
k = len + fromIndex
k = k >= 0 ? k : 0
end
while k < len
if array.has_property(k.to_s)
if strict_equality(array.get(k.to_s), searchElement)
return k.to_f
end
end
k += 1
end
-1.0
end
|
.prototype_join(builtin, this, separator) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 94
def prototype_join(builtin, this, separator)
array, len = _to_array(this, builtin)
if separator == undefined
separator = ','
end
sep = to_string(separator)
res = []
array.get_items.each do |item|
res << ((item == undefined || item == null) ? '' : to_string(item))
end
res.join(sep)
end
|
.prototype_lastIndexOf(builtin, this, searchElement, *fromIndex) ⇒ Object
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 286
def prototype_lastIndexOf(builtin, this, searchElement, *fromIndex)
array, len = _to_array(this, builtin)
return -1.0 if len == 0
fromIndex = fromIndex.length > 0 ? to_integer(fromIndex[0]) : len - 1
if fromIndex >= 0
k = [fromIndex, len - 1].min
else
k = len + fromIndex
end
while k >= 0
if array.has_property(k.to_s)
if strict_equality(array.get(k.to_s), searchElement)
return k.to_f
end
end
k -= 1
end
-1.0
end
|
.prototype_map(builtin, this, callbackfn, thisArg) ⇒ Object
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 349
def prototype_map(builtin, this, callbackfn, thisArg)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
new_array = builtin.new_array(len)
len.times do |k|
if array.has_property(k.to_s)
value = callbackfn.call(thisArg, [array.get(k.to_s), k.to_f, array])
new_array.put(k.to_s, value, false)
end
end
new_array
end
|
.prototype_pop(builtin, this) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 107
def prototype_pop(builtin, this)
array, len = _to_array(this, builtin)
if len == 0
array.put('length', 0.0, true)
return undefined
end
len = (len - 1).to_f
index = to_string(len)
element = array.get(index)
array.delete(index, true)
array.put('length', len, true)
element
end
|
.prototype_push(builtin, this, *args) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 121
def prototype_push(builtin, this, *args)
array, len = _to_array(this, builtin)
args.each do |item|
array.put(len.to_s, item, true)
len += 1
end
array.put('length', len.to_f, true)
len.to_f
end
|
.prototype_reduce(builtin, this, callbackfn, *initialValue) ⇒ Object
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 383
def prototype_reduce(builtin, this, callbackfn, *initialValue)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
if len == 0 && initialValue.length == 0
raise make_error('TypeError', 'Reduce of empty array with no initial value')
end
k = 0
if initialValue.length > 0
accumulator = initialValue[0]
else
k_present = false
while k < len && !k_present
if array.has_property(k.to_s)
accumulator = array.get(k.to_s)
else
k_present = true
end
k += 1
end
unless k_present
raise make_error('TypeError', 'Reduce of empty array with no initial value')
end
end
while k < len
if array.has_property(k.to_s)
accumulator = callbackfn.call(undefined, [accumulator, array.get(k.to_s), k.to_f, array])
end
k += 1
end
accumulator
end
|
.prototype_reduceRight(builtin, this, callbackfn, *initialValue) ⇒ Object
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 419
def prototype_reduceRight(builtin, this, callbackfn, *initialValue)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
if len == 0 && initialValue.length == 0
raise make_error('TypeError', 'Reduce of empty array with no initial value')
end
k = len - 1
if initialValue.length > 0
accumulator = initialValue[0]
else
k_present = false
while k >= 0 && !k_present
if array.has_property(k.to_s)
accumulator = array.get(k.to_s)
else
k_present = true
end
k -= 1
end
unless k_present
raise make_error('TypeError', 'Reduce of empty array with no initial value')
end
end
while k >= 0
if array.has_property(k.to_s)
accumulator = callbackfn.call(undefined, [accumulator, array.get(k.to_s), k.to_f, array])
end
k -= 1
end
accumulator
end
|
.prototype_reverse(builtin, this) ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 131
def prototype_reverse(builtin, this)
array = to_object(this, builtin)
items = array.get_items
items.reverse!
len = to_uint32(array.get('length'))
has_props = len.times.map { |i| array.has_property(i.to_s) }
has_props.reverse!
items.each_index do |index|
if has_props[index]
array.put(index.to_s, items[index], true)
else
array.delete(index.to_s)
end
end
array
end
|
.prototype_shift(builtin, this) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 148
def prototype_shift(builtin, this)
array, len = _to_array(this, builtin)
if len == 0
array.put('length', 0.0, true)
return undefined
end
first = array.get('0')
(1...len).each do |k|
_move_item(array, k, k - 1)
end
array.delete((len-1).to_s, true)
array.put('length', (len-1).to_f, true)
first
end
|
.prototype_slice(builtin, this, i_start, i_end) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 163
def prototype_slice(builtin, this, i_start, i_end)
array, len = _to_array(this, builtin)
relativeStart = to_integer(i_start)
k = relativeStart < 0 ? [0, len + relativeStart].max : [len, relativeStart].min
relativeEnd = i_end == undefined ? len : to_integer(i_end)
final = relativeEnd < 0 ? [len + relativeEnd,0].max : [len, relativeEnd].min
res = []
(k...final).each do |i|
if array.has_property(i.to_s)
res << array.get(i.to_s)
end
end
result = builtin.new_array
result.set_items(res)
result
end
|
.prototype_some(builtin, this, callbackfn, thisArg) ⇒ Object
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 321
def prototype_some(builtin, this, callbackfn, thisArg)
array, len = _to_array(this, builtin)
unless is_callable(callbackfn)
raise make_error('TypeError', 'callbackfn must be a function')
end
len.times do |k|
if array.has_property(k.to_s)
if to_boolean(callbackfn.call(thisArg, [array.get(k.to_s), k.to_f, array]))
return true
end
end
end
false
end
|
.prototype_sort(builtin, this, comparefn) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 202
def prototype_sort(builtin, this, comparefn)
obj, len = _to_array(this, builtin)
items = len.times.map { |i| obj.has_property(i.to_s) ? obj.get(i.to_s) : nil }
items.sort! { |a, b| _internal_sort(a, b, comparefn) }
len.times do |i|
if items[i].nil?
obj.delete(i.to_s, true)
else
obj.put(i.to_s, items[i], true)
end
end
obj
end
|
.prototype_splice(builtin, this, start, deleteCount, *args) ⇒ Object
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 216
def prototype_splice(builtin, this, start, deleteCount, *args)
obj, len = _to_array(this, builtin)
relativeStart = to_integer(start)
actualStart = relativeStart < 0 ? [0, len + relativeStart].max : [len, relativeStart].min
actualDeleteCount = [[to_integer(deleteCount), 0].max, len - actualStart].min
k = 0
new_array = builtin.new_array
while k < actualDeleteCount
from = (actualStart + k).to_s
if obj.has_property(from)
new_array.put(k.to_s, obj.get(from))
end
k += 1
end
if args.length < actualDeleteCount
k = actualStart
while k < len - actualDeleteCount
_move_item(obj, k + actualDeleteCount, k + args.length)
k += 1
end
elsif args.length > actualDeleteCount
k = len - actualDeleteCount
while k > actualStart
_move_item(obj, k + actualDeleteCount - 1, k + args.length - 1)
k -= 1
end
end
k = actualStart
args.each do |item|
obj.put(k.to_s, item, true)
k += 1
end
obj.put('length', (len - actualDeleteCount + args.length).to_f, true)
new_array
end
|
.prototype_toLocaleString(builtin, this) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 39
def prototype_toLocaleString(builtin, this)
array, len = _to_array(this, builtin)
return '' if len == 0
res = []
len.times do |i|
item = array.get(i.to_s)
if item == undefined || item == null
res << ''
else
item_obj = to_object(item, builtin)
str_func = item_obj.get('toLocaleString')
unless is_callable(str_func)
raise make_error('TypeError', "toLocaleString method of item at index #{i} is not callable")
end
res << to_string(str_func.call(item_obj, []))
end
end
res.join(',')
end
|
.prototype_toString(builtin, this) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 30
def prototype_toString(builtin, this)
array = to_object(this, builtin)
func = array.get('join')
unless is_callable(func)
func = builtin.object_prototype.get('toString')
end
func.call(this, [])
end
|
.prototype_unshift(builtin, this, *args) ⇒ Object
254
255
256
257
258
259
260
261
|
# File 'lib/ruby_run_js/object_methods/js_array.rb', line 254
def prototype_unshift(builtin, this, *args)
array, len = _to_array(this, builtin)
arg_count = args.length
len.downto(1) { |k| _move_item(array, k - 1, k + arg_count - 1) }
args.each_index { |i| array.put(i.to_s, args[i], true) }
array.put('length', len + arg_count, true)
(len + arg_count).to_f
end
|