Loading lib/jsduck/esprima.rb +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. Loading @@ -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 Loading Loading
lib/jsduck/esprima.rb +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. Loading @@ -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 Loading