Class: Myco::CodeLoader::AbstractLoader
- Inherits:
-
Object
- Object
- Myco::CodeLoader::AbstractLoader
- Defined in:
- lib/myco/code_loader.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#ast ⇒ Object
Returns the value of attribute ast.
-
#block_environment ⇒ Object
Returns the value of attribute block_environment.
-
#compiled_code ⇒ Object
Returns the value of attribute compiled_code.
-
#constant_scope ⇒ Object
Returns the value of attribute constant_scope.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#generator ⇒ Object
Returns the value of attribute generator.
-
#line ⇒ Object
Returns the value of attribute line.
-
#receiver ⇒ Object
Returns the value of attribute receiver.
-
#string ⇒ Object
Returns the value of attribute string.
-
#variable_scope ⇒ Object
Returns the value of attribute variable_scope.
Instance Method Summary collapse
-
#bind_to(kwargs = {}) ⇒ Object
TODO: fix rubinius JIT issue and use “cscope:nil, vscope:nil, receiver:nil, call_depth:1” here.
- #compile ⇒ Object
- #emit_rb!(filename = nil) ⇒ Object
- #emit_rbc!(filename = nil) ⇒ Object
-
#initialize(filename, line = 1) ⇒ AbstractLoader
constructor
A new instance of AbstractLoader.
- #is_rb? ⇒ Boolean
- #is_rbc? ⇒ Boolean
- #load ⇒ Object
- #make_ast ⇒ Object
- #make_block_environment ⇒ Object
- #make_compiled_code ⇒ Object
- #make_generator ⇒ Object
- #make_string ⇒ Object
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
#ast ⇒ Object
Returns the value of attribute ast.
108 109 110 |
# File 'lib/myco/code_loader.rb', line 108 def ast @ast end |
#block_environment ⇒ Object
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_code ⇒ Object
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_scope ⇒ Object
Returns the value of attribute constant_scope.
105 106 107 |
# File 'lib/myco/code_loader.rb', line 105 def constant_scope @constant_scope end |
#filename ⇒ Object
Returns the value of attribute filename.
104 105 106 |
# File 'lib/myco/code_loader.rb', line 104 def filename @filename end |
#generator ⇒ Object
Returns the value of attribute generator.
109 110 111 |
# File 'lib/myco/code_loader.rb', line 109 def generator @generator end |
#line ⇒ Object
Returns the value of attribute line.
104 105 106 |
# File 'lib/myco/code_loader.rb', line 104 def line @line end |
#receiver ⇒ Object
Returns the value of attribute receiver.
105 106 107 |
# File 'lib/myco/code_loader.rb', line 105 def receiver @receiver end |
#string ⇒ Object
Returns the value of attribute string.
107 108 109 |
# File 'lib/myco/code_loader.rb', line 107 def string @string end |
#variable_scope ⇒ Object
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 |
#compile ⇒ Object
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
214 |
# File 'lib/myco/code_loader.rb', line 214 def is_rb?; false end |
#is_rbc? ⇒ Boolean
215 |
# File 'lib/myco/code_loader.rb', line 215 def is_rbc?; false end |
#load ⇒ Object
189 190 191 192 |
# File 'lib/myco/code_loader.rb', line 189 def load compile @block_environment.call_on_instance(@receiver) end |
#make_ast ⇒ Object
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 = "Error while parsing #{filename}:\n" + e. raise e.class, , e.backtrace end ast = ast_root_for(ast) ast.file = filename.to_sym ast.variable_scope = @variable_scope @ast = ast end |
#make_block_environment ⇒ Object
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_code ⇒ Object
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_generator ⇒ Object
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_string ⇒ Object
128 129 130 |
# File 'lib/myco/code_loader.rb', line 128 def make_string @string = File.read(filename) end |