Skip to content

Commit

Permalink
[java] expand the dependency circle with Context and ThreadContext
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Berends committed Aug 28, 2010
1 parent 1de869a commit 78103c0
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 44 deletions.
53 changes: 25 additions & 28 deletions java/runtime/Makefile
Expand Up @@ -4,7 +4,8 @@ ALL_BUILD_TARGETS = \
Rakudo/Metamodel/Hints.class \
Rakudo/Serialization/SerializationContext.class \
Rakudo/Runtime/ExecutionDomain.class \
Rakudo/Metamodel/RakudoObject.class
Rakudo/Metamodel/RakudoObject.class \
Rakudo/Metamodel/REPRRegistry.class

# Rakudo/Metamodel/KnowHOW/KnowHOWREPR.class

Expand All @@ -13,8 +14,10 @@ OTHER_DEPENDENT_TARGETS = \
Rakudo/Runtime/Parameter.class \
Rakudo/Runtime/Signature.class \
Rakudo/Metamodel/Representation.class \
Rakudo/Metamodel/REPRRegistry.class \
Rakudo/Metamodel/Representations/RakudoCodeRef.class \
Rakudo/Runtime/Context.class \
Rakudo/Runtime/ThreadContext.class \
Rakudo/Metamodel/IFindMethod.class \
Rakudo/Metamodel/SharedTable.class

all: $(ALL_BUILD_TARGETS)
Expand All @@ -30,44 +33,37 @@ Rakudo/Runtime/ExecutionDomain.class: Rakudo/Runtime/ExecutionDomain.java
javac Rakudo/Runtime/ExecutionDomain.java

# There is a long circular dependency from RakudoObject to SharedTable.
# Handle it by compiling all the files involved in a single step.
# Handle it by compiling all the interdependent files in a single step.
# Thanks jnthn++ for the suggestion.
Rakudo/Metamodel/RakudoObject.class: Rakudo/Metamodel/RakudoObject.java \
Rakudo/Serialization/SerializationContext.class \
Rakudo/Metamodel/Representation.java \
Rakudo/Metamodel/REPRRegistry.java \
Rakudo/Runtime/Lexpad.java \
Rakudo/Runtime/Parameter.java \
Rakudo/Runtime/Signature.java \
Rakudo/Metamodel/Representations/RakudoCodeRef.java \
Rakudo/Runtime/Context.java \
Rakudo/Runtime/ThreadContext.java \
Rakudo/Metamodel/IFindMethod.java \
Rakudo/Metamodel/SharedTable.java
javac \
Rakudo/Metamodel/RakudoObject.java \
Rakudo/Metamodel/Representation.java \
Rakudo/Metamodel/REPRRegistry.java \
Rakudo/Runtime/Lexpad.java \
Rakudo/Runtime/Parameter.java \
Rakudo/Runtime/Signature.java \
Rakudo/Metamodel/Representations/RakudoCodeRef.java \
Rakudo/Runtime/Context.java \
Rakudo/Runtime/ThreadContext.java \
Rakudo/Metamodel/IFindMethod.java \
Rakudo/Metamodel/SharedTable.java

Rakudo/Metamodel/Representations/RakudoCodeRef.class: Rakudo/Metamodel/Representations/RakudoCodeRef.java \
javac Rakudo/Runtime/ThreadContext.class
javac Rakudo/Metamodel/Representations/RakudoCodeRef.java

Rakudo/Runtime/Context.class: Rakudo/Runtime/Context.java \
Rakudo/Metamodel/Representations/RakudoCodeRef.class \
Rakudo/Metamodel/RakudoObject.class
javac Rakudo/Runtime/Context.java

Rakudo/Runtime/ThreadContext.class: Rakudo/Runtime/ThreadContext.java \
Rakudo/Runtime/Context.class \
Rakudo/Metamodel/RakudoObject.class
javac Rakudo/Runtime/ThreadContext.java
Rakudo/Metamodel/REPRRegistry.class: Rakudo/Metamodel/REPRRegistry.java
javac Rakudo/Metamodel/REPRRegistry.java

# Dependency critical paths (1 2 and 3 are circular)
# (partial diagram - less significant dependencies omitted for simplicity)
#
#
# Hints
# |
# SerializationContext ExecutionDomain
Expand All @@ -77,17 +73,18 @@ Rakudo/Runtime/ThreadContext.class: Rakudo/Runtime/ThreadContext.java \
# | Parameter |
# | | |
# | Signature Representation
# | | | | |
# | +-+---> RakudoCodeRef | REPRRegistry
# | | | | |
# 1 2 3 Context |
# | | | | |
# | | +--- ThreadContext |
# | | | |
# | | IFindMethod |
# | | | |
# | +--+---> RakudoCodeRef |
# | | | | |
# 1 2 3 Context |
# | | | | |
# | | +---- ThreadContext |
# | | | |
# | | IFindMethod |
# | | | |
# +-+-------------- SharedTable
# +-+-------------- SharedTable REPRRegistry
# |
#

Rakudo/Metamodel/IFindMethod.class: Rakudo/Metamodel/IFindMethod.java
Rakudo/Metamodel/RakudoObject.class \
Expand Down
9 changes: 9 additions & 0 deletions java/runtime/Rakudo/Metamodel/IFindMethod.java
@@ -0,0 +1,9 @@
package Rakudo.Metamodel;

import Rakudo.Metamodel.RakudoObject;
import Rakudo.Runtime.ThreadContext;

public interface IFindMethod {
public RakudoObject FindMethod(
ThreadContext tc, RakudoObject ro, String s, int i);
}
4 changes: 2 additions & 2 deletions java/runtime/Rakudo/Metamodel/RakudoObject.java
Expand Up @@ -13,8 +13,8 @@ public interface RakudoObject
/// Every object must have a way to refer to the shared table,
/// which contains the commonalities this object has.
/// </summary>
// SharedTable getSTable();
// void setSTable( SharedTable st );
SharedTable getSTable();
void setSTable( SharedTable st );
// SharedTable STable { get; set; } // the C# version

/// <summary>
Expand Down
18 changes: 11 additions & 7 deletions java/runtime/Rakudo/Metamodel/Representations/RakudoCodeRef.java
@@ -1,7 +1,7 @@
package Rakudo.Metamodel.Representations;

import java.util.HashMap; // HashMap
//import Rakudo.Runtime.Context;
import Rakudo.Runtime.Context;
//import Rakudo.Runtime.ThreadContext;
import Rakudo.Runtime.Lexpad;
import Rakudo.Runtime.Parameter;
Expand All @@ -25,10 +25,14 @@ public final class RakudoCodeRef implements Representation
/// </summary>
public final class Instance implements RakudoObject
{
private SharedTable st;
public SharedTable getSTable(){return st;}
public void setSTable( SharedTable st ){return;}

private SerializationContext sc;
public SerializationContext getSC() { return new SerializationContext(); }
public SerializationContext getSC() { return sc; }
public void setSC( SerializationContext sc ) {;}
/*
/* TODO
/// <summary>
/// The code body - the thing that actually runs instructions.
/// </summary>
Expand All @@ -49,12 +53,12 @@ public final class Instance implements RakudoObject
/// Signature object.
/// </summary>
public Signature Sig;
/*

/// <summary>
/// The context currently using this sub.
/// </summary>
public Context CurrentContext;
/*
/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -67,7 +71,7 @@ public Instance(SharedTable sharedTable)
public RakudoObject type_object_for(RakudoObject how){return new Instance();}
public RakudoObject instance_of(RakudoObject what){return new Instance();}
public boolean defined(RakudoObject obj){return false;}
/*
/* TODO
/// <summary>
/// Create a new type object.
/// </summary>
Expand Down Expand Up @@ -98,7 +102,7 @@ public RakudoObject type_object_for(RakudoObject MetaPackage)
/// Creates an instance of the type with the given type object.
/// </summary>
/// <param name="WHAT"></param>
/// <returns></returns>
/// <returns></returns> TODO
public RakudoObject instance_of(RakudoObject rakudoobject)
{
return new Instance(rakudoobject.STable);
Expand Down
14 changes: 7 additions & 7 deletions java/runtime/Rakudo/Metamodel/SharedTable.java
Expand Up @@ -2,6 +2,7 @@

import Rakudo.Metamodel.RakudoObject;
import Rakudo.Metamodel.Representation;
import Rakudo.Runtime.ThreadContext;
import Rakudo.Serialization.SerializationContext;

/// <summary>
Expand All @@ -15,13 +16,12 @@ public class SharedTable
/// <summary>
/// This finds a method with the given name or using a hint.
/// </summary>
// public Object FindMethod =
// new IFindMethod() {
// public Object FindMethod() {
// return new Object();
// }
// };

public IFindMethod FindMethod =
new IFindMethod() { // anonymous class instead of lambda-expression
public RakudoObject FindMethod(ThreadContext tc, RakudoObject ro, String s, int hint) {
return ro;
}
};

// public Func<ThreadContext, RakudoObject, string, int, RakudoObject> FindMethod =
// (TC, Obj, Name, Hint) =>
Expand Down
102 changes: 102 additions & 0 deletions java/runtime/Rakudo/Runtime/Context.java
@@ -0,0 +1,102 @@
package Rakudo.Runtime;

import java.util.*; // HashMap
import Rakudo.Metamodel.RakudoObject;
import Rakudo.Metamodel.Representations.RakudoCodeRef;

/// <summary>
/// A context represents a given invocation of a block. (Note this is
/// fairly sketchy at the moment.)
/// </summary>
public class Context
{
/// <summary>
/// The static chain.
/// </summary>
public Context Outer;

/// <summary>
/// The dynamic chain.
/// </summary>
public Context Caller;

/// <summary>
/// The static code object.
/// </summary>
public RakudoCodeRef.Instance StaticCodeObject;

/// <summary>
/// Lexpad. Note that we'll in the end have something much smarter
/// for this but it'll do for now.
/// </summary>
public HashMap<String, RakudoObject> LexPad;

/// <summary>
/// The capture passed as part of the current call.
/// </summary>
public RakudoObject Capture;

/// <summary>
/// Creates an empty, uninitialized context.
/// </summary>
public Context()
{
}

/// <summary>
/// Initializes the context.
/// </summary>
/// <param name="StaticCodeObject"></param>
/// <param name="Caller"></param>
public Context(RakudoCodeRef.Instance StaticCodeObject, Context Caller, RakudoObject Capture)
{
// Set up static code object and caller pointers.
this.StaticCodeObject = StaticCodeObject;
this.Caller = Caller;
this.Capture = Capture;

// Static sub object should have this as the current
// context.
// StaticCodeObject.CurrentContext = this;

// Lex pad should be copy of the static one.
// XXX This isn't quite what we want in the long run, but it
// does fine for now.
// this.LexPad = new HashMap<String, RakudoObject>(StaticCodeObject.StaticLexPad);

// Set outer context.
RakudoCodeRef.Instance OuterBlock = StaticCodeObject.OuterBlock;
// if (OuterBlock.CurrentContext != null)
// {
// this.Outer = OuterBlock.CurrentContext;
// }
// else
{
// Auto-close. In this we go setting up fake contexts
// that use the static lexpad until we find a real one.
Context CurContext = this;
while (OuterBlock != null)
{
// If we found a block with a context, we're done.
// if (OuterBlock.CurrentContext != null)
// {
// CurContext.Outer = OuterBlock.CurrentContext;
// break;
// }

// Build the fake context.
Context OuterContext = new Context();
OuterContext.StaticCodeObject = OuterBlock;
// OuterContext.LexPad = OuterBlock.StaticLexPad;

// Link it.
CurContext.Outer = OuterContext;

// Step back one level.
CurContext = OuterContext;
OuterBlock = OuterBlock.OuterBlock;
}
}
}
}

41 changes: 41 additions & 0 deletions java/runtime/Rakudo/Runtime/ThreadContext.java
@@ -0,0 +1,41 @@
package Rakudo.Runtime;

import Rakudo.Metamodel.RakudoObject;
import Rakudo.Runtime.Context;

/// <summary>
/// We have one of these per thread that we are running.
/// </summary>
public class ThreadContext
{
/// <summary>
/// The execution domain that we're operating under.
/// </summary>
public ExecutionDomain Domain;

/// <summary>
/// The current context we're in.
/// </summary>
public Context CurrentContext;

/// <summary>
/// The type object of the bool type we box to.
/// </summary>
public RakudoObject DefaultBoolBoxType;

/// <summary>
/// The type object of the integer type we box to.
/// </summary>
public RakudoObject DefaultIntBoxType;

/// <summary>
/// The type object of the number type we box to.
/// </summary>
public RakudoObject DefaultNumBoxType;

/// <summary>
/// The type object of the string type we box to.
/// </summary>
public RakudoObject DefaultStrBoxType;
}

0 comments on commit 78103c0

Please sign in to comment.