Commit b49e003b authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Switch back from using ExecJS to using v8 directly.

As we no more support JRuby, we can require therubyracer as a dependency.

This reverts commit aba71287.
parent b18415e8
Loading
Loading
Loading
Loading
+7 −9
Original line number Diff line number Diff line
require 'execjs'
require 'v8'
require 'json'
require 'singleton'

module JsDuck

  # Runs Esprima.js through execjs (this will select any available
  # JavaScript runtime - preferably therubyracer on MRI and JScript
  # on Windows).
  # Runs Esprima.js through V8.
  #
  # Initialized as singleton to avoid loading the esprima.js more
  # than once - otherwise performace will severely suffer.
@@ -14,17 +12,17 @@ module JsDuck
    include Singleton

    def initialize
      esprima_path = File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))+"/esprima/esprima.js";
      esprima = IO.read(esprima_path)
      helper = "function runEsprima(js) { return JSON.stringify(esprima.parse(js, {comment: true, range: true, raw: true})); }"
      @context = ExecJS.compile(esprima + "\n\n" + helper)
      @v8 = V8::Context.new
      esprima = File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__))))+"/esprima/esprima.js";
      @v8.load(esprima)
    end

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