Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* invariants runtime.
*/

goog.provide('jspb.asserts');
goog.module('jspb.asserts');
goog.module.declareLegacyNamespace();

/**
* Throws an exception with the given message and "Assertion failed" prefixed
Expand All @@ -20,7 +21,7 @@ goog.provide('jspb.asserts');
* @param {!Array<*>} givenArgs The substitution arguments for givenMessage.
* @throws {Error} When the value is not a number.
*/
jspb.asserts.doAssertFailure = function(defaultMessage, defaultArgs, givenMessage, givenArgs) {
function doAssertFailure(defaultMessage, defaultArgs, givenMessage, givenArgs) {
let message = 'Assertion failed';
let args;
if (givenMessage) {
Expand All @@ -46,13 +47,12 @@ jspb.asserts.doAssertFailure = function(defaultMessage, defaultArgs, givenMessag
* @return {T} The value of the condition.
* @throws {Error} When the condition evaluates to false.
*/

jspb.asserts.assert = function(condition, opt_message, ...args) {
function assert(condition, opt_message, ...args) {
if (!condition) {
jspb.asserts.doAssertFailure('', null, opt_message, args);
doAssertFailure('', null, opt_message, args);
}
return condition;
};
}


/**
Expand All @@ -63,14 +63,14 @@ jspb.asserts.assert = function(condition, opt_message, ...args) {
* @return {string} The value, guaranteed to be a string when asserts enabled.
* @throws {Error} When the value is not a string.
*/
jspb.asserts.assertString = function(value, opt_message, ...args) {
function assertString(value, opt_message, ...args) {
if (typeof value !== 'string') {
jspb.asserts.doAssertFailure(
doAssertFailure(
'Expected string but got %s: %s.', [goog.typeOf(value), value],
opt_message, args);
}
return /** @type {string} */ (value);
};
}


/**
Expand All @@ -81,14 +81,14 @@ jspb.asserts.assertString = function(value, opt_message, ...args) {
* @return {!Array<?>} The value, guaranteed to be a non-null array.
* @throws {Error} When the value is not an array.
*/
jspb.asserts.assertArray = function(value, opt_message, ...args) {
function assertArray(value, opt_message, ...args) {
if (!Array.isArray(value)) {
jspb.asserts.doAssertFailure(
doAssertFailure(
'Expected array but got %s: %s.', [goog.typeOf(value), value],
opt_message, args);
}
return /** @type {!Array<?>} */ (value);
};
}

/**
* Triggers a failure. This function is useful in case when we want to add a
Expand All @@ -98,7 +98,7 @@ jspb.asserts.assertArray = function(value, opt_message, ...args) {
* switch(type) {
* case FOO: doSomething(); break;
* case BAR: doSomethingElse(); break;
* default: jspb.asserts.JspbFail('Unrecognized type: ' + type);
* default: jspb.asserts.fail('Unrecognized type: ' + type);
* // We have only 2 types - "default:" section is unreachable code.
* }
* </pre>
Expand All @@ -108,11 +108,11 @@ jspb.asserts.assertArray = function(value, opt_message, ...args) {
* @return {void}
* @throws {Error} Failure.
*/
jspb.asserts.fail = function(opt_message, ...args) {
function fail(opt_message, ...args) {
throw new Error(
'Failure' + (opt_message ? ': ' + opt_message : ''),
args);
};
}

/**
* Checks if the value is an instance of the user-defined type.
Expand All @@ -130,15 +130,15 @@ jspb.asserts.fail = function(opt_message, ...args) {
* @return {T}
* @template T
*/
jspb.asserts.assertInstanceof = function(value, type, opt_message, ...args) {
function assertInstanceof(value, type, opt_message, ...args) {
if (!(value instanceof type)) {
jspb.asserts.doAssertFailure(
doAssertFailure(
'Expected instanceof %s but got %s.',
[jspb.asserts.getType(type), jspb.asserts.getType(value)],
[getType(type), getType(value)],
opt_message, args);
}
return value;
};
}

/**
* Returns the type of a value. If a constructor is passed, and a suitable
Expand All @@ -147,7 +147,7 @@ jspb.asserts.assertInstanceof = function(value, type, opt_message, ...args) {
* @return {string} The best display name for the value, or 'unknown type name'.
* @private
*/
jspb.asserts.getType = function(value) {
function getType(value) {
if (value instanceof Function) {
return value.displayName || value.name || 'unknown type name';
} else if (value instanceof Object) {
Expand All @@ -157,3 +157,13 @@ jspb.asserts.getType = function(value) {
return value === null ? 'null' : typeof value;
}
}

exports = {
doAssertFailure,
assert,
assertString,
assertArray,
fail,
assertInstanceof,
getType
};
3 changes: 1 addition & 2 deletions commonjs/rewrite_tests_for_commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ var module = null;
var pkg = null;

// Header: goes in every file at the top.
console.log("var global = Function('return this')();");
console.log("var global = globalThis;");
console.log("var googleProtobuf = require('google-protobuf');");
console.log("var testdeps = require('testdeps_commonjs');");
console.log("global.COMPILED = testdeps.COMPILED;");
console.log("global.goog = testdeps.goog;");
console.log("global.jspb = googleProtobuf.jspb;");
console.log("");

lineReader.on('line', function (line) {
Expand Down
54 changes: 29 additions & 25 deletions debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
* @fileoverview Utilities to debug JSPB based proto objects.
*/

goog.provide('jspb.debug');
goog.module('jspb.debug');
goog.module.declareLegacyNamespace();

goog.require('goog.array');
goog.require('goog.object');
const googArray = goog.require('goog.array');
const googObject = goog.require('goog.object');

goog.require('jspb.asserts');
goog.require('jspb.Map');
goog.require('jspb.Message');
const asserts = goog.require('jspb.asserts');
const Map = goog.require('jspb.Map');
const Message = goog.require('jspb.Message');


/**
Expand All @@ -49,22 +50,21 @@ goog.require('jspb.Message');
* work in obfuscated and or optimized code.
* Use this in environments where {@see jspb.Message.prototype.toObject} is
* not available for code size reasons.
* @param {jspb.Message} message A jspb.Message.
* @param {Message} message A jspb.Message.
* @return {Object}
* @export
*/
jspb.debug.dump = function(message) {
function dump(message) {
if (!goog.DEBUG) {
return null;
}
jspb.asserts.assertInstanceof(message, jspb.Message,
asserts.assertInstanceof(message, Message,
'jspb.Message instance expected');
/** @type {Object} */
var object = message;
jspb.asserts.assert(object['getExtension'],
asserts.assert(object['getExtension'],
'Only unobfuscated and unoptimized compilation modes supported.');
return /** @type {Object} */ (jspb.debug.dump_(message));
};
return /** @type {Object} */ (dump_(message));
}


/**
Expand All @@ -75,7 +75,7 @@ jspb.debug.dump = function(message) {
* @return {*}
* @private
*/
jspb.debug.dump_ = function(thing) {
function dump_(thing) {
var type = goog.typeOf(thing);
var message = thing; // Copy because we don't want type inference on thing.
if (type == 'number' || type == 'string' || type == 'boolean' ||
Expand All @@ -90,20 +90,20 @@ jspb.debug.dump_ = function(thing) {
}

if (type == 'array') {
jspb.asserts.assertArray(thing);
return goog.array.map(thing, jspb.debug.dump_);
asserts.assertArray(thing);
return googArray.map(thing, dump_);
}

if (message instanceof jspb.Map) {
if (message instanceof Map) {
var mapObject = {};
var entries = message.entries();
for (var entry = entries.next(); !entry.done; entry = entries.next()) {
mapObject[entry.value[0]] = jspb.debug.dump_(entry.value[1]);
mapObject[entry.value[0]] = dump_(entry.value[1]);
}
return mapObject;
}

jspb.asserts.assertInstanceof(message, jspb.Message,
asserts.assertInstanceof(message, Message,
'Only messages expected: ' + thing);
var ctor = message.constructor;
var messageName = ctor.name || ctor.displayName;
Expand All @@ -117,7 +117,7 @@ jspb.debug.dump_ = function(thing) {
var has = 'has' + match[1];
if (!thing[has] || thing[has]()) {
var val = thing[name]();
object[jspb.debug.formatFieldName_(match[1])] = jspb.debug.dump_(val);
object[formatFieldName_(match[1])] = dump_(val);
}
}
}
Expand All @@ -132,18 +132,18 @@ jspb.debug.dump_ = function(thing) {
if (/^\d+$/.test(id)) {
var ext = ctor['extensions'][id];
var extVal = thing.getExtension(ext);
var fieldName = goog.object.getKeys(ext.fieldName)[0];
var fieldName = googObject.getKeys(ext.fieldName)[0];
if (extVal != null) {
if (!extensionsObject) {
extensionsObject = object['$extensions'] = {};
}
extensionsObject[jspb.debug.formatFieldName_(fieldName)] =
jspb.debug.dump_(extVal);
extensionsObject[formatFieldName_(fieldName)] =
dump_(extVal);
}
}
}
return object;
};
}


/**
Expand All @@ -153,9 +153,13 @@ jspb.debug.dump_ = function(thing) {
* @return {string}
* @private
*/
jspb.debug.formatFieldName_ = function(name) {
function formatFieldName_(name) {
// Name may be in TitleCase.
return name.replace(/^[A-Z]/, function(c) {
return c.toLowerCase();
});
}

exports = {
dump
};
72 changes: 72 additions & 0 deletions extension_field_binary_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://protobuf.dev/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/**
* @fileoverview Definition of jspb.ExtensionFieldBinaryInfo.
*/

goog.module('jspb.ExtensionFieldBinaryInfo');
goog.module.declareLegacyNamespace();

const ExtensionFieldInfo = goog.require('jspb.ExtensionFieldInfo');
goog.requireType('jspb.BinaryReader');
goog.requireType('jspb.BinaryWriter');

/**
* Stores binary-related information for a single extension field.
* @param {!ExtensionFieldInfo<T>} fieldInfo
* @param {function(this:jspb.BinaryReader,number,?,?)} binaryReaderFn
* @param {function(this:jspb.BinaryWriter,number,?)
* |function(this:jspb.BinaryWriter,number,?,?,?,?,?)} binaryWriterFn
* @param {function(?,?)=} opt_binaryMessageSerializeFn
* @param {function(?,?)=} opt_binaryMessageDeserializeFn
* @param {boolean=} opt_isPacked
* @constructor
* @struct
* @template T
*/
const ExtensionFieldBinaryInfo = function (
fieldInfo, binaryReaderFn, binaryWriterFn, opt_binaryMessageSerializeFn,
opt_binaryMessageDeserializeFn, opt_isPacked) {
/** @const */
this.fieldInfo = fieldInfo;
/** @const */
this.binaryReaderFn = binaryReaderFn;
/** @const */
this.binaryWriterFn = binaryWriterFn;
/** @const */
this.binaryMessageSerializeFn = opt_binaryMessageSerializeFn;
/** @const */
this.binaryMessageDeserializeFn = opt_binaryMessageDeserializeFn;
/** @const */
this.isPacked = opt_isPacked;
};

exports = ExtensionFieldBinaryInfo;
Loading
Loading