Skip to content

Commit

Permalink
First stab at serializable AST via Externalizable. Still not faster t…
Browse files Browse the repository at this point in the history
…han parsing.
  • Loading branch information
headius committed Jun 15, 2010
1 parent 60f553c commit ee7db8e
Show file tree
Hide file tree
Showing 181 changed files with 3,029 additions and 180 deletions.
5 changes: 4 additions & 1 deletion lib/ruby/site_ruby/1.8/rubygems/source_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ def load_specification(file_name)
end.untaint

begin
gemspec = eval spec_code, binding, file_name
p "loading: #{file_name}"
Kernel.load file_name
gemspec = $last_gemspec
# gemspec = eval spec_code, binding, file_name

if gemspec.is_a?(Gem::Specification)
gemspec.loaded_from = file_name
Expand Down
2 changes: 2 additions & 0 deletions lib/ruby/site_ruby/1.8/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ def initialize name = nil, version = nil
yield self if block_given?

@@gather.call(self) if @@gather

$last_gemspec = self
end

##
Expand Down
73 changes: 69 additions & 4 deletions src/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.Stack;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -126,9 +125,12 @@
import com.kenai.constantine.platform.Errno;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.EnumSet;
import java.util.concurrent.atomic.AtomicLong;
import java.util.zip.GZIPOutputStream;
import org.jruby.ast.RootNode;
import org.jruby.exceptions.Unrescuable;
import org.jruby.internal.runtime.methods.DynamicMethod;
Expand Down Expand Up @@ -2529,19 +2531,82 @@ private void printErrorPos(ThreadContext context, PrintStream errorStream) {
}
}
}

public void loadFile(String scriptName, InputStream in, boolean wrap) {
IRubyObject self = wrap ? TopSelfFactory.createTopSelf(this) : getTopSelf();
ThreadContext context = getCurrentContext();
String file = context.getFile();

try {
secure(4); /* should alter global state */

context.setFile(scriptName);
context.preNodeEval(objectClass, self, scriptName);

Node node = parseFile(in, scriptName, null);

node.interpret(this, context, self, Block.NULL_BLOCK);
} catch (JumpException.ReturnJump rj) {
return;
} finally {
context.postNodeEval();
context.setFile(file);
}
}

public void loadFile(String scriptName, String filename, InputStream in, boolean wrap) {
IRubyObject self = wrap ? TopSelfFactory.createTopSelf(this) : getTopSelf();
ThreadContext context = getCurrentContext();
String file = context.getFile();

try {
secure(4); /* should alter global state */

context.setFile(scriptName);
context.preNodeEval(objectClass, self, scriptName);

parseFile(in, scriptName, null).interpret(this, context, self, Block.NULL_BLOCK);
Node node = parseFile(in, scriptName, null);

if (config.getSaveRbjFiles()) {
try {
System.out.println("saving: " + filename + "j");
FileOutputStream fos = new FileOutputStream(filename + "j");
// GZIPOutputStream zos = new GZIPOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(node);
oos.close();
// zos.close();
fos.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
// ignore
ioe.printStackTrace();
// } catch (Exception e) {
// e.printStackTrace();
}
}
node.interpret(this, context, self, Block.NULL_BLOCK);
} catch (JumpException.ReturnJump rj) {
return;
} finally {
context.postNodeEval();
context.setFile(file);
}
}

public void loadFile(String scriptName, Node node, boolean wrap) {
IRubyObject self = wrap ? TopSelfFactory.createTopSelf(this) : getTopSelf();
ThreadContext context = getCurrentContext();
String file = context.getFile();

try {
secure(4); /* should alter global state */

context.setFile(scriptName);
context.preNodeEval(objectClass, self, scriptName);

node.interpret(this, context, self, Block.NULL_BLOCK);
} catch (JumpException.ReturnJump rj) {
return;
} finally {
Expand Down
12 changes: 12 additions & 0 deletions src/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public boolean shouldPrecompileAll() {
private String inPlaceBackupExtension = null;
private boolean parserDebug = false;
private String threadDumpSignal = null;
private boolean saveRbjFiles = false;

private int safeLevel = 0;

Expand Down Expand Up @@ -1171,6 +1172,9 @@ private void processArgument() {
INLINE_DYNCALL_ENABLED = true;
RubyException.TRACE_TYPE = RubyException.RUBY_COMPILED;
break FOR;
} else if (argument.equals("--rbj")) {
saveRbjFiles = true;
break FOR;
} else if (argument.equals("--1.9")) {
setCompatVersion(CompatVersion.RUBY1_9);
break FOR;
Expand Down Expand Up @@ -1548,4 +1552,12 @@ public ASTCompiler newCompiler() {
public String getThreadDumpSignal() {
return threadDumpSignal;
}

public boolean getSaveRbjFiles() {
return saveRbjFiles;
}

public void setSaveRbjFiles(boolean saveRbjFiles) {
this.saveRbjFiles = saveRbjFiles;
}
}
20 changes: 20 additions & 0 deletions src/org/jruby/ast/AliasNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
***** END LICENSE BLOCK *****/
package org.jruby.ast;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.List;

import org.jruby.Ruby;
Expand All @@ -44,9 +47,14 @@
/** Represents an alias statement (<code>alias newName oldName</code>).
*/
public class AliasNode extends Node {
private static final long serialVersionUID = 0L;
private Node oldName;
private Node newName;

public AliasNode() {
super();
}

public AliasNode(ISourcePosition position, Node newName, Node oldName) {
super(position);
this.oldName = oldName;
Expand Down Expand Up @@ -92,4 +100,16 @@ public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject se

return RuntimeHelpers.defineAlias(context, newerName, olderName);
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeObject(oldName);
out.writeObject(newName);
}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
oldName = (Node)in.readObject();
newName = (Node)in.readObject();
}
}
24 changes: 22 additions & 2 deletions src/org/jruby/ast/AndNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
***** END LICENSE BLOCK *****/
package org.jruby.ast;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.List;

import org.jruby.Ruby;
Expand All @@ -44,8 +47,13 @@
* Represents a && (and) operator.
*/
public class AndNode extends Node implements BinaryOperatorNode {
private final Node firstNode;
private final Node secondNode;
private static final long serialVersionUID = 0L;
private Node firstNode;
private Node secondNode;

public AndNode() {
super();
}

public AndNode(ISourcePosition position, Node firstNode, Node secondNode) {
super(position);
Expand Down Expand Up @@ -93,4 +101,16 @@ public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject se

return secondNode.interpret(runtime, context, self, aBlock);
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeObject(firstNode);
out.writeObject(secondNode);
}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
firstNode = (Node)in.readObject();
secondNode = (Node)in.readObject();
}
}
20 changes: 20 additions & 0 deletions src/org/jruby/ast/ArgAuxillaryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
***** END LICENSE BLOCK *****/
package org.jruby.ast;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.List;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.lexer.yacc.ISourcePosition;
Expand All @@ -35,9 +38,14 @@
* @author enebo
*/
public class ArgAuxillaryNode extends Node {
private static final long serialVersionUID = 0L;
private String name;
private int offset;

public ArgAuxillaryNode() {
super();
}

public ArgAuxillaryNode(ISourcePosition position, String name, int offset) {
super(position);
this.name = name;
Expand Down Expand Up @@ -65,4 +73,16 @@ public Object accept(NodeVisitor visitor) {
public List<Node> childNodes() {
throw new UnsupportedOperationException("Not supported yet.");
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeUTF(name);
out.writeInt(offset);
}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
name = in.readUTF();
offset = in.readInt();
}
}
24 changes: 22 additions & 2 deletions src/org/jruby/ast/ArgsCatNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
***** END LICENSE BLOCK *****/
package org.jruby.ast;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.List;

import org.jruby.Ruby;
Expand All @@ -42,8 +45,13 @@
import org.jruby.runtime.builtin.IRubyObject;

public class ArgsCatNode extends Node {
private final Node firstNode;
private final Node secondNode;
private static final long serialVersionUID = 0L;
private Node firstNode;
private Node secondNode;

public ArgsCatNode() {
super();
}

public ArgsCatNode(ISourcePosition position, Node firstNode, Node secondNode) {
super(position);
Expand Down Expand Up @@ -89,4 +97,16 @@ public IRubyObject interpret(Ruby runtime, ThreadContext context, IRubyObject se

return RuntimeHelpers.ensureRubyArray(runtime, args).concat(secondArgs);
}

public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeObject(firstNode);
out.writeObject(secondNode);
}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
firstNode = (Node)in.readObject();
secondNode = (Node)in.readObject();
}
}
5 changes: 5 additions & 0 deletions src/org/jruby/ast/ArgsNoArgNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
* @author enebo
*/
public class ArgsNoArgNode extends ArgsNode {
private static final long serialVersionUID = 0L;
public ArgsNoArgNode() {
super();
}

public ArgsNoArgNode(ISourcePosition position) {
super(position, null, null, null, null, null);
}
Expand Down
Loading

0 comments on commit ee7db8e

Please sign in to comment.