diff --git a/compiler/src/main/cpp/capnpc-java.c++ b/compiler/src/main/cpp/capnpc-java.c++ index 80115464..4501ddeb 100644 --- a/compiler/src/main/cpp/capnpc-java.c++ +++ b/compiler/src/main/cpp/capnpc-java.c++ @@ -981,16 +981,16 @@ private: spaces(indent), " public ", type, ".Reader get", titleCase, "() {\n", unionDiscrim.check, - spaces(indent), " return ", type, ".factory.fromPointerReader(", - "_getPointerField(", offset,"),", defaultParams, ");\n", + spaces(indent), " return ", + "_getPointerField(", type, ".factory,", offset,",", defaultParams, ");\n", spaces(indent), " }\n", "\n"), kj::strTree( kj::mv(unionDiscrim.builderIsDecl), spaces(indent), " public final ", type, ".Builder get", titleCase, "() {\n", unionDiscrim.check, - spaces(indent), " return ", type, ".factory.fromPointerBuilder(" - "_getPointerField(", offset, "),", defaultParams,");\n", + spaces(indent), " return ", + "_getPointerField(", type, ".factory, ", offset, ", ", defaultParams, ");\n", spaces(indent), " }\n", spaces(indent), " public final void set", titleCase, "(", type, ".Reader value) {\n", unionDiscrim.set, @@ -1069,8 +1069,7 @@ private: spaces(indent), " public final ", readerClass, " get", titleCase, "() {\n", - spaces(indent), " return (", listFactory, ").fromPointerReader(_getPointerField(", offset, "),", - defaultParams, ");\n", + spaces(indent), " return _getPointerField(", listFactory, ", ", offset, ", ", defaultParams, ");\n", spaces(indent), " }\n", "\n"), @@ -1082,8 +1081,7 @@ private: spaces(indent), " public final ", builderClass, " get", titleCase, "() {\n", - spaces(indent), " return (", listFactory, ").fromPointerBuilder(_getPointerField(", offset, "),", - defaultParams, ");\n", + spaces(indent), " return _getPointerField(", listFactory, ", ", offset, ", ", defaultParams, ");\n", spaces(indent), " }\n", spaces(indent), " public final void set", titleCase, "(", readerClass, " value) {\n", @@ -1301,9 +1299,10 @@ private: true, kj::strTree(spaces(indent), "public static final ", typeName_, ".Reader ", upperCase, " =\n", - spaces(indent), " ", typeName_, ".factory.fromPointerReader" - "(new org.capnproto.PointerReader(Schemas.b_", - kj::hex(proto.getId()), ",", schema.getValueSchemaOffset(), ",0x7fffffff), null, 0);\n") + spaces(indent), " ", + "new org.capnproto.AnyPointer.Reader(new org.capnproto.PointerReader(Schemas.b_", + kj::hex(proto.getId()), ",", schema.getValueSchemaOffset(), ",0x7fffffff)).getAs(", + typeName_, ".factory);\n") }; } diff --git a/runtime/src/main/java/org/capnproto/AnyPointer.java b/runtime/src/main/java/org/capnproto/AnyPointer.java index 160cc6a8..0dd12637 100644 --- a/runtime/src/main/java/org/capnproto/AnyPointer.java +++ b/runtime/src/main/java/org/capnproto/AnyPointer.java @@ -10,7 +10,7 @@ public Reader(PointerReader reader) { } public final T getAs(FromPointerReader factory) { - return factory.fromPointerReader(this.reader, null, 0); + return factory.fromPointerReader(this.reader.segment, this.reader.pointer, null, 0, this.reader.nestingLimit); } } diff --git a/runtime/src/main/java/org/capnproto/FromPointerBuilder.java b/runtime/src/main/java/org/capnproto/FromPointerBuilder.java index dc1ac036..78ac0e5d 100644 --- a/runtime/src/main/java/org/capnproto/FromPointerBuilder.java +++ b/runtime/src/main/java/org/capnproto/FromPointerBuilder.java @@ -1,5 +1,5 @@ package org.capnproto; public interface FromPointerBuilder { - T fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset); + T fromPointerBuilder(SegmentBuilder segment, int pointer, SegmentReader defaultSegment, int defaultOffset); } diff --git a/runtime/src/main/java/org/capnproto/FromPointerReader.java b/runtime/src/main/java/org/capnproto/FromPointerReader.java index 27cde66c..77b602df 100644 --- a/runtime/src/main/java/org/capnproto/FromPointerReader.java +++ b/runtime/src/main/java/org/capnproto/FromPointerReader.java @@ -1,5 +1,5 @@ package org.capnproto; public interface FromPointerReader { - T fromPointerReader(PointerReader reader, SegmentReader defaultSegment, int defaultOffset); + T fromPointerReader(SegmentReader segment, int pointer, SegmentReader defaultSegment, int defaultOffset, int nestingLimit); } diff --git a/runtime/src/main/java/org/capnproto/ListBuilder.java b/runtime/src/main/java/org/capnproto/ListBuilder.java index 4e429b3c..63f86c7d 100644 --- a/runtime/src/main/java/org/capnproto/ListBuilder.java +++ b/runtime/src/main/java/org/capnproto/ListBuilder.java @@ -111,4 +111,10 @@ protected final PointerBuilder _getPointerElement(int index) { (this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD); } + protected final T _getPointerElement(FromPointerBuilder factory, int index, SegmentReader defaultSegment, int defaultOffset) { + return factory.fromPointerBuilder( + this.segment, + (this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD, + defaultSegment, defaultOffset); + } } diff --git a/runtime/src/main/java/org/capnproto/ListFactory.java b/runtime/src/main/java/org/capnproto/ListFactory.java index 8e2fdeee..6a472d64 100644 --- a/runtime/src/main/java/org/capnproto/ListFactory.java +++ b/runtime/src/main/java/org/capnproto/ListFactory.java @@ -6,20 +6,22 @@ public abstract class ListFactory implements ListBuilder.Factor final byte elementSize; ListFactory(byte elementSize) {this.elementSize = elementSize;} - public final Reader fromPointerReader(PointerReader reader, SegmentReader defaultSegment, int defaultOffset) { + public final Reader fromPointerReader(SegmentReader segment, int pointer, + SegmentReader defaultSegment, int defaultOffset, + int nestingLimit) { return WireHelpers.readListPointer(this, - reader.segment, - reader.pointer, + segment, + pointer, defaultSegment, defaultOffset, this.elementSize, - reader.nestingLimit); + nestingLimit); } - public Builder fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset) { + public Builder fromPointerBuilder(SegmentBuilder segment, int pointer, SegmentReader defaultSegment, int defaultOffset) { return WireHelpers.getWritableListPointer(this, - builder.pointer, - builder.segment, + pointer, + segment, this.elementSize, defaultSegment, defaultOffset); diff --git a/runtime/src/main/java/org/capnproto/ListList.java b/runtime/src/main/java/org/capnproto/ListList.java index 249a6bf3..4da9ac47 100644 --- a/runtime/src/main/java/org/capnproto/ListList.java +++ b/runtime/src/main/java/org/capnproto/ListList.java @@ -41,7 +41,7 @@ public Reader(FromPointerReader factory, } public T get(int index) { - return this.factory.fromPointerReader(_getPointerElement(index), null, 0); + return _getPointerElement(this.factory, index, null, 0); } } @@ -62,7 +62,7 @@ public final T init(int index, int size) { } public final T get(int index) { - return this.factory.fromPointerBuilder(_getPointerElement(index), null, 0); + return _getPointerElement(this.factory, index, null, 0); } } } diff --git a/runtime/src/main/java/org/capnproto/ListReader.java b/runtime/src/main/java/org/capnproto/ListReader.java index 1d5143db..1e0c3e28 100644 --- a/runtime/src/main/java/org/capnproto/ListReader.java +++ b/runtime/src/main/java/org/capnproto/ListReader.java @@ -91,4 +91,12 @@ protected PointerReader _getPointerElement(int index) { (this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD, this.nestingLimit); } + + protected T _getPointerElement(FromPointerReader factory, int index, SegmentReader defaultSegment, int defaultOffset) { + return factory.fromPointerReader(this.segment, + (this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD, + defaultSegment, + defaultOffset, + this.nestingLimit); + } } diff --git a/runtime/src/main/java/org/capnproto/StructBuilder.java b/runtime/src/main/java/org/capnproto/StructBuilder.java index 4406b384..eb3be9ea 100644 --- a/runtime/src/main/java/org/capnproto/StructBuilder.java +++ b/runtime/src/main/java/org/capnproto/StructBuilder.java @@ -153,4 +153,8 @@ protected final PointerBuilder _getPointerField(int index) { return new PointerBuilder(this.segment, this.pointers + index); } + protected final T _getPointerField(FromPointerBuilder factory, int index, SegmentReader defaultSegment, int defaultOffset) { + return factory.fromPointerBuilder(this.segment, this.pointers + index, defaultSegment, defaultOffset); + } + } diff --git a/runtime/src/main/java/org/capnproto/StructFactory.java b/runtime/src/main/java/org/capnproto/StructFactory.java index c9c754a5..49ae6e6e 100644 --- a/runtime/src/main/java/org/capnproto/StructFactory.java +++ b/runtime/src/main/java/org/capnproto/StructFactory.java @@ -3,19 +3,22 @@ public abstract class StructFactory implements FromPointerBuilder, StructBuilder.Factory, InitFromPointerBuilder, FromPointerReader, StructReader.Factory { - public Builder fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset) { - return WireHelpers.getWritableStructPointer(this, builder.pointer, builder.segment, this.structSize(), + public final Reader fromPointerReader(SegmentReader segment, int pointer, + SegmentReader defaultSegment, int defaultOffset, + int nestingLimit) { + return WireHelpers.readStructPointer(this, + segment, + pointer, + defaultSegment, defaultOffset, + nestingLimit); + } + public final Builder fromPointerBuilder(SegmentBuilder segment, int pointer, SegmentReader defaultSegment, int defaultOffset) { + return WireHelpers.getWritableStructPointer(this, pointer, segment, this.structSize(), defaultSegment, defaultOffset); } - public Builder initFromPointerBuilder(PointerBuilder builder) { + public final Builder initFromPointerBuilder(PointerBuilder builder) { return WireHelpers.initStructPointer(this, builder.pointer, builder.segment, this.structSize()); } - public Reader fromPointerReader(PointerReader reader, SegmentReader defaultSegment, int defaultOffset) { - return WireHelpers.readStructPointer(this, - reader.segment, - reader.pointer, - defaultSegment, defaultOffset, - reader.nestingLimit); - } + public abstract Reader asReader(Builder builder); } diff --git a/runtime/src/main/java/org/capnproto/StructList.java b/runtime/src/main/java/org/capnproto/StructList.java index 9393f3f3..06c49d54 100644 --- a/runtime/src/main/java/org/capnproto/StructList.java +++ b/runtime/src/main/java/org/capnproto/StructList.java @@ -27,10 +27,11 @@ public final Builder constructBuilder(SegmentBuilder segment, return new Builder (factory, segment, ptr, elementCount, step, structDataSize, structPointerCount); } - public final Builder fromPointerBuilder(PointerBuilder builder, SegmentReader defaultSegment, int defaultOffset) { + public final Builder fromPointerBuilder(SegmentBuilder segment, int pointer, + SegmentReader defaultSegment, int defaultOffset) { return WireHelpers.getWritableStructListPointer(this, - builder.pointer, - builder.segment, + pointer, + segment, factory.structSize(), defaultSegment, defaultOffset); diff --git a/runtime/src/main/java/org/capnproto/StructReader.java b/runtime/src/main/java/org/capnproto/StructReader.java index 88744265..471df5e5 100644 --- a/runtime/src/main/java/org/capnproto/StructReader.java +++ b/runtime/src/main/java/org/capnproto/StructReader.java @@ -144,4 +144,20 @@ protected final PointerReader _getPointerField(int ptrIndex) { return new PointerReader(); } } + + protected final T _getPointerField(FromPointerReader factory, int ptrIndex, SegmentReader defaultSegment, int defaultOffset) { + if (ptrIndex < this.pointerCount) { + return factory.fromPointerReader(this.segment, + this.pointers + ptrIndex, + defaultSegment, + defaultOffset, + this.nestingLimit); + } else { + return factory.fromPointerReader(SegmentReader.EMPTY, + 0, + defaultSegment, + defaultOffset, + this.nestingLimit); + } + } }