Skip to content

Commit

Permalink
[jvm] Implement nqp::serializetobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Nov 11, 2018
1 parent 19653bf commit 846b8fc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -2426,6 +2426,7 @@ QAST::OperationsJAST.map_classlib_core_op('scobjcount', $TYPE_OPS, 'scobjcount',
QAST::OperationsJAST.map_classlib_core_op('setobjsc', $TYPE_OPS, 'setobjsc', [$RT_OBJ, $RT_OBJ], $RT_OBJ, :tc);
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_STR, :tc);
QAST::OperationsJAST.map_classlib_core_op('serializetobuf', $TYPE_OPS, 'serializetobuf', [$RT_OBJ, $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);
QAST::OperationsJAST.map_classlib_core_op('scwbdisable', $TYPE_OPS, 'scwbdisable', [], $RT_INT, :tc);
Expand Down
27 changes: 26 additions & 1 deletion src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java
Expand Up @@ -5189,7 +5189,7 @@ public static String serialize(SixModelObject scRef, SixModelObject sh, ThreadCo
((SCRefInstance)scRef).referencedSC,
stringHeap);

String serialized = sw.serialize();
String serialized = Base64.encode(sw.serialize());

int index = 0;
for (String s : stringHeap) {
Expand All @@ -5203,6 +5203,31 @@ public static String serialize(SixModelObject scRef, SixModelObject sh, ThreadCo
throw ExceptionHandling.dieInternal(tc, "serialize was not passed a valid SCRef");
}
}
public static SixModelObject serializetobuf(SixModelObject scRef, SixModelObject sh, SixModelObject type, ThreadContext tc) {
if (scRef instanceof SCRefInstance) {
ArrayList<String> stringHeap = new ArrayList<String>();
SerializationWriter sw = new SerializationWriter(tc,
((SCRefInstance)scRef).referencedSC,
stringHeap);

SixModelObject buf = type.st.REPR.allocate(tc, type.st);

byte[] serialized = sw.serialize().array();

Buffers.stashBytes(tc, buf, serialized);

int index = 0;
for (String s : stringHeap) {
tc.native_s = s;
sh.bind_pos_native(tc, index++);
}

return buf;
}
else {
throw ExceptionHandling.dieInternal(tc, "serialize was not passed a valid SCRef");
}
}
public static String deserialize(String blob, SixModelObject scRef, SixModelObject sh, SixModelObject cr, SixModelObject conflict, ThreadContext tc) throws IOException {
if (scRef instanceof SCRefInstance) {
SerializationContext sc = ((SCRefInstance)scRef).referencedSC;
Expand Down
Expand Up @@ -8,7 +8,6 @@
import java.util.List;
import java.util.Map;

import org.perl6.nqp.runtime.Base64;
import org.perl6.nqp.runtime.CallFrame;
import org.perl6.nqp.runtime.CodeRef;
import org.perl6.nqp.runtime.ExceptionHandling;
Expand Down Expand Up @@ -104,7 +103,7 @@ public SerializationWriter(ThreadContext tc, SerializationContext sc, ArrayList<
this.contextsListPos = 0;
}

public String serialize() {
public ByteBuffer serialize() {
/* Initialize string heap so first entry is the NULL string. */
sh.add(null);

Expand Down Expand Up @@ -402,7 +401,7 @@ public void writeSTableRef(STable st) {
}

/* Concatenates the various output segments into a single binary string. */
private String concatenateOutputs() {
private ByteBuffer concatenateOutputs() {
int output_size = 0;
int offset = 0;

Expand Down Expand Up @@ -518,8 +517,7 @@ private String concatenateOutputs() {
if (offset != output_size)
throw new RuntimeException("Serialization sanity check failed: offset != output_size");

/* Base 64 encode and return. */
return Base64.encode(output);
return output;
}

/* This handles the serialization of an object, which largely involves a
Expand Down

0 comments on commit 846b8fc

Please sign in to comment.