Module: LiveAST::Loader

Defined in:
lib/live_ast/loader.rb

Class Method Summary collapse

Class Method Details

.find_file(file) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/live_ast/loader.rb', line 42

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


22
23
24
25
26
27
28
29
30
# File 'lib/live_ast/loader.rb', line 22

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



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

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



51
52
53
54
55
56
57
58
59
# File 'lib/live_ast/loader.rb', line 51

def search_paths(file)
  return file if File.file? file

  $LOAD_PATH.each do |path|
    target = File.join(path, file)
    return target if File.file? target
  end
  nil
end

.suppress_warningsObject



32
33
34
35
36
37
38
39
40
# File 'lib/live_ast/loader.rb', line 32

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