Module: Opal

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

Overview

rubocop:disable Layout/EmptyLineBetweenDefs, Style/SingleLineMethods

Defined Under Namespace

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

Constant Summary collapse

Server =
Opal::Sprockets::Server
VERSION =

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

'2.0.0dev'
VERSION_MAJOR_MINOR =

Only major and minor parts of version

VERSION.split('.').first(2).join('.').freeze
SystemRunner =

Generic runner that will resort to calling an external program.

->(data, &block) do
  options  = data[:options] || {}
  builder  = data.fetch(:builder).call
  output   = data.fetch(:output)

  env      = options.fetch(:env, {})
  debug    = options.fetch(:debug, false) || RUBY_ENGINE == 'opal'

  ext = builder.output_extension

  if options[:directory]
    tempdir = Dir.mktmpdir('opal-system-runner-')
    builder.compile_to_directory(tempdir, with_source_map: !options[:no_source_map])
    cmd = block.call(
      VirtualFileObject.new(path: File.join(tempdir, "index.#{ext}"))
    )
  else
    # Temporary issue with UTF-8, Base64, source maps and opalopal
    code = builder.compiled_source(
      with_source_map: !(options[:no_source_map] || RUBY_ENGINE == 'opal')
    )

    tempfile =
      if debug
        File.new("opal-system-runner.#{ext}", 'wb')
      else
        Tempfile.new(['opal-system-runner', ".#{ext}"], mode: File::BINARY)
      end

    tempfile.write code
    cmd = block.call tempfile
  end

  if RUBY_PLATFORM == 'opal'
    # Opal doesn't support neither `out:` nor `IO.try_convert` nor `open3`
    system(env, *cmd)
    $?.exitstatus
  elsif IO.try_convert(output) && RUBY_PLATFORM != 'java'
    system(env, *cmd, out: output)
    $?.exitstatus
  else
    # JRuby (v9.2) doesn't support using `out:` to redirect output.
    require 'open3'
    captured_output, status = Open3.capture2(env, *cmd)
    output.write captured_output
    status.exitstatus
  end
ensure
  tempfile.close if tempfile
  FileUtils.remove_entry(tempdir) if tempdir && !debug
end

Instance Attribute Summary collapse

Attributes included from Deprecations

#raise_on_deprecation

Class Method Summary collapse

Methods included from Project::Collection

add_project, all_projects, has_project?, project_of, projects, setup_project, use_gem

Methods included from Deprecations

deprecation

Instance Attribute Details

#builder_schedulerObject

Returns the value of attribute builder_scheduler.



16
17
18
# File 'opal/lib/opal/builder/scheduler.rb', line 16

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 'opal/lib/opal/cache.rb', line 13

def cache=(value)
  @cache = value
end

Class Method Details

.add_opal_location_to_error(opal_location, error) ⇒ Object



44
45
46
47
48
49
# File 'opal/lib/opal/errors.rb', line 44

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.



20
21
22
# File 'opal/lib/opal/paths.rb', line 20

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

.append_paths(*paths) ⇒ Object

Same as #append_path but can take multiple paths.



25
26
27
28
29
# File 'opal/lib/opal/paths.rb', line 25

def self.append_paths(*paths)
  paths.each { |i| setup_project(i) }
  @paths.concat(paths)
  nil
end

.cacheObject



15
16
17
18
19
20
21
22
# File 'opal/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 'opal/lib/opal/compiler.rb', line 30

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

.dependent_filesObject

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



33
34
35
36
37
38
39
40
41
42
43
# File 'opal/lib/opal/paths.rb', line 33

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}/{Opalfile,Gemfile*,*.gemspec,lib/**/*}"]

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

  files
end

.gem_dirObject



6
7
8
9
10
11
12
13
14
# File 'opal/lib/opal/paths.rb', line 6

def self.gem_dir
  if defined? Opal::GEM_DIR
    # This is for the case of opalopal. __FILE__ and __dir__ are unreliable
    # in this case.
    Opal::GEM_DIR
  else
    File.expand_path('..', __dir__)
  end
end

.opal_location_from_error(error) ⇒ Object



37
38
39
40
41
42
# File 'opal/lib/opal/errors.rb', line 37

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



47
48
49
# File 'opal/lib/opal/paths.rb', line 47

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)



53
54
55
56
57
58
# File 'opal/lib/opal/paths.rb', line 53

def self.reset_paths!
  @paths = []
  @projects = []
  setup_project(gem_dir)
  nil
end