Class: Twostroke::Runtime::Types::StringObject

Inherits:
Object
  • Object
show all
Defined in:
lib/twostroke/runtime/types/string_object.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#_class, #data, #extensible, #prototype

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#can_put, #construct, #constructing?, #default_value, #define_own_property, #delete, #delete!, #generic_items, #get_own_property, #get_property, #has_accessor, #proto_put, #put, set_global_prototype, #typeof

Methods inherited from Value

#has_instance, #typeof

Constructor Details

#initialize(string) ⇒ StringObject

Returns a new instance of StringObject.



10
11
12
13
14
# File 'lib/twostroke/runtime/types/string_object.rb', line 10

def initialize(string)
  @prototype = StringObject.constructor_function.get("prototype")
  @string = string
  super()
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



9
10
11
# File 'lib/twostroke/runtime/types/string_object.rb', line 9

def string
  @string
end

Class Method Details

.constructor_functionObject



4
5
6
7
# File 'lib/twostroke/runtime/types/string_object.rb', line 4

def self.constructor_function
  @@constructor_function ||=
    Function.new(->(scope, this, args) { this.constructing? ? Types.to_object(Types.to_string(args[0] || Undefined.new)) : Types.to_string(args[0]) }, nil, "String", [])
end

Instance Method Details

#each_enumerable_property(&bk) ⇒ Object



46
47
48
49
# File 'lib/twostroke/runtime/types/string_object.rb', line 46

def each_enumerable_property(&bk)
  (0...string.length).map(&:to_s).each &bk
  super &bk
end

#get(prop, this = self) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/twostroke/runtime/types/string_object.rb', line 20

def get(prop, this = self)
  if prop =~ /\A\d+\z/
    String.new string[prop.to_i]
  else
    super prop, this
  end
end

#has_own_property(prop) ⇒ Object



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

def has_own_property(prop)
  if prop =~ /\A\d+\z/
    i = prop.to_i
    i >= 0 && i < string.size
  else
    super prop
  end
end

#has_property(prop) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/twostroke/runtime/types/string_object.rb', line 28

def has_property(prop)
  if prop =~ /\A\d+\z/
    i = prop.to_i
    i >= 0 && i < string.size
  else
    super prop
  end
end

#primitive_valueObject



16
17
18
# File 'lib/twostroke/runtime/types/string_object.rb', line 16

def primitive_value
  String.new string
end