Skip to content

Commit

Permalink
Tweaks here and there to make procs serializable.
Browse files Browse the repository at this point in the history
Caveats:

* Only interpreted procs will serialize, since compiled/jitted don't safe their bytecode.
* Wherever a class is referenced (object metaclass, const scoping) the name is stored and looked up on the other side.
* Assumes we're loading into the one global org.jruby.Ruby instance; this could be made Thread-local as well.
  • Loading branch information
headius committed Mar 23, 2011
1 parent ee7db8e commit 3f63adc
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/org/jruby/RubyBasicObject.java
Expand Up @@ -27,6 +27,7 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -1897,4 +1898,14 @@ private void callFinalizer(IRubyObject finalizer) {
finalizer, "call", id);
}
}

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeUTF(metaClass.getName());
}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
metaClass = (RubyClass)Ruby.getGlobalRuntime().getClassFromPath(in.readUTF());
}
}
17 changes: 17 additions & 0 deletions src/org/jruby/parser/StaticScope.java
Expand Up @@ -483,4 +483,21 @@ public String toString() {

return buf.toString();
}

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
if (cref != null) {
out.writeObject(cref.getName());
} else {
out.writeObject(null);
}
}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
Object crefName = in.readObject();
if (crefName != null) {
cref = Ruby.getGlobalRuntime().getClassFromPath(crefName.toString());
}
}
}
5 changes: 3 additions & 2 deletions src/org/jruby/runtime/Binding.java
Expand Up @@ -32,20 +32,21 @@
***** END LICENSE BLOCK *****/
package org.jruby.runtime;

import java.io.Serializable;
import org.jruby.RubyModule;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.builtin.IRubyObject;

/**
* Internal live representation of a block ({...} or do ... end).
*/
public class Binding {
public class Binding implements Serializable {

/**
* frame of method which defined this block
*/
private final Frame frame;
private final RubyModule klass;
private transient final RubyModule klass;

private Visibility visibility;
/**
Expand Down
3 changes: 2 additions & 1 deletion src/org/jruby/runtime/Block.java
Expand Up @@ -41,14 +41,15 @@

package org.jruby.runtime;

import java.io.Serializable;
import org.jruby.RubyModule;
import org.jruby.RubyProc;
import org.jruby.runtime.builtin.IRubyObject;

/**
* Internal live representation of a block ({...} or do ... end).
*/
public final class Block {
public final class Block implements Serializable {
public enum Type { NORMAL, PROC, LAMBDA, THREAD }

/**
Expand Down
3 changes: 2 additions & 1 deletion src/org/jruby/runtime/BlockBody.java
Expand Up @@ -41,6 +41,7 @@

package org.jruby.runtime;

import java.io.Serializable;
import org.jruby.RubyArray;
import org.jruby.RubyModule;
import org.jruby.ast.IterNode;
Expand All @@ -53,7 +54,7 @@
/**
* The executable body portion of a closure.
*/
public abstract class BlockBody {
public abstract class BlockBody implements Serializable {
// FIXME: Maybe not best place, but move it to a good home
public static final int ZERO_ARGS = 0;
public static final int MULTIPLE_ASSIGNMENT = 1;
Expand Down
3 changes: 2 additions & 1 deletion src/org/jruby/runtime/DynamicScope.java
Expand Up @@ -8,6 +8,7 @@

package org.jruby.runtime;

import java.io.Serializable;
import org.jruby.Ruby;
import org.jruby.parser.BlockStaticScope;
import org.jruby.runtime.scope.ManyVarsDynamicScope;
Expand All @@ -21,7 +22,7 @@
import org.jruby.runtime.scope.ThreeVarDynamicScope;
import org.jruby.runtime.scope.TwoVarDynamicScope;

public abstract class DynamicScope {
public abstract class DynamicScope implements Serializable {
// Static scoping information for this scope
protected final StaticScope staticScope;

Expand Down
5 changes: 3 additions & 2 deletions src/org/jruby/runtime/Frame.java
Expand Up @@ -32,6 +32,7 @@
***** END LICENSE BLOCK *****/
package org.jruby.runtime;

import java.io.Serializable;
import org.jruby.RubyModule;
import org.jruby.runtime.builtin.IRubyObject;

Expand Down Expand Up @@ -60,9 +61,9 @@
*
* @see ThreadContext
*/
public final class Frame {
public final class Frame implements Serializable {
/** The class against which this call is executing. */
private RubyModule klazz;
private transient RubyModule klazz;

/** The 'self' for this frame. */
private IRubyObject self;
Expand Down
3 changes: 2 additions & 1 deletion src/org/jruby/runtime/InterpretedBlock.java
Expand Up @@ -32,6 +32,7 @@
***** END LICENSE BLOCK *****/
package org.jruby.runtime;

import java.io.Serializable;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.ast.IterNode;
Expand Down Expand Up @@ -76,7 +77,7 @@
*
* @see SharedScopeBlock, CompiledBlock
*/
public class InterpretedBlock extends BlockBody {
public class InterpretedBlock extends BlockBody implements Serializable {
/** This block has no arguments at all (simple secondary optimization @see assignerFor for an
* explanation).
*/
Expand Down
3 changes: 2 additions & 1 deletion src/org/jruby/runtime/assigner/Assigner.java
Expand Up @@ -29,6 +29,7 @@

package org.jruby.runtime.assigner;

import java.io.Serializable;
import org.jruby.Ruby;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.runtime.Block;
Expand All @@ -50,7 +51,7 @@
* There is also some logic about expanded/non-expanded arguments. This refers to
* ParserSupport.new_yield and YieldNode expanded attribute.
*/
public abstract class Assigner {
public abstract class Assigner implements Serializable {

public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, Block block);
public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject value1,
Expand Down

0 comments on commit 3f63adc

Please sign in to comment.