Module: Ryac::DriverFile

Defined in:
lib/ryac/driver_file.rb,
sig/ryac/driver_file.rbs

Overview

The two-file layout of a program with lazy regions: the minified program as a pure library — loader, core and aliases, nothing that runs — and a runner holding the regions, which loads the library, registers them and starts the program with an expression given on its command line:

ruby driver.rb minify.rb --exec "Optcarrot::NES.new.run" game.nes

The runner strips its own arguments before anything else reads ARGV, so the program's option parsing sees only what follows. Registering after the load means a dynamic require the core runs while loading — rather than from a method called later, the shape that made the file lazy — would miss its region and fall through to the loader's real require.

Constant Summary collapse

HEADER =

Returns:

  • (String)
"# Generated by ryac. Usage: ruby driver.rb CORE [--exec EXPR] [ARGS...]\n# Loads CORE, registers the regions below, then evals EXPR at top level\n# with ARGS left in ARGV. Arguments after \"--\" are never read.\ncore = ARGV.shift or abort(\"usage: ruby \#{$0} CORE [--exec EXPR] [ARGS...]\")\nstop = ARGV.index(\"--\")\nat = ARGV.index(\"--exec\")\nexpr = ARGV.slice!(at, 2)[1] if at && (!stop || at < stop)\nload core\n"
"eval(expr, TOPLEVEL_BINDING, \"--exec\") if expr\n"

Class Method Summary collapse

Class Method Details

.split(full_content) ⇒ [String, String]

Splits the program's full content into [core, driver]: the region registrations go to the driver, one per line, everything else stays the core in its original order.

Parameters:

  • full_content (String)

Returns:

  • ([String, String])

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ryac/driver_file.rb', line 33

def self.split(full_content)
  result = Prism.parse(full_content)
  raise Pipeline::InvalidOutputError.new('program', result.errors) unless result.errors.none?

  registrations = [] #: Array[String]
  core = [] #: Array[String]
  result.value.statements.body.each do |statement|
    (LazyRegions.registration_lambda(statement) ? registrations : core) << statement.slice
  end
  raise MinifyError, 'cannot write a driver file: the program has no lazy regions' if registrations.empty?

  [core.join(';'), "#{HEADER}#{registrations.join("\n")}\n#{FOOTER}"]
end