lib/minjs/ecma262/lit.rb in minjs-0.1.10 vs lib/minjs/ecma262/lit.rb in minjs-0.2.0

- old
+ new

@@ -197,16 +197,25 @@ def ==(obj) self.class == obj.class and @val == obj.val end def to_js(options = {}) - t = "\"" + 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 + 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 @@ -225,11 +234,15 @@ t << "\\x#{"%02x" % c}" else t << [c].pack("U*") end end - t << "\"" + if dq <= sq + t << "\"" + else + t << "\'" + end end def to_ecma262_boolean if @val.length == 0 false @@ -452,11 +465,17 @@ end end class ECMA262Object < Literal include Ctype - private + attr_reader :val + + #val is tupple [[k,v],[k,v],...] + def initialize(val) + @val = val + end + def idname?(name) return false if name.length == 0 s = name.codepoints return false unless identifier_start?(s[0]) s.unshift @@ -464,17 +483,10 @@ return false unless identifier_part?(code) end return true end - public - attr_reader :val - - def initialize(val) - @val = val - end - def deep_dup self.class.new(@val.collect{|x, y| [x.deep_dup, y ? y.deep_dup : y]}) end def traverse(parent, &block) @@ -630,9 +642,27 @@ self.class == obj.class and self.val == obj.val end def to_js(options = {}) val.to_s + end + + def binding_env(type = :var) + return nil if context.nil? + if type == :var + v = context.var_env + else + v = context.lex_env + end + + while v + if v.record.binding[val] + return v + else + v = v.outer + end + end + nil end end ID_THIS = IdentifierName.get(nil, :this) ID_VAR = IdentifierName.get(nil, :var)