Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement a bunch of SC-related ops.
  • Loading branch information
jnthn committed Jan 18, 2013
1 parent 555cc21 commit 06284c1
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
13 changes: 13 additions & 0 deletions lib/QAST/JASTCompiler.nqp
Expand Up @@ -1185,6 +1185,19 @@ QAST::OperationsJAST.add_core_op('substr', -> $qastcomp, $op {

# serialization context opcodes
QAST::OperationsJAST.map_classlib_core_op('sha1', $TYPE_OPS, 'sha1', [$RT_STR], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('createsc', $TYPE_OPS, 'createsc', [$RT_STR], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('scsetobj', $TYPE_OPS, 'scsetobj', [$RT_OBJ, $RT_INT, $RT_OBJ], $RT_OBJ);
QAST::OperationsJAST.map_classlib_core_op('scsetcode', $TYPE_OPS, 'scsetcode', [$RT_OBJ, $RT_INT, $RT_OBJ], $RT_OBJ);
QAST::OperationsJAST.map_classlib_core_op('scgetobj', $TYPE_OPS, 'scgetobj', [$RT_OBJ, $RT_INT], $RT_OBJ);
QAST::OperationsJAST.map_classlib_core_op('scgethandle', $TYPE_OPS, 'scgethandle', [$RT_OBJ], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('scgetobjidx', $TYPE_OPS, 'scgetobjidx', [$RT_OBJ, $RT_OBJ], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('scsetdesc', $TYPE_OPS, 'scsetdesc', [$RT_OBJ, $RT_STR], $RT_STR);
QAST::OperationsJAST.map_classlib_core_op('scobjcount', $TYPE_OPS, 'scobjcount', [$RT_OBJ], $RT_INT);
QAST::OperationsJAST.map_classlib_core_op('setobjsc', $TYPE_OPS, 'setobjsc', [$RT_OBJ, $RT_OBJ], $RT_OBJ);
QAST::OperationsJAST.map_classlib_core_op('getobjsc', $TYPE_OPS, 'getobjsc', [$RT_OBJ], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('serialize', $TYPE_OPS, 'serialize', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
QAST::OperationsJAST.map_classlib_core_op('deserialize', $TYPE_OPS, 'deserialize', [$RT_STR, $RT_OBJ, $RT_OBJ, $RT_OBJ, $RT_OBJ], $RT_STR, :tc);
QAST::OperationsJAST.map_classlib_core_op('wval', $TYPE_OPS, 'wval', [$RT_STR, $RT_INT], $RT_OBJ, :tc);

#bitwise opcodes
QAST::OperationsJAST.map_classlib_core_op('bitor_i', $TYPE_OPS, 'bitor_i', [$RT_INT, $RT_INT], $RT_INT);
Expand Down
9 changes: 7 additions & 2 deletions src/org/perl6/nqp/runtime/GlobalContext.java
Expand Up @@ -47,6 +47,11 @@ public class GlobalContext {
*/
public SixModelObject BOOTStr;

/**
* SCRef type; a basic, method-less type with the SCRef REPR.
*/
public SixModelObject SCRef;

/**
* The main, startup thread's ThreadContext.
*/
Expand All @@ -65,7 +70,7 @@ public class GlobalContext {
/**
* Serialization context wrapper object hash.
*/
public HashMap<String, SixModelObject> scWrappers;
public HashMap<String, SixModelObject> scRefs;

/**
* Initializes the runtime environment.
Expand All @@ -74,7 +79,7 @@ public GlobalContext()
{
hllConfiguration = new HashMap<String, HLLConfig>();
scs = new HashMap<String, SerializationContext>();
scWrappers = new HashMap<String, SixModelObject>();
scRefs = new HashMap<String, SixModelObject>();

mainThread = new ThreadContext(this);
KnowHOWBootstrapper.bootstrap(mainThread);
Expand Down
107 changes: 107 additions & 0 deletions src/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -7,6 +7,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;
Expand Down Expand Up @@ -1040,6 +1041,112 @@ public static String sha1(String str) throws NoSuchAlgorithmException, Unsupport
}
return sb.toString();
}
public static SixModelObject createsc(String handle, ThreadContext tc) {
if (tc.gc.scs.containsKey(handle))
throw new RuntimeException("SC with handle " + handle + "already exists");

SerializationContext sc = new SerializationContext(handle);
tc.gc.scs.put(handle, sc);

SixModelObject SCRef = tc.gc.SCRef;
SCRefInstance ref = (SCRefInstance)SCRef.st.REPR.allocate(tc, SCRef.st);
ref.referencedSC = sc;
tc.gc.scRefs.put(handle, ref);

return ref;
}
public static SixModelObject scsetobj(SixModelObject scRef, long idx, SixModelObject obj) {
if (scRef instanceof SCRefInstance) {
((SCRefInstance)scRef).referencedSC.root_objects.set((int)idx, obj);
return obj;
}
else {
throw new RuntimeException("scsetobj can only operate on an SCRef");
}
}
public static SixModelObject scsetcode(SixModelObject scRef, long idx, SixModelObject obj) {
if (scRef instanceof SCRefInstance) {
if (obj instanceof CodeRef) {
((SCRefInstance)scRef).referencedSC.root_codes.set((int)idx, (CodeRef)obj);
return obj;
}
else {
throw new RuntimeException("scsetcode can only store a CodeRef");
}
}
else {
throw new RuntimeException("scsetcode can only operate on an SCRef");
}
}
public static SixModelObject scgetobj(SixModelObject scRef, long idx) {
if (scRef instanceof SCRefInstance) {
return ((SCRefInstance)scRef).referencedSC.root_objects.get((int)idx);
}
else {
throw new RuntimeException("scgetobj can only operate on an SCRef");
}
}
public static String scgethandle(SixModelObject scRef) {
if (scRef instanceof SCRefInstance) {
return ((SCRefInstance)scRef).referencedSC.handle;
}
else {
throw new RuntimeException("scgethandle can only operate on an SCRef");
}
}
public static long scgetobjidx(SixModelObject scRef, SixModelObject find) {
if (scRef instanceof SCRefInstance) {
int idx = ((SCRefInstance)scRef).referencedSC.root_objects.indexOf(find);
if (idx < 0)
throw new RuntimeException("Object does not exist in this SC");
return idx;
}
else {
throw new RuntimeException("scgetobjidx can only operate on an SCRef");
}
}
public static String scsetdesc(SixModelObject scRef, String desc) {
if (scRef instanceof SCRefInstance) {
((SCRefInstance)scRef).referencedSC.description = desc;
return desc;
}
else {
throw new RuntimeException("scsetdesc can only operate on an SCRef");
}
}
public static long scobjcount(SixModelObject scRef) {
if (scRef instanceof SCRefInstance) {
return ((SCRefInstance)scRef).referencedSC.root_objects.size();
}
else {
throw new RuntimeException("scobjcount can only operate on an SCRef");
}
}
public static SixModelObject setobjsc(SixModelObject obj, SixModelObject scRef) {
if (scRef instanceof SCRefInstance) {
obj.sc = ((SCRefInstance)scRef).referencedSC;
return obj;
}
else {
throw new RuntimeException("setobjsc requires an SCRef");
}
}
public static SixModelObject getobjsc(SixModelObject obj, ThreadContext tc) {
SerializationContext sc = obj.sc;
if (!tc.gc.scRefs.containsKey(sc.handle)) {
SixModelObject SCRef = tc.gc.SCRef;
SCRefInstance ref = (SCRefInstance)SCRef.st.REPR.allocate(tc, SCRef.st);
ref.referencedSC = sc;
tc.gc.scRefs.put(sc.handle, ref);
}
return tc.gc.scRefs.get(sc.handle);
}
public static String serialize(SixModelObject scRef, SixModelObject sh) {
throw new RuntimeException("Serialization NYI");
}
public static String deserialize(String blob, SixModelObject scRef, SixModelObject sh, SixModelObject cr, SixModelObject conflict) {
throw new RuntimeException("Deserialization NYI");
}
public static SixModelObject wval(String sc, long idx, ThreadContext tc) {
return tc.gc.scs.get(sc).root_objects.get((int)idx);
}
Expand Down
1 change: 1 addition & 0 deletions src/org/perl6/nqp/sixmodel/KnowHOWBootstrapper.java
Expand Up @@ -16,6 +16,7 @@ public static void bootstrap(ThreadContext tc)
tc.gc.BOOTInt = bootType(tc, "BOOTInt", "P6int");
tc.gc.BOOTNum = bootType(tc, "BOOTNum", "P6num");
tc.gc.BOOTStr = bootType(tc, "BOOTStr", "P6str");
tc.gc.SCRef = bootType(tc, "SCRef", "SCRef");
Ops.setboolspec(tc.gc.BOOTIter, BoolificationSpec.MODE_ITER, null, tc);
}

Expand Down
9 changes: 9 additions & 0 deletions src/org/perl6/nqp/sixmodel/SerializationContext.java
Expand Up @@ -28,4 +28,13 @@ public class SerializationContext {
* that the object was originally from. */
public ArrayList<Integer> rep_indexes;
public ArrayList<SerializationContext> rep_scs;

public SerializationContext(String handle) {
this.handle = handle;
this.root_objects = new ArrayList<SixModelObject>();
this.root_stables = new ArrayList<STable>();
this.root_codes = new ArrayList<CodeRef>();
this.rep_indexes = new ArrayList<Integer>();
this.rep_scs = new ArrayList<SerializationContext>();
}
}
2 changes: 1 addition & 1 deletion src/org/perl6/nqp/sixmodel/reprs/SCRefInstance.java
Expand Up @@ -4,5 +4,5 @@
import org.perl6.nqp.sixmodel.SixModelObject;

public class SCRefInstance extends SixModelObject {
public SerializationContext sc;
public SerializationContext referencedSC;
}

0 comments on commit 06284c1

Please sign in to comment.