From 95d0898c3f5d57382e296f7e477e64934a971a2a Mon Sep 17 00:00:00 2001 From: David Renshaw Date: Mon, 6 Oct 2014 09:35:26 -0400 Subject: [PATCH] basic support for struct constants. still broken --- compiler/src/main/cpp/capnpc-java.c++ | 16 +++---- .../scala/org/capnproto/EncodingTest.scala | 5 +++ compiler/src/test/schema/test.capnp | 42 +++++++++++++++++++ .../org/capnproto/GeneratedClassSupport.java | 4 +- .../java/org/capnproto/PointerReader.java | 4 +- .../java/org/capnproto/SegmentReader.java | 2 +- 6 files changed, 60 insertions(+), 13 deletions(-) diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index ffbf40a..b4fd948 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -978,7 +978,7 @@ private: uint64_t typeId = field.getContainingStruct().getProto().getId(); kj::String defaultParams = defaultOffset == 0 ? kj::str() : kj::str( - "Schemas.b_", kj::hex(typeId), ", ", defaultOffset * 8, ", ", defaultSize); + "Schemas.b_", kj::hex(typeId), ".buffer, ", defaultOffset * 8, ", ", defaultSize); kj::String blobKind = typeBody.which() == schema::Type::TEXT ? kj::str("Text") : kj::str("Data"); kj::String setterInputType = typeBody.which() == schema::Type::TEXT ? kj::str("String") : kj::str("byte []"); @@ -1343,7 +1343,7 @@ private: kj::strTree(spaces(indent), "public static final org.capnproto.Text.Reader ", upperCase, " = new org.capnproto.Text.Reader(Schemas.b_", - kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(), + kj::hex(proto.getId()), ".buffer, ", schema.getValueSchemaOffset(), ", ", constProto.getValue().getText().size(), ");\n") }; } @@ -1354,18 +1354,18 @@ private: kj::strTree(spaces(indent), "public static final org.capnproto.Data.Reader ", upperCase, " = new org.capnproto.Data.Reader(Schemas.b_", - kj::hex(proto.getId()), ", ", schema.getValueSchemaOffset(), + kj::hex(proto.getId()), ".buffer, ", schema.getValueSchemaOffset(), ", ", constProto.getValue().getData().size(), ");\n") }; } case schema::Value::STRUCT: { - kj::String constType = kj::strTree( - "::capnp::_::ConstStruct<", typeName_, ">").flatten(); return ConstText { true, - kj::strTree("const ", constType, ' ', scope, upperCase, "(::capnp::schemas::b_", - kj::hex(proto.getId()), ".words + ", schema.getValueSchemaOffset(), ");\n") + kj::strTree(spaces(indent), + "public static final ", typeName_, ".Reader ", upperCase, "=\n", + spaces(indent), " new ", typeName_, ".Reader((new org.capnproto.PointerReader(Schemas.b_", + kj::hex(proto.getId()), ",", schema.getValueSchemaOffset(), ",0x7fffffff)).getStruct());\n") }; } @@ -1490,7 +1490,7 @@ private: // Java limits method code size to 64KB. Maybe we should use class.getResource()? auto schemaDef = kj::strTree( - "public static final java.nio.ByteBuffer b_", hexId, " =\n", + "public static final org.capnproto.SegmentReader b_", hexId, " =\n", " org.capnproto.GeneratedClassSupport.decodeRawBytes(\n", " ", kj::mv(schemaLiteral), " \"\"", ");\n"); diff --git a/compiler/src/test/scala/org/capnproto/EncodingTest.scala b/compiler/src/test/scala/org/capnproto/EncodingTest.scala index 6a5f275..e54e869 100644 --- a/compiler/src/test/scala/org/capnproto/EncodingTest.scala +++ b/compiler/src/test/scala/org/capnproto/EncodingTest.scala @@ -140,6 +140,11 @@ class EncodingSuite extends FunSuite { (TestConstants.DATA_CONST.toArray()) should equal (TestUtil.data("bar")); (TestConstants.ENUM_CONST) should equal (TestEnum.CORGE); + { + val subReader = TestConstants.STRUCT_CONST; + //subReader.getBoolField() should equal (true); + //subReader.getInt8Field() should equal (-12); + } } diff --git a/compiler/src/test/schema/test.capnp b/compiler/src/test/schema/test.capnp index faea936..ed46c6c 100644 --- a/compiler/src/test/schema/test.capnp +++ b/compiler/src/test/schema/test.capnp @@ -270,6 +270,48 @@ struct TestConstants { const float64Const :Float64 = -123e45; const textConst :Text = "foo"; const dataConst :Data = "bar"; + const structConst :TestAllTypes = ( + voidField = void, + boolField = true, + int8Field = -12, + int16Field = 3456, + int32Field = -78901234, + int64Field = 56789012345678, + uInt8Field = 90, + uInt16Field = 1234, + uInt32Field = 56789012, + uInt64Field = 345678901234567890, + float32Field = -1.25e-10, + float64Field = 345, + textField = "baz", + dataField = "qux", + structField = ( + textField = "nested", + structField = (textField = "really nested")), + enumField = baz, + # interfaceField can't have a default + + voidList = [void, void, void], + boolList = [false, true, false, true, true], + int8List = [12, -34, -0x80, 0x7f], + int16List = [1234, -5678, -0x8000, 0x7fff], + int32List = [12345678, -90123456, -0x80000000, 0x7fffffff], + int64List = [123456789012345, -678901234567890, -0x8000000000000000, 0x7fffffffffffffff], + uInt8List = [12, 34, 0, 0xff], + uInt16List = [1234, 5678, 0, 0xffff], + uInt32List = [12345678, 90123456, 0, 0xffffffff], + uInt64List = [123456789012345, 678901234567890, 0, 0xffffffffffffffff], + float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37], + float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306], + textList = ["quux", "corge", "grault"], + dataList = ["garply", "waldo", "fred"], + structList = [ + (textField = "x structlist 1"), + (textField = "x structlist 2"), + (textField = "x structlist 3")], + enumList = [qux, bar, grault] + # interfaceList can't have a default + ); # ... const enumConst :TestEnum = corge; } diff --git a/runtime/src/main/java/org/capnproto/GeneratedClassSupport.java b/runtime/src/main/java/org/capnproto/GeneratedClassSupport.java index 7def2ba..c697402 100644 --- a/runtime/src/main/java/org/capnproto/GeneratedClassSupport.java +++ b/runtime/src/main/java/org/capnproto/GeneratedClassSupport.java @@ -1,9 +1,9 @@ package org.capnproto; public final class GeneratedClassSupport { - public static java.nio.ByteBuffer decodeRawBytes(String s) { + public static SegmentReader decodeRawBytes(String s) { try { - return java.nio.ByteBuffer.wrap(s.getBytes("ISO_8859-1")).asReadOnlyBuffer(); + return new SegmentReader(java.nio.ByteBuffer.wrap(s.getBytes("ISO_8859-1")).asReadOnlyBuffer(), null); } catch (Exception e) { throw new Error("could not decode raw bytes from String"); } diff --git a/runtime/src/main/java/org/capnproto/PointerReader.java b/runtime/src/main/java/org/capnproto/PointerReader.java index a26960d..3fa30db 100644 --- a/runtime/src/main/java/org/capnproto/PointerReader.java +++ b/runtime/src/main/java/org/capnproto/PointerReader.java @@ -6,8 +6,8 @@ public final class PointerReader { final int nestingLimit; public PointerReader() { - this.segment = null; - this.pointer = 0; // XXX ? + this.segment = SegmentReader.EMPTY; + this.pointer = 0; this.nestingLimit = 0x7fffffff; } diff --git a/runtime/src/main/java/org/capnproto/SegmentReader.java b/runtime/src/main/java/org/capnproto/SegmentReader.java index 756362d..ed6f1cb 100644 --- a/runtime/src/main/java/org/capnproto/SegmentReader.java +++ b/runtime/src/main/java/org/capnproto/SegmentReader.java @@ -4,7 +4,7 @@ import java.nio.ByteBuffer; public class SegmentReader { - final ByteBuffer buffer; + public final ByteBuffer buffer; final Arena arena; public SegmentReader(ByteBuffer buffer, Arena arena) {