Skip to content

Commit 23f14a6

Browse files
committed
CLI: Static code statically resolves types[..], see #715
1 parent 6f8f2c1 commit 23f14a6

File tree

9 files changed

+312
-586
lines changed

9 files changed

+312
-586
lines changed

cli/targets/static.js

+3-31
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ function static_target(root, options, callback) {
3636
push((config.es6 ? "const " : "var ") + aliases.map(function(name) { return "$" + name + " = $protobuf." + name; }).join(", ") + ";");
3737
push("");
3838
}
39-
if (config.comments)
40-
push("// Lazily resolved type references");
41-
push((config.es6 ? "const" : "var") + " $lazyTypes = [];");
42-
push("");
4339
if (config.comments) {
4440
if (root.comment)
4541
pushComment("@fileoverview " + root.comment);
@@ -49,10 +45,6 @@ function static_target(root, options, callback) {
4945
var rootProp = cliUtil.safeProp(config.root || "default");
5046
push((config.es6 ? "const" : "var") + " $root = $protobuf.roots" + rootProp + " || ($protobuf.roots" + rootProp + " = {});");
5147
buildNamespace(null, root);
52-
push("");
53-
if (config.comments)
54-
push("// Resolve lazy type references to actual types");
55-
push("$util.lazyResolve($root, $lazyTypes);");
5648
return callback(null, out.join("\n"));
5749
} catch (err) {
5850
return callback(err);
@@ -217,7 +209,9 @@ function buildFunction(type, functionName, gen, scope) {
217209
.replace(/\b(?!\.)Writer\b/g, "$Writer") // use common aliases instead of binding through an iife
218210
.replace(/\b(?!\.)Reader\b/g, "$Reader") // "
219211
.replace(/\b(?!\.)util\.\b/g, "$util.") // "
220-
.replace(/\b(?!\.)types\[\b/g, "$types["); // "
212+
.replace(/\b(?!\.)types\[(\d+)\]/g, function($0, $1) {
213+
return "$root" + type.fieldsArray[$1].resolvedType.fullName;
214+
});
221215

222216
if (config.beautify)
223217
code = beautifyCode(code);
@@ -362,28 +356,6 @@ function buildType(ref, type) {
362356
push("});");
363357
});
364358

365-
var hasTypes = false;
366-
var types = [];
367-
type.fieldsArray.forEach(function(field, index) {
368-
if (field.resolve().resolvedType) { // including enums!
369-
hasTypes = true;
370-
types.push(index + ": "+JSON.stringify(field.resolvedType.fullName.substring(1)));
371-
}
372-
});
373-
374-
if (hasTypes && (config.encode || config.decode || config.verify || config.convert)) {
375-
push("");
376-
if (config.comments)
377-
push("// Lazily resolved type references");
378-
push((config.es6 ? "const" : "var") + " $types = {");
379-
++indent;
380-
types.forEach(function(line, i) {
381-
push(line + (i === types.length - 1 ? "" : ","));
382-
});
383-
--indent;
384-
push("}; $lazyTypes.push($types);");
385-
}
386-
387359
if (config.create) {
388360
push("");
389361
pushComment([

src/class.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Class.generate = function generate(type) { // eslint-disable-line no-unused-vars
9797
* @param {Type} type Reflected message type
9898
* @param {*} [ctor] Custom constructor to set up, defaults to create a generic one if omitted
9999
* @returns {Message} Message prototype
100-
* @deprecated Assign the constructor to {@link Type#ctor} instead
100+
* @deprecated since 6.7.0 it's possible to just assign a new constructor to {@link Type#ctor}
101101
*/
102102
Class.create = Class;
103103

src/util/minimal.js

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ util.oneOfSetter = function setOneOf(fieldNames) {
314314
* @param {Root} root Root instanceof
315315
* @param {Object.<number,string|ReflectionObject>} lazyTypes Type names
316316
* @returns {undefined}
317+
* @deprecated since 6.7.0 static code does not emit lazy types anymore
317318
*/
318319
util.lazyResolve = function lazyResolve(root, lazyTypes) {
319320
for (var i = 0; i < lazyTypes.length; ++i) {

tests/data/comments.js

-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ var $protobuf = require("../../minimal");
66
// Common aliases
77
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
88

9-
// Lazily resolved type references
10-
var $lazyTypes = [];
11-
129
// Exported root namespace
1310
var $root = $protobuf.roots.test_comments || ($protobuf.roots.test_comments = {});
1411

@@ -379,7 +376,4 @@ $root.Test3 = (function() {
379376
return values;
380377
})();
381378

382-
// Resolve lazy type references to actual types
383-
$util.lazyResolve($root, $lazyTypes);
384-
385379
module.exports = $root;

tests/data/convert.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ var $protobuf = require("../../minimal");
66
// Common aliases
77
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
88

9-
// Lazily resolved type references
10-
var $lazyTypes = [];
11-
129
// Exported root namespace
1310
var $root = $protobuf.roots.test_convert || ($protobuf.roots.test_convert = {});
1411

@@ -85,12 +82,6 @@ $root.Message = (function() {
8582
*/
8683
Message.prototype.int64Map = $util.emptyObject;
8784

88-
// Lazily resolved type references
89-
var $types = {
90-
6: "Message.SomeEnum",
91-
7: "Message.SomeEnum"
92-
}; $lazyTypes.push($types);
93-
9485
/**
9586
* Creates a new Message instance using the specified properties.
9687
* @param {Object.<string,*>=} [properties] Properties to set
@@ -471,11 +462,11 @@ $root.Message = (function() {
471462
object.bytesRepeated[j] = options.bytes === String ? $util.base64.encode(message.bytesRepeated[j], 0, message.bytesRepeated[j].length) : options.bytes === Array ? Array.prototype.slice.call(message.bytesRepeated[j]) : message.bytesRepeated[j];
472463
}
473464
if (message.enumVal !== undefined && message.enumVal !== null && message.hasOwnProperty("enumVal"))
474-
object.enumVal = options.enums === String ? $types[6][message.enumVal] : message.enumVal;
465+
object.enumVal = options.enums === String ? $root.Message.SomeEnum[message.enumVal] : message.enumVal;
475466
if (message.enumRepeated && message.enumRepeated.length) {
476467
object.enumRepeated = [];
477468
for (var j = 0; j < message.enumRepeated.length; ++j)
478-
object.enumRepeated[j] = options.enums === String ? $types[7][message.enumRepeated[j]] : message.enumRepeated[j];
469+
object.enumRepeated[j] = options.enums === String ? $root.Message.SomeEnum[message.enumRepeated[j]] : message.enumRepeated[j];
479470
}
480471
var keys2;
481472
if (message.int64Map && (keys2 = Object.keys(message.int64Map)).length) {
@@ -524,7 +515,4 @@ $root.Message = (function() {
524515
return Message;
525516
})();
526517

527-
// Resolve lazy type references to actual types
528-
$util.lazyResolve($root, $lazyTypes);
529-
530518
module.exports = $root;

tests/data/mapbox/vector_tile.js

+16-38
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ var $protobuf = require("../../../minimal");
66
// Common aliases
77
var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
88

9-
// Lazily resolved type references
10-
var $lazyTypes = [];
11-
129
// Exported root namespace
1310
var $root = $protobuf.roots.test_vector_tile || ($protobuf.roots.test_vector_tile = {});
1411

@@ -42,11 +39,6 @@ $root.vector_tile = (function() {
4239
*/
4340
Tile.prototype.layers = $util.emptyArray;
4441

45-
// Lazily resolved type references
46-
var $types = {
47-
0: "vector_tile.Tile.Layer"
48-
}; $lazyTypes.push($types);
49-
5042
/**
5143
* Creates a new Tile instance using the specified properties.
5244
* @param {Object.<string,*>=} [properties] Properties to set
@@ -67,7 +59,7 @@ $root.vector_tile = (function() {
6759
writer = $Writer.create();
6860
if (message.layers !== undefined && message.hasOwnProperty("layers"))
6961
for (var i = 0; i < message.layers.length; ++i)
70-
$types[0].encode(message.layers[i], writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
62+
$root.vector_tile.Tile.Layer.encode(message.layers[i], writer.uint32(/* id 3, wireType 2 =*/26).fork()).ldelim();
7163
return writer;
7264
};
7365

@@ -99,7 +91,7 @@ $root.vector_tile = (function() {
9991
case 3:
10092
if (!(message.layers && message.layers.length))
10193
message.layers = [];
102-
message.layers.push($types[0].decode(reader, reader.uint32()));
94+
message.layers.push($root.vector_tile.Tile.Layer.decode(reader, reader.uint32()));
10395
break;
10496
default:
10597
reader.skipType(tag & 7);
@@ -134,7 +126,7 @@ $root.vector_tile = (function() {
134126
if (!Array.isArray(message.layers))
135127
return "layers: array expected";
136128
for (var i = 0; i < message.layers.length; ++i) {
137-
var error = $types[0].verify(message.layers[i]);
129+
var error = $root.vector_tile.Tile.Layer.verify(message.layers[i]);
138130
if (error)
139131
return "layers." + error;
140132
}
@@ -158,7 +150,7 @@ $root.vector_tile = (function() {
158150
for (var i = 0; i < object.layers.length; ++i) {
159151
if (typeof object.layers[i] !== "object")
160152
throw TypeError(".vector_tile.Tile.layers: object expected");
161-
message.layers[i] = $types[0].fromObject(object.layers[i]);
153+
message.layers[i] = $root.vector_tile.Tile.Layer.fromObject(object.layers[i]);
162154
}
163155
}
164156
return message;
@@ -188,7 +180,7 @@ $root.vector_tile = (function() {
188180
if (message.layers && message.layers.length) {
189181
object.layers = [];
190182
for (var j = 0; j < message.layers.length; ++j)
191-
object.layers[j] = $types[0].toObject(message.layers[j], options);
183+
object.layers[j] = $root.vector_tile.Tile.Layer.toObject(message.layers[j], options);
192184
}
193185
return object;
194186
};
@@ -592,11 +584,6 @@ $root.vector_tile = (function() {
592584
*/
593585
Feature.prototype.geometry = $util.emptyArray;
594586

595-
// Lazily resolved type references
596-
var $types = {
597-
2: "vector_tile.Tile.GeomType"
598-
}; $lazyTypes.push($types);
599-
600587
/**
601588
* Creates a new Feature instance using the specified properties.
602589
* @param {Object.<string,*>=} [properties] Properties to set
@@ -839,7 +826,7 @@ $root.vector_tile = (function() {
839826
object.tags[j] = message.tags[j];
840827
}
841828
if (message.type !== undefined && message.type !== null && message.hasOwnProperty("type"))
842-
object.type = options.enums === String ? $types[2][message.type] : message.type;
829+
object.type = options.enums === String ? $root.vector_tile.Tile.GeomType[message.type] : message.type;
843830
if (message.geometry && message.geometry.length) {
844831
object.geometry = [];
845832
for (var j = 0; j < message.geometry.length; ++j)
@@ -921,12 +908,6 @@ $root.vector_tile = (function() {
921908
*/
922909
Layer.prototype.extent = 4096;
923910

924-
// Lazily resolved type references
925-
var $types = {
926-
2: "vector_tile.Tile.Feature",
927-
4: "vector_tile.Tile.Value"
928-
}; $lazyTypes.push($types);
929-
930911
/**
931912
* Creates a new Layer instance using the specified properties.
932913
* @param {Object.<string,*>=} [properties] Properties to set
@@ -948,13 +929,13 @@ $root.vector_tile = (function() {
948929
writer.uint32(/* id 1, wireType 2 =*/10).string(message.name);
949930
if (message.features !== undefined && message.hasOwnProperty("features"))
950931
for (var i = 0; i < message.features.length; ++i)
951-
$types[2].encode(message.features[i], writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim();
932+
$root.vector_tile.Tile.Feature.encode(message.features[i], writer.uint32(/* id 2, wireType 2 =*/18).fork()).ldelim();
952933
if (message.keys !== undefined && message.hasOwnProperty("keys"))
953934
for (var i = 0; i < message.keys.length; ++i)
954935
writer.uint32(/* id 3, wireType 2 =*/26).string(message.keys[i]);
955936
if (message.values !== undefined && message.hasOwnProperty("values"))
956937
for (var i = 0; i < message.values.length; ++i)
957-
$types[4].encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
938+
$root.vector_tile.Tile.Value.encode(message.values[i], writer.uint32(/* id 4, wireType 2 =*/34).fork()).ldelim();
958939
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
959940
writer.uint32(/* id 5, wireType 0 =*/40).uint32(message.extent);
960941
writer.uint32(/* id 15, wireType 0 =*/120).uint32(message.version);
@@ -995,7 +976,7 @@ $root.vector_tile = (function() {
995976
case 2:
996977
if (!(message.features && message.features.length))
997978
message.features = [];
998-
message.features.push($types[2].decode(reader, reader.uint32()));
979+
message.features.push($root.vector_tile.Tile.Feature.decode(reader, reader.uint32()));
999980
break;
1000981
case 3:
1001982
if (!(message.keys && message.keys.length))
@@ -1005,7 +986,7 @@ $root.vector_tile = (function() {
1005986
case 4:
1006987
if (!(message.values && message.values.length))
1007988
message.values = [];
1008-
message.values.push($types[4].decode(reader, reader.uint32()));
989+
message.values.push($root.vector_tile.Tile.Value.decode(reader, reader.uint32()));
1009990
break;
1010991
case 5:
1011992
message.extent = reader.uint32();
@@ -1051,7 +1032,7 @@ $root.vector_tile = (function() {
10511032
if (!Array.isArray(message.features))
10521033
return "features: array expected";
10531034
for (var i = 0; i < message.features.length; ++i) {
1054-
var error = $types[2].verify(message.features[i]);
1035+
var error = $root.vector_tile.Tile.Feature.verify(message.features[i]);
10551036
if (error)
10561037
return "features." + error;
10571038
}
@@ -1067,7 +1048,7 @@ $root.vector_tile = (function() {
10671048
if (!Array.isArray(message.values))
10681049
return "values: array expected";
10691050
for (var i = 0; i < message.values.length; ++i) {
1070-
var error = $types[4].verify(message.values[i]);
1051+
var error = $root.vector_tile.Tile.Value.verify(message.values[i]);
10711052
if (error)
10721053
return "values." + error;
10731054
}
@@ -1098,7 +1079,7 @@ $root.vector_tile = (function() {
10981079
for (var i = 0; i < object.features.length; ++i) {
10991080
if (typeof object.features[i] !== "object")
11001081
throw TypeError(".vector_tile.Tile.Layer.features: object expected");
1101-
message.features[i] = $types[2].fromObject(object.features[i]);
1082+
message.features[i] = $root.vector_tile.Tile.Feature.fromObject(object.features[i]);
11021083
}
11031084
}
11041085
if (object.keys) {
@@ -1115,7 +1096,7 @@ $root.vector_tile = (function() {
11151096
for (var i = 0; i < object.values.length; ++i) {
11161097
if (typeof object.values[i] !== "object")
11171098
throw TypeError(".vector_tile.Tile.Layer.values: object expected");
1118-
message.values[i] = $types[4].fromObject(object.values[i]);
1099+
message.values[i] = $root.vector_tile.Tile.Value.fromObject(object.values[i]);
11191100
}
11201101
}
11211102
if (object.extent !== undefined && object.extent !== null)
@@ -1157,7 +1138,7 @@ $root.vector_tile = (function() {
11571138
if (message.features && message.features.length) {
11581139
object.features = [];
11591140
for (var j = 0; j < message.features.length; ++j)
1160-
object.features[j] = $types[1].toObject(message.features[j], options);
1141+
object.features[j] = $root.vector_tile.Tile.Feature.toObject(message.features[j], options);
11611142
}
11621143
if (message.keys && message.keys.length) {
11631144
object.keys = [];
@@ -1167,7 +1148,7 @@ $root.vector_tile = (function() {
11671148
if (message.values && message.values.length) {
11681149
object.values = [];
11691150
for (var j = 0; j < message.values.length; ++j)
1170-
object.values[j] = $types[3].toObject(message.values[j], options);
1151+
object.values[j] = $root.vector_tile.Tile.Value.toObject(message.values[j], options);
11711152
}
11721153
if (message.extent !== undefined && message.extent !== null && message.hasOwnProperty("extent"))
11731154
object.extent = message.extent;
@@ -1202,7 +1183,4 @@ $root.vector_tile = (function() {
12021183
return vector_tile;
12031184
})();
12041185

1205-
// Resolve lazy type references to actual types
1206-
$util.lazyResolve($root, $lazyTypes);
1207-
12081186
module.exports = $root;

0 commit comments

Comments
 (0)