8000 Remove "jspb" prefix from assert function names · benjaminp/protobuf-javascript@58b8533 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58b8533

Browse files
committed
Remove "jspb" prefix from assert function names
The jspb.asserts namespace should be sufficient to prevent closure from pruning the asserts, so remove the redundant prefix from the function names.
1 parent b4da468 commit 58b8533

File tree

11 files changed

+193
-252
lines changed

11 files changed

+193
-252
lines changed

asserts.js

Lines changed: 23 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,6 @@
1111

1212
goog.provide('jspb.asserts');
1313

14-
/**
15-
* Error object for failed assertions.
16-
*
17-
* @extends {Error}
18-
* @final
19-
*/
20-
21-
class JspbAssertionError extends Error {
22-
/**
23-
* @param {string} messagePattern The pattern that was used to form message.
24-
* @param {!Array<*>} messageArgs The items to substitute into the pattern.
25-
*/
26-
constructor(messagePattern, messageArgs) {
27-
super(subs(messagePattern, messageArgs));
28-
29-
/**
30-
* The message pattern used to format the error message. Error handlers can
31-
* use this to uniquely identify the assertion.
32-
* @type {string}
33-
*/
34-
this.messagePattern = messagePattern;
35-
}
36-
}
37-
38-
jspb.asserts.JspbAssertionError = JspbAssertionError;
39-
40-
/**
41-
* The default error handler.
42-
* @param {!JspbAssertionError} e The exception to be handled.
43-
* @return {void}
44-
*/
45-
jspb.asserts.JSPB_DEFAULT_ERROR_HANDLER = function(e) {
46-
throw e;
47-
}
48-
49-
50-
51-
/**
52-
* The handler responsible for throwing or logging assertion errors.
53-
* @type {function(!JspbAssertionError)}
54-
*/
55-
let errorHandler_ = jspb.asserts.JSPB_DEFAULT_ERROR_HANDLER;
56-
57-
5814
/**
5915
* Does simple python-style string substitution.
6016
* subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".
@@ -85,7 +41,7 @@ function subs(pattern, subs) {
8541
* @param {?Array<*>} defaultArgs The substitution arguments for defaultMessage.
8642
* @param {string|undefined} givenMessage Message supplied by the caller.
8743
* @param {!Array<*>} givenArgs The substitution arguments for givenMessage.
88-
* @throws {JspbAssertionError} When the value is not a number.
44+
* @throws {Error} When the value is not a number.
8945
*/
9046
function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
9147
let message = 'Assertion failed';
@@ -101,36 +57,22 @@ function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
10157
// a stack trace is added to var message above. With this, a stack trace is
10258
// not added until this line (it causes the extra garbage to be added after
10359
// the assertion message instead of in the middle of it).
104-
const e = new JspbAssertionError('' + message, args || []);
105-
errorHandler_(e);
60+
throw new Error('' + message, args || []);
10661
}
10762

108-
/**
109-
* Sets a custom error handler that can be used to customize the behavior of
110-
* assertion failures, for example by turning all assertion failures into log
111-
* messages.
112-
* @param {function(!JspbAssertionError)} errorHandler
113-
* @return {void}
114-
*/
115-
jspb.asserts.setJspbErrorHandler = function(errorHandler) {
116-
errorHandler_ = errorHandler;
117-
};
118-
119-
12063
/**
12164
* Checks if the condition evaluates to true.
12265
* @template T
12366
* @param {T} condition The condition to check.
12467
* @param {string=} opt_message Error message in case of failure.
125-
* @param {...*} var_args The items to substitute into the failure message.
68+
* @param {...*} args The items to substitute into the failure message.
12669
* @return {T} The value of the condition.
127-
* @throws {JspbAssertionError} When the condition evaluates to false.
70+
* @throws {Error} When the condition evaluates to false.
12871
*/
12972

130-
jspb.asserts.jspbAssert = function(condition, opt_message, var_args) {
73+
jspb.asserts.assert = function(condition, opt_message, ...args) {
13174
if (!condition) {
132-
doAssertFailure(
133-
'', null, opt_message, Array.prototype.slice.call(arguments, 2));
75+
doAssertFailure('', null, opt_message, args);
13476
}
13577
return condition;
13678
};
@@ -140,15 +82,15 @@ jspb.asserts.jspbAssert = function(condition, opt_message, var_args) {
14082
* Checks if the value is a string.
14183
* @param {*} value The value to check.
14284
* @param {string=} opt_message Error message in case of failure.
143-
* @param {...*} var_args The items to substitute into the failure message.
85+
* @param {...*} args The items to substitute into the failure message.
14486
* @return {string} The value, guaranteed to be a string when asserts enabled.
145-
* @throws {JspbAssertionError} When the value is not a string.
87+
* @throws {Error} When the value is not a string.
14688
*/
147-
jspb.asserts.jspbAssertString = function(value, opt_message, var_args) {
89+
jspb.asserts.assertString = function(value, opt_message, ...args) {
14890
if (typeof value !== 'string') {
14991
doAssertFailure(
15092
'Expected string but got %s: %s.', [goog.typeOf(value), value],
151-
opt_message, Array.prototype.slice.call(arguments, 2));
93+
opt_message, args);
15294
}
15395
return /** @type {string} */ (value);
15496
};
@@ -158,15 +100,15 @@ jspb.asserts.jspbAssertString = function(value, opt_message, var_args) {
158100
* Checks if the value is an Array.
159101
* @param {*} value The value to check.
160102
* @param {string=} opt_message Error message in case of failure.
161-
* @param {...*} var_args The items to substitute into the failure message.
103+
* @param {...*} args The items to substitute into the failure message.
162104
* @return {!Array<?>} The value, guaranteed to be a non-null array.
163-
* @throws {JspbAssertionError} When the value is not an array.
105+
* @throws {Error} When the value is not an array.
164106
*/
165-
jspb.asserts.jspbAssertArray = function(value, opt_message, var_args) {
107+
jspb.asserts.assertArray = function(value, opt_message, ...args) {
166108
if (!Array.isArray(value)) {
167109
doAssertFailure(
168110
'Expected array but got %s: %s.', [goog.typeOf(value), value],
169-
opt_message, Array.prototype.slice.call(arguments, 2));
111+
opt_message, args);
170112
}
171113
return /** @type {!Array<?>} */ (value);
172114
};
@@ -185,14 +127,14 @@ jspb.asserts.jspbAssertArray = function(value, opt_message, var_args) {
185127
* </pre>
186128
*
187129 * @param {string=} opt_message Error message in case of failure.
188-
* @param {...*} var_args The items to substitute into the failure message.
130+
* @param {...*} args The items to substitute into the failure message.
189131
* @return {void}
190-
* @throws {JspbAssertionError} Failure.
132+
* @throws {Error} Failure.
191133
*/
192-
jspb.asserts.jspbFail = function(opt_message, var_args) {
193-
errorHandler_(new JspbAssertionError(
134+
jspb.asserts.fail = function(opt_message, ...args) {
135+
throw new Error(
194136
'Failure' + (opt_message ? ': ' + opt_message : ''),
195-
Array.prototype.slice.call(arguments, 1)));
137+
args);
196138
};
197139

198140
/**
@@ -205,17 +147,17 @@ jspb.asserts.jspbFail = function(opt_message, var_args) {
205147
* @param {?} value The value to check.
206148
* @param {function(new: T, ...)} type A user-defined constructor.
207149
* @param {string=} opt_message Error message in case of failure.
208-
* @param {...*} var_args The items to substitute into the failure message.
209-
* @throws {JspbAssertionError} When the value is not an instance of
150+
* @param {...*} args The items to substitute into the failure message.
151+
* @throws {Error} When the value is not an instance of
210152
* type.
211153
* @return {T}
212154
* @template T
213155
*/
214-
jspb.asserts.jspbAssertInstanceof = function(value, type, opt_message, var_args) {
156+
jspb.asserts.assertInstanceof = function(value, type, opt_message, ...args) {
215157
if (!(value instanceof type)) {
216158
doAssertFailure(
217159
'Expected instanceof %s but got %s.', [getType(type), getType(value)],
218-
opt_message, Array.prototype.slice.call(arguments, 3));
160+
opt_message, args);
219161
}
220162
return value;
221163
};

binary/decoder.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ jspb.BinaryDecoder.prototype.setCursor = function(cursor) {
235235
*/
236236
jspb.BinaryDecoder.prototype.advance = function(count) {
237237
this.cursor_ += count;
238-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
238+
jspb.asserts.assert(this.cursor_ <= this.end_);
239239
};
240240

241241

@@ -316,7 +316,7 @@ jspb.BinaryDecoder.prototype.readSplitVarint64 = function(convert) {
316316
}
317317

318318
// If we did not see the terminator, the encoding was invalid.
319-
jspb.asserts.jspbFail('Failed to read varint, encoding is invalid.');
319+
jspb.asserts.fail('Failed to read varint, encoding is invalid.');
320320
this.error_ = true;
321321
};
322322

@@ -422,31 +422,31 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint32 = function() {
422422
var x = (temp & 0x7F);
423423
if (temp < 128) {
424424
this.cursor_ += 1;
425-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
425+
jspb.asserts.assert(this.cursor_ <= this.end_);
426426
return x;
427427
}
428428

429429
temp = bytes[this.cursor_ + 1];
430430
x |= (temp & 0x7F) << 7;
431431
if (temp < 128) {
432432
this.cursor_ += 2;
433-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
433+
jspb.asserts.assert(this.cursor_ <= this.end_);
434434
return x;
435435
}
436436

437437
temp = bytes[this.cursor_ + 2];
438438
x |= (temp & 0x7F) << 14;
439439
if (temp < 128) {
440440
this.cursor_ += 3;
441-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
441+
jspb.asserts.assert(this.cursor_ <= this.end_);
442442
return x;
443443
}
444444

445445
temp = bytes[this.cursor_ + 3];
446446
x |= (temp & 0x7F) << 21;
447447
if (temp < 128) {
448448
this.cursor_ += 4;
449-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
449+
jspb.asserts.assert(this.cursor_ <= this.end_);
450450
return x;
451451
}
452452

@@ -456,7 +456,7 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint32 = function() {
456456
// We're reading the high bits of an unsigned varint. The byte we just read
457457
// also contains bits 33 through 35, which we're going to discard.
458458
this.cursor_ += 5;
459-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
459+
jspb.asserts.assert(this.cursor_ <= this.end_);
460460
return x >>> 0;
461461
}
462462

@@ -467,10 +467,10 @@ jspb.BinaryDecoder.prototype.readUnsignedVarint32 = function() {
467467
bytes[this.cursor_++] >= 128 && bytes[this.cursor_++] >= 128 &&
468468
bytes[this.cursor_++] >= 128) {
469469
// If we get here, the varint is too long.
470-
jspb.asserts.jspbAssert(false);
470+
jspb.asserts.assert(false);
471471
}
472472

473-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
473+
jspb.asserts.assert(this.cursor_ <= this.end_);
474474
return x;
475475
};
476476

@@ -638,7 +638,7 @@ jspb.BinaryDecoder.prototype.readZigzagVarint64String = function() {
638638
jspb.BinaryDecoder.prototype.readUint8 = function() {
639639
var a = this.bytes_[this.cursor_ + 0];
640640
this.cursor_ += 1;
641-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
641+
jspb.asserts.assert(this.cursor_ <= this.end_);
642642
return a;
643643
};
644644

@@ -652,7 +652,7 @@ jspb.BinaryDecoder.prototype.readUint16 = function() {
652652
var a = this.bytes_[this.cursor_ + 0];
653653
var b = this.bytes_[this.cursor_ + 1];
654654
this.cursor_ += 2;
655-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
655+
jspb.asserts.assert(this.cursor_ <= this.end_);
656656
return (a << 0) | (b << 8);
657657
};
658658

@@ -668,7 +668,7 @@ jspb.BinaryDecoder.prototype.readUint32 = function() {
668668
var c = this.bytes_[this.cursor_ + 2];
669669
var d = this.bytes_[this.cursor_ + 3];
670670
this.cursor_ += 4;
671-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
671+
jspb.asserts.assert(this.cursor_ <= this.end_);
672672
return ((a << 0) | (b << 8) | (c << 16) | (d << 24)) >>> 0;
673673
};
674674

@@ -710,7 +710,7 @@ jspb.BinaryDecoder.prototype.readUint64String = function() {
710710
jspb.BinaryDecoder.prototype.readInt8 = function() {
711711
var a = this.bytes_[this.cursor_ + 0];
712712
this.cursor_ += 1;
713-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
713+
jspb.asserts.assert(this.cursor_ <= this.end_);
714714
return (a << 24) >> 24;
715715
};
716716

@@ -724,7 +724,7 @@ jspb.BinaryDecoder.prototype.readInt16 = function() {
724724
var a = this.bytes_[this.cursor_ + 0];
725725
var b = this.bytes_[this.cursor_ + 1];
726726
this.cursor_ += 2;
727-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
727+
jspb.asserts.assert(this.cursor_ <= this.end_);
728728
return (((a << 0) | (b << 8)) << 16) >> 16;
729729
};
730730

@@ -740,7 +740,7 @@ jspb.BinaryDecoder.prototype.readInt32 = function() {
740740
var c = this.bytes_[this.cursor_ + 2];
741741
var d = this.bytes_[this.cursor_ + 3];
742742
this.cursor_ += 4;
743-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
743+
jspb.asserts.assert(this.cursor_ <= this.end_);
744744
return (a << 0) | (b << 8) | (c << 16) | (d << 24);
745745
};
746746

@@ -902,14 +902,14 @@ jspb.BinaryDecoder.prototype.readStringWithLength = function() {
902902
jspb.BinaryDecoder.prototype.readBytes = function(length) {
903903
if (length < 0 || this.cursor_ + length > this.bytes_.length) {
904904
this.error_ = true;
905-
jspb.asserts.jspbFail('Invalid byte length!');
905+
jspb.asserts.fail('Invalid byte length!');
906906
return new Uint8Array(0);
907907
}
908908

909909
var result = this.bytes_.subarray(this.cursor_, this.cursor_ + length);
910910

911911
this.cursor_ += length;
912-
jspb.asserts.jspbAssert(this.cursor_ <= this.end_);
912+
jspb.asserts.assert(this.cursor_ <= this.end_);
913913
return result;
914914
};
915915

0 commit comments

Comments
 (0)
0