Class: Minjs::ECMA262::ECMA262String

Inherits:
Literal show all
Includes:
Ctype
Defined in:
lib/minjs/ecma262/lit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ctype

#decimal_digit?, #hex_digit?, #identifier_part?, #identifier_start?, #idname?, #line_terminator?, #octal_digit?, #white_space?

Methods inherited from Literal

#lt?, #priority, #to_exp?, #ws?

Methods inherited from Base

#add_remove_paren, #concat, #replace, #to_s

Constructor Details

#initialize(val) ⇒ ECMA262String



251
252
253
# File 'lib/minjs/ecma262/lit.rb', line 251

def initialize(val)
  @val = val
end

Instance Attribute Details

#valObject (readonly)

Returns the value of attribute val.



249
250
251
# File 'lib/minjs/ecma262/lit.rb', line 249

def val
  @val
end

Instance Method Details

#==(obj) ⇒ Object



263
264
265
# File 'lib/minjs/ecma262/lit.rb', line 263

def ==(obj)
  self.class == obj.class and @val == obj.val
end

#deep_dupObject



255
256
257
# File 'lib/minjs/ecma262/lit.rb', line 255

def deep_dup
  self.class.new(@val)
end

#ecma262_typeofObject



428
429
430
# File 'lib/minjs/ecma262/lit.rb', line 428

def ecma262_typeof
  :string
end

#left_hand_side_exp?Boolean



312
313
314
# File 'lib/minjs/ecma262/lit.rb', line 312

def left_hand_side_exp?
  true
end

#to_ecma262_booleanObject



316
317
318
319
320
321
322
# File 'lib/minjs/ecma262/lit.rb', line 316

def to_ecma262_boolean
  if @val.length == 0
    false
  else
    true
  end
end

#to_ecma262_numberObject

9.3.1 ToNumber Applied to the String Type



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/minjs/ecma262/lit.rb', line 328

def to_ecma262_number
  begin
    pos1 = pos0 = pos = 0
    v = @val.codepoints
    while true
      return 0 if v[pos].nil? # ToInteger(empty string) => 0
      if white_space?(v[pos]) or line_terminator?(v[pos])
        pos += 1
      else
        break
      end
    end
    #hex
    if v[pos] == 0x30 and (v[pos+1] == 0x78 || v[pos+1] == 0x58) and hex_digit?(v[pos+2])
      base = 16
      pos += 2
      pos0 = pos
      while true
        break if v[pos].nil?
        if hex_digit?(v[pos])
          pos += 1
        else
          break
        end
      end
    #decimal
    else
      base = 10
      sign = 1
      pos0 = pos
      if v[pos].nil?
        raise :error
      elsif v[pos] == 0x2b #+
        pos += 1
      elsif v[pos] == 0x2d #-
        sign = -1
        pos += 1
      end
      has_decimal = false
      has_exp = false

      while true
        break if v[pos].nil?
        if v[pos] >= 0x30 and v[pos] <= 0x39
          pos += 1
        elsif v[pos] == 0x2e #.
          pos += 1
          has_decimal = true
          break;
        else
          break
        end
      end
      if has_decimal
        while true
          break if v[pos].nil?
          if v[pos] >= 0x30 and v[pos] <= 0x39
            pos += 1
          elsif v[pos] == 0x45 or v[pos] == 0x65 #E/e
            pos += 1
            has_exp = true
            break;
          else
            break
          end
        end
      end
      if has_exp
        if v[pos] == 0x2b #+
          pos += 1
        else v[pos] == 0x2d #-
          pos += 1
        end
        while true
          break if v[pos].nil?
          if v[pos] >= 0x30 and v[pos] <= 0x39
            pos += 1
          else
            break
          end
        end
      end
    end
    pos1 = pos
    while white_space?(v[pos]) or line_terminator?(v[pos])
      raise :error if v[pos].nil?
      pos += 1
    end
    raise :error unless v[pos].nil?
    if base == 16
      ret = v[pos0...pos1].pack("U*").to_i(base)
    else
      ret = v[pos0...pos1].pack("U*").to_f
    end
  rescue => e
    ret = nil #Float::NAN
  end
  ret
end

#to_ecma262_stringObject



324
325
326
# File 'lib/minjs/ecma262/lit.rb', line 324

def to_ecma262_string
  @val.dup
end

#to_js(options = {}) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/minjs/ecma262/lit.rb', line 267

def to_js(options = {})
  dq = @val.to_s.each_codepoint.select{|x| x == 0x22}.length
  sq = @val.to_s.each_codepoint.select{|x| x == 0x27}.length
  if dq <= sq
    t = "\""
  else
    t = "\'"
  end

  @val.to_s.each_codepoint do |c|
    if c == 0x5c
      t << ('\\\\')
    elsif c == 0x22 and dq <= sq
      t << ('\"')
    elsif c == 0x27 and dq > sq
      t << ('\\\'')
    elsif c >= 0x20 and c <= 0x7f
      t << ("%c" % c)
    elsif c == 8
      t << '\\b'
    elsif c == 9
      t << '\\t'
    elsif c == 0xa
      t << '\\n'
    elsif c == 0xb
      t << '\\v'
    elsif c == 0xc
      t << '\\v'
    elsif c == 0xd
      t << '\\r'
    elsif c == 0
      t << '\\0'
    elsif c < 0x20
      t << "\\x#{"%02x" % c}"
    else
      t << [c].pack("U*")
    end
  end
  if dq <= sq
    t << "\""
  else
    t << "\'"
  end
end

#traverse(parent) {|_self, parent| ... } ⇒ Object

Yields:

  • (_self, parent)

Yield Parameters:



259
260
261
# File 'lib/minjs/ecma262/lit.rb', line 259

def traverse(parent)
  yield self, parent
end