|
|
@ -35,6 +35,7 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
goog.provide('jspb.ExtensionFieldInfo'); |
|
|
|
goog.provide('jspb.ExtensionFieldInfo'); |
|
|
|
|
|
|
|
goog.provide('jspb.ExtensionFieldBinaryInfo'); |
|
|
|
goog.provide('jspb.Message'); |
|
|
|
goog.provide('jspb.Message'); |
|
|
|
|
|
|
|
|
|
|
|
goog.require('goog.array'); |
|
|
|
goog.require('goog.array'); |
|
|
@ -84,19 +85,12 @@ goog.forwardDeclare('xid.String'); |
|
|
|
* @param {?function(new: jspb.Message, Array=)} ctor |
|
|
|
* @param {?function(new: jspb.Message, Array=)} ctor |
|
|
|
* @param {?function((boolean|undefined),!jspb.Message):!Object} toObjectFn |
|
|
|
* @param {?function((boolean|undefined),!jspb.Message):!Object} toObjectFn |
|
|
|
* @param {number} isRepeated |
|
|
|
* @param {number} isRepeated |
|
|
|
* @param {?function(number,?)=} opt_binaryReaderFn |
|
|
|
|
|
|
|
* @param {?function(number,?)|function(number,?,?,?,?,?)=} opt_binaryWriterFn |
|
|
|
|
|
|
|
* @param {?function(?,?)=} opt_binaryMessageSerializeFn |
|
|
|
|
|
|
|
* @param {?function(?,?)=} opt_binaryMessageDeserializeFn |
|
|
|
|
|
|
|
* @param {?boolean=} opt_isPacked |
|
|
|
|
|
|
|
* @constructor |
|
|
|
* @constructor |
|
|
|
* @struct |
|
|
|
* @struct |
|
|
|
* @template T |
|
|
|
* @template T |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
jspb.ExtensionFieldInfo = function(fieldNumber, fieldName, ctor, toObjectFn, |
|
|
|
jspb.ExtensionFieldInfo = function(fieldNumber, fieldName, ctor, toObjectFn, |
|
|
|
isRepeated, opt_binaryReaderFn, opt_binaryWriterFn, |
|
|
|
isRepeated) { |
|
|
|
opt_binaryMessageSerializeFn, opt_binaryMessageDeserializeFn, |
|
|
|
|
|
|
|
opt_isPacked) { |
|
|
|
|
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.fieldIndex = fieldNumber; |
|
|
|
this.fieldIndex = fieldNumber; |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
@ -106,20 +100,37 @@ jspb.ExtensionFieldInfo = function(fieldNumber, fieldName, ctor, toObjectFn, |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.toObjectFn = toObjectFn; |
|
|
|
this.toObjectFn = toObjectFn; |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.binaryReaderFn = opt_binaryReaderFn; |
|
|
|
this.isRepeated = isRepeated; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Stores binary-related information for a single extension field. |
|
|
|
|
|
|
|
* @param {!jspb.ExtensionFieldInfo<T>} fieldInfo |
|
|
|
|
|
|
|
* @param {?function(number,?)=} binaryReaderFn |
|
|
|
|
|
|
|
* @param {?function(number,?)|function(number,?,?,?,?,?)=} binaryWriterFn |
|
|
|
|
|
|
|
* @param {?function(?,?)=} opt_binaryMessageSerializeFn |
|
|
|
|
|
|
|
* @param {?function(?,?)=} opt_binaryMessageDeserializeFn |
|
|
|
|
|
|
|
* @param {?boolean=} opt_isPacked |
|
|
|
|
|
|
|
* @constructor |
|
|
|
|
|
|
|
* @struct |
|
|
|
|
|
|
|
* @template T |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
jspb.ExtensionFieldBinaryInfo = function(fieldInfo, binaryReaderFn, binaryWriterFn, |
|
|
|
|
|
|
|
binaryMessageSerializeFn, binaryMessageDeserializeFn, isPacked) { |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.binaryWriterFn = opt_binaryWriterFn; |
|
|
|
this.fieldInfo = fieldInfo; |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.binaryMessageSerializeFn = opt_binaryMessageSerializeFn; |
|
|
|
this.binaryReaderFn = binaryReaderFn; |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.binaryMessageDeserializeFn = opt_binaryMessageDeserializeFn; |
|
|
|
this.binaryWriterFn = binaryWriterFn; |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.isRepeated = isRepeated; |
|
|
|
this.binaryMessageSerializeFn = binaryMessageSerializeFn; |
|
|
|
/** @const */ |
|
|
|
/** @const */ |
|
|
|
this.isPacked = opt_isPacked; |
|
|
|
this.binaryMessageDeserializeFn = binaryMessageDeserializeFn; |
|
|
|
|
|
|
|
/** @const */ |
|
|
|
|
|
|
|
this.isPacked = isPacked; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @return {boolean} Does this field represent a sub Message? |
|
|
|
* @return {boolean} Does this field represent a sub Message? |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -491,11 +502,13 @@ jspb.Message.toObjectExtension = function(proto, obj, extensions, |
|
|
|
jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, |
|
|
|
jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, |
|
|
|
getExtensionFn) { |
|
|
|
getExtensionFn) { |
|
|
|
for (var fieldNumber in extensions) { |
|
|
|
for (var fieldNumber in extensions) { |
|
|
|
var fieldInfo = extensions[fieldNumber]; |
|
|
|
var binaryFieldInfo = extensions[fieldNumber]; |
|
|
|
|
|
|
|
var fieldInfo = binaryFieldInfo.fieldInfo; |
|
|
|
|
|
|
|
|
|
|
|
// The old codegen doesn't add the extra fields to ExtensionFieldInfo, so we
|
|
|
|
// The old codegen doesn't add the extra fields to ExtensionFieldInfo, so we
|
|
|
|
// need to gracefully error-out here rather than produce a null dereference
|
|
|
|
// need to gracefully error-out here rather than produce a null dereference
|
|
|
|
// below.
|
|
|
|
// below.
|
|
|
|
if (!fieldInfo.binaryWriterFn) { |
|
|
|
if (!binaryFieldInfo.binaryWriterFn) { |
|
|
|
throw new Error('Message extension present that was generated ' + |
|
|
|
throw new Error('Message extension present that was generated ' + |
|
|
|
'without binary serialization support'); |
|
|
|
'without binary serialization support'); |
|
|
|
} |
|
|
|
} |
|
|
@ -508,16 +521,17 @@ jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, |
|
|
|
// message may require binary support, so we can *only* catch this error
|
|
|
|
// message may require binary support, so we can *only* catch this error
|
|
|
|
// here, at runtime (and this decoupled codegen is the whole point of
|
|
|
|
// here, at runtime (and this decoupled codegen is the whole point of
|
|
|
|
// extensions!).
|
|
|
|
// extensions!).
|
|
|
|
if (fieldInfo.binaryMessageSerializeFn) { |
|
|
|
if (binaryFieldInfo.binaryMessageSerializeFn) { |
|
|
|
fieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, |
|
|
|
binaryFieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, |
|
|
|
value, fieldInfo.binaryMessageSerializeFn); |
|
|
|
value, binaryFieldInfo.binaryMessageSerializeFn); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw new Error('Message extension present holding submessage ' + |
|
|
|
throw new Error('Message extension present holding submessage ' + |
|
|
|
'without binary support enabled, and message is ' + |
|
|
|
'without binary support enabled, and message is ' + |
|
|
|
'being serialized to binary format'); |
|
|
|
'being serialized to binary format'); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
fieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, value); |
|
|
|
binaryFieldInfo.binaryWriterFn.call( |
|
|
|
|
|
|
|
writer, fieldInfo.fieldIndex, value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -535,12 +549,13 @@ jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
jspb.Message.readBinaryExtension = function(msg, reader, extensions, |
|
|
|
jspb.Message.readBinaryExtension = function(msg, reader, extensions, |
|
|
|
getExtensionFn, setExtensionFn) { |
|
|
|
getExtensionFn, setExtensionFn) { |
|
|
|
var fieldInfo = extensions[reader.getFieldNumber()]; |
|
|
|
var binaryFieldInfo = extensions[reader.getFieldNumber()]; |
|
|
|
if (!fieldInfo) { |
|
|
|
var fieldInfo = binaryFieldInfo.fieldInfo; |
|
|
|
|
|
|
|
if (!binaryFieldInfo) { |
|
|
|
reader.skipField(); |
|
|
|
reader.skipField(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!fieldInfo.binaryReaderFn) { |
|
|
|
if (!binaryFieldInfo.binaryReaderFn) { |
|
|
|
throw new Error('Deserializing extension whose generated code does not ' + |
|
|
|
throw new Error('Deserializing extension whose generated code does not ' + |
|
|
|
'support binary format'); |
|
|
|
'support binary format'); |
|
|
|
} |
|
|
|
} |
|
|
@ -548,14 +563,14 @@ jspb.Message.readBinaryExtension = function(msg, reader, extensions, |
|
|
|
var value; |
|
|
|
var value; |
|
|
|
if (fieldInfo.isMessageType()) { |
|
|
|
if (fieldInfo.isMessageType()) { |
|
|
|
value = new fieldInfo.ctor(); |
|
|
|
value = new fieldInfo.ctor(); |
|
|
|
fieldInfo.binaryReaderFn.call( |
|
|
|
binaryFieldInfo.binaryReaderFn.call( |
|
|
|
reader, value, fieldInfo.binaryMessageDeserializeFn); |
|
|
|
reader, value, binaryFieldInfo.binaryMessageDeserializeFn); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// All other types.
|
|
|
|
// All other types.
|
|
|
|
value = fieldInfo.binaryReaderFn.call(reader); |
|
|
|
value = binaryFieldInfo.binaryReaderFn.call(reader); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (fieldInfo.isRepeated && !fieldInfo.isPacked) { |
|
|
|
if (fieldInfo.isRepeated && !binaryFieldInfo.isPacked) { |
|
|
|
var currentList = getExtensionFn.call(msg, fieldInfo); |
|
|
|
var currentList = getExtensionFn.call(msg, fieldInfo); |
|
|
|
if (!currentList) { |
|
|
|
if (!currentList) { |
|
|
|
setExtensionFn.call(msg, fieldInfo, [value]); |
|
|
|
setExtensionFn.call(msg, fieldInfo, [value]); |
|
|
|