Class: Atomy::Module

Inherits:
Module show all
Defined in:
lib/atomy/rubygems.rb,
lib/atomy/module.rb

Overview

this is here so it’s more likely to be filtered out of caller checks

(e.g. sinatra/base)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModule

Returns a new instance of Module.



20
21
22
23
24
25
26
27
28
29
# File 'lib/atomy/module.rb', line 20

def initialize
  extend self

  @exported_modules = []

  # easy accessor for the current module via LexicalScope lookup
  const_set(:Self, self)

  super
end

Instance Attribute Details

#exported_modulesObject (readonly)

Module

Modules users of this module should automatically use.



18
19
20
# File 'lib/atomy/module.rb', line 18

def exported_modules
  @exported_modules
end

#fileObject

Symbol

Absolute path to the file the module was loaded from.



10
11
12
# File 'lib/atomy/module.rb', line 10

def file
  @file
end

Instance Method Details

#compile(gen, node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/atomy/module.rb', line 35

def compile(gen, node)
  gen.set_line(node.line) if node.respond_to?(:line) && node.line

  expanded = node
  while expanded.is_a?(Atomy::Grammar::AST::Node)
    expanded = expand(expanded)
  end

  expanded.bytecode(gen, self)
rescue
  puts "when compiling: #{node}"
  raise
end

#compile_contextObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/atomy/module.rb', line 107

def compile_context
  return @compile_context if @compile_context

  scope = Rubinius::LexicalScope.new(
    self,
    Rubinius::LexicalScope.new(Object))

  meth = proc {}.block.compiled_code
  meth. = nil
  meth.name = :__script__
  meth.scope = scope

  variables =
    Rubinius::VariableScope.synthesize(
      meth, self, nil, self, nil, Rubinius::Tuple.new(0))

  if @file
    script = meth.create_script
    script.file_path = @file.to_s
    script.data_path = File.expand_path(@file.to_s)
    script.make_main!

    scope.script = script
  end

  @compile_context = Binding.setup(variables, meth, scope)
end

#evaluate(node, binding = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/atomy/module.rb', line 49

def evaluate(node, binding = nil)
  binding ||=
    Binding.setup(
      Rubinius::VariableScope.of_sender,
      Rubinius::CompiledCode.of_sender,
      Rubinius::LexicalScope.of_sender,
      self)

  code = Atomy::Compiler.compile(
    node,
    self,
    Atomy::EvalLocalState.new(binding.variables))

  code. :for_eval, true

  block = Atomy::Compiler.construct_block(code, binding)
  block.call
end

#expand(node) ⇒ Object

Node -> (Node | Code)

Raises:



98
99
100
# File 'lib/atomy/module.rb', line 98

def expand(node)
  raise UnknownCode.new(node)
end

#export(*modules) ⇒ Object



31
32
33
# File 'lib/atomy/module.rb', line 31

def export(*modules)
  @exported_modules.concat(modules)
end

#inspectObject



76
77
78
79
80
81
82
# File 'lib/atomy/module.rb', line 76

def inspect
  if @file
    super.sub(/>$/, " #{@file}>")
  else
    super
  end
end

#load(path) ⇒ Object



72
73
74
# File 'lib/atomy/module.rb', line 72

def load(path)
  Atomy::CodeLoader.load(path)
end

#pattern(node) ⇒ Object

Node -> Code::Pattern

Raises:



103
104
105
# File 'lib/atomy/module.rb', line 103

def pattern(node)
  raise UnknownPattern.new(node)
end

#require(path) ⇒ Object



68
69
70
# File 'lib/atomy/module.rb', line 68

def require(path)
  Atomy::CodeLoader.require(path)
end

#use(mod) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/atomy/module.rb', line 84

def use(mod)
  extend mod
  include mod

  if mod.is_a?(self.class)
    mod.exported_modules.each do |m|
      use(m)
    end
  end

  mod
end