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

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

#itemsObject

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_functionObject



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



71
72
73
74
75
76
77
78
79
80
# File 'lib/twostroke/runtime/types/array.rb', line 71

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



82
83
84
85
# File 'lib/twostroke/runtime/types/array.rb', line 82

def each_enumerable_property(&bk)
  (0...items.size).map(&:to_s).each &bk
  super &bk
end

#get(prop, this = self) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/twostroke/runtime/types/array.rb', line 37

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



62
63
64
65
66
67
68
69
# File 'lib/twostroke/runtime/types/array.rb', line 62

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



53
54
55
56
57
58
59
60
# File 'lib/twostroke/runtime/types/array.rb', line 53

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

#lengthObject



33
34
35
# File 'lib/twostroke/runtime/types/array.rb', line 33

def length
  items.size
end

#put(prop, val, this = self) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/twostroke/runtime/types/array.rb', line 45

def put(prop, val, this = self)
  if prop =~ /\A\d+\z/
    items[prop.to_i] = val
  else
    super prop, val, this
  end
end

#to_rubyObject



29
30
31
# File 'lib/twostroke/runtime/types/array.rb', line 29

def to_ruby
  items.map &:to_ruby
end