Permalink
Browse files

Rebasing from trunk (almost done one depot error to figure out yet)

git-svn-id: http://svn.codehaus.org/jruby/branches/enebo_lexical@2412 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information...
1 parent 849b019 commit a2d2ab2febc5f5d725197902f58526e170b95ee2 @enebo enebo committed Nov 9, 2006
View
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
- <classpathentry excluding="**/CVS/*" output="build/classes/test" kind="src" path="test"/>
+ <classpathentry excluding="**/CVS/*" kind="src" output="build/classes/test" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/bsf.jar"/>
<classpathentry kind="lib" path="lib/junit.jar"/>
<classpathentry kind="lib" path="lib/ant.jar"/>
<classpathentry kind="lib" path="lib/jvyaml.jar"/>
<classpathentry kind="lib" path="lib/plaincharset.jar"/>
<classpathentry kind="lib" path="lib/asm-2.2.2.jar"/>
+ <classpathentry kind="lib" path="lib/jline-0.9.91-SNAPSHOT.jar"/>
<classpathentry kind="output" path="build/classes/jruby"/>
</classpath>
View
@@ -63,7 +63,7 @@ set _STARTJAVA="%JAVA_HOME%\bin\java"
set _RUNJAVA="%JAVA_HOME%\bin\java"
rem ----- Set Up The Runtime Classpath ----------------------------------------
-set CP=%JRUBY_HOME%\lib\jruby.jar;%JRUBY_HOME%\lib\jvyaml.jar;%JRUBY_HOME%\lib\plaincharset.jar;%JRUBY_HOME%\lib\asm-2.2.2.jar
+set CP=%JRUBY_HOME%\lib\jruby.jar;%JRUBY_HOME%\lib\jvyaml.jar;%JRUBY_HOME%\lib\plaincharset.jar;%JRUBY_HOME%\lib\asm-2.2.2.jar;%JRUBY_HOME%\lib\jline-0.9.91-SNAPSHOT.jar
if not "%CLASSPATH%" == "" goto gotCP
set CLASSPATH=%CP%
Binary file not shown.
@@ -6,7 +6,7 @@
include_class "org.jruby.util.JRubyClassLoader"
include_class "org.jruby.runtime.builtin.IRubyObject"
-compiler = InstructionCompiler2.new("MyCompiledScript", __FILE__);
+compiler = InstructionCompiler2.new();
# pure ruby version
def fib_ruby(n)
@@ -26,22 +26,7 @@ def fib_java(n)
fib_java(n - 2) + fib_java(n - 1)
end
end
-EOS
-def fib_iter_ruby(n)
- i = 0
- j = 1
- cur = 1
- while cur <= n
- k = i
- i = j
- j = k + j
- cur = cur + 1
- end
- i
-end
-
-fib_iter_java_str = <<EOS
def fib_iter_java(n)
i = 0
j = 1
@@ -54,11 +39,9 @@ def fib_iter_java(n)
end
i
end
-EOS
-# other tests
-comp_test_str = <<EOS
def comp_test
+ begin
nil
1111111111111111111111111111111111111111111111111111111111
1.0
@@ -71,36 +54,50 @@ def comp_test
puts x
x = x + 1
end
+ @@x = 5
+ p @@x
+end
end
EOS
+def fib_iter_ruby(n)
+ i = 0
+ j = 1
+ cur = 1
+ while cur <= n
+ k = i
+ i = j
+ j = k + j
+ cur = cur + 1
+ end
+ i
+end
+
# parse and compile
fib_java_n = JRuby.parse(fib_java_str, __FILE__);
-fib_iter_java_n = JRuby.parse(fib_iter_java_str, __FILE__);
-comp_test_n = JRuby.parse(comp_test_str, __FILE__);
begin
- fib_java_n.accept(compiler);
- fib_iter_java_n.accept(compiler);
- comp_test_n.accept(compiler);
+ class_and_method = compiler.compile("MyCompiledScript", __FILE__, fib_java_n);
rescue Exception => e
puts e
exit(1)
end
-# create the class
-script_class = JRubyClassLoader.new.define_class("MyCompiledScript", compiler.class_writer.to_byte_array())
+# create the classloader
+script_class_loader = JRubyClassLoader.new
# write it out, just for fun
-FileOutputStream.new("MyCompiledScript.class").write(compiler.class_writer.to_byte_array());
+classes = {}
+compiler.class_writers.each do |k,v|
+ FileOutputStream.new("#{k}.class").write(v.to_byte_array())
+ classes[k] = script_class_loader.define_class(k, v.to_byte_array())
+end
# bind method to Kernel#fib_java
-cbf = JRuby.runtime.callback_factory(script_class)
-fib_callback = cbf.get_singleton_method("fib_java", IRubyObject.java_class)
-compiler.define_module_function(JRuby.runtime, "Kernel", "fib_java", fib_callback);
-fib_iter_callback = cbf.get_singleton_method("fib_iter_java", IRubyObject.java_class)
-compiler.define_module_function(JRuby.runtime, "Kernel", "fib_iter_java", fib_iter_callback);
-comp_callback = cbf.get_singleton_method("comp_test", IRubyObject.java_class)
-compiler.define_module_function(JRuby.runtime, "Kernel", "comp_test", comp_callback);
+script_class = classes["MyCompiledScript$MultiStub0"]
+stub = script_class.newInstance
+compiler.define_module_function(JRuby.runtime, "Kernel", "fib_java", stub, 1, org.jruby.runtime.Arity.singleArgument, org.jruby.runtime.Visibility::PUBLIC);
+compiler.define_module_function(JRuby.runtime, "Kernel", "fib_iter_java", stub, 2, org.jruby.runtime.Arity.singleArgument, org.jruby.runtime.Visibility::PUBLIC);
+compiler.define_module_function(JRuby.runtime, "Kernel", "comp_test", stub, 3, org.jruby.runtime.Arity.noArguments, org.jruby.runtime.Visibility::PUBLIC);
# a simple benchmarking function
def time(str)
@@ -109,6 +106,8 @@ def time(str)
puts "Time for #{str}: #{Time.now - t}"
end
+p comp_test(nil)
+
# time interpreted versus compiled
time("bi-recursive, interpreted") { fib_ruby(30) }
time("bi-recursive, compiled") { fib_java(30) }
@@ -120,4 +119,3 @@ def time(str)
time("iterative, interpreted") { fib_iter_ruby(500000) }
time("iterative, compiled") { fib_iter_java(500000) }
-p comp_test(nil)
View
@@ -13,8 +13,9 @@
#
# Copyright (C) 2002 Anders Bengtsson <ndrsbngtssn@yahoo.se>
# Copyright (C) 2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
-# Copyright (C) 2004-2005 Thomas E Enebo <enebo@acm.org>
+# Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
# Copyright (C) 2004 David Corbin <dcorbin@users.sourceforge.net>
+# Copyright (C) 2006 Michael Studman <me@michaelstudman.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
@@ -457,7 +458,7 @@ def const_missing(sym)
def method_missing(sym, *args)
if sym.to_s.downcase[0] == sym.to_s[0]
- Package.create_package sym, sym
+ Package.create_package sym, sym, Java
else
JavaUtilities.get_proxy_class "#{sym}"
end
@@ -469,16 +470,18 @@ def initialize(name)
@name = name
end
+ def singleton; class << self; self; end; end
+
def method_missing(sym, *args)
if sym.to_s.downcase[0] == sym.to_s[0]
- self.class.create_package sym, "#{@name}.#{sym}"
+ self.class.create_package sym, "#{@name}.#{sym}", singleton
else
JavaUtilities.get_proxy_class "#{@name}.#{sym}"
end
end
class << self
- def create_package(sym, package_name, cls=self)
+ def create_package(sym, package_name, cls)
package = Java::Package.new package_name
cls.send(:define_method, sym) { package }
package
@@ -487,10 +490,12 @@ def create_package(sym, package_name, cls=self)
end
end
-Java::Package.create_package(:java, :java, Kernel)
-Java::Package.create_package(:javax, :javax, Kernel)
-Java::Package.create_package(:org, :org, Kernel)
-Java::Package.create_package(:com, :com, Kernel)
+# Create convenience methods for top-level java packages so we do not need to prefix
+# with 'Java::'. We undef these methods within Package in case we run into 'com.foo.com'.
+[:java, :javax, :com, :org].each do |meth|
+ Java::Package.create_package(meth, meth, Kernel)
+ Java::Package.send(:undef_method, meth)
+end
require 'builtin/java/exceptions'
require 'builtin/java/collections'
View
@@ -45,6 +45,8 @@
public IRubyObject eval(Node node);
+ public IRubyObject compileAndRun(Node node);
+
public RubyClass getObject();
public RubyModule getKernel();
View
@@ -135,6 +135,7 @@ public void printUsage() {
out.println(" -Idirectory specify $LOAD_PATH directory (may be used more than once)");
out.println(" -- optional -- before rubyfile.rb for compatibility with ruby");
out.println(" -O run with ObjectSpace disabled (improves performance)");
+ out.println(" -C pre-compile scripts before running (EXPERIMENTAL)");
hasPrintedUsage = true;
}
}
@@ -174,8 +175,11 @@ private void runInterpreter(IRuby runtime, Reader reader, String filename) {
try {
initializeRuntime(runtime, filename);
Node parsedScript = getParsedScript(runtime, reader, filename);
+ if (commandline.isCompilerEnabled()) {
+ runtime.compileAndRun(parsedScript);
+ } else {
runtime.eval(parsedScript);
-
+ }
} finally {
runtime.tearDown();
}
View
@@ -49,6 +49,8 @@
import java.util.Stack;
import org.jruby.ast.Node;
+import org.jruby.ast.executable.InstructionCompiler2;
+import org.jruby.ast.executable.Script;
import org.jruby.common.RubyWarnings;
import org.jruby.evaluator.EvaluationState;
import org.jruby.exceptions.JumpException;
@@ -66,6 +68,7 @@
import org.jruby.libraries.StringScannerLibrary;
import org.jruby.libraries.ZlibLibrary;
import org.jruby.libraries.YamlLibrary;
+import org.jruby.ext.Readline;
import org.jruby.parser.Parser;
import org.jruby.runtime.Block;
import org.jruby.runtime.CacheMap;
@@ -93,6 +96,7 @@
import org.jruby.runtime.builtin.meta.TimeMetaClass;
import org.jruby.runtime.load.LoadService;
import org.jruby.util.BuiltinScript;
+import org.jruby.util.JRubyClassLoader;
import org.jruby.util.JRubyFile;
import org.jruby.util.collections.SinglyLinkedList;
@@ -244,6 +248,43 @@ public IRubyObject eval(Node node) {
}
}
+ public IRubyObject compileAndRun(Node node) {
+ try {
+ ThreadContext tc = getCurrentContext();
+ String classname = node.getPosition().getFile();
+ if (classname.endsWith(".rb")) {
+ classname = classname.substring(0, classname.length() - 3);
+ }
+ InstructionCompiler2 compiler = new InstructionCompiler2();
+ compiler.compile(classname, node.getPosition().getFile(), node);
+
+ JRubyClassLoader loader = new JRubyClassLoader();
+ Class scriptClass = compiler.loadClasses(loader);
+
+ Script script = (Script)scriptClass.newInstance();
+
+ return script.run(tc, tc.getFrameSelf());
+ } catch (JumpException je) {
+ if (je.getJumpType() == JumpException.JumpType.ReturnJump) {
+ return (IRubyObject)je.getSecondaryData();
+ } else {
+ throw je;
+ }
+ } catch (ClassNotFoundException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ } catch (InstantiationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ } catch (IllegalAccessException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
+ }
+
public RubyClass getObject() {
return objectClass;
}
@@ -472,6 +513,7 @@ private void initLibraries() {
loadService.registerBuiltin("strscan.rb", new StringScannerLibrary());
loadService.registerBuiltin("zlib.rb", new ZlibLibrary());
loadService.registerBuiltin("yaml_internal.rb", new YamlLibrary());
+ loadService.registerBuiltin("readline.rb", new Readline.Service());
}
private void initCoreClasses() {
@@ -227,14 +227,14 @@ private RubyModule getModuleWithInstanceVar(String name) {
* @param name The variable name to set
* @param value The value to set it to
*/
- public void setClassVar(String name, IRubyObject value) {
+ public IRubyObject setClassVar(String name, IRubyObject value) {
RubyModule module = getModuleWithInstanceVar(name);
if (module == null) {
module = this;
}
- module.setInstanceVariable(name, value, CVAR_TAINT_ERROR, CVAR_FREEZE_ERROR);
+ return module.setInstanceVariable(name, value, CVAR_TAINT_ERROR, CVAR_FREEZE_ERROR);
}
/**
@@ -198,10 +198,10 @@ public IRubyObject op_cmp(IRubyObject other) {
}
}
long millis_other = (other instanceof RubyTime) ? ((RubyTime) other).getTimeInMillis() : RubyNumeric.num2long(other) * 1000;
-
- if (millis > millis_other) {
+ long usec_other = (other instanceof RubyTime) ? ((RubyTime)other).usec : 0;
+ if (millis > millis_other || (millis == millis_other && usec > usec_other)) {
return RubyFixnum.one(getRuntime());
- } else if (millis < millis_other) {
+ } else if (millis < millis_other || (millis == millis_other && usec < usec_other)) {
return RubyFixnum.minus_one(getRuntime());
} else {
return RubyFixnum.zero(getRuntime());
@@ -241,6 +241,14 @@ public RubyInteger usec() {
return getRuntime().newFixnum(microseconds());
}
+ public void setMicroseconds(long mic) {
+ long millis = getTimeInMillis() % 1000;
+ long withoutMillis = getTimeInMillis() - millis;
+ withoutMillis += (mic / 1000);
+ cal.setTimeInMillis(withoutMillis);
+ usec = mic % 1000;
+ }
+
public long microseconds() {
return getTimeInMillis() % 1000 * 1000 + usec;
}
Oops, something went wrong.

0 comments on commit a2d2ab2

Please sign in to comment.