Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Get hash serialization in place.
  • Loading branch information
jnthn committed Mar 9, 2013
1 parent 8d205a7 commit 38f58ba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/org/perl6/nqp/sixmodel/SerializationReader.java
Expand Up @@ -494,6 +494,10 @@ public long readLong() {
return orig.getLong();
}

public int readInt32() {
return orig.getInt();
}

public double readDouble() {
return orig.getDouble();
}
Expand Down
15 changes: 14 additions & 1 deletion src/org/perl6/nqp/sixmodel/SerializationWriter.java
Expand Up @@ -162,6 +162,12 @@ public void writeInt(long value) {
this.growToHold(currentBuffer, 8);
outputs[currentBuffer].putLong(value);
}

/* Writing function for 32-bit native integers. */
public void writeInt32(int value) {
this.growToHold(currentBuffer, 4);
outputs[currentBuffer].putInt(value);
}

/* Writing function for native numbers. */
public void writeNum(double value) {
Expand Down Expand Up @@ -191,6 +197,11 @@ public void writeObjRef(SixModelObject ref) {
outputs[currentBuffer].putInt(ref.sc.root_objects.indexOf(ref));
}

/* Writes a hash; just delegate to the REPR. */
private void writeHashStrVar(SixModelObject ref) {
ref.st.REPR.serialize(tc, this, ref);
}

/* Writing function for references to things. */
public void writeRef(SixModelObject ref) {
/* Work out what kind of thing we have and determine the discriminator. */
Expand Down Expand Up @@ -284,9 +295,11 @@ else if (ref.sc != null) {
case REFVAR_VM_ARR_STR:
writeArrayStr(ref);
break;
*/
case REFVAR_VM_HASH_STR_VAR:
writeHashStrVar(ref);
break;
/*
case REFVAR_STATIC_CODEREF:
case REFVAR_CLONED_CODEREF:
writeCodeRef(ref);
Expand All @@ -295,7 +308,7 @@ else if (ref.sc != null) {
throw new RuntimeException("Serialization Error: Unimplemented object type writeRef");
}
}

/* Writing function for references to STables. */
public void writeSTableRef(STable st) {
int[] idxs = getSTableRefInfo(st);
Expand Down
22 changes: 20 additions & 2 deletions src/org/perl6/nqp/sixmodel/reprs/VMHash.java
Expand Up @@ -2,7 +2,6 @@

import java.util.HashMap;

import org.perl6.nqp.runtime.ExceptionHandling;
import org.perl6.nqp.runtime.ThreadContext;
import org.perl6.nqp.sixmodel.*;

Expand Down Expand Up @@ -34,6 +33,25 @@ public SixModelObject deserialize_stub(ThreadContext tc, STable st) {

public void deserialize_finish(ThreadContext tc, STable st,
SerializationReader reader, SixModelObject obj) {
throw ExceptionHandling.dieInternal(tc, "VMHash deserialization NYI");
HashMap<String, SixModelObject> storage = ((VMHashInstance)obj).storage;
int elems = reader.readInt32();
for (int i = 0; i < elems; i++) {
String key = reader.readStr();
SixModelObject value = reader.readRef();
storage.put(key, value);
}
}

public void serialize(ThreadContext tc, SerializationWriter writer, SixModelObject obj) {
HashMap<String, SixModelObject> storage = ((VMHashInstance)obj).storage;

/* Write out element count. */
writer.writeInt32(storage.size());

/* Write elements, as key,value,key,value etc. */
for (String key : storage.keySet()) {
writer.writeStr(key);
writer.writeRef(storage.get(key));
}
}
}

0 comments on commit 38f58ba

Please sign in to comment.