Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement nqp::ctx, nqp::ctxouter, nqp::ctxcaller.
  • Loading branch information
jnthn committed Jan 23, 2013
1 parent 39a9db0 commit eb06569
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/QAST/JASTCompiler.nqp
Expand Up @@ -1149,6 +1149,11 @@ QAST::OperationsJAST.add_hll_unbox('', $RT_STR, -> $qastcomp {
$il
});

# Context introspection
QAST::OperationsJAST.map_classlib_core_op('ctx', $TYPE_OPS, 'ctx', [], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('ctxouter', $TYPE_OPS, 'ctxouter', [$RT_OBJ], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('ctxcaller', $TYPE_OPS, 'ctxcaller', [$RT_OBJ], $RT_OBJ, :tc);

# Default way to do positional and associative lookups.
QAST::OperationsJAST.map_classlib_core_op('positional_get', $TYPE_OPS, 'atpos', [$RT_OBJ, $RT_INT], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('positional_bind', $TYPE_OPS, 'bindpos', [$RT_OBJ, $RT_INT, $RT_OBJ], $RT_OBJ, :tc);
Expand Down
44 changes: 39 additions & 5 deletions src/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -13,11 +13,7 @@
import java.util.HashMap;

import org.perl6.nqp.sixmodel.*;
import org.perl6.nqp.sixmodel.reprs.SCRefInstance;
import org.perl6.nqp.sixmodel.reprs.VMArray;
import org.perl6.nqp.sixmodel.reprs.VMHash;
import org.perl6.nqp.sixmodel.reprs.VMHashInstance;
import org.perl6.nqp.sixmodel.reprs.VMIterInstance;
import org.perl6.nqp.sixmodel.reprs.*;

/**
* Contains complex operations that are more involved that the simple ops that the
Expand Down Expand Up @@ -377,6 +373,44 @@ public static SixModelObject getdynlex(String name, ThreadContext tc) {
return null;
}

/* Context introspection. */
public static SixModelObject ctx(ThreadContext tc) {
SixModelObject ContextRef = tc.gc.ContextRef;
SixModelObject wrap = ContextRef.st.REPR.allocate(tc, ContextRef.st);
((ContextRefInstance)wrap).context = tc.curFrame;
return wrap;
}
public static SixModelObject ctxouter(SixModelObject ctx, ThreadContext tc) {
if (ctx instanceof ContextRefInstance) {
CallFrame outer = ((ContextRefInstance)ctx).context.outer;
if (outer == null)
return null;

SixModelObject ContextRef = tc.gc.ContextRef;
SixModelObject wrap = ContextRef.st.REPR.allocate(tc, ContextRef.st);
((ContextRefInstance)wrap).context = outer;
return wrap;
}
else {
throw new RuntimeException("ctxouter requires an operand with REPR ContextRef");
}
}
public static SixModelObject ctxcaller(SixModelObject ctx, ThreadContext tc) {
if (ctx instanceof ContextRefInstance) {
CallFrame caller = ((ContextRefInstance)ctx).context.caller;
if (caller == null)
return null;

SixModelObject ContextRef = tc.gc.ContextRef;
SixModelObject wrap = ContextRef.st.REPR.allocate(tc, ContextRef.st);
((ContextRefInstance)wrap).context = caller;
return wrap;
}
else {
throw new RuntimeException("ctxcaller requires an operand with REPR ContextRef");
}
}

/* Argument setting. */
public static void arg(long v, long[] args, int i) { args[i] = v; }
public static void arg(double v, double[] args, int i) { args[i] = v; }
Expand Down

0 comments on commit eb06569

Please sign in to comment.