Sha256: 27a03978659aee6d1e1231acf149ba903d3a5f4543f61a0c05e5b39dd1984130

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'duby'
require 'duby/jvm/source_compiler'
require 'jruby'
require 'stringio'
require File.join(File.dirname(__FILE__), 'test_jvm_compiler')

class TestJavacCompiler < TestJVMCompiler
  import javax.tools.ToolProvider
  import java.util.Arrays
  def javac(files)
    compiler = ToolProvider.system_java_compiler
    fm = compiler.get_standard_file_manager(nil, nil, nil)
    units = fm.get_java_file_objects_from_strings(Arrays.as_list(files.to_java :string))
    unless compiler.get_task(nil, fm, nil, nil, nil, units).call
      raise "Compilation error"
    end
    loader = org.jruby.util.ClassCache::OneShotClassLoader.new(
        JRuby.runtime.jruby_class_loader)
    classes = []
    files.each do |name|
      classfile = name.sub /java$/, 'class'
      if File.exist? classfile
        bytecode = IO.read(classfile)
        cls = loader.define_class(name[0..-6], bytecode.to_java_bytes)
        classes << JavaUtilities.get_proxy_class(cls.name)
        @tmp_classes << name
        @tmp_classes << classfile 
      end
    end
    classes
  end
  
  def compile(code)
    File.unlink(*@tmp_classes)
    @tmp_classes.clear
    AST.type_factory = Duby::JVM::Types::TypeFactory.new
    ast = AST.parse(code)
    name = "script" + System.nano_time.to_s
    typer = Typer::JVM.new(name)
    ast.infer(typer)
    typer.resolve(true)
    compiler = Compiler::JavaSource.new(name)
    ast.compile(compiler, false)
    java_files = []
    compiler.generate do |name, builder|
      bytes = builder.generate
      open("#{name}", "w") do |f|
        f << bytes
      end
      java_files << name
    end
    classes = javac(java_files)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
duby-0.0.1 test/test_javac_compiler.rb