-
Notifications
You must be signed in to change notification settings - Fork 15.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaScript: segregate references to binary functionality #1802
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ | |
* @author [email protected] (Mark Rawling) | ||
*/ | ||
|
||
goog.provide('jspb.ExtensionFieldBinaryInfo'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ordering |
||
goog.provide('jspb.ExtensionFieldInfo'); | ||
goog.provide('jspb.Message'); | ||
|
||
|
@@ -84,19 +85,12 @@ goog.forwardDeclare('xid.String'); | |
* @param {?function(new: jspb.Message, Array=)} ctor | ||
* @param {?function((boolean|undefined),!jspb.Message):!Object} toObjectFn | ||
* @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 | ||
* @struct | ||
* @template T | ||
*/ | ||
jspb.ExtensionFieldInfo = function(fieldNumber, fieldName, ctor, toObjectFn, | ||
isRepeated, opt_binaryReaderFn, opt_binaryWriterFn, | ||
opt_binaryMessageSerializeFn, opt_binaryMessageDeserializeFn, | ||
opt_isPacked) { | ||
isRepeated) { | ||
/** @const */ | ||
this.fieldIndex = fieldNumber; | ||
/** @const */ | ||
|
@@ -106,20 +100,37 @@ jspb.ExtensionFieldInfo = function(fieldNumber, fieldName, ctor, toObjectFn, | |
/** @const */ | ||
this.toObjectFn = toObjectFn; | ||
/** @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 */ | ||
this.fieldInfo = fieldInfo; | ||
/** @const */ | ||
this.binaryWriterFn = opt_binaryWriterFn; | ||
this.binaryReaderFn = binaryReaderFn; | ||
/** @const */ | ||
this.binaryMessageSerializeFn = opt_binaryMessageSerializeFn; | ||
this.binaryWriterFn = binaryWriterFn; | ||
/** @const */ | ||
this.binaryMessageDeserializeFn = opt_binaryMessageDeserializeFn; | ||
this.binaryMessageSerializeFn = binaryMessageSerializeFn; | ||
/** @const */ | ||
this.isRepeated = isRepeated; | ||
this.binaryMessageDeserializeFn = binaryMessageDeserializeFn; | ||
/** @const */ | ||
this.isPacked = opt_isPacked; | ||
this.isPacked = isPacked; | ||
}; | ||
|
||
|
||
/** | ||
* @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, | ||
getExtensionFn) { | ||
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 | ||
// need to gracefully error-out here rather than produce a null dereference | ||
// below. | ||
if (!fieldInfo.binaryWriterFn) { | ||
if (!binaryFieldInfo.binaryWriterFn) { | ||
throw new Error('Message extension present that was generated ' + | ||
'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 | ||
// here, at runtime (and this decoupled codegen is the whole point of | ||
// extensions!). | ||
if (fieldInfo.binaryMessageSerializeFn) { | ||
fieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, | ||
value, fieldInfo.binaryMessageSerializeFn); | ||
if (binaryFieldInfo.binaryMessageSerializeFn) { | ||
binaryFieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, | ||
value, binaryFieldInfo.binaryMessageSerializeFn); | ||
} else { | ||
throw new Error('Message extension present holding submessage ' + | ||
'without binary support enabled, and message is ' + | ||
'being serialized to binary format'); | ||
} | ||
} else { | ||
fieldInfo.binaryWriterFn.call(writer, fieldInfo.fieldIndex, value); | ||
binaryFieldInfo.binaryWriterFn.call( | ||
writer, fieldInfo.fieldIndex, value); | ||
} | ||
} | ||
} | ||
|
@@ -535,27 +549,28 @@ jspb.Message.serializeBinaryExtensions = function(proto, writer, extensions, | |
*/ | ||
jspb.Message.readBinaryExtension = function(msg, reader, extensions, | ||
getExtensionFn, setExtensionFn) { | ||
var fieldInfo = extensions[reader.getFieldNumber()]; | ||
if (!fieldInfo) { | ||
var binaryFieldInfo = extensions[reader.getFieldNumber()]; | ||
var fieldInfo = binaryFieldInfo.fieldInfo; | ||
if (!binaryFieldInfo) { | ||
reader.skipField(); | ||
return; | ||
} | ||
if (!fieldInfo.binaryReaderFn) { | ||
if (!binaryFieldInfo.binaryReaderFn) { | ||
throw new Error('Deserializing extension whose generated code does not ' + | ||
'support binary format'); | ||
} | ||
|
||
var value; | ||
if (fieldInfo.isMessageType()) { | ||
value = new fieldInfo.ctor(); | ||
fieldInfo.binaryReaderFn.call( | ||
reader, value, fieldInfo.binaryMessageDeserializeFn); | ||
binaryFieldInfo.binaryReaderFn.call( | ||
reader, value, binaryFieldInfo.binaryMessageDeserializeFn); | ||
} else { | ||
// 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); | ||
if (!currentList) { | ||
setExtensionFn.call(msg, fieldInfo, [value]); | ||
|
@@ -747,29 +762,16 @@ jspb.Message.getFieldProto3 = function(msg, fieldNumber, defaultValue) { | |
* of serialization/parsing callbacks (which are required by the map at | ||
* construction time, and the map may be constructed here). | ||
* | ||
* The below callbacks are used to allow the map to serialize and parse its | ||
* binary wire format data. Their purposes are described in more detail in | ||
* `jspb.Map`'s constructor documentation. | ||
* | ||
* @template K, V | ||
* @param {!jspb.Message} msg | ||
* @param {number} fieldNumber | ||
* @param {boolean|undefined} noLazyCreate | ||
* @param {?=} opt_valueCtor | ||
* @param {function(number,K)=} opt_keyWriterFn | ||
* @param {function():K=} opt_keyReaderFn | ||
* @param {function(number,V)|function(number,V,?)| | ||
* function(number,V,?,?,?,?)=} opt_valueWriterFn | ||
* @param {function():V| | ||
* function(V,function(?,?))=} opt_valueReaderFn | ||
* @param {function(?,?)|function(?,?,?,?,?)=} opt_valueWriterCallback | ||
* @param {function(?,?)=} opt_valueReaderCallback | ||
* @return {!jspb.Map<K, V>|undefined} | ||
* @protected | ||
*/ | ||
jspb.Message.getMapField = function(msg, fieldNumber, noLazyCreate, | ||
opt_valueCtor, opt_keyWriterFn, opt_keyReaderFn, opt_valueWriterFn, | ||
opt_valueReaderFn, opt_valueWriterCallback, opt_valueReaderCallback) { | ||
opt_valueCtor) { | ||
if (!msg.wrappers_) { | ||
msg.wrappers_ = {}; | ||
} | ||
|
@@ -787,10 +789,7 @@ jspb.Message.getMapField = function(msg, fieldNumber, noLazyCreate, | |
} | ||
return msg.wrappers_[fieldNumber] = | ||
new jspb.Map( | ||
/** @type {!Array<!Array<!Object>>} */ (arr), | ||
opt_keyWriterFn, opt_keyReaderFn, opt_valueWriterFn, | ||
opt_valueReaderFn, opt_valueCtor, opt_valueWriterCallback, | ||
opt_valueReaderCallback); | ||
/** @type {!Array<!Array<!Object>>} */ (arr), opt_valueCtor); | ||
} | ||
}; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goog.require ordering