Skip to content

Commit

Permalink
Basic mixins that just add methods.
Browse files Browse the repository at this point in the history
Case of adding extra attributes is still to do.
  • Loading branch information
jnthn committed Feb 1, 2013
1 parent 9c45812 commit bc07c18
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/org/perl6/nqp/sixmodel/reprs/P6Opaque.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,28 @@ public SixModelObject allocate(ThreadContext tc, STable st) {
}
}

public void change_type(ThreadContext tc, SixModelObject obj, SixModelObject newType) {
// Ensure target type is also P6opaque-based.
if (!(newType.st.REPR instanceof P6Opaque))
throw new RuntimeException("P6opaque can only rebless to another P6opaque-based type");

// Ensure that the MROs overlap properly.
P6OpaqueREPRData ourREPRData = (P6OpaqueREPRData)obj.st.REPRData;
P6OpaqueREPRData targetREPRData = (P6OpaqueREPRData)newType.st.REPRData;
if (ourREPRData.classHandles.length > targetREPRData.classHandles.length)
throw new RuntimeException("Incompatible MROs in P6opaque rebless");
for (int i = 0; i < ourREPRData.classHandles.length; i++) {
if (ourREPRData.classHandles[i] != targetREPRData.classHandles[i])
throw new RuntimeException("Incompatible MROs in P6opaque rebless");
}

// If there's a different number of attributes, need to set up delegate.
// TODO

// Switch STable over to the new type.
obj.st = newType.st;
}

private class ByteClassLoader extends ClassLoader {
private byte[] bytes;

Expand Down Expand Up @@ -423,25 +445,30 @@ public void deserialize_repr_data(ThreadContext tc, STable st, SerializationRead

// Finally, read in the name to index mapping.
int numClasses = (int)reader.readLong();
REPRData.classHandles = new SixModelObject[numClasses];
REPRData.nameToHintMap = new HashMap[numClasses];
ArrayList<SixModelObject> classHandles = new ArrayList<SixModelObject>();
ArrayList<HashMap<String, Integer>> nameToHintMaps = new ArrayList<HashMap<String, Integer>>();
for (int i = 0; i < numClasses; i++) {
REPRData.classHandles[i] = reader.readRef();
SixModelObject classHandle = reader.readRef();
SixModelObject nameToHintObject = reader.readRef();
if (nameToHintObject == null) {
/* Nothing to do. */
}
else if (nameToHintObject instanceof VMHashInstance) {
HashMap<String, Integer> nameToHintMap = new HashMap<String, Integer>();
HashMap<String, SixModelObject> origMap = ((VMHashInstance)nameToHintObject).storage;
for (String key : origMap.keySet())
nameToHintMap.put(key, (int)origMap.get(key).get_int(tc));
REPRData.nameToHintMap[i] = nameToHintMap;
if (origMap.size() > 0) {
for (String key : origMap.keySet())
nameToHintMap.put(key, (int)origMap.get(key).get_int(tc));
classHandles.add(classHandle);
nameToHintMaps.add(nameToHintMap);
}
}
else {
throw new RuntimeException("Unexpected hint map representation in deserialize");
}
}
REPRData.classHandles = classHandles.toArray(new SixModelObject[0]);
REPRData.nameToHintMap = nameToHintMaps.toArray(new HashMap[0]);

// Finally, reassemble the Java backing type.
ArrayList<AttrInfo> attrInfoList = new ArrayList<AttrInfo>();
Expand Down

0 comments on commit bc07c18

Please sign in to comment.