Module: RubyRunJs::JsRegExpMethods

Extended by:
Helper
Defined in:
lib/ruby_run_js/object_methods/js_regexp.rb

Class Method Summary collapse

Methods included from Helper

check_object, get_member, get_member_dot, is_accessor_descriptor, is_callable, is_data_descriptor, is_generic_descriptor, is_primitive, make_error, strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Class Method Details

.check_regexp(obj) ⇒ Object



79
80
81
82
83
# File 'lib/ruby_run_js/object_methods/js_regexp.rb', line 79

def check_regexp(obj)
  unless this.js_type == :Object && this.js_class == 'RegExp'
    raise make_error('TypeError', 'Called on non-regexp object')
  end
end

.constructor(builtin, this, pattern, flags) ⇒ Object



10
11
12
13
14
15
# File 'lib/ruby_run_js/object_methods/js_regexp.rb', line 10

def constructor(builtin, this, pattern, flags)
  if pattern.js_class == 'RegExp' && flags == undefined
    return pattern
  end
  constructor_new(builtin, this, pattern, flags)
end

.constructor_new(builtin, this, pattern, flags) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_run_js/object_methods/js_regexp.rb', line 17

def constructor_new(builtin, this, pattern, flags)
  if pattern.js_class == 'RegExp' && flags == undefined
    return pattern
  elsif pattern.js_class == 'RegExp' && flags != undefined
    raise make_error('TypeError', 'cannot construct RegExp with RegExp and flags')
  else
    pattern = pattern == undefined ? '' : to_string(pattern)
    flags = flags == undefined ? '' : to_string(flags)
  end

  flags.each_char do |c| 
    unless 'gim'.include?(c)
      raise make_error('SyntaxError', "Invalid flags supplied to RegExp constructor #{flags}")
    end
  end

  if flags.chars.to_set.length != flags.length
    raise make_error('SyntaxError', "Invalid flags supplied to RegExp constructor #{flags}")
  end

  builtin.new_regexp(pattern, flags)
end

.prototype_exec(builtin, this, string) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_run_js/object_methods/js_regexp.rb', line 40

def prototype_exec(builtin, this, string)
  check_regexp(this)
  str = to_string(string)
  length = str.length
  last_index = this.get('lastIndex')
  i = to_integer(last_index)
  global = this.get('global')
  unless global
    i = 0
  end
  match_succeeded = false
  while !match_succeeded
    if i < 0 || i > length
      this.put('lastIndex', 0.0, true)
      return null
    end
    match_data = this.pattern.match(str, i)
    if match_data.nil?
      i += 1
    else
      match_succeeded = true
    end
  end
  e = match_data.end(0)
  if global
    this.put('lastIndex', e, true)
  end
  captures = match_data.captures
  result = builtin.new_array_with_items(captures)
  result.put('index', match_data.begin(0).to_f)
  result.put('input', str)
  result
end

.prototype_test(builtin, this, string) ⇒ Object



74
75
76
77
# File 'lib/ruby_run_js/object_methods/js_regexp.rb', line 74

def prototype_test(builtin, this, string)
  result = prototype_exec(builtin, this, string)
  result != null
end

.prototype_toString(builtin, this) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ruby_run_js/object_methods/js_regexp.rb', line 85

def prototype_toString(builtin, this)
  check_regexp(this)
  flags = ''
  if this.flag_global
    flags += 'g'
  end
  if this.flag_ignore_case
    flags += 'i'
  end
  if this.flag_multiline
    flags += 'm'
  end
  v = this.body == '' ? '(?:)' : this.body
  "/#{v}/#{flags}"
end