module Opal

Opal is a ruby to javascript compiler, with a runtime for running in any JavaScript environment.

rubocop:disable Layout/EmptyLineBetweenDefs, Style/SingleLineMethods

Constants

CONST_NAME_REGEXP
FORBIDDEN_CONST_NAME_CHARS
FORBIDDEN_ENDING_IDENTIFIER_CHARS
FORBIDDEN_STARTING_IDENTIFIER_CHARS
INLINE_IDENTIFIER_REGEXP
REGEXP_END
REGEXP_START
Server
VERSION

WHEN RELEASING: Remember to update RUBY_ENGINE_VERSION in opal/corelib/constants.rb too!

Attributes

builder_scheduler[RW]
cache[W]

Public Class Methods

add_opal_location_to_error(opal_location, error) click to toggle source
# File lib/opal/errors.rb, line 41
def self.add_opal_location_to_error(opal_location, error)
  backtrace = error.backtrace.to_a
  backtrace.unshift opal_location.to_s
  error.set_backtrace backtrace
  error
end
append_path(path) click to toggle source

Add a file path to opals load path. Any gem containing ruby code that Opal has access to should add a load path through this method. Load paths added here should only be paths which contain code targeted at being compiled by Opal.

# File lib/opal/paths.rb, line 23
def self.append_path(path)
  append_paths(path)
end
append_paths(*paths) click to toggle source

Same as append_path but can take multiple paths.

# File lib/opal/paths.rb, line 28
def self.append_paths(*paths)
  @paths.concat(paths)
  nil
end
cache() click to toggle source
# File lib/opal/cache.rb, line 15
def self.cache
  @cache ||=
    if RUBY_ENGINE == 'opal' || ENV['OPAL_CACHE_DISABLE'] || !Cache::FileCache.find_dir
      Cache::NullCache.new
    else
      Cache::FileCache.new
    end
end
compile(source, options = {}) click to toggle source

Compile a string of ruby code into javascript.

@example

Opal.compile "ruby_code"
# => "string of javascript code"

@see Opal::Compiler.new for compiler options

@param source [String] ruby source @param options [Hash] compiler options @return [String] javascript code

# File lib/opal/compiler.rb, line 30
def self.compile(source, options = {})
  Compiler.new(source, options).compile
end
core_dir() click to toggle source
# File lib/opal/paths.rb, line 10
def self.core_dir
  File.expand_path('../../../opal', __FILE__)
end
dependent_files() click to toggle source

All files that Opal depends on while compiling (for cache keying and watching)

# File lib/opal/paths.rb, line 35
def self.dependent_files
  # We want to ensure the compiler and any Gemfile/gemspec (for development)
  # stays untouched
  opal_path = File.expand_path('..', Opal.gem_dir)
  files = Dir["#{opal_path}/{Gemfile*,*.gemspec,lib/**/*}"]

  # Also check if parser wasn't changed:
  files += $LOADED_FEATURES.grep(%r{lib/(parser|ast)})

  files
end
gem_dir() click to toggle source

We use this file from inside Opal as well, and __dir__ is not yet supported. rubocop:disable Style/ExpandPathArguments

# File lib/opal/paths.rb, line 6
def self.gem_dir
  File.expand_path('../..', __FILE__)
end
opal_location_from_error(error) click to toggle source
# File lib/opal/errors.rb, line 34
def self.opal_location_from_error(error)
  opal_location = OpalBacktraceLocation.new
  opal_location.location = error.location if error.respond_to?(:location)
  opal_location.diagnostic = error.diagnostic if error.respond_to?(:diagnostic)
  opal_location
end
paths() click to toggle source
# File lib/opal/paths.rb, line 86
def self.paths
  @paths.freeze
end
reset_paths!() click to toggle source

Resets Opal.paths to the default value (includes ‘corelib`, `stdlib`, `opal/lib`, `ast` gem and `parser` gem)

# File lib/opal/paths.rb, line 92
def self.reset_paths!
  @paths = [core_dir, std_dir, gem_dir]
  if RUBY_ENGINE != 'opal'
    use_gem 'ast'
    use_gem 'parser'
  end
  nil
end
std_dir() click to toggle source
# File lib/opal/paths.rb, line 14
def self.std_dir
  File.expand_path('../../../stdlib', __FILE__)
end