From a59a5c403eacf61e30443a56f03a451abe69d8ed Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Wed, 24 Sep 2014 12:18:52 -0400 Subject: [PATCH] basic support for initGroup --- compiler/src/main/cpp/capnpc-java.c++ | 86 +++++++++++++------ .../scala/org/capnproto/EncodingTest.scala | 2 +- compiler/src/test/schema/test.capnp | 1 + .../java/org/capnproto/PointerBuilder.java | 4 + .../main/java/org/capnproto/WireHelpers.java | 8 ++ 5 files changed, 72 insertions(+), 29 deletions(-) diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index 33c5a56..c6bfb4b 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -415,18 +415,18 @@ private: static kj::StringPtr maskType(schema::Type::Which whichType) { switch (whichType) { - case schema::Type::BOOL: return "bool"; - case schema::Type::INT8: return " ::uint8_t"; - case schema::Type::INT16: return " ::uint16_t"; - case schema::Type::INT32: return " ::uint32_t"; - case schema::Type::INT64: return " ::uint64_t"; - case schema::Type::UINT8: return " ::uint8_t"; - case schema::Type::UINT16: return " ::uint16_t"; - case schema::Type::UINT32: return " ::uint32_t"; - case schema::Type::UINT64: return " ::uint64_t"; - case schema::Type::FLOAT32: return " ::uint32_t"; - case schema::Type::FLOAT64: return " ::uint64_t"; - case schema::Type::ENUM: return " ::uint16_t"; + case schema::Type::BOOL: return "boolean"; + case schema::Type::INT8: return "byte"; + case schema::Type::INT16: return "short"; + case schema::Type::INT32: return "int"; + case schema::Type::INT64: return "long"; + case schema::Type::UINT8: return "byte"; + case schema::Type::UINT16: return "short"; + case schema::Type::UINT32: return "int"; + case schema::Type::UINT64: return "long"; + case schema::Type::FLOAT32: return "int"; + case schema::Type::FLOAT64: return "long"; + case schema::Type::ENUM: return "short"; case schema::Type::VOID: case schema::Type::TEXT: @@ -440,6 +440,34 @@ private: KJ_UNREACHABLE; } + static kj::StringPtr maskZeroLiteral(schema::Type::Which whichType) { + switch (whichType) { + case schema::Type::BOOL: return "false"; + case schema::Type::INT8: return "(byte)0"; + case schema::Type::INT16: return "(short)0"; + case schema::Type::INT32: return "0"; + case schema::Type::INT64: return "0L"; + case schema::Type::UINT8: return "(byte)0"; + case schema::Type::UINT16: return "(short)0"; + case schema::Type::UINT32: return "0"; + case schema::Type::UINT64: return "0L"; + case schema::Type::FLOAT32: return "0"; + case schema::Type::FLOAT64: return "0L"; + case schema::Type::ENUM: return "(short)0"; + + case schema::Type::VOID: + case schema::Type::TEXT: + case schema::Type::DATA: + case schema::Type::LIST: + case schema::Type::STRUCT: + case schema::Type::INTERFACE: + case schema::Type::ANY_POINTER: + KJ_FAIL_REQUIRE("Should only be called for data types."); + } + KJ_UNREACHABLE; + } + + struct Slot { schema::Type::Which whichType; uint offset; @@ -647,7 +675,24 @@ private: spaces(indent), " return new ", scope, titleCase, ".Builder(_builder);\n", spaces(indent), " }\n", spaces(indent), " public final ", titleCase, ".Builder init", titleCase, "() {\n", - spaces(indent), " throw new Error();\n", + unionDiscrim.set, + KJ_MAP(slot, slots) { + switch (sectionFor(slot.whichType)) { + case Section::NONE: + return kj::strTree(); + case Section::DATA: + return kj::strTree( + spaces(indent), + " _builder.set", toTitleCase(maskType(slot.whichType)), + "Field(", slot.offset, ",", maskZeroLiteral(slot.whichType), + ");\n"); + case Section::POINTERS: + return kj::strTree( + spaces(indent), " _builder.getPointerField(", slot.offset, ").clear();\n"); + } + KJ_UNREACHABLE; + }, + " return new ", scope, titleCase, ".Builder(_builder);\n", spaces(indent), " }\n", "\n"), @@ -669,21 +714,6 @@ private: "}\n"), "inline ", scope, titleCase, "::Builder ", scope, "Builder::init", titleCase, "() {\n", unionDiscrim.set, - KJ_MAP(slot, slots) { - switch (sectionFor(slot.whichType)) { - case Section::NONE: - return kj::strTree(); - case Section::DATA: - return kj::strTree( - " _builder.setDataField<", maskType(slot.whichType), ">(", - slot.offset, " * ::capnp::ELEMENTS, 0);\n"); - case Section::POINTERS: - return kj::strTree( - " _builder.getPointerField(", slot.offset, - " * ::capnp::POINTERS).clear();\n"); - } - KJ_UNREACHABLE; - }, " return ", scope, titleCase, "::Builder(_builder);\n" "}\n") }; diff --git a/compiler/src/test/scala/org/capnproto/EncodingTest.scala b/compiler/src/test/scala/org/capnproto/EncodingTest.scala index a0d0432..eebb424 100644 --- a/compiler/src/test/scala/org/capnproto/EncodingTest.scala +++ b/compiler/src/test/scala/org/capnproto/EncodingTest.scala @@ -35,7 +35,7 @@ class EncodingSuite extends FunSuite { val builder = new MessageBuilder(); val root = builder.initRoot(TestGroups.factory); - //val foo = root.getGroups().initFoo(); + val foo = root.getGroups().initFoo(); } diff --git a/compiler/src/test/schema/test.capnp b/compiler/src/test/schema/test.capnp index 50b236d..1cb55d8 100644 --- a/compiler/src/test/schema/test.capnp +++ b/compiler/src/test/schema/test.capnp @@ -168,6 +168,7 @@ struct TestGroups { corge @0 :Int32; grault @2 :Int64; garply @8 :Text; + quux @9 : Void; } bar :group { corge @3 :Int32; diff --git a/runtime/src/main/java/org/capnproto/PointerBuilder.java b/runtime/src/main/java/org/capnproto/PointerBuilder.java index e2bcc16..4cf07e7 100644 --- a/runtime/src/main/java/org/capnproto/PointerBuilder.java +++ b/runtime/src/main/java/org/capnproto/PointerBuilder.java @@ -58,4 +58,8 @@ public final class PointerBuilder { throw new Error("unimplemented"); } + public final void clear() { + WireHelpers.zeroObject(this.segment, this.pointer); + this.segment.buffer.putLong(this.pointer * 8, 0L); + } } diff --git a/runtime/src/main/java/org/capnproto/WireHelpers.java b/runtime/src/main/java/org/capnproto/WireHelpers.java index 27da268..319a56c 100644 --- a/runtime/src/main/java/org/capnproto/WireHelpers.java +++ b/runtime/src/main/java/org/capnproto/WireHelpers.java @@ -135,6 +135,14 @@ final class WireHelpers { } } + public static void zeroObject(SegmentBuilder segment, int refOffset) { + //# Zero out the pointed-to object. Use when the pointer is + //# about to be overwritten making the target object no longer + //# reachable. + + // TODO + } + public static StructBuilder initStructPointer(int refOffset, SegmentBuilder segment, StructSize size) {