Module: LiveAST::Loader

Defined in:
lib/live_ast/loader.rb

Class Method Summary collapse

Class Method Details

.find_file(file) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/live_ast/loader.rb', line 39

def find_file(file)
  if file.index Linker::REVISION_TOKEN
    raise "refusing to load file with revision token: `#{file}'"
  end
  search_paths(file) or
    raise LoadError, "cannot load such file -- #{file}"
end


20
21
22
23
24
25
26
27
# File 'lib/live_ast/loader.rb', line 20

def header_footer(wrap)
  return "class << Object.new;", ";end", true if wrap
  locals = NATIVE_EVAL.call("local_variables", TOPLEVEL_BINDING)

  params = locals.empty? ? "" : ("|;" + locals.join(",") + "|")

  return "lambda do #{params}", ";end.call", locals.empty?
end

.load(file, wrap) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/live_ast/loader.rb', line 4

def load(file, wrap)
  file = find_file(file)

  # guards to protect toplevel locals
  header, footer, warnings_ok = header_footer(wrap)

  parser_src = Reader.read(file)
  evaler_src = header << parser_src << footer

  run = lambda do
    Evaler.eval(parser_src, evaler_src, TOPLEVEL_BINDING, file, 1)
  end
  warnings_ok ? run.call : suppress_warnings(&run)
  true
end

.search_paths(file) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/live_ast/loader.rb', line 47

def search_paths(file)
  return file if File.file? file
  $LOAD_PATH.each do |path|
    target = path + "/" + file
    return target if File.file? target
  end
  nil
end

.suppress_warningsObject



29
30
31
32
33
34
35
36
37
# File 'lib/live_ast/loader.rb', line 29

def suppress_warnings
  previous = $VERBOSE
  $VERBOSE = nil
  begin
    yield
  ensure
    $VERBOSE ||= previous
  end
end