Module: Opal

Extended by:
Deprecations, UseGem
Defined in:
lib/opal/requires.rb,
lib/opal/os.rb,
lib/opal/cli.rb,
lib/opal/erb.rb,
lib/opal/hike.rb,
lib/opal/repl.rb,
lib/opal/util.rb,
lib/opal/cache.rb,
lib/opal/paths.rb,
lib/opal/config.rb,
lib/opal/errors.rb,
lib/opal/builder.rb,
lib/opal/version.rb,
lib/opal/ast/node.rb,
lib/opal/compiler.rb,
lib/opal/fragment.rb,
lib/opal/nodes/if.rb,
lib/opal/rewriter.rb,
lib/opal/nodes/def.rb,
lib/opal/nodes/top.rb,
lib/opal/nodes/args.rb,
lib/opal/nodes/base.rb,
lib/opal/nodes/call.rb,
lib/opal/nodes/defs.rb,
lib/opal/nodes/hash.rb,
lib/opal/nodes/iter.rb,
lib/opal/source_map.rb,
lib/opal/ast/builder.rb,
lib/opal/ast/matcher.rb,
lib/opal/cli_options.rb,
lib/opal/cli_runners.rb,
lib/opal/eof_content.rb,
lib/opal/nodes/array.rb,
lib/opal/nodes/class.rb,
lib/opal/nodes/logic.rb,
lib/opal/nodes/masgn.rb,
lib/opal/nodes/scope.rb,
lib/opal/nodes/super.rb,
lib/opal/nodes/while.rb,
lib/opal/nodes/yield.rb,
lib/opal/path_reader.rb,
lib/opal/deprecations.rb,
lib/opal/nodes/lambda.rb,
lib/opal/nodes/module.rb,
lib/opal/nodes/rescue.rb,
lib/opal/nodes/arglist.rb,
lib/opal/nodes/closure.rb,
lib/opal/nodes/defined.rb,
lib/opal/nodes/helpers.rb,
lib/opal/nodes/literal.rb,
lib/opal/nodes/args/arg.rb,
lib/opal/nodes/x_string.rb,
lib/opal/regexp_anchors.rb,
lib/opal/rewriters/base.rb,
lib/opal/cli_runners/gjs.rb,
lib/opal/nodes/constants.rb,
lib/opal/nodes/variables.rb,
lib/opal/cache/file_cache.rb,
lib/opal/cli_runners/deno.rb,
lib/opal/builder_scheduler.rb,
lib/opal/nodes/definitions.rb,
lib/opal/builder_processors.rb,
lib/opal/cli_runners/chrome.rb,
lib/opal/cli_runners/nodejs.rb,
lib/opal/cli_runners/safari.rb,
lib/opal/cli_runners/server.rb,
lib/opal/nodes/call_special.rb,
lib/opal/cli_runners/firefox.rb,
lib/opal/cli_runners/nashorn.rb,
lib/opal/cli_runners/quickjs.rb,
lib/opal/nodes/args/fake_arg.rb,
lib/opal/rewriters/arguments.rb,
lib/opal/rewriters/dump_args.rb,
lib/opal/rewriters/mlhs_args.rb,
lib/opal/rewriters/numblocks.rb,
lib/opal/nodes/node_with_args.rb,
lib/opal/parser/source_buffer.rb,
lib/opal/nodes/args/parameters.rb,
lib/opal/nodes/singleton_class.rb,
lib/opal/parser/default_config.rb,
lib/opal/rewriters/inline_args.rb,
lib/opal/cli_runners/mini_racer.rb,
lib/opal/nodes/args/arity_check.rb,
lib/opal/rewriters/for_rewriter.rb,
lib/opal/rewriters/forward_args.rb,
lib/opal/cli_runners/applescript.rb,
lib/opal/rewriters/block_to_iter.rb,
lib/opal/rewriters/dot_js_syntax.rb,
lib/opal/nodes/args/extract_kwarg.rb,
lib/opal/rewriters/thrower_finder.rb,
lib/opal/builder_scheduler/prefork.rb,
lib/opal/nodes/args/extract_kwargs.rb,
lib/opal/nodes/args/extract_optarg.rb,
lib/opal/nodes/args/extract_restarg.rb,
lib/opal/rewriters/pattern_matching.rb,
lib/opal/rewriters/returnable_logic.rb,
lib/opal/rewriters/targeted_patches.rb,
lib/opal/nodes/args/extract_kwoptarg.rb,
lib/opal/nodes/args/extract_post_arg.rb,
lib/opal/rewriters/js_reserved_words.rb,
lib/opal/rewriters/opal_engine_check.rb,
lib/opal/builder_scheduler/sequential.rb,
lib/opal/nodes/args/extract_block_arg.rb,
lib/opal/nodes/args/extract_kwrestarg.rb,
lib/opal/nodes/args/prepare_post_args.rb,
lib/opal/nodes/args/initialize_iterarg.rb,
lib/opal/nodes/args/extract_post_optarg.rb,
lib/opal/nodes/node_with_args/shortcuts.rb,
lib/opal/rewriters/deduplicate_arg_name.rb,
lib/opal/nodes/args/initialize_shadowarg.rb,
lib/opal/nodes/args/ensure_kwargs_are_kwargs.rb,
lib/opal/rewriters/rubyspec/filters_rewriter.rb,
lib/opal/rewriters/binary_operator_assignment.rb,
lib/opal/rewriters/logical_operator_assignment.rb,
lib/opal/rewriters/hashes/key_duplicates_rewriter.rb

Overview

rubocop:disable Layout/EmptyLineBetweenDefs, Style/SingleLineMethods

Defined Under Namespace

Modules: AST, BuilderProcessors, Cache, CliRunners, Config, Deprecations, ERB, Hike, MagicComments, Nodes, OS, Parser, Rewriters, Rubyspec, SourceMap, UseGem, Util Classes: Builder, BuilderScheduler, CLI, CLIOptions, CompilationError, Compiler, EofContent, Error, Fragment, GemNotFound, OpalBacktraceLocation, ParsingError, PathReader, REPL, Rewriter, RewritingError, SimpleServer, SyntaxError

Constant Summary collapse

Server =
Opal::Sprockets::Server
VERSION =

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

'1.8.2'

Instance Attribute Summary collapse

Attributes included from Deprecations

#raise_on_deprecation

Class Method Summary collapse

Methods included from UseGem

use_gem

Methods included from Deprecations

deprecation

Instance Attribute Details

#builder_schedulerObject

Returns the value of attribute builder_scheduler.



14
15
16
# File 'lib/opal/builder_scheduler.rb', line 14

def builder_scheduler
  @builder_scheduler
end

#cache=(value) ⇒ Object (writeonly)

A Sprockets-compatible cache, for example an instance of Opal::Cache::FileCache or Opal::Cache::NullCache.



13
14
15
# File 'lib/opal/cache.rb', line 13

def cache=(value)
  @cache = value
end

Class Method Details

.add_opal_location_to_error(opal_location, error) ⇒ Object



41
42
43
44
45
46
# 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) ⇒ Object

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.



23
24
25
# File 'lib/opal/paths.rb', line 23

def self.append_path(path)
  append_paths(path)
end

.append_paths(*paths) ⇒ Object

Same as #append_path but can take multiple paths.



28
29
30
31
# File 'lib/opal/paths.rb', line 28

def self.append_paths(*paths)
  @paths.concat(paths)
  nil
end

.cacheObject



15
16
17
18
19
20
21
22
# 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 = {}) ⇒ String

Compile a string of ruby code into javascript.

Examples:


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

Parameters:

  • source (String)

    ruby source

  • options (Hash) (defaults to: {})

    compiler options

Returns:

  • (String)

    javascript code

See Also:

  • for compiler options


30
31
32
# File 'lib/opal/compiler.rb', line 30

def self.compile(source, options = {})
  Compiler.new(source, options).compile
end

.core_dirObject



10
11
12
# File 'lib/opal/paths.rb', line 10

def self.core_dir
  File.expand_path('../../../opal', __FILE__)
end

.dependent_filesObject

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



35
36
37
38
39
40
41
42
43
44
45
# 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_dirObject

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



6
7
8
# File 'lib/opal/paths.rb', line 6

def self.gem_dir
  File.expand_path('../..', __FILE__)
end

.opal_location_from_error(error) ⇒ Object



34
35
36
37
38
39
# 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

.pathsObject



86
87
88
# File 'lib/opal/paths.rb', line 86

def self.paths
  @paths.freeze
end

.reset_paths!Object

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



92
93
94
95
96
97
98
99
# 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_dirObject



14
15
16
# File 'lib/opal/paths.rb', line 14

def self.std_dir
  File.expand_path('../../../stdlib', __FILE__)
end