Class: Myco::CodeLoader::AbstractLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/myco/code_loader.rb

Direct Known Subclasses

BytecodeLoader, MycoLoader, RubyLoader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, line = 1) ⇒ AbstractLoader

Returns a new instance of AbstractLoader.



113
114
115
116
# File 'lib/myco/code_loader.rb', line 113

def initialize filename, line = 1
  @filename = filename
  @line     = line
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



108
109
110
# File 'lib/myco/code_loader.rb', line 108

def ast
  @ast
end

#block_environmentObject

Returns the value of attribute block_environment.



111
112
113
# File 'lib/myco/code_loader.rb', line 111

def block_environment
  @block_environment
end

#compiled_codeObject

Returns the value of attribute compiled_code.



110
111
112
# File 'lib/myco/code_loader.rb', line 110

def compiled_code
  @compiled_code
end

#constant_scopeObject

Returns the value of attribute constant_scope.



105
106
107
# File 'lib/myco/code_loader.rb', line 105

def constant_scope
  @constant_scope
end

#filenameObject

Returns the value of attribute filename.



104
105
106
# File 'lib/myco/code_loader.rb', line 104

def filename
  @filename
end

#generatorObject

Returns the value of attribute generator.



109
110
111
# File 'lib/myco/code_loader.rb', line 109

def generator
  @generator
end

#lineObject

Returns the value of attribute line.



104
105
106
# File 'lib/myco/code_loader.rb', line 104

def line
  @line
end

#receiverObject

Returns the value of attribute receiver.



105
106
107
# File 'lib/myco/code_loader.rb', line 105

def receiver
  @receiver
end

#stringObject

Returns the value of attribute string.



107
108
109
# File 'lib/myco/code_loader.rb', line 107

def string
  @string
end

#variable_scopeObject

Returns the value of attribute variable_scope.



105
106
107
# File 'lib/myco/code_loader.rb', line 105

def variable_scope
  @variable_scope
end

Instance Method Details

#bind_to(kwargs = {}) ⇒ Object

TODO: fix rubinius JIT issue and use “cscope:nil, vscope:nil, receiver:nil, call_depth:1” here



119
120
121
122
123
124
125
126
# File 'lib/myco/code_loader.rb', line 119

def bind_to kwargs={}
  loc = Rubinius::VM.backtrace(kwargs.fetch(:call_depth, 1), true).first
  @constant_scope = kwargs[:cscope] || loc.constant_scope
  @variable_scope = kwargs[:vscope] || loc.variables
  @receiver =     kwargs[:receiver] || loc.instance_variable_get(:@receiver)
  
  self
end

#compileObject



185
186
187
# File 'lib/myco/code_loader.rb', line 185

def compile
  @block_environment || make_block_environment
end

#emit_rb!(filename = nil) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/myco/code_loader.rb', line 194

def emit_rb! filename=nil
  @ast || make_ast
  
  filename = emit_filename('.rb', filename)
  return nil unless filename
  
  File.open(filename, "w+") { |file| file.write(@ast.to_ruby_code) }
end

#emit_rbc!(filename = nil) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/myco/code_loader.rb', line 203

def emit_rbc! filename=nil
  @compiled_code || make_compiled_code
  
  filename = emit_filename('.rbc', filename)
  return nil unless filename
  
  compiled_file_type.dump(
    @compiled_code, filename, Rubinius::Signature, Rubinius::RUBY_LIB_VERSION
  )
end

#is_rb?Boolean

Returns:

  • (Boolean)


214
# File 'lib/myco/code_loader.rb', line 214

def is_rb?;  false end

#is_rbc?Boolean

Returns:

  • (Boolean)


215
# File 'lib/myco/code_loader.rb', line 215

def is_rbc?; false end

#loadObject



189
190
191
192
# File 'lib/myco/code_loader.rb', line 189

def load
  compile
  @block_environment.call_on_instance(@receiver)
end

#make_astObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/myco/code_loader.rb', line 132

def make_ast
  @string || make_string
  
  parser = new_parser
  begin
    ast = parser.parse_string(@string)
  rescue Exception => e
    full_message = "Error while parsing #{filename}:\n" + e.message
    raise e.class, full_message, e.backtrace
  end
  
  ast = ast_root_for(ast)
  ast.file = filename.to_sym
  ast.variable_scope = @variable_scope
  
  @ast = ast
end

#make_block_environmentObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/myco/code_loader.rb', line 170

def make_block_environment
  @compiled_code || make_compiled_code
  code = @compiled_code
  
  code.scope = @constant_scope
  script = Rubinius::CompiledCode::Script.new(code, @filename, true)
  script.eval_source = @string
  code.scope.script = script
  
  be = Rubinius::BlockEnvironment.new
  be.under_context(@variable_scope, code)
  
  @block_environment = be
end

#make_compiled_codeObject



162
163
164
165
166
167
168
# File 'lib/myco/code_loader.rb', line 162

def make_compiled_code
  @generator || make_generator
  
  code = @generator.package(Rubinius::CompiledCode)
  
  @compiled_code = code
end

#make_generatorObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/myco/code_loader.rb', line 150

def make_generator
  @ast || make_ast
  
  g = new_generator
  @ast.bytecode(g)
  
  g.close
  g.encode
  
  @generator = g
end

#make_stringObject



128
129
130
# File 'lib/myco/code_loader.rb', line 128

def make_string
  @string = File.read(filename)
end