2012-07-26 13:25:23 -04:00
|
|
|
module Mutant
|
2012-11-21 14:31:01 -05:00
|
|
|
# Base class for code loaders
|
2012-07-26 13:25:23 -04:00
|
|
|
class Loader
|
2012-11-21 14:31:01 -05:00
|
|
|
include AbstractClass
|
2012-08-15 22:10:54 -04:00
|
|
|
extend MethodObject
|
2012-07-26 13:25:23 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-11-21 14:31:01 -05:00
|
|
|
# Run the loader
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
abstract_method :run
|
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Initialize and insert mutation into vm
|
|
|
|
#
|
|
|
|
# @param [Rubinius::AST::Script] root
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize(root)
|
2012-08-16 16:59:25 -04:00
|
|
|
@root = Helper.deep_clone(root)
|
2012-11-21 14:31:01 -05:00
|
|
|
run
|
2012-08-19 23:01:26 -04:00
|
|
|
end
|
|
|
|
|
2012-11-21 14:31:01 -05:00
|
|
|
# Eval based loader
|
|
|
|
class Eval < self
|
|
|
|
private
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-11-21 14:31:01 -05:00
|
|
|
# Run loader
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def run
|
2012-11-21 18:10:50 -05:00
|
|
|
puts source
|
2012-11-21 14:31:01 -05:00
|
|
|
eval(source, TOPLEVEL_BINDING)
|
|
|
|
end
|
2012-08-19 15:25:11 -04:00
|
|
|
|
2012-11-21 14:31:01 -05:00
|
|
|
# Return source
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source
|
|
|
|
ToSource.to_source(@root)
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-11-21 14:31:01 -05:00
|
|
|
# Rubinius script node based loaded
|
|
|
|
class Rubinius < self
|
|
|
|
private
|
|
|
|
|
|
|
|
# Run loader
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def run(root)
|
|
|
|
Rubinius.run_script(compiled_code)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return compiled code
|
|
|
|
#
|
|
|
|
# @return [Rubinius::CompiledCode]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
# FIXME: rbx on travis is older than on my devbox.
|
|
|
|
#
|
|
|
|
def compiled_code
|
|
|
|
_script = script
|
|
|
|
_script.respond_to?(:compiled_code) ? _script.compiled_code : _script.compiled_method
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return code script
|
|
|
|
#
|
|
|
|
# @return [Rubinius::CompiledCode::Script]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def script
|
|
|
|
compiled_code_raw.create_script
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return compiled code for node
|
|
|
|
#
|
|
|
|
# @return [Rubinius::CompiledCode]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def compiled_code_raw
|
|
|
|
compiler.run
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return compiler loaded with mutated ast
|
|
|
|
#
|
|
|
|
# @return [Rubinius::Compiler]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def compiler
|
|
|
|
Rubinius::Compiler.new(:bytecode, :compiled_method).tap do |compiler|
|
|
|
|
compiler.generator.input(@root)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return script node
|
|
|
|
#
|
|
|
|
# @param [Rubinius::AST::Node] node
|
|
|
|
#
|
|
|
|
# @return [Rubinius::AST::Script]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def script(node)
|
|
|
|
Rubinius::AST::Script.new(node).tap do |script|
|
|
|
|
script.file = source_path
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|