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
# 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, "no such file to load -- #{file}"
end


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

def header_footer(wrap)
  if wrap
    return "class << Object.new;", ";end", true
  else
    locals = NATIVE_EVAL.call("local_variables", TOPLEVEL_BINDING)
  
    params = locals.empty? ? "" : ("|;" + locals.join(",") + "|")
  
    return "lambda do #{params}", ";end.call", locals.empty?
  end
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



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

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



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