Class: Minjs::ECMA262::ECMA262String

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Literal

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

Methods inherited from Base

#concat, #replace, #to_s

Constructor Details

#initialize(val) ⇒ ECMA262String

Returns a new instance of ECMA262String.



185
186
187
# File 'lib/minjs/ecma262/lit.rb', line 185

def initialize(val)
  @val = val
end

Instance Attribute Details

#valObject (readonly)

Returns the value of attribute val.



183
184
185
# File 'lib/minjs/ecma262/lit.rb', line 183

def val
  @val
end

Instance Method Details

#==(obj) ⇒ Object



197
198
199
# File 'lib/minjs/ecma262/lit.rb', line 197

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

#deep_dupObject



189
190
191
# File 'lib/minjs/ecma262/lit.rb', line 189

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

#ecma262_typeofObject



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

def ecma262_typeof
  :string
end

#to_ecma262_booleanObject



246
247
248
249
250
251
252
# File 'lib/minjs/ecma262/lit.rb', line 246

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

#to_js(options = {}) ⇒ Object



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
# File 'lib/minjs/ecma262/lit.rb', line 201

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:



193
194
195
# File 'lib/minjs/ecma262/lit.rb', line 193

def traverse(parent)
  yield self, parent
end