Module: Klenod::Runtime

Defined in:
lib/klenod/runtime.rb,
lib/klenod/runtime/mod.rb,
lib/klenod/runtime/bundle.rb,
lib/klenod/runtime/version.rb,
lib/klenod/runtime/source_map.rb,
lib/klenod/runtime/bundle_format.rb,
lib/klenod/runtime/backtrace_rewriter.rb

Defined Under Namespace

Modules: BundleFormat, Generated, SourceMap Classes: AssetReference, AssetSpec, BacktraceRewriter, Bundle, BundleFormatError, DefaultImport, ImportSpec, LazyImport, Mod, ModuleSpec

Constant Summary collapse

EXECUTABLE_BUNDLE_MARKER =
"\n__END__\n".b.freeze
VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.load_bundle(source, source_root: nil) ⇒ Object



14
15
16
# File 'lib/klenod/runtime.rb', line 14

def self.load_bundle(source, source_root: nil)
  Bundle.load(source, source_root: source_root)
end

.load_bundle_in_box(source, source_root: nil, box: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/klenod/runtime.rb', line 18

def self.load_bundle_in_box(source, source_root: nil, box: nil)
  unless defined?(Ruby::Box) && Ruby::Box.enabled?
    raise "Ruby::Box is disabled. Set RUBY_BOX=1 environment variable to use Ruby::Box."
  end

  bytes = source.respond_to?(:read) ? source.read : File.binread(source)
  box = prepare_box(box)
  box::Klenod::Runtime::BundleFormat.load_bytes(bytes, source_root: source_root)
end

.load_executable_bundle(path, source_root: nil) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
# File 'lib/klenod/runtime.rb', line 43

def self.load_executable_bundle(path, source_root: nil)
  bytes = File.binread(path)
  marker_index = bytes.index(EXECUTABLE_BUNDLE_MARKER)
  raise ArgumentError, "Missing __END__ marker in executable bundle: #{path}" unless marker_index

  payload = bytes.byteslice(marker_index + EXECUTABLE_BUNDLE_MARKER.bytesize, bytes.bytesize)
  BundleFormat.load_bytes(payload, source_root: source_root)
end

.prepare_box(box = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/klenod/runtime.rb', line 28

def self.prepare_box(box = nil)
  unless defined?(Ruby::Box) && Ruby::Box.enabled?
    raise "Ruby::Box is disabled. Set RUBY_BOX=1 environment variable to use Ruby::Box."
  end

  box ||= Ruby::Box.new
  box.require(File.expand_path(__FILE__)) unless runtime_loaded_in_box?(box)
  box
end

.runtime_loaded_in_box?(box) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/klenod/runtime.rb', line 38

def self.runtime_loaded_in_box?(box)
  box.const_defined?(:Klenod, false) &&
    box::Klenod.const_defined?(:Runtime, false)
end