Sha256: 1bb04fb837120e5ff8d5ec86cb8d74805993068b916028b73b924276043e1156

Contents?: true

Size: 1.04 KB

Versions: 6

Compression:

Stored size: 1.04 KB

Contents

require 'v8'
require 'jsduck/util/json'
require 'jsduck/util/singleton'

module JsDuck

  # Runs Esprima.js through V8.
  #
  # Initialized as singleton to avoid loading the esprima.js more
  # than once - otherwise performace will severely suffer.
  class Esprima
    include Util::Singleton

    def initialize
      @v8 = V8::Context.new
      esprima = File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))+"/esprima/esprima.js";

      # Esprima attempts to assign to window.esprima, but our v8
      # engine has no global window variable defined.  So define our
      # own and then grab esprima out from it again.
      @v8.eval("var window = {};")
      @v8.load(esprima)
      @v8.eval("var esprima = window.esprima;")
    end

    # Parses JavaScript source code using Esprima.js
    #
    # Returns the resulting AST
    def parse(input)
      @v8['js'] = input
      json = @v8.eval("JSON.stringify(esprima.parse(js, {comment: true, range: true, raw: true}))")
      return Util::Json.parse(json, :max_nesting => false)
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jsduck-4.3.2 lib/jsduck/esprima.rb
jsduck-4.3.1 lib/jsduck/esprima.rb
jsduck-4.3.0 lib/jsduck/esprima.rb
jsduck-4.2.1 lib/jsduck/esprima.rb
jsduck-4.2.0 lib/jsduck/esprima.rb
jsduck-4.1.1 lib/jsduck/esprima.rb