Module: RubyRunJs::JsStringMethods

Extended by:
Helper
Defined in:
lib/ruby_run_js/object_methods/js_string.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

._replace_template(matched_string, offset_start, offset_end, captures, replace_value, whole_string) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 140

def _replace_template(matched_string, offset_start, offset_end, captures, replace_value, whole_string)
  result = ''
  i = 0
  while i < replace_value.length - 1
    if replace_value[i] == '$'
      if replace_value[i + 1] == '$'
        result += '$'
        i += 2
        next
      elsif replace_value[i + 1] == '&'
        result += matched_string
        i += 2
        next
      elsif replace_value[i + 1] == '`'
        result += whole_string[0, offset_start]
        i += 2
        next
      elsif replace_value[i + 1] == '\''
        result += whole_string[offset_end, whole_string.length]
        i += 2
        next
      elsif replace_value[i + 1] =~ /[0-9]/
        digit = replace_value[i + 1]
        if i + 2 < replace_value.length && replace_value[i + 2] =~ /[0-9]/
          digit += replace_value[i + 2]
        end
        num = digit.to_i
        if num == 0 || num >= captures.length
          result += '$' + digit
        else
          result += captures[num - 1]
        end
        i += 1 + digit.length
        next
      end
    end
    result += replace_value[i]
    i += 1
  end
  if i < replace_value.length
    result += replace_value[-1]
  end
  result
end

._split_match(str, q, regexp) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 270

def _split_match(str, q, regexp)
  if regexp.js_class == 'RegExp'
    match_data = regexp.pattern.match(str, q)
    if match_data
      return [match_data.end(0), match_data.captures]
    else
      return nil
    end
  end
  # regexp must be string
  r = regexp.length
  s = str.length
  if str[q..].start_with?(regexp)
    return [q + r, []]
  end
  return nil
end

.constructor(builtin, this, *args) ⇒ Object



9
10
11
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 9

def constructor(builtin, this, *args)
  args.length > 0 ? to_string(args[0]) : ''
end

.constructor_fromCharCode(builtin, this, *chars) ⇒ Object



17
18
19
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 17

def constructor_fromCharCode(builtin, this, *chars)
  chars.map { |char| to_uint16(char).chr(Encoding::UTF_8) }.join('')
end

.constructor_new(builtin, this, *args) ⇒ Object



13
14
15
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 13

def constructor_new(builtin, this, *args)
  builtin.new_string(args.length > 0 ? to_string(args[0]) : '')
end

.prototype_charAt(builtin, this, pos) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 39

def prototype_charAt(builtin, this, pos)
  check_object(this)
  str = to_string(this)
  pos = to_integer(pos)
  size = str.length
  if pos < 0 || pos >= size
    return ''
  end
  str[pos]
end

.prototype_charCodeAt(builtin, this, pos) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 50

def prototype_charCodeAt(builtin, this, pos)
  check_object(this)
  str = to_string(this)
  pos = to_integer(pos)
  size = str.length
  if pos < 0 || pos >= size
    return Float::NAN
  end
  str[pos].ord.to_f
end

.prototype_concat(builtin, this, *strings) ⇒ Object



61
62
63
64
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 61

def prototype_concat(builtin, this, *strings)
  check_object(this)
  to_string(this) + strings.map { |s| to_string(s) }.join('')
end

.prototype_indexOf(builtin, this, searchString, position) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 66

def prototype_indexOf(builtin, this, searchString, position)
  check_object(this)
  str = to_string(this)
  search_str = to_string(searchString)
  pos = position == undefined ? 0 : to_integer(position)
  len = str.length
  start = [[0, pos].max, len].min
  r = str.index(search_str, start)
  r.nil? ? -1.0 : r.to_f
end

.prototype_lastIndexOf(builtin, this, searchString, position) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 77

def prototype_lastIndexOf(builtin, this, searchString, position)
  check_object(this)
  str = to_string(this)
  search_str = to_string(searchString)
  len = str.length
  pos = position == undefined ? Float::NAN : to_number(position)
  pos = pos.nan? ? len : to_integer(pos)
  start = [[0, pos].max, len].min
  r = str.rindex(search_str, start)
  r.nil? ? -1.0 : r.to_f
end

.prototype_localeCompare(builtin, this, that) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 89

def prototype_localeCompare(builtin, this, that)
  check_object(this)
  str = to_string(this)
  that = to_string(that)
  if str > that
    return 1.0
  elsif str < that
    return -1.0
  else
    return 0.0
  end
end

.prototype_match(builtin, this, regexp) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 102

def prototype_match(builtin, this, regexp)
  check_object(this)
  str = to_string(this)
  rx = regexp.js_class == 'RegExp' ? regexp : builtin.new_regexp(str, '')
  exec_func = rx.prototype.get('exec')
  unless rx.get('global')
    return exec_func.call(rx, [str])
  end
  rx.put('lastIndex', 0.0)
  results = []
  previous_last_index = 0
  n = 0
  last_match = true
  while last_match
    result = exec_func.call(rx, [str])
    if result == null 
      last_match = false
    else
      this_index = rx.get('lastIndex')
      if this_index == previous_last_index
        rx.put('lastIndex', this_index + 1)
        previous_last_index = this_index + 1
      else
        previous_last_index = this_index
      end
      match_str = result.get('0')
      results << match_str
    end
  end

  if results.empty?
    return null
  end
  js_result = builtin.new_array()
  js_result.set_items(results)
  js_result
end

.prototype_replace(builtin, this, searchValue, replaceValue) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 185

def prototype_replace(builtin, this, searchValue, replaceValue)
  check_object(this)
  str = to_string(this)

  result = ''

  is_func = true
  unless is_callable(replaceValue)
    replaceValue = to_string(replaceValue)
    is_func = false
  end

  if searchValue.js_class == 'RegExp'
    if searchValue.get('global')
      last_index = 0
      match_data = searchValue.pattern.match(str, 0)
      while match_data
        result += str[last_index, match_data.begin(0) - last_index]

        if is_func
          args = [match_data[0]]
          args += match_data.captures
          args << match_data.begin(0)
          args << str
          result += to_string(replaceValue.call(this, args))
        else
          result += _replace_template(match_data[0], match_data.begin(0), match_data.end(0), match_data.captures, replaceValue, str)
        end

        last_index = match_data.end(0)
        match_data = searchValue.pattern.match(str, last_index)
      end
      result += str[last_index..]
      return result
    else
      match_data = searchValue.pattern.match(str)
      if match_data.nil?
        return str
      end
      captures = match_data.captures
      offset_start = match_data.begin(0)
      offset_end = match_data.end(0)
      matched_string = match_data[0]
    end
  else
    matched_string = to_string(searchValue)
    index = str.index(matched_string)
    if index.nil?
      return str
    end
    offset_start = index
    offset_end = index + matched_string.length
    captures = []
  end
  result = str[0,offset_start]
  if is_func
    args = [matched_string] + captures + [offset_start, str]
    result += to_string(replaceValue.call(this, args))
  else
    result += _replace_template(matched_string, offset_start, offset_end, captures, replaceValue, str)
  end
  result += str[offset_end..]
  result
end

.prototype_search(builtin, this, regexp) ⇒ Object



250
251
252
253
254
255
256
257
258
259
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 250

def prototype_search(builtin, this, regexp)
  check_object(this)
  str = to_string(this)
  regexp = regexp.js_class == 'RegExp' ? regexp : builtin.new_regexp(regexp, '')
  match_data = regexp.pattern.match(str)
  if match_data
    return match_data.begin(0).to_f
  end
  return -1.0
end

.prototype_slice(builtin, this, i_start, i_end) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 261

def prototype_slice(builtin, this, i_start, i_end)
  check_object(this)
  str = to_string(this)
  start = to_integer(i_start)
  length = str.length
  i_end = i_end == undefined ? length : to_integer(i_end)
  str[start...i_end]
end

.prototype_split(builtin, this, separator, limit) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 288

def prototype_split(builtin, this, separator, limit)
  check_object(this)
  str = to_string(this)
  results = []
  lim = limit == undefined ? (2**32 - 1) : to_uint32(limit)
  length = str.length
  sep = separator.js_class == 'RegExp' ? separator : to_string(separator)
  return result if lim == 0
  if separator == undefined
    results << str
  elsif length == 0
    z = _split_match(str, 0, sep)
    if z.nil?
      results << str
    end
    return builtin.new_array_with_items(results)
  end
  i = 0
  q = i
  while q != length
    z = _split_match(str, q, sep)
    if z.nil?
      q += 1
    else
      end_index = z[0]
      captures = z[1]
      if end_index == i
        q += 1
      else
        results << str[i...q]
        if results.length == lim
          return builtin.new_array_with_items(results)
        end
        i = end_index
        captures.each do |cap|
          results << cap
          if results.length == lim
            return builtin.new_array_with_items(results)
          end
        end
        q = i
      end
    end
  end
  results << str[q..]
  builtin.new_array_with_items(results)
end

.prototype_substring(builtin, this, i_start, i_end) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 336

def prototype_substring(builtin, this, i_start, i_end)
  check_object(this)
  str = to_string(this)
  length = str.length
  i_start = to_integer(i_start)
  i_end = i_end == undefined ? length : to_integer(i_end)
  f_start = [[i_start, 0].max, length].min
  f_end = [[i_end, 0].max, length].min
  from = [f_start, f_end].min
  to = [f_start, f_end].max
  str[from...to]
end

.prototype_toLocaleLowerCase(builtin, this) ⇒ Object



355
356
357
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 355

def prototype_toLocaleLowerCase(builtin, this)
  prototype_toLowerCase(builtin, this)
end

.prototype_toLocaleUpperCase(builtin, this) ⇒ Object



365
366
367
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 365

def prototype_toLocaleUpperCase(builtin, this)
  prototype_toUpperCase(builtin, this)
end

.prototype_toLowerCase(builtin, this) ⇒ Object



349
350
351
352
353
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 349

def prototype_toLowerCase(builtin, this)
  check_object(this)
  str = to_string(this)
  str.downcase
end

.prototype_toString(builtin, this) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 21

def prototype_toString(builtin, this)
  unless this.js_type == :String || this.js_type == :Object && this.js_class == 'String'
    raise make_error('TypeError', 'String.prototype.toString is not generic')
  end

  return this if this.js_type == :String
  return this.value
end

.prototype_toUpperCase(builtin, this) ⇒ Object



359
360
361
362
363
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 359

def prototype_toUpperCase(builtin, this)
  check_object(this)
  str = to_string(this)
  str.upcase
end

.prototype_trim(builtin, this) ⇒ Object



369
370
371
372
373
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 369

def prototype_trim(builtin, this)
  check_object(this)
  str = to_string(this)
  str.strip
end

.prototype_valueOf(builtin, this) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/ruby_run_js/object_methods/js_string.rb', line 30

def prototype_valueOf(builtin, this)
  unless this.js_type == :String || this.js_type == :Object && this.js_class == 'String'
    raise make_error('TypeError', 'String.prototype.valueOf is not generic')
  end

  return this if this.js_type == :String
  return this.value
end