Skip to content

Instantly share code, notes, and snippets.

@headius

headius/.diff Secret

Created January 16, 2020 00:18
Show Gist options
  • Save headius/00242e561beb79785fb5bcb48ee21647 to your computer and use it in GitHub Desktop.
Save headius/00242e561beb79785fb5bcb48ee21647 to your computer and use it in GitHub Desktop.
diff --git a/spec/compiler/general_spec.rb b/spec/compiler/general_spec.rb
index 8fd1ec296d..26aa511bf1 100644
--- a/spec/compiler/general_spec.rb
+++ b/spec/compiler/general_spec.rb
@@ -26,6 +26,65 @@ module InterpreterSpecUtils
def self.name; "interpreter"; end
end
+module PersistenceSpecUtils
+ include CompilerSpecUtils
+
+ def run_in_method(src, filename = caller_locations[0].path, line = caller_locations[0].lineno)
+ run( "def __temp; #{src}; end; __temp", filename, line)
+ end
+
+ def run(src, filename = caller_locations[0].path, line = caller_locations[0].lineno)
+ yield encode_decode_run(src, filename, line) unless (ENV['PERSISTENCE_TEST'] == 'false')
+ end
+
+ def self.name; "persistence"; end
+
+ private
+
+ def encode_decode(src, filename, line)
+ node = JRuby.parse(src, filename, false, line)
+ runtime = JRuby.runtime
+
+ # This logic is a mix of logic from InterpretedIRMethod's JIT, o.j.Ruby's script compilation, and IRScriptBody's
+ # interpret. We need to figure out a cleaner path.
+
+ scope = node.getStaticScope
+ currModule = scope.getModule
+ if currModule == nil
+ scope.setModule currModule = runtime.top_self.class
+ end
+
+ manager = runtime.getIRManager()
+ method = org.jruby.ir.IRBuilder.build_root(manager, node).scope
+
+ # encode and decode
+ baos = java.io.ByteArrayOutputStream.new
+ writer = org.jruby.ir.persistence.IRWriterStream.new(baos)
+ org.jruby.ir.persistence.IRWriter.persist(writer, method)
+
+ bais = java.io.ByteArrayInputStream.new(baos.to_byte_array)
+ reader = org.jruby.ir.persistence.IRReaderStream.new(manager, bais, org.jruby.util.ByteList.new(filename.to_java.getBytes))
+ method = org.jruby.ir.persistence.IRReader.load(manager, reader)
+
+ return org.jruby.internal.runtime.methods.InterpretedIRMethod.new(
+ method,
+ org.jruby.runtime.Visibility::PUBLIC,
+ currModule)
+ end
+
+ def encode_decode_run(src, filename, line)
+ cls = encode_decode(src, filename, line - 1) # compiler expects zero-based lines
+
+ cls.call(
+ JRuby.runtime.current_context,
+ JRuby.runtime.top_self,
+ JRuby.runtime.top_self.class,
+ "script",
+ IRubyObject[0].new,
+ Block::NULL_BLOCK)
+ end
+end
+
module JITSpecUtils
include CompilerSpecUtils
@@ -91,6 +150,7 @@ end
modes = []
modes << InterpreterSpecUtils unless (ENV['INTERPRETER_TEST'] == 'false')
+modes << PersistenceSpecUtils unless (ENV['PERSISTENCE_TEST'] == 'false')
modes << JITSpecUtils unless (ENV['COMPILER_TEST'] == 'false')
Block = org.jruby.runtime.Block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment