Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dotnet] Start working towards some closure support by adding new_clo…
…sure and capture_outer ops.
  • Loading branch information
jnthn committed Oct 29, 2010
1 parent ac08da5 commit 1bb02e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions dotnet/runtime/Metamodel/Representations/RakudoCodeRef.cs
Expand Up @@ -44,6 +44,11 @@ public sealed class Instance : RakudoObject
/// </summary>
public Context CurrentContext;

/// <summary>
/// The outer context to use for the next invocation, if any.
/// </summary>
public Context OuterForNextInvocation;

/// <summary>
/// Exception handlers this block has, if any.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions dotnet/runtime/Runtime/Ops.cs
Expand Up @@ -746,5 +746,42 @@ public static RakudoObject throw_lexical(ThreadContext TC, RakudoObject Exceptio
Exceptions.ExceptionDispatcher.DieFromUnhandledException(TC, ExceptionObject);
return null; // Unreachable; above call exits always.
}

/// <summary>
/// Makes the outer context of the provided block be set to the current
/// context.
/// </summary>
/// <param name="TC"></param>
/// <param name="Block"></param>
/// <returns></returns>
public static RakudoObject capture_outer(ThreadContext TC, RakudoCodeRef.Instance Block)
{
Block.OuterForNextInvocation = TC.CurrentContext;
return Block;
}

/// <summary>
/// Creates a clone of the given code object, and makes its outer context
/// be set to the current context.
/// </summary>
/// <param name="TC"></param>
/// <param name="Block"></param>
/// <returns></returns>
public static RakudoObject new_closure(ThreadContext TC, RakudoCodeRef.Instance Block)
{
// Clone all but OuterForNextInvocation and SC (since it's a new
// object and doesn't live in any SC yet).
var NewBlock = new RakudoCodeRef.Instance(Block.STable);
NewBlock.Body = Block.Body;
NewBlock.CurrentContext = Block.CurrentContext;
NewBlock.Handlers = Block.Handlers;
NewBlock.OuterBlock = Block.OuterBlock;
NewBlock.Sig = Block.Sig;
NewBlock.StaticLexPad = Block.StaticLexPad;

// Set the outer for next invocation and return the cloned block.
NewBlock.OuterForNextInvocation = TC.CurrentContext;
return NewBlock;
}
}
}

0 comments on commit 1bb02e7

Please sign in to comment.