Skip to content

Commit 9f216f3

Browse files
authored
Update to goog.modules (#260)
1 parent e50318a commit 9f216f3

File tree

8 files changed

+599
-468
lines changed

8 files changed

+599
-468
lines changed

asserts.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
* invariants runtime.
1010
*/
1111

12-
goog.provide('jspb.asserts');
12+
goog.module('jspb.asserts');
13+
goog.module.declareLegacyNamespace();
1314

1415
/**
1516
* Throws an exception with the given message and "Assertion failed" prefixed
@@ -20,7 +21,7 @@ goog.provide('jspb.asserts');
2021
* @param {!Array<*>} givenArgs The substitution arguments for givenMessage.
2122
* @throws {Error} When the value is not a number.
2223
*/
23-
jspb.asserts.doAssertFailure = function(defaultMessage, defaultArgs, givenMessage, givenArgs) {
24+
function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
2425
let message = 'Assertion failed';
2526
let args;
2627
if (givenMessage) {
@@ -46,13 +47,12 @@ jspb.asserts.doAssertFailure = function(defaultMessage, defaultArgs, givenMessag
4647
* @return {T} The value of the condition.
4748
* @throws {Error} When the condition evaluates to false.
4849
*/
49-
50-
jspb.asserts.assert = function(condition, opt_message, ...args) {
50+
function assert(condition, opt_message, ...args) {
5151
if (!condition) {
52-
jspb.asserts.doAssertFailure('', null, opt_message, args);
52+
doAssertFailure('', null, opt_message, args);
5353
}
5454
return condition;
55-
};
55+
}
5656

5757

5858
/**
@@ -63,14 +63,14 @@ jspb.asserts.assert = function(condition, opt_message, ...args) {
6363
* @return {string} The value, guaranteed to be a string when asserts enabled.
6464
* @throws {Error} When the value is not a string.
6565
*/
66-
jspb.asserts.assertString = function(value, opt_message, ...args) {
66+
function assertString(value, opt_message, ...args) {
6767
if (typeof value !== 'string') {
68-
jspb.asserts.doAssertFailure(
68+
doAssertFailure(
6969
'Expected string but got %s: %s.', [goog.typeOf(value), value],
7070
opt_message, args);
7171
}
7272
return /** @type {string} */ (value);
73-
};
73+
}
7474

7575

7676
/**
@@ -81,14 +81,14 @@ jspb.asserts.assertString = function(value, opt_message, ...args) {
8181
* @return {!Array<?>} The value, guaranteed to be a non-null array.
8282
* @throws {Error} When the value is not an array.
8383
*/
84-
jspb.asserts.assertArray = function(value, opt_message, ...args) {
84+
function assertArray(value, opt_message, ...args) {
8585
if (!Array.isArray(value)) {
86-
jspb.asserts.doAssertFailure(
86+
doAssertFailure(
8787
'Expected array but got %s: %s.', [goog.typeOf(value), value],
8888
opt_message, args);
8989
}
9090
return /** @type {!Array<?>} */ (value);
91-
};
91+
}
9292

9393
/**
9494
* Triggers a failure. This function is useful in case when we want to add a
@@ -98,7 +98,7 @@ jspb.asserts.assertArray = function(value, opt_message, ...args) {
9898
* switch(type) {
9999
* case FOO: doSomething(); break;
100100
* case BAR: doSomethingElse(); break;
101-
* default: jspb.asserts.JspbFail('Unrecognized type: ' + type);
101+
* default: jspb.asserts.fail('Unrecognized type: ' + type);
102102
* // We have only 2 types - "default:" section is unreachable code.
103103
* }
104104
* </pre>
@@ -108,11 +108,11 @@ jspb.asserts.assertArray = function(value, opt_message, ...args) {
108108
* @return {void}
109109
* @throws {Error} Failure.
110110
*/
111-
jspb.asserts.fail = function(opt_message, ...args) {
111+
function fail(opt_message, ...args) {
112112
throw new Error(
113113
'Failure' + (opt_message ? ': ' + opt_message : ''),
114114
args);
115-
};
115+
}
116116

117117
/**
118118
* Checks if the value is an instance of the user-defined type.
@@ -130,15 +130,15 @@ jspb.asserts.fail = function(opt_message, ...args) {
130130
* @return {T}
131131
* @template T
132132
*/
133-
jspb.asserts.assertInstanceof = function(value, type, opt_message, ...args) {
133+
function assertInstanceof(value, type, opt_message, ...args) {
134134
if (!(value instanceof type)) {
135-
jspb.asserts.doAssertFailure(
135+
doAssertFailure(
136136
'Expected instanceof %s but got %s.',
137-
[jspb.asserts.getType(type), jspb.asserts.getType(value)],
137+
[getType(type), getType(value)],
138138
opt_message, args);
139139
}
140140
return value;
141-
};
141+
}
142142

143143
/**
144144
* Returns the type of a value. If a constructor is passed, and a suitable
@@ -147,7 +147,7 @@ jspb.asserts.assertInstanceof = function(value, type, opt_message, ...args) {
147147
* @return {string} The best display name for the value, or 'unknown type name'.
148148
* @private
149149
*/
150-
jspb.asserts.getType = function(value) {
150+
function getType(value) {
151151
if (value instanceof Function) {
152152
return value.displayName || value.name || 'unknown type name';
153153
} else if (value instanceof Object) {
@@ -157,3 +157,13 @@ jspb.asserts.getType = function(value) {
157157
return value === null ? 'null' : typeof value;
158158
}
159159
}
160+
161+
exports = {
162+
doAssertFailure,
163+
assert,
164+
assertString,
165+
assertArray,
166+
fail,
167+
assertInstanceof,
168+
getType
169+
};

commonjs/rewrite_tests_for_commonjs.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,11 @@ var module = null;
6363
var pkg = null;
6464

6565
// Header: goes in every file at the top.
66-
console.log("var global = Function('return this')();");
66+
console.log("var global = globalThis;");
6767
console.log("var googleProtobuf = require('google-protobuf');");
6868
console.log("var testdeps = require('testdeps_commonjs');");
6969
console.log("global.COMPILED = testdeps.COMPILED;");
7070
console.log("global.goog = testdeps.goog;");
71-
console.log("global.jspb = googleProtobuf.jspb;");
7271
console.log("");
7372

7473
lineReader.on('line', function (line) {

debug.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@
3232
* @fileoverview Utilities to debug JSPB based proto objects.
3333
*/
3434

35-
goog.provide('jspb.debug');
35+
goog.module('jspb.debug');
36+
goog.module.declareLegacyNamespace();
3637

37-
goog.require('goog.array');
38-
goog.require('goog.object');
38+
const googArray = goog.require('goog.array');
39+
const googObject = goog.require('goog.object');
3940

40-
goog.require('jspb.asserts');
41-
goog.require('jspb.Map');
42-
goog.require('jspb.Message');
41+
const asserts = goog.require('jspb.asserts');
42+
const Map = goog.require('jspb.Map');
43+
const Message = goog.require('jspb.Message');
4344

4445

4546
/**
@@ -49,22 +50,21 @@ goog.require('jspb.Message');
4950
* work in obfuscated and or optimized code.
5051
* Use this in environments where {@see jspb.Message.prototype.toObject} is
5152
* not available for code size reasons.
52-
* @param {jspb.Message} message A jspb.Message.
53+
* @param {Message} message A jspb.Message.
5354
* @return {Object}
54-
* @export
5555
*/
56-
jspb.debug.dump = function(message) {
56+
function dump(message) {
5757
if (!goog.DEBUG) {
5858
return null;
5959
}
60-
jspb.asserts.assertInstanceof(message, jspb.Message,
60+
asserts.assertInstanceof(message, Message,
6161
'jspb.Message instance expected');
6262
/** @type {Object} */
6363
var object = message;
64-
jspb.asserts.assert(object['getExtension'],
64+
asserts.assert(object['getExtension'],
6565
'Only unobfuscated and unoptimized compilation modes supported.');
66-
return /** @type {Object} */ (jspb.debug.dump_(message));
67-
};
66+
return /** @type {Object} */ (dump_(message));
67+
}
6868

6969

7070
/**
@@ -75,7 +75,7 @@ jspb.debug.dump = function(message) {
7575
* @return {*}
7676
* @private
7777
*/
78-
jspb.debug.dump_ = function(thing) {
78+
function dump_(thing) {
7979
var type = goog.typeOf(thing);
8080
var message = thing; // Copy because we don't want type inference on thing.
8181
if (type == 'number' || type == 'string' || type == 'boolean' ||
@@ -90,20 +90,20 @@ jspb.debug.dump_ = function(thing) {
9090
}
9191

9292
if (type == 'array') {
93-
jspb.asserts.assertArray(thing);
94-
return goog.array.map(thing, jspb.debug.dump_);
93+
asserts.assertArray(thing);
94+
return googArray.map(thing, dump_);
9595
}
9696

97-
if (message instanceof jspb.Map) {
97+
if (message instanceof Map) {
9898
var mapObject = {};
9999
var entries = message.entries();
100100
for (var entry = entries.next(); !entry.done; entry = entries.next()) {
101-
mapObject[entry.value[0]] = jspb.debug.dump_(entry.value[1]);
101+
mapObject[entry.value[0]] = dump_(entry.value[1]);
102102
}
103103
return mapObject;
104104
}
105105

106-
jspb.asserts.assertInstanceof(message, jspb.Message,
106+
asserts.assertInstanceof(message, Message,
107107
'Only messages expected: ' + thing);
108108
var ctor = message.constructor;
109109
var messageName = ctor.name || ctor.displayName;
@@ -117,7 +117,7 @@ jspb.debug.dump_ = function(thing) {
117117
var has = 'has' + match[1];
118118
if (!thing[has] || thing[has]()) {
119119
var val = thing[name]();
120-
object[jspb.debug.formatFieldName_(match[1])] = jspb.debug.dump_(val);
120+
object[formatFieldName_(match[1])] = dump_(val);
121121
}
122122
}
123123
}
@@ -132,18 +132,18 @@ jspb.debug.dump_ = function(thing) {
132132
if (/^\d+$/.test(id)) {
133133
var ext = ctor['extensions'][id];
134134
var extVal = thing.getExtension(ext);
135-
var fieldName = goog.object.getKeys(ext.fieldName)[0];
135+
var fieldName = googObject.getKeys(ext.fieldName)[0];
136136
if (extVal != null) {
137137
if (!extensionsObject) {
138138
extensionsObject = object['$extensions'] = {};
139139
}
140-
extensionsObject[jspb.debug.formatFieldName_(fieldName)] =
141-
jspb.debug.dump_(extVal);
140+
extensionsObject[formatFieldName_(fieldName)] =
141+
dump_(extVal);
142142
}
143143
}
144144
}
145145
return object;
146-
};
146+
}
147147

148148

149149
/**
@@ -153,9 +153,13 @@ jspb.debug.dump_ = function(thing) {
153153
* @return {string}
154154
* @private
155155
*/
156-
jspb.debug.formatFieldName_ = function(name) {
156+
function formatFieldName_(name) {
157157
// Name may be in TitleCase.
158158
return name.replace(/^[A-Z]/, function(c) {
159159
return c.toLowerCase();
160160
});
161+
}
162+
163+
exports = {
164+
dump
161165
};

extension_field_binary_info.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
// https://protobuf.dev/
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above
12+
// copyright notice, this list of conditions and the following disclaimer
13+
// in the documentation and/or other materials provided with the
14+
// distribution.
15+
// * Neither the name of Google Inc. nor the names of its
16+
// contributors may be used to endorse or promote products derived from
17+
// this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
/**
32+
* @fileoverview Definition of jspb.ExtensionFieldBinaryInfo.
33+
*/
34+
35+
goog.module('jspb.ExtensionFieldBinaryInfo');
36+
goog.module.declareLegacyNamespace();
37+
38+
const ExtensionFieldInfo = goog.require('jspb.ExtensionFieldInfo');
39+
goog.requireType('jspb.BinaryReader');
40+
goog.requireType('jspb.BinaryWriter');
41+
42+
/**
43+
* Stores binary-related information for a single extension field.
44+
* @param {!ExtensionFieldInfo<T>} fieldInfo
45+
* @param {function(this:jspb.BinaryReader,number,?,?)} binaryReaderFn
46+
* @param {function(this:jspb.BinaryWriter,number,?)
47+
* |function(this:jspb.BinaryWriter,number,?,?,?,?,?)} binaryWriterFn
48+
* @param {function(?,?)=} opt_binaryMessageSerializeFn
49+
* @param {function(?,?)=} opt_binaryMessageDeserializeFn
50+
* @param {boolean=} opt_isPacked
51+
* @constructor
52+
* @struct
53+
* @template T
54+
*/
55+
const ExtensionFieldBinaryInfo = function (
56+
fieldInfo, binaryReaderFn, binaryWriterFn, opt_binaryMessageSerializeFn,
57+
opt_binaryMessageDeserializeFn, opt_isPacked) {
58+
/** @const */
59+
this.fieldInfo = fieldInfo;
60+
/** @const */
61+
this.binaryReaderFn = binaryReaderFn;
62+
/** @const */
63+
this.binaryWriterFn = binaryWriterFn;
64+
/** @const */
65+
this.binaryMessageSerializeFn = opt_binaryMessageSerializeFn;
66+
/** @const */
67+
this.binaryMessageDeserializeFn = opt_binaryMessageDeserializeFn;
68+
/** @const */
69+
this.isPacked = opt_isPacked;
70+
};
71+
72+
exports = ExtensionFieldBinaryInfo;

0 commit comments

Comments
 (0)