Project import generated by Copybara

PiperOrigin-RevId: 300708221
This commit is contained in:
Rafi Kamal 2020-03-13 00:57:50 -07:00 committed by Copybara-Service
parent f57ae5eac3
commit 422053f3bc
12 changed files with 1049 additions and 1087 deletions

View File

@ -6,7 +6,7 @@ goog.module('protobuf.benchmark.KernelCodeSizeBenchmarkAllTypes');
const ByteString = goog.require('protobuf.ByteString');
const Int64 = goog.require('protobuf.Int64');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
const TestMessage = goog.require('protobuf.testing.binary.TestMessage');
const {ensureCommonBaseLine} = goog.require('protobuf.benchmark.codeSize.codeSizeBase');
@ -17,7 +17,7 @@ ensureCommonBaseLine();
* @return {string}
*/
function accessAllTypes() {
const message = new TestMessage(LazyAccessor.createEmpty());
const message = new TestMessage(Kernel.createEmpty());
message.addPackedBoolElement(1, true);
message.addPackedBoolIterable(1, [true]);

View File

@ -20,7 +20,7 @@
goog.module('protobuf.benchmark.KernelCodeSizeBenchmarkPopularTypes');
const Int64 = goog.require('protobuf.Int64');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
const TestMessage = goog.require('protobuf.testing.binary.TestMessage');
const {ensureCommonBaseLine} = goog.require('protobuf.benchmark.codeSize.codeSizeBase');
@ -31,7 +31,7 @@ ensureCommonBaseLine();
* @return {string}
*/
function accessAllTypes() {
const message = new TestMessage(LazyAccessor.createEmpty());
const message = new TestMessage(Kernel.createEmpty());
message.addRepeatedMessageElement(1, message, TestMessage.instanceCreator);
message.addRepeatedMessageIterable(1, [message], TestMessage.instanceCreator);

View File

@ -3,7 +3,7 @@
*/
goog.module('proto.conformance.ConformanceRequest');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
const WireFormat = goog.require('proto.conformance.WireFormat');
/**
@ -19,8 +19,8 @@ class ConformanceRequest {
* @private
*/
constructor(bytes) {
/** @private @const {!LazyAccessor} */
this.accessor_ = LazyAccessor.fromArrayBuffer(bytes);
/** @private @const {!Kernel} */
this.accessor_ = Kernel.fromArrayBuffer(bytes);
}
/**

View File

@ -4,7 +4,7 @@
goog.module('proto.conformance.ConformanceResponse');
const ByteString = goog.require('protobuf.ByteString');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
/**
* Handwritten code of conformance.ConformanceResponse.
@ -19,8 +19,8 @@ class ConformanceResponse {
* @private
*/
constructor(bytes) {
/** @private @const {!LazyAccessor} */
this.accessor_ = LazyAccessor.fromArrayBuffer(bytes);
/** @private @const {!Kernel} */
this.accessor_ = Kernel.fromArrayBuffer(bytes);
}
/**

View File

@ -4,7 +4,7 @@
goog.module('proto.conformance.TestAllTypesProto2');
const InternalMessage = goog.require('protobuf.binary.InternalMessage');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
/**
* Handwritten code of conformance.TestAllTypesProto2.
@ -14,18 +14,18 @@ const LazyAccessor = goog.require('protobuf.runtime.Kernel');
*/
class TestAllTypesProto2 {
/**
* @param {!LazyAccessor=} accessor
* @param {!Kernel=} accessor
* @private
*/
constructor(accessor = LazyAccessor.createEmpty()) {
/** @private @const {!LazyAccessor} */
constructor(accessor = Kernel.createEmpty()) {
/** @private @const {!Kernel} */
this.accessor_ = accessor;
}
/**
* @override
* @package
* @return {!LazyAccessor}
* @return {!Kernel}
*/
internalGetKernel() {
return this.accessor_;
@ -34,7 +34,7 @@ class TestAllTypesProto2 {
/**
* Create a request instance with the given bytes data.
* If we directly use the accessor created by the binary decoding, the
* LazyAccessor instance will only copy the same data over for encoding. By
* Kernel instance will only copy the same data over for encoding. By
* explicitly fetching data from the previous accessor and setting all fields
* into a new accessor, we will actually test encoding/decoding for the binary
* format.
@ -43,7 +43,7 @@ class TestAllTypesProto2 {
*/
static deserialize(bytes) {
const msg = new TestAllTypesProto2();
const requestAccessor = LazyAccessor.fromArrayBuffer(bytes);
const requestAccessor = Kernel.fromArrayBuffer(bytes);
if (requestAccessor.hasFieldNumber(1)) {
const value = requestAccessor.getInt32WithDefault(1);

View File

@ -4,7 +4,7 @@
goog.module('proto.conformance.TestAllTypesProto3');
const InternalMessage = goog.require('protobuf.binary.InternalMessage');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
/**
* Handwritten code of conformance.TestAllTypesProto3.
@ -14,18 +14,18 @@ const LazyAccessor = goog.require('protobuf.runtime.Kernel');
*/
class TestAllTypesProto3 {
/**
* @param {!LazyAccessor=} accessor
* @param {!Kernel=} accessor
* @private
*/
constructor(accessor = LazyAccessor.createEmpty()) {
/** @private @const {!LazyAccessor} */
constructor(accessor = Kernel.createEmpty()) {
/** @private @const {!Kernel} */
this.accessor_ = accessor;
}
/**
* @override
* @package
* @return {!LazyAccessor}
* @return {!Kernel}
*/
internalGetKernel() {
return this.accessor_;
@ -34,7 +34,7 @@ class TestAllTypesProto3 {
/**
* Create a request instance with the given bytes data.
* If we directly use the accessor created by the binary decoding, the
* LazyAccessor instance will only copy the same data over for encoding. By
* Kernel instance will only copy the same data over for encoding. By
* explicitly fetching data from the previous accessor and setting all fields
* into a new accessor, we will actually test encoding/decoding for the binary
* format.
@ -43,7 +43,7 @@ class TestAllTypesProto3 {
*/
static deserialize(bytes) {
const msg = new TestAllTypesProto3();
const requestAccessor = LazyAccessor.fromArrayBuffer(bytes);
const requestAccessor = Kernel.fromArrayBuffer(bytes);
if (requestAccessor.hasFieldNumber(1)) {
const value = requestAccessor.getInt32WithDefault(1);

View File

@ -4,7 +4,7 @@
*/
goog.module('protobuf.binary.InternalMessage');
const LazyAccessor = goog.requireType('protobuf.runtime.Kernel');
const Kernel = goog.requireType('protobuf.runtime.Kernel');
/**
* Interface that needs to be implemented by messages implemented with the
@ -16,7 +16,7 @@ const LazyAccessor = goog.requireType('protobuf.runtime.Kernel');
class InternalMessage {
/**
* @package
* @return {!LazyAccessor}
* @return {!Kernel}
*/
internalGetKernel() {}
}

View File

@ -1,15 +1,15 @@
/**
* @fileoverview LazyAccessor is a class to provide type-checked accessing
* @fileoverview Kernel is a class to provide type-checked accessing
* (read/write bool/int32/string/...) on binary data.
*
* When creating the LazyAccessor with the binary data, there is no deep
* When creating the Kernel with the binary data, there is no deep
* decoding done (which requires full type information). The deep decoding is
* deferred until the first time accessing (when accessors can provide
* full type information).
*
* Because accessors can be statically analyzed and stripped, unlike eager
* binary decoding (which requires the full type information of all defined
* fields), LazyAccessor will only need the full type information of used
* fields), Kernel will only need the full type information of used
* fields.
*/
goog.module('protobuf.runtime.Kernel');
@ -53,12 +53,12 @@ function checkIsInternalMessage(obj) {
/**
* Checks if the instanceCreator returns an instance that implements the
* InternalMessage interface.
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @template T
*/
function checkInstanceCreator(instanceCreator) {
if (CHECK_TYPE) {
const emptyMessage = instanceCreator(LazyAccessor.createEmpty());
const emptyMessage = instanceCreator(Kernel.createEmpty());
checkFunctionExists(emptyMessage.internalGetKernel);
}
}
@ -156,7 +156,7 @@ function mergeMessageArrays(indexArray, bufferDecoder) {
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {number=} pivot
* @return {!LazyAccessor}
* @return {!Kernel}
*/
function readAccessor(indexArray, bufferDecoder, pivot = undefined) {
checkState(indexArray.length > 0);
@ -173,7 +173,7 @@ function readAccessor(indexArray, bufferDecoder, pivot = undefined) {
});
accessorBuffer = mergeMessageArrays(indexArray, bufferDecoder);
}
return LazyAccessor.fromBufferDecoder_(accessorBuffer, pivot);
return Kernel.fromBufferDecoder_(accessorBuffer, pivot);
}
/**
@ -181,7 +181,7 @@ function readAccessor(indexArray, bufferDecoder, pivot = undefined) {
* This is used to implement parsing singular message fields.
* @param {!Array<!IndexEntry>} indexArray
* @param {!BufferDecoder} bufferDecoder
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
@ -247,10 +247,10 @@ class ArrayIterable {
* at the first access.
* @final
*/
class LazyAccessor {
class Kernel {
/**
* Create a LazyAccessor for the given binary bytes.
* The bytes array is kept by the LazyAccessor. DON'T MODIFY IT.
* Create a Kernel for the given binary bytes.
* The bytes array is kept by the Kernel. DON'T MODIFY IT.
* @param {!ArrayBuffer} arrayBuffer Binary bytes.
* @param {number=} pivot Fields with a field number no greater than the pivot
* value will be stored in an array for fast access. Other fields will be
@ -259,15 +259,15 @@ class LazyAccessor {
* value to the max field number of the message unless the field numbers
* are too sparse. If the value is not set, a default value specified in
* storage.js will be used.
* @return {!LazyAccessor}
* @return {!Kernel}
*/
static fromArrayBuffer(arrayBuffer, pivot = undefined) {
const bufferDecoder = BufferDecoder.fromArrayBuffer(arrayBuffer);
return LazyAccessor.fromBufferDecoder_(bufferDecoder, pivot);
return Kernel.fromBufferDecoder_(bufferDecoder, pivot);
}
/**
* Creates an empty LazyAccessor.
* Creates an empty Kernel.
* @param {number=} pivot Fields with a field number no greater than the pivot
* value will be stored in an array for fast access. Other fields will be
* stored in a map. A higher pivot value can improve runtime performance
@ -275,22 +275,22 @@ class LazyAccessor {
* value to the max field number of the message unless the field numbers
* are too sparse. If the value is not set, a default value specified in
* storage.js will be used.
* @return {!LazyAccessor}
* @return {!Kernel}
*/
static createEmpty(pivot = undefined) {
return new LazyAccessor(/* bufferDecoder= */ null, new Storage(pivot));
return new Kernel(/* bufferDecoder= */ null, new Storage(pivot));
}
/**
* Create a LazyAccessor for the given binary bytes.
* The bytes array is kept by the LazyAccessor. DON'T MODIFY IT.
* Create a Kernel for the given binary bytes.
* The bytes array is kept by the Kernel. DON'T MODIFY IT.
* @param {!BufferDecoder} bufferDecoder Binary bytes.
* @param {number|undefined} pivot
* @return {!LazyAccessor}
* @return {!Kernel}
* @private
*/
static fromBufferDecoder_(bufferDecoder, pivot) {
return new LazyAccessor(bufferDecoder, buildIndex(bufferDecoder, pivot));
return new Kernel(bufferDecoder, buildIndex(bufferDecoder, pivot));
}
/**
@ -310,10 +310,10 @@ class LazyAccessor {
/**
* Creates a shallow copy of the accessor.
* @return {!LazyAccessor}
* @return {!Kernel}
*/
shallowCopy() {
return new LazyAccessor(this.bufferDecoder_, this.fields_.shallowCopy());
return new Kernel(this.bufferDecoder_, this.fields_.shallowCopy());
}
/**
@ -676,7 +676,7 @@ class LazyAccessor {
* getMessageOrNull after getMessage will not register the encoder.
*
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {?T}
* @template T
@ -698,7 +698,7 @@ class LazyAccessor {
* getMessageAttach after getMessage will not register the encoder.
*
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
@ -707,7 +707,7 @@ class LazyAccessor {
checkInstanceCreator(instanceCreator);
let instance = this.getMessageOrNull(fieldNumber, instanceCreator, pivot);
if (!instance) {
instance = instanceCreator(LazyAccessor.createEmpty());
instance = instanceCreator(Kernel.createEmpty());
this.setField_(fieldNumber, instance, writeMessage);
}
return instance;
@ -726,7 +726,7 @@ class LazyAccessor {
* getMessageAttach, since these methods register the encoder.
*
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {T}
* @template T
@ -740,8 +740,7 @@ class LazyAccessor {
// Returns an empty message as the default value if the field doesn't exist.
// We don't pass the default value to getFieldWithDefault_ to reduce object
// allocation.
return message === null ? instanceCreator(LazyAccessor.createEmpty()) :
message;
return message === null ? instanceCreator(Kernel.createEmpty()) : message;
}
/**
@ -749,7 +748,7 @@ class LazyAccessor {
* it hasn't been set.
* @param {number} fieldNumber
* @param {number=} pivot
* @return {?LazyAccessor}
* @return {?Kernel}
*/
getMessageAccessorOrNull(fieldNumber, pivot = undefined) {
checkFieldNumber(fieldNumber);
@ -1501,7 +1500,7 @@ class LazyAccessor {
* Returns an Array instance containing boolean values for the given field
* number.
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number|undefined} pivot
* @return {!Array<T>}
* @template T
@ -1531,8 +1530,8 @@ class LazyAccessor {
const subMessageBuffer = reader.readDelimited(
checkDefAndNotNull(this.bufferDecoder_),
Field.getStartIndex(indexArray[i]));
result[i] = instanceCreator(
LazyAccessor.fromBufferDecoder_(subMessageBuffer, pivot));
result[i] =
instanceCreator(Kernel.fromBufferDecoder_(subMessageBuffer, pivot));
}
field.setCache(result, writeRepeatedMessage);
@ -1542,7 +1541,7 @@ class LazyAccessor {
/**
* Returns the element at index for the given field number as a message.
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number} index
* @param {number=} pivot
* @return {T}
@ -1560,7 +1559,7 @@ class LazyAccessor {
* Returns an Iterable instance containing message values for the given field
* number.
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number=} pivot
* @return {!Iterable<T>}
* @template T
@ -1578,7 +1577,7 @@ class LazyAccessor {
* field number.
* @param {number} fieldNumber
* @param {number=} pivot
* @return {!Iterable<!LazyAccessor>}
* @return {!Iterable<!Kernel>}
*/
getRepeatedMessageAccessorIterable(fieldNumber, pivot = undefined) {
checkFieldNumber(fieldNumber);
@ -1593,9 +1592,8 @@ class LazyAccessor {
value => checkIsInternalMessage(value).internalGetKernel()));
}
const readMessageFunc = (bufferDecoder, start) =>
LazyAccessor.fromBufferDecoder_(
reader.readDelimited(bufferDecoder, start), pivot);
const readMessageFunc = (bufferDecoder, start) => Kernel.fromBufferDecoder_(
reader.readDelimited(bufferDecoder, start), pivot);
const array = readRepeatedNonPrimitive(
checkDefAndNotNull(field.getIndexArray()),
checkDefAndNotNull(this.bufferDecoder_), readMessageFunc);
@ -1605,7 +1603,7 @@ class LazyAccessor {
/**
* Returns the size of the repeated field.
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @return {number}
* @param {number=} pivot
* @template T
@ -1804,7 +1802,7 @@ class LazyAccessor {
/**
* Sets a proto Message to the field with the given field number.
* Instead of working with the LazyAccessor inside of the message directly, we
* Instead of working with the Kernel inside of the message directly, we
* need the message instance to keep its reference equality for subsequent
* gettings.
* @param {number} fieldNumber
@ -3757,7 +3755,7 @@ class LazyAccessor {
* Adds all message values into the field for the given field number.
* @param {number} fieldNumber
* @param {!Iterable<!InternalMessage>} values
* @param {function(!LazyAccessor):!InternalMessage} instanceCreator
* @param {function(!Kernel):!InternalMessage} instanceCreator
* @param {number=} pivot
*/
addRepeatedMessageIterable(
@ -3779,7 +3777,7 @@ class LazyAccessor {
* the given index.
* @param {number} fieldNumber
* @param {!InternalMessage} value
* @param {function(!LazyAccessor):!InternalMessage} instanceCreator
* @param {function(!Kernel):!InternalMessage} instanceCreator
* @param {number} index
* @param {number=} pivot
* @throws {!Error} if index is out of range when check mode is critical
@ -3799,7 +3797,7 @@ class LazyAccessor {
* Adds a single message value into the field for the given field number.
* @param {number} fieldNumber
* @param {!InternalMessage} value
* @param {function(!LazyAccessor):!InternalMessage} instanceCreator
* @param {function(!Kernel):!InternalMessage} instanceCreator
* @param {number=} pivot
*/
addRepeatedMessageElement(
@ -3809,4 +3807,4 @@ class LazyAccessor {
}
}
exports = LazyAccessor;
exports = Kernel;

View File

@ -1,5 +1,5 @@
/**
* @fileoverview Tests to make sure LazyAccessor can read data in a backward
* @fileoverview Tests to make sure Kernel can read data in a backward
* compatible way even when protobuf schema changes according to the rules
* defined in
* https://developers.google.com/protocol-buffers/docs/proto#updating and
@ -16,7 +16,7 @@ goog.setTestOnly();
const ByteString = goog.require('protobuf.ByteString');
const Int64 = goog.require('protobuf.Int64');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
const TestMessage = goog.require('protobuf.testing.binary.TestMessage');
const {CHECK_CRITICAL_STATE} = goog.require('protobuf.internal.checks');
@ -39,26 +39,26 @@ function getCharacterCodes(str) {
describe('optional -> repeated compatibility', () => {
it('is maintained for scalars', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setInt32(1, 1);
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0x8, 0x1));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getRepeatedInt32Size(1)).toEqual(1);
expect(newAccessor.getRepeatedInt32Element(1, 0)).toEqual(1);
});
it('is maintained for messages', () => {
const message = new TestMessage(LazyAccessor.createEmpty());
const message = new TestMessage(Kernel.createEmpty());
message.setInt32(1, 1);
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setMessage(1, message);
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x8, 0x1));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getRepeatedMessageSize(1, TestMessage.instanceCreator))
.toEqual(1);
expect(
@ -68,43 +68,43 @@ describe('optional -> repeated compatibility', () => {
});
it('is maintained for bytes', () => {
const message = new TestMessage(LazyAccessor.createEmpty());
const message = new TestMessage(Kernel.createEmpty());
message.setInt32(1, 1);
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setBytes(
1, ByteString.fromArrayBuffer(createArrayBuffer(0xA, 0xB)));
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0xA, 0xB));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getRepeatedBytesSize(1)).toEqual(1);
expect(newAccessor.getRepeatedBoolElement(1, 0))
.toEqual(ByteString.fromArrayBuffer(createArrayBuffer(0xA, 0xB)));
});
it('is maintained for strings', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setString(1, 'hello');
const serializedData = oldAccessor.serialize();
expect(serializedData)
.toEqual(createArrayBuffer(0xA, 0x5, 0x68, 0x65, 0x6C, 0x6C, 0x6F));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getRepeatedStringSize(1)).toEqual(1);
expect(newAccessor.getRepeatedStringElement(1, 0)).toEqual('hello');
});
});
describe('LazyAccessor repeated -> optional compatibility', () => {
describe('Kernel repeated -> optional compatibility', () => {
it('is maintained for unpacked scalars', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.addUnpackedInt32Element(1, 0);
oldAccessor.addUnpackedInt32Element(1, 1);
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0x8, 0x0, 0x8, 0x1));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getInt32WithDefault(1)).toEqual(1);
expect(newAccessor.serialize()).toEqual(serializedData);
});
@ -112,26 +112,26 @@ describe('LazyAccessor repeated -> optional compatibility', () => {
// repeated -> optional transformation is not supported for packed fields yet:
// go/proto-schema-repeated
it('is not maintained for packed scalars', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.addPackedInt32Element(1, 0);
oldAccessor.addPackedInt32Element(1, 1);
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x0, 0x1));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
if (CHECK_CRITICAL_STATE) {
expect(() => newAccessor.getInt32WithDefault(1)).toThrow();
}
});
it('is maintained for messages', () => {
const message1 = new TestMessage(LazyAccessor.createEmpty());
const message1 = new TestMessage(Kernel.createEmpty());
message1.setInt32(1, 1);
const message2 = new TestMessage(LazyAccessor.createEmpty());
const message2 = new TestMessage(Kernel.createEmpty());
message2.setInt32(1, 2);
message2.setInt32(2, 3);
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.addRepeatedMessageElement(
1, message1, TestMessage.instanceCreator);
oldAccessor.addRepeatedMessageElement(
@ -141,7 +141,7 @@ describe('LazyAccessor repeated -> optional compatibility', () => {
.toEqual(createArrayBuffer(
0xA, 0x2, 0x8, 0x1, 0xA, 0x4, 0x8, 0x2, 0x10, 0x3));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
// Values from message1 and message2 have been merged
const newMessage = newAccessor.getMessage(1, TestMessage.instanceCreator);
expect(newMessage.getRepeatedInt32Size(1)).toEqual(2);
@ -153,7 +153,7 @@ describe('LazyAccessor repeated -> optional compatibility', () => {
});
it('is maintained for bytes', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.addRepeatedBytesElement(
1, ByteString.fromArrayBuffer(createArrayBuffer(0xA, 0xB)));
oldAccessor.addRepeatedBytesElement(
@ -162,14 +162,14 @@ describe('LazyAccessor repeated -> optional compatibility', () => {
expect(serializedData)
.toEqual(createArrayBuffer(0xA, 0x2, 0xA, 0xB, 0xA, 0x2, 0xC, 0xD));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getBytesWithDefault(1))
.toEqual(ByteString.fromArrayBuffer(createArrayBuffer(0xC, 0xD)));
expect(newAccessor.serialize()).toEqual(serializedData);
});
it('is maintained for strings', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.addRepeatedStringElement(1, 'hello');
oldAccessor.addRepeatedStringElement(1, 'world');
const serializedData = oldAccessor.serialize();
@ -178,7 +178,7 @@ describe('LazyAccessor repeated -> optional compatibility', () => {
0xA, 0x5, ...getCharacterCodes('hello'), 0xA, 0x5,
...getCharacterCodes('world')));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getStringWithDefault(1)).toEqual('world');
expect(newAccessor.serialize()).toEqual(serializedData);
});
@ -186,64 +186,64 @@ describe('LazyAccessor repeated -> optional compatibility', () => {
describe('Type change', () => {
it('is supported for fixed32 -> sfixed32', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setFixed32(1, 4294967295);
const serializedData = oldAccessor.serialize();
expect(serializedData)
.toEqual(createArrayBuffer(0xD, 0xFF, 0xFF, 0xFF, 0xFF));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getSfixed32WithDefault(1)).toEqual(-1);
expect(newAccessor.serialize()).toEqual(serializedData);
});
it('is supported for sfixed32 -> fixed32', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setSfixed32(1, -1);
const serializedData = oldAccessor.serialize();
expect(serializedData)
.toEqual(createArrayBuffer(0xD, 0xFF, 0xFF, 0xFF, 0xFF));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getFixed32WithDefault(1)).toEqual(4294967295);
expect(newAccessor.serialize()).toEqual(serializedData);
});
it('is supported for fixed64 -> sfixed64', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setFixed64(1, Int64.fromHexString('0xFFFFFFFFFFFFFFFF'));
const serializedData = oldAccessor.serialize();
expect(serializedData)
.toEqual(createArrayBuffer(
0x9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getSfixed64WithDefault(1)).toEqual(Int64.fromInt(-1));
expect(newAccessor.serialize()).toEqual(serializedData);
});
it('is supported for sfixed64 -> fixed64', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setSfixed64(1, Int64.fromInt(-1));
const serializedData = oldAccessor.serialize();
expect(serializedData)
.toEqual(createArrayBuffer(
0x9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getFixed64WithDefault(1))
.toEqual(Int64.fromHexString('0xFFFFFFFFFFFFFFFF'));
expect(newAccessor.serialize()).toEqual(serializedData);
});
it('is supported for bytes -> message', () => {
const oldAccessor = LazyAccessor.createEmpty();
const oldAccessor = Kernel.createEmpty();
oldAccessor.setBytes(
1, ByteString.fromArrayBuffer(createArrayBuffer(0x8, 0x1)));
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x8, 0x1));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
const message = newAccessor.getMessage(1, TestMessage.instanceCreator);
expect(message.getInt32WithDefault(1)).toEqual(1);
expect(message.serialize()).toEqual(createArrayBuffer(0x8, 0x1));
@ -251,14 +251,14 @@ describe('Type change', () => {
});
it('is supported for message -> bytes', () => {
const oldAccessor = LazyAccessor.createEmpty();
const message = new TestMessage(LazyAccessor.createEmpty());
const oldAccessor = Kernel.createEmpty();
const message = new TestMessage(Kernel.createEmpty());
message.setInt32(1, 1);
oldAccessor.setMessage(1, message);
const serializedData = oldAccessor.serialize();
expect(serializedData).toEqual(createArrayBuffer(0xA, 0x2, 0x8, 0x1));
const newAccessor = LazyAccessor.fromArrayBuffer(serializedData);
const newAccessor = Kernel.fromArrayBuffer(serializedData);
expect(newAccessor.getBytesWithDefault(1))
.toEqual(ByteString.fromArrayBuffer(createArrayBuffer(0x8, 0x1)));
expect(newAccessor.serialize()).toEqual(serializedData);

View File

@ -1,20 +1,20 @@
/**
* @fileoverview LazyAccessor wrapper message.
* @fileoverview Kernel wrapper message.
*/
goog.module('protobuf.testing.binary.TestMessage');
const ByteString = goog.require('protobuf.ByteString');
const Int64 = goog.require('protobuf.Int64');
const InternalMessage = goog.require('protobuf.binary.InternalMessage');
const LazyAccessor = goog.require('protobuf.runtime.Kernel');
const Kernel = goog.require('protobuf.runtime.Kernel');
/**
* A protobuf message implemented as a LazyAccessor wrapper.
* A protobuf message implemented as a Kernel wrapper.
* @implements {InternalMessage}
*/
class TestMessage {
/**
* @param {!LazyAccessor} kernel
* @param {!Kernel} kernel
* @return {!TestMessage}
*/
static instanceCreator(kernel) {
@ -22,17 +22,17 @@ class TestMessage {
}
/**
* @param {!LazyAccessor} kernel
* @param {!Kernel} kernel
*/
constructor(kernel) {
/** @private @const {!LazyAccessor} */
/** @private @const {!Kernel} */
this.kernel_ = kernel;
}
/**
* @override
* @package
* @return {!LazyAccessor}
* @return {!Kernel}
*/
internalGetKernel() {
return this.kernel_;
@ -182,7 +182,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @return {?T}
* @template T
*/
@ -192,7 +192,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @return {T}
* @template T
*/
@ -202,7 +202,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @return {T}
* @template T
*/
@ -212,7 +212,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @return {?LazyAccessor}
* @return {?Kernel}
* @template T
*/
getMessageAccessorOrNull(fieldNumber) {
@ -594,7 +594,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number} index
* @return {T}
* @template T
@ -606,7 +606,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @return {!Iterable<T>}
* @template T
*/
@ -617,7 +617,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @return {!Iterable<!LazyAccessor>}
* @return {!Iterable<!Kernel>}
* @template T
*/
getRepeatedMessageAccessorIterable(fieldNumber) {
@ -626,7 +626,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @return {number}
* @template T
*/
@ -1728,7 +1728,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {!Iterable<T>} values
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @template T
*/
addRepeatedMessageIterable(fieldNumber, values, instanceCreator) {
@ -1739,7 +1739,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {T} value
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @param {number} index
* @throws {!Error} if index is out of range when check mode is critical
* @template T
@ -1752,7 +1752,7 @@ class TestMessage {
/**
* @param {number} fieldNumber
* @param {T} value
* @param {function(!LazyAccessor):T} instanceCreator
* @param {function(!Kernel):T} instanceCreator
* @template T
*/
addRepeatedMessageElement(fieldNumber, value, instanceCreator) {