Class: Twostroke::Runtime::Types::Array
- Inherits:
-
Object
show all
- Defined in:
- lib/twostroke/runtime/types/array.rb
Instance Attribute Summary collapse
Attributes inherited from Object
#_class, #accessors, #data, #extensible, #properties, #prototype
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Object
#can_put, #construct, #constructing?, #default_value, #define_own_property, #delete!, #generic_items, #get_own_property, #get_property, #has_accessor, #proto_put, set_global_prototype, #typeof
Methods inherited from Value
#has_instance, #typeof
Constructor Details
#initialize(items = []) ⇒ Array
Returns a new instance of Array.
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/twostroke/runtime/types/array.rb', line 18
def initialize(items = [])
@prototype = Array.constructor_function.get("prototype")
super()
@items = items
define_own_property "length", get: ->(this) { Types::Number.new this.items.size }, set: ->(this,val) do
Lib.throw_type_error "Array.prototype.length is not generic" unless this.is_a? Types::Array
len = Types.to_uint32(val)
this.items = this.items[0...len]
end, writable: true, enumerable: false
end
|
Instance Attribute Details
Returns the value of attribute items.
17
18
19
|
# File 'lib/twostroke/runtime/types/array.rb', line 17
def items
@items
end
|
Class Method Details
.constructor_function ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
|
# File 'lib/twostroke/runtime/types/array.rb', line 4
def self.constructor_function
@@constructor_function ||=
Function.new(->(scope, this, args) do
if args.length.zero?
Array.new
elsif args.length == 1
Array.new([nil] * Twostroke::Runtime::Types.to_uint32(args[0]))
else
Array.new args
end
end, nil, "Array", [])
end
|
Instance Method Details
#delete(prop) ⇒ Object
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/twostroke/runtime/types/array.rb', line 67
def delete(prop)
if prop =~ /\A\d+\z/
i = prop.to_i
if i >= 0 && i < items.size && !items[i].nil?
items[i] = nil
return true
end
end
super prop
end
|
#each_enumerable_property(&bk) ⇒ Object
78
79
80
81
|
# File 'lib/twostroke/runtime/types/array.rb', line 78
def each_enumerable_property(&bk)
(0...items.size).map(&:to_s).each &bk
super &bk
end
|
#get(prop, this = self) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/twostroke/runtime/types/array.rb', line 33
def get(prop, this = self)
if prop =~ /\A\d+\z/
items[prop.to_i] || Undefined.new
else
super prop, this
end
end
|
#has_own_property(prop) ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/twostroke/runtime/types/array.rb', line 58
def has_own_property(prop)
if prop =~ /\A\d+\z/
i = prop.to_i
i >= 0 && i < items.size && !items[i].nil?
else
super prop
end
end
|
#has_property(prop) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/twostroke/runtime/types/array.rb', line 49
def has_property(prop)
if prop =~ /\A\d+\z/
i = prop.to_i
i >= 0 && i < items.size && !items[i].nil?
else
super prop
end
end
|
29
30
31
|
# File 'lib/twostroke/runtime/types/array.rb', line 29
def length
items.size
end
|
#put(prop, val, this = self) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/twostroke/runtime/types/array.rb', line 41
def put(prop, val, this = self)
if prop =~ /\A\d+\z/
items[prop.to_i] = val
else
super prop, val, this
end
end
|