class Opal::Optimizer

Constants

VERSION

Attributes

ast[RW]
corelib[RW]
corelib_calls[RW]
corelib_source[RW]
exports[RW]
function_calls[RW]
opal_version[RW]

Public Class Methods

new(code, exports: "", force_corelib: true) click to toggle source
# File lib/opal/optimizer.rb, line 17
def initialize(code, exports: "", force_corelib: true)
  @ast = parse_js(code)

  @corelib = @ast.value.find do |i|
    es = code[i.range.from.index..i.range.to.index]
    if es.start_with?("(function(undefined) {\n  // @note\n"\
                      "  //   A few conventions for the documentation of this file:") &&
       es.end_with?("TypeError.$$super = Error;\n}).call(this);")
      @opal_version = 1.0
    elsif es.start_with?("(function(global_object) {\n  \"use strict\";\n\n  // @note\n  "\
                         "//   A few conventions for the documentation of this file:") &&
          es.end_with?("TypeError.$$super = Error;\n}).call(this);")
      @opal_version = 1.1
    end
  end

  raise ArgumentError, "Couldn't deduce Opal version based on this content" if force_corelib && !@corelib

  @corelib_source = @corelib.value.value.value.value.function_body.value if @corelib

  reload

  # Are exports js or do we need to compile them first?
  unless [nil, ""].include?(exports) || exports.start_with?("(function(")
    exports = Opal::Compiler.new(exports).compile
  end
  @exports = Opal::Optimizer.new(exports, exports: nil, force_corelib: false) unless exports == nil
end

Public Instance Methods

optimize() click to toggle source
# File lib/opal/optimizer.rb, line 54
def optimize
  Step::TreeShaking.new(self).run
  Step::CollapseStubs.new(self).run

  @ast.to_ecma
end
reload() click to toggle source
# File lib/opal/optimizer.rb, line 46
def reload
  @function_calls = ast.pointcut(FunctionCallNode).matches
  @corelib_calls = @function_calls.select do |i|
    i.value_path?(DotAccessorNode, ResolveNode, "Opal")
  end.group_by { |i| i.value.accessor }
  @corelib_calls = Hash.new { [] }.merge(@corelib_calls)
end