Sha256: eee4c9c2212a7ce853027b6bfc323fb1889d7ed11cec72392cc35cd867a31f0b

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Vernacular
  # Represents a file that contains Ruby source code that can be read from and
  # compiled down to instruction sequences.
  class SourceFile
    attr_reader :source_path, :iseq_path

    def initialize(source_path, iseq_path)
      @source_path = source_path
      @iseq_path   = iseq_path
    end

    def dump
      source = File.read(source_path)
      Vernacular.modifiers.each do |modifier|
        source = modifier.modify(source)
      end

      iseq = RubyVM::InstructionSequence.compile(source)
      digest = ::Digest::MD5.file(source_path).digest
      File.binwrite(iseq_path, iseq.to_binary("MD5:#{digest}"))
      iseq
    rescue SyntaxError, RuntimeError
      nil
    end

    def load
      if File.exist?(iseq_path) &&
         (File.mtime(source_path) <= File.mtime(iseq_path))
        RubyVM::InstructionSequence.load_from_binary(File.binread(iseq_path))
      else
        dump
      end
    end

    def self.load_iseq(source_path)
      new(
        source_path,
        File.join(Vernacular.iseq_dir, Vernacular.iseq_path_for(source_path))
      ).load
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vernacular-0.0.2 lib/vernacular/source_file.rb
vernacular-0.0.1 lib/vernacular/source_file.rb