8000 Sync from Piper @425656941 · graup/protobuf-javascript@31aec6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 31aec6f

Browse files
committed
Sync from Piper @425656941
PROTOBUF_SYNC_PIPER
2 parents c8284ae + b02d5b4 commit 31aec6f

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

commonjs/export.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ goog.require('jspb.ExtensionFieldInfo');
1515
goog.require('jspb.Message');
1616
goog.require('jspb.Map');
1717

18-
exports.Map = jspb.Map;
19-
exports.Message = jspb.Message;
20-
exports.BinaryReader = jspb.BinaryReader;
21-
exports.BinaryWriter = jspb.BinaryWriter;
22-
exports.ExtensionFieldInfo = jspb.ExtensionFieldInfo;
23-
exports.ExtensionFieldBinaryInfo = jspb.ExtensionFieldBinaryInfo;
18+
if (typeof exports === 'object') {
19+
exports.Map = jspb.Map;
20+
exports.Message = jspb.Message;
21+
exports.BinaryReader = jspb.BinaryReader;
22+
exports.BinaryWriter = jspb.BinaryWriter;
23+
exports.ExtensionFieldInfo = jspb.ExtensionFieldInfo;
24+
exports.ExtensionFieldBinaryInfo = jspb.ExtensionFieldBinaryInfo;
2425

25-
// These are used by generated code but should not be used directly by clients.
26-
exports.exportSymbol = goog.exportSymbol;
27-
exports.inherits = goog.inherits;
28-
exports.object = {extend: goog.object.extend};
29-
exports.typeOf = goog.typeOf;
26+
// These are used by generated code but should not be used directly by
27+
// clients.
28+
exports.exportSymbol = goog.exportSymbol;
29+
exports.inherits = goog.inherits;
30+
exports.object = {extend: goog.object.extend};
31+
exports.typeOf = goog.typeOf;
32+
}

experimental/benchmarks/code_size/apps_jspb/all_types_proto2.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function accessAllTypes() {
119119
arrayVal[0] = true;
120120
msgAllTypes.setRepeatedBoolList(arrayVal);
121121
msgAllTypes.setRepeatedBytesList(['']);
122-
arrayVal = msgAllTypes.getRepeatedBytesList();
122+
arrayVal = msgAllTypes.getRepeatedBytesList_asB64();
123123
arrayVal[0] = '';
124124
msgAllTypes.setRepeatedBytesList(arrayVal);
125125
msgPackedTypes.setPackedDoubleList([1.0]);
@@ -233,8 +233,7 @@ function accessAllTypes() {
233233

234234
let s = '';
235235
s += msgAllTypes.getOptionalBool() || false;
236-
s += msgAllTypes.getOptionalBytes() || '';
237-
// s += msgAllTypes.getOptionalBytes_asB64() || "";
236+
s += msgAllTypes.getOptionalBytes_asB64() || '';
238237
// s += msgAllTypes.getOptionalBytes_asU8() || new Uint8Array([]);
239238
s += msgAllTypes.getOptionalDouble() || 0.0;
240239
s += msgAllTypes.getOptionalFixed32() || 0;
@@ -254,10 +253,9 @@ function accessAllTypes() {
254253
s += msgAllTypes.getRepeatedBoolList();
255254
s += msgAllTypes.getRepeatedBoolList()[0];
256255
s += msgAllTypes.getRepeatedBoolList().length;
257-
s += msgAllTypes.getRepeatedBytesList();
258-
s += msgAllTypes.getRepeatedBytesList()[0];
259-
s += msgAllTypes.getRepeatedBytesList().length;
260256
s += msgAllTypes.getRepeatedBytesList_asB64();
257+
s += msgAllTypes.getRepeatedBytesList_asB64()[0];
258+
s += msgAllTypes.getRepeatedBytesList_asB64().length;
261259
s += msgAllTypes.getRepeatedBytesList_asU8();
262260
s += msgAllTypes.getRepeatedDoubleList();
263261
s += msgAllTypes.getRepeatedDoubleList()[0];

experimental/benchmarks/code_size/apps_jspb/all_types_proto3.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function accessAllTypes() {
119119
arrayVal[0] = true;
120120
msgAllTypes.setRepeatedBoolList(arrayVal);
121121
msgAllTypes.setRepeatedBytesList(['']);
122-
arrayVal = msgAllTypes.getRepeatedBytesList();
122+
arrayVal = msgAllTypes.getRepeatedBytesList_asB64();
123123
arrayVal[0] = '';
124124
msgAllTypes.setRepeatedBytesList(arrayVal);
125125
msgPackedTypes.setPackedDoubleList([1.0]);
@@ -233,8 +233,7 @@ function accessAllTypes() {
233233

234234
let s = '';
235235
s += msgAllTypes.getOptionalBool() || false;
236-
s += msgAllTypes.getOptionalBytes() || '';
237-
// s += msgAllTypes.getOptionalBytes_asB64() || "";
236+
s += msgAllTypes.getOptionalBytes_asB64() || '';
238237
// s += msgAllTypes.getOptionalBytes_asU8() || new Uint8Array([]);
239238
s += msgAllTypes.getOptionalDouble() || 0.0;
240239
s += msgAllTypes.getOptionalFixed32() || 0;
@@ -254,10 +253,9 @@ function accessAllTypes() {
254253
s += msgAllTypes.getRepeatedBoolList();
255254
s += msgAllTypes.getRepeatedBoolList()[0];
256255
s += msgAllTypes.getRepeatedBoolList().length;
257-
s += msgAllTypes.getRepeatedBytesList();
258-
s += msgAllTypes.getRepeatedBytesList()[0];
259-
s += msgAllTypes.getRepeatedBytesList().length;
260256
s += msgAllTypes.getRepeatedBytesList_asB64();
257+
s += msgAllTypes.getRepeatedBytesList_asB64()[0];
258+
s += msgAllTypes.getRepeatedBytesList_asB64().length;
261259
s += msgAllTypes.getRepeatedBytesList_asU8();
262260
s += msgAllTypes.getRepeatedDoubleList();
263261
s += msgAllTypes.getRepeatedDoubleList()[0];

experimental/runtime/kernel/reader.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,6 @@ function readSfixed64(bufferDecoder, start) {
9797
return Int64.fromBits(lowBits, highBits);
9898
}
9999

100-
/**
101-
* Reads a sint32 value from the binary bytes encoded as varint.
102-
* @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
103-
* @param {number} start Start of the data.
104-
* @return {number}
105-
* @package
106-
*/
107-
function readSint32(bufferDecoder, start) {
108-
const bits = bufferDecoder.getUnsignedVarint32At(start);
109-
// Truncate upper bits and convert from zig zag to signd int
110-
return (bits >>> 1) ^ -(bits & 0x01);
111-
}
112-
113100
/**
114101
* Reads a sint64 value from the binary bytes encoded as varint.
115102
* @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
@@ -125,6 +112,17 @@ function readSint64(bufferDecoder, start) {
125112
return Int64.fromBits(decodedLowerBits, decodedUpperBits);
126113
}
127114

115+
/**
116+
* Reads a sint32 value from the binary bytes encoded as varint.
117+
* @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.
118+
* @param {number} start Start of the data.
119+
* @return {number}
120+
* @package
121+
*/
122+
function readSint32(bufferDecoder, start) {
123+
return readSint64(bufferDecoder, start).getLowBits();
124+
}
125+
128126
/**
129127
* Read a subarray of bytes representing a length delimited field.
130128
* @param {!BufferDecoder} bufferDecoder Binary format encoded bytes.

src/google/protobuf/compiler/js/js_generator.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3625,7 +3625,19 @@ void Generator::GenerateFile(const GeneratorOptions& options,
36253625
if (options.import_style == GeneratorOptions::kImportCommonJsStrict) {
36263626
printer->Print("var proto = {};\n\n");
36273627
} else {
3628-
printer->Print("var global = Function('return this')();\n\n");
3628+
// To get the global object we call a function with .call(null), this will
3629+
// set "this" inside the function to the global object. This does not work
3630+
// if we are running in strict mode ("use strict"), so we fallback to the
3631+
// following things (in order from first to last):
3632+
// - window: defined in browsers
3633+
// - global: defined in most server side environments like NodeJS
3634+
// - self: defined inside Web Workers (WorkerGlobalScope)
3635+
// - Function('return this')(): this will work on most platforms, but it
3636+
// may be blocked by things like CSP.
3637+
// Function('') is almost the same as eval('')
3638+
printer->Print(
3639+
"var global = (function() { return this || window || global || self "
3640+
"|| Function('return this')(); }).call(null);\n\n");
36293641
}
36303642

36313643
for (int i = 0; i < file->dependency_count(); i++) {

0 commit comments

Comments
 (0)
0