package org.perl6.nqp.sixmodel.reprs;

import org.perl6.nqp.runtime.CallFrame;
import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.StaticCodeInfo;
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.SixModelObject;

public class ContextRefInstance extends SixModelObject {
    public CallFrame context;
    
    public SixModelObject at_key_boxed(ThreadContext tc, String key) {
        Integer idx = context.codeRef.staticInfo.oTryGetLexicalIdx(key);
        return idx == null ? null : context.oLex[idx];
    }
    
    public void at_key_native(ThreadContext tc, String key) {
        Integer idx = context.codeRef.staticInfo.iTryGetLexicalIdx(key);
        if (idx != null) {
            tc.native_i = context.iLex[idx];
            tc.native_type = ThreadContext.NATIVE_INT;
            return;
        }
        idx = context.codeRef.staticInfo.nTryGetLexicalIdx(key);
        if (idx != null) {
            tc.native_n = context.nLex[idx];
            tc.native_type = ThreadContext.NATIVE_NUM;
            return;
        }
        idx = context.codeRef.staticInfo.sTryGetLexicalIdx(key);
        if (idx != null) {
            tc.native_s = context.sLex[idx];
            tc.native_type = ThreadContext.NATIVE_STR;
            return;
        }
        throw ExceptionHandling.dieInternal(tc, "No lexical " + key + " in this lexpad");
    }
    
    public long exists_key(ThreadContext tc, String key) {
        StaticCodeInfo sci = context.codeRef.staticInfo;
        return sci.oTryGetLexicalIdx(key) != null ||
               sci.iTryGetLexicalIdx(key) != null ||
               sci.nTryGetLexicalIdx(key) != null ||
               sci.sTryGetLexicalIdx(key) != null
               ? 1 : 0;
    }
}

@@ -1387,32 +1387,1194 @@
 d%22);%0A    %7D%0A    %0A
+    public void bind_key_boxed(ThreadContext tc, String key, SixModelObject value) %7B%0A        Integer idx = context.codeRef.staticInfo.oTryGetLexicalIdx(key);%0A        if (idx == null)%0A            throw ExceptionHandling.dieInternal(tc, %22No lexical %22 + key + %22 in this lexpad%22);%0A        context.oLex%5Bidx%5D = value;%0A    %7D%0A    %0A    public void bind_key_native(ThreadContext tc, String key) %7B%0A        Integer idx = context.codeRef.staticInfo.iTryGetLexicalIdx(key);%0A        if (idx != null) %7B%0A            context.iLex%5Bidx%5D = tc.native_i;%0A            tc.native_type = ThreadContext.NATIVE_INT;%0A            return;%0A        %7D%0A        idx = context.codeRef.staticInfo.nTryGetLexicalIdx(key);%0A        if (idx != null) %7B%0A            context.nLex%5Bidx%5D = tc.native_n;%0A            tc.native_type = ThreadContext.NATIVE_NUM;%0A            return;%0A        %7D%0A        idx = context.codeRef.staticInfo.sTryGetLexicalIdx(key);%0A        if (idx != null) %7B%0A            context.sLex%5Bidx%5D = tc.native_s;%0A            tc.native_type = ThreadContext.NATIVE_STR;%0A            return;%0A        %7D%0A        throw ExceptionHandling.dieInternal(tc, %22No lexical %22 + key + %22 in this lexpad%22);%0A    %7D%0A    %0A
     public long 

Implement keyed binding in ContextRef REPR.