Class: Twostroke::Runtime::Types::RegExp

Inherits:
Object
  • Object
show all
Defined in:
lib/twostroke/runtime/types/regexp.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!, #each_enumerable_property, #generic_items, #get, #get_own_property, #get_property, #has_accessor, #has_own_property, #has_property, #proto_put, #put, set_global_prototype, #typeof

Methods inherited from Value

#has_instance, #typeof

Constructor Details

#initialize(regexp_source, options) ⇒ RegExp

Returns a new instance of RegExp.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/twostroke/runtime/types/regexp.rb', line 12

def initialize(regexp_source, options)
  opts = 0
  (options ||= "").each_char do |opt|
    opts |= case opt
    when "m"; Regexp::MULTILINE
    when "i"; Regexp::IGNORECASE
    else; 0
    end
  end
  @regexp = Regexp.new regexp_source, opts
  @global = options.include? "g"
  @prototype = RegExp.constructor_function.get("prototype")
  super()
end

Instance Attribute Details

#globalObject (readonly)

Returns the value of attribute global.



11
12
13
# File 'lib/twostroke/runtime/types/regexp.rb', line 11

def global
  @global
end

#regexpObject (readonly)

Returns the value of attribute regexp.



10
11
12
# File 'lib/twostroke/runtime/types/regexp.rb', line 10

def regexp
  @regexp
end

Class Method Details

.constructor_functionObject



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

def self.constructor_function
  @@constructor_function ||=
    Function.new(->(scope, this, args) {
      RegExp.new Twostroke::Runtime::Types.to_string(args[0] || Undefined.new).string, args[1] && Twostroke::Runtime::Types.to_string(args[1]).string
    }, nil, "RegExp", [])
end

Instance Method Details

#primitive_valueObject



27
28
29
# File 'lib/twostroke/runtime/types/regexp.rb', line 27

def primitive_value
  String.new(regexp.inspect + (@global ? "g" : ""))
end