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, #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, #delete!, #generic_items, #get_own_property, #get_property, #has_accessor, #proto_put, #put, set_global_prototype, #typeof

Methods inherited from Value

#has_instance

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



49
50
51
52
# File 'lib/twostroke/runtime/types/string_object.rb', line 49

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
27
28
29
# File 'lib/twostroke/runtime/types/string_object.rb', line 20

def get(prop, this = self)
  if prop =~ /\A\d+\z/
    idx = prop.to_i
    unless idx < 0 or idx >= string.length
      String.new string[idx]
    end
  else
    super prop, this
  end
end

#has_own_property(prop) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/twostroke/runtime/types/string_object.rb', line 40

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



31
32
33
34
35
36
37
38
# File 'lib/twostroke/runtime/types/string_object.rb', line 31

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

#to_rubyObject



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

def to_ruby
  string
end