Skip to content

Commit

Permalink
[java/runtime] catch up to dotnet in throw and catch handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mberends committed Oct 25, 2010
1 parent 1364eb1 commit 07f57e1
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 77 deletions.
18 changes: 13 additions & 5 deletions java/runtime/Makefile
Expand Up @@ -9,11 +9,13 @@
# SerializationContext
# | |
# +------------------------> RakudoObject |
# | | | | |
# | Parameter Lexpad | |
# | | | | |
# | Signature | Representation |
# | | | | | | |
# | | | | | |
# | Parameter | Lexpad | |
# | | | | | |
# | | Handler | | |
# | | | | | |
# | Signature | | Representation |
# | | | | | | | |
# | +--+----------------> RakudoCodeRef | | |
# | | | | | | |
# 1 2 3 ExecutionDomain Context | | |
Expand Down Expand Up @@ -63,6 +65,7 @@ ALL_BUILD_TARGETS = \

OTHER_DEPENDENT_TARGETS = \
$(CLASSES)Rakudo/Runtime/CaptureHelper.class \
$(CLASSES)Rakudo/Runtime/Exceptions/Handler.class \
$(CLASSES)Rakudo/Runtime/Exceptions/LeaveStackUnwinderException.class \
$(CLASSES)Rakudo/Runtime/Lexpad.class \
$(CLASSES)Rakudo/Runtime/Parameter.class \
Expand Down Expand Up @@ -123,6 +126,7 @@ $(CLASSES)Rakudo/Metamodel/RakudoObject.class: Rakudo/Metamodel/RakudoObject.jav
Rakudo/Metamodel/Representations/RakudoCodeRef.java \
Rakudo/Metamodel/REPRRegistry.java \
Rakudo/Runtime/Context.java \
Rakudo/Runtime/Exceptions/Handler.java \
Rakudo/Runtime/Exceptions/LeaveStackUnwinderException.java \
Rakudo/Runtime/MultiDispatch/LexicalCandidateFinder.java \
Rakudo/Runtime/MultiDispatch/MultiDispatcher.java \
Expand All @@ -143,6 +147,7 @@ $(CLASSES)Rakudo/Metamodel/RakudoObject.class: Rakudo/Metamodel/RakudoObject.jav
Rakudo/Metamodel/Representations/RakudoCodeRef.java \
Rakudo/Metamodel/REPRRegistry.java \
Rakudo/Runtime/Context.java \
Rakudo/Runtime/Exceptions/Handler.java \
Rakudo/Runtime/Exceptions/LeaveStackUnwinderException.java \
Rakudo/Runtime/MultiDispatch/LexicalCandidateFinder.java \
Rakudo/Runtime/MultiDispatch/MultiDispatcher.java \
Expand Down Expand Up @@ -348,6 +353,9 @@ Rakudo/Runtime/CodeObjectUtility.java: ../../dotnet/runtime/Runtime/CodeObjectUt
Rakudo/Runtime/Context.java: ../../dotnet/runtime/Runtime/Context.cs
@echo "todo: Rakudo/Runtime/Context.java is older than ../../dotnet/runtime/Runtime/Context.cs"

Rakudo/Runtime/Exceptions/Handler.java: ../../dotnet/runtime/Runtime/Exceptions/Handler.cs
@echo "todo: Rakudo/Runtime/Exceptions/Handler.java is older than ../../dotnet/runtime/Runtime/Exceptions/Handler.cs"

Rakudo/Runtime/Exceptions/LeaveStackUnwinderException.java: ../../dotnet/runtime/Runtime/Exceptions/LeaveStackUnwinderException.cs
@echo "todo: Rakudo/Runtime/Exceptions/LeaveStackUnwinderException.java is older than ../../dotnet/runtime/Runtime/Exceptions/LeaveStackUnwinderException.cs"

Expand Down
10 changes: 6 additions & 4 deletions java/runtime/Rakudo/Init.java
Expand Up @@ -40,6 +40,7 @@ public static ThreadContext Initialize(String settingName)
// Bootstrap the meta-model.
RegisterRepresentations();
RakudoObject knowHOW = KnowHOWBootstrapper.Bootstrap();
RakudoObject knowHOWAttribute = KnowHOWBootstrapper.SetupKnowHOWAttribute(knowHOW);

// Either load a named setting or use the fake bootstrapping one.
Context settingContext =
Expand All @@ -61,6 +62,7 @@ public static ThreadContext Initialize(String settingName)
threadContext.DefaultNumBoxType = settingContext.LexPad.GetByName("NQPNum");
threadContext.DefaultStrBoxType = settingContext.LexPad.GetByName("NQPStr");
threadContext.DefaultListType = settingContext.LexPad.GetByName("NQPList");

return threadContext;
}

Expand Down Expand Up @@ -96,7 +98,7 @@ private static Context BootstrapSetting(RakudoObject KnowHOW)
System.err.println( "calling new Context from Init" );
Context settingContext = new Context();
settingContext.LexPad = new Lexpad(new String[]
{ "KnowHOW", "capture", "NQPInt", "NQPNum", "NQPStr", "NQPList", "LLCode", "list" });
{ "KnowHOW", "capture", "NQPInt", "NQPNum", "NQPStr", "NQPList", "NQPCode", "list" });
RakudoCodeRef.IFunc_Body funcBody = new RakudoCodeRef.IFunc_Body()
{ // create an anonymous class
public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject capture) {
Expand Down Expand Up @@ -128,7 +130,7 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject cap
/// <param name="Name"></param>
/// <param name="KnowHOW"></param>
/// <returns></returns>
public static Context LoadSetting(String settingName, RakudoObject knowHOW)
public static Context LoadSetting(String settingName, RakudoObject knowHOW, RakudoObject knowHOWAttribute)
{
// Load the assembly.
System.err.println("Init.LoadSetting begin loading " + settingName );
Expand Down Expand Up @@ -192,9 +194,10 @@ public static Context LoadSetting(String settingName, RakudoObject knowHOW)
// Fudge a few more things in.
// XXX Should be able to toss all of these but KnowHOW.
settingContext.LexPad.Extend(new String[]
{ "KnowHOW", "print", "say", "capture", "LLCode" });
{ "KnowHOW", "KnowHOWAttribute", "print", "say", "capture", "LLCode" });

settingContext.LexPad.SetByName("KnowHOW", knowHOW);
settingContext.LexPad.SetByName("KnowHOWAttribute", knowHOWAttribute);

RakudoCodeRef.IFunc_Body funcPrint = new RakudoCodeRef.IFunc_Body()
{ // create an anonymous class
Expand Down Expand Up @@ -222,7 +225,6 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject cap
};
settingContext.LexPad.SetByName("say", CodeObjectUtility.WrapNativeMethod(funcSay));
settingContext.LexPad.SetByName("capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null,null));
settingContext.LexPad.SetByName("LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(null, knowHOW.getSTable().REPR.instance_of(null, knowHOW)));

return settingContext;
}
Expand Down
61 changes: 47 additions & 14 deletions java/runtime/Rakudo/Metamodel/KnowHOW/KnowHOWBootstrapper.java
Expand Up @@ -23,8 +23,7 @@
/// for implementing the various other bits of the object model.
/// Works in conjunction with KnowHOWREPR.
/// </summary>
public class KnowHOWBootstrapper
// public static class KnowHOWBootstrapper // the C# version
public class KnowHOWBootstrapper // public static in the C# version
{
/// <summary>
/// Bootstraps the KnowHOW. This is were things "bottom out" in the
Expand Down Expand Up @@ -117,9 +116,7 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject
return HOW.Methods.get(Ops.unbox_str(tc, Positionals[1]));
else {
// throw new NoSuchMethodException("No such method " + Ops.unbox_str(tc, Positionals[1]));
System.err.println("No such method " + Ops.unbox_str(tc, Positionals[1]));
System.exit(1);
return null;
throw new UnsupportedOperationException("No such method " + Ops.unbox_str(tc, Positionals[1]));
}
}
};
Expand All @@ -141,6 +138,9 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject
{ // create an anonymous class
public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject capture)
{
// Safe to just return a P6list instance that points at
// the same thing we hold internally, since a list is
// immutable.
KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)CaptureHelper.GetPositional(capture, 0);
RakudoObject Result = tc.DefaultListType.getSTable().REPR.instance_of(tc, tc.DefaultListType);
((P6list.Instance)Result).Storage = HOW.Attributes;
Expand Down Expand Up @@ -181,11 +181,11 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject ignored, RakudoObject
KnowHOWHOW.Methods.put(key, KnowHOWMeths.get(key));

// We need to clone the STable.
SharedTable STableCopy = new SharedTable();
STableCopy.HOW = KnowHOWHOW;
STableCopy.WHAT = KnowHOW.getSTable().WHAT;
STableCopy.REPR = KnowHOW.getSTable().REPR;
KnowHOWHOW.setSTable(STableCopy);
SharedTable sTableCopy = new SharedTable();
sTableCopy.HOW = KnowHOWHOW;
sTableCopy.WHAT = KnowHOW.getSTable().WHAT;
sTableCopy.REPR = KnowHOW.getSTable().REPR;
KnowHOWHOW.setSTable(sTableCopy);

// And put a fake FindMethod in there that just looks in the
// dictionary.
Expand All @@ -197,10 +197,7 @@ public RakudoObject FindMethod(ThreadContext tc, RakudoObject obj, String name,
if (MTable.containsKey(name))
return MTable.get(name);
else {
// throw new InvalidOperationException("No such method " + Name);
System.err.println("No such method " + name);
System.exit(1);
return null;
throw new UnsupportedOperationException("No such method " + name);
}
}
};
Expand All @@ -212,5 +209,41 @@ public RakudoObject FindMethod(ThreadContext tc, RakudoObject obj, String name,
// And we should be done.
return KnowHOW;
}

/// <summary>
/// Sets up the KnowHOWAttribute object/class, which actually is a
/// KnowHOW.
/// </summary>
/// <returns></returns>
public static RakudoObject SetupKnowHOWAttribute(RakudoObject knowHOW)
{
// Create a new HOW instance.
KnowHOWREPR.KnowHOWInstance HOW = (KnowHOWREPR.KnowHOWInstance)knowHOW.getSTable().REPR.instance_of(null, knowHOW);

// We base the attribute on P6str, since we just want to store an
// attribute name for now.
RakudoObject knowHOWAttribute = REPRRegistry.get_REPR_by_name("P6str").type_object_for(null, HOW);

// Add methods new and Str.
RakudoCodeRef.IFunc_Body meth_new = new RakudoCodeRef.IFunc_Body() { // the C# version uses a lambda
public RakudoObject Invoke(ThreadContext tc, RakudoObject obj, RakudoObject capture)
{
RakudoObject WHAT = CaptureHelper.GetPositional(capture, 0).getSTable().WHAT;
String name = Ops.unbox_str(tc, CaptureHelper.GetNamed(capture, "name"));
return Ops.box_str(tc, name, WHAT);
}
};
HOW.Methods.put("new", CodeObjectUtility.WrapNativeMethod(meth_new));
RakudoCodeRef.IFunc_Body meth_name = new RakudoCodeRef.IFunc_Body() { // the C# version uses a lambda
public RakudoObject Invoke(ThreadContext tc, RakudoObject obj, RakudoObject capture)
{
RakudoObject self = CaptureHelper.GetPositional(capture, 0);
return Ops.box_str(tc, Ops.unbox_str(tc, self), tc.DefaultStrBoxType);
}
};
HOW.Methods.put("name", CodeObjectUtility.WrapNativeMethod(meth_name));

return knowHOWAttribute;
}
}

6 changes: 3 additions & 3 deletions java/runtime/Rakudo/Metamodel/RakudoObject.java
Expand Up @@ -13,9 +13,9 @@ public abstract class RakudoObject
/// Every object must have a way to refer to the shared table,
/// which contains the commonalities this object has.
/// </summary>
private SharedTable STable;
public SharedTable getSTable() { return STable; }
public void setSTable( SharedTable st ) { STable = st; }
private SharedTable sTable;
public SharedTable getSTable() { return sTable; }
public void setSTable( SharedTable st ) { sTable = st; }
// SharedTable STable { get; set; } // the C# version

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

import java.util.HashMap;

import Rakudo.Runtime.Context;
import Rakudo.Runtime.Exceptions.Handler;
import Rakudo.Runtime.ThreadContext;
import Rakudo.Runtime.Lexpad;
import Rakudo.Runtime.Parameter;
Expand All @@ -23,8 +25,8 @@ public final class RakudoCodeRef implements Representation
// implement it, as the Java equivalent of a C# lambda expression.
// Used in for example: CodeObjectUtility
public interface IFunc_Body {
public RakudoObject Invoke(ThreadContext tc, RakudoObject ro1, RakudoObject ro2);
}
public RakudoObject Invoke(ThreadContext tc, RakudoObject meth, RakudoObject capt);
} // C# has public Func<ThreadContext, RakudoObject, RakudoObject, RakudoObject>; // TODO: why 4 parameters and not 3?

/// <summary>
/// This is how the boxed form of a P6str looks.
Expand All @@ -35,7 +37,6 @@ public final class Instance extends RakudoObject
/// The code body - the thing that actually runs instructions.
/// </summary>
public RakudoCodeRef.IFunc_Body Body; // IFunc_Body is defined above Instance
// public Func<ThreadContext, RakudoObject, RakudoObject, RakudoObject> Body;

/// <summary>
/// The static lexpad.
Expand All @@ -57,6 +58,11 @@ public final class Instance extends RakudoObject
/// </summary>
public Context CurrentContext;

/// <summary>
/// Exception handlers this block has, if any.
/// </summary>
public Handler[] Handlers;

/// <summary>
/// Constructor.
/// </summary>
Expand All @@ -81,10 +87,10 @@ public RakudoObject type_object_for(ThreadContext tc, RakudoObject MetaPackage)

// Also twiddle the S-Table's Invoke to invoke the contained
// function.
sTable.Invoke = new IFunc_Body() { // create an anonymous class
public RakudoObject Invoke( ThreadContext TC, RakudoObject Obj, RakudoObject Cap )
sTable.Invoke = new IFunc_Body() { // the C# version has a lambda
public RakudoObject Invoke( ThreadContext tci, RakudoObject meth, RakudoObject capt )
{
return ((RakudoCodeRef.Instance)Obj).Body.Invoke(TC, Obj, Cap);
return ((RakudoCodeRef.Instance)meth).Body.Invoke(tci, meth, capt);
}
};
return sTable.WHAT;
Expand Down

0 comments on commit 07f57e1

Please sign in to comment.