Skip to content

Commit 5f07302

Browse files
committed
Static target: emit $prototype only if there are any prototype properties, see #540; utf8 1.0.2
1 parent ecbb4a5 commit 5f07302

File tree

11 files changed

+132
-154
lines changed

11 files changed

+132
-154
lines changed

cli/targets/static.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ function buildType(ref, type) {
166166
--indent;
167167
push("}");
168168

169-
push("");
170-
push("/** @alias " + fullName + ".prototype */");
171-
push("var $prototype = " + name(type.name) + ".prototype;");
169+
if (type.fieldsArray.length || type.oneofsArray.length) {
170+
push("");
171+
push("/** @alias " + fullName + ".prototype */");
172+
push("var $prototype = " + name(type.name) + ".prototype;");
173+
}
172174

173175
// default values
174176
type.fieldsArray.forEach(function(field) {

dist/protobuf.js

Lines changed: 51 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/protobuf.min.js.gz

25 Bytes
Binary file not shown.

dist/protobuf.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@protobufjs/codegen": "1.0.1",
4242
"@protobufjs/eventemitter": "1.0.1",
4343
"@protobufjs/pool": "1.0.1",
44-
"@protobufjs/utf8": "1.0.1"
44+
"@protobufjs/utf8": "1.0.2"
4545
},
4646
"optionalDependencies": {
4747
"long": "^3.2.0"

src/class.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,36 @@ function Class(type) {
2727
Class.create = function create(type, ctor) {
2828
if (!(type instanceof Type))
2929
throw _TypeError("type", "a Type");
30-
var clazz = ctor;
31-
if (clazz) {
32-
if (typeof clazz !== 'function')
30+
if (ctor) {
31+
if (typeof ctor !== 'function')
3332
throw _TypeError("ctor", "a function");
3433
} else
35-
clazz = (function(MessageCtor) { // eslint-disable-line wrap-iife
34+
ctor = (function(MessageCtor) { // eslint-disable-line wrap-iife
3635
return function Message(properties) {
3736
MessageCtor.call(this, properties);
3837
};
3938
})(Message);
4039

4140
// Let's pretend...
42-
clazz.constructor = Class;
41+
ctor.constructor = Class;
4342

4443
// new Class() -> Message.prototype
45-
var prototype = clazz.prototype = new Message();
46-
prototype.constructor = clazz;
44+
var prototype = ctor.prototype = new Message();
45+
prototype.constructor = ctor;
4746

4847
// Static methods on Message are instance methods on Class and vice versa.
49-
util.merge(clazz, Message, true);
48+
util.merge(ctor, Message, true);
5049

5150
// Classes and messages reference their reflected type
52-
clazz.$type = type;
51+
ctor.$type = type;
5352
prototype.$type = type;
5453

5554
// Messages have non-enumerable default values on their prototype
5655
type.getFieldsArray().forEach(function(field) {
57-
field.resolve();
5856
// objects on the prototype must be immmutable. users must assign a new object instance and
5957
// cannot use Array#push on empty arrays on the prototype for example, as this would modify
6058
// the value on the prototype for ALL messages of this type. Hence, these objects are frozen.
61-
prototype[field.name] = Array.isArray(field.defaultValue)
59+
prototype[field.name] = Array.isArray(field.resolve().defaultValue)
6260
? util.emptyArray
6361
: util.isObject(field.defaultValue)
6462
? util.emptyObject
@@ -70,23 +68,21 @@ Class.create = function create(type, ctor) {
7068
util.prop(prototype, oneof.resolve().name, {
7169
get: function getVirtual() {
7270
// > If the parser encounters multiple members of the same oneof on the wire, only the last member seen is used in the parsed message.
73-
var keys = Object.keys(this);
74-
for (var i = keys.length - 1; i > -1; --i)
71+
for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
7572
if (oneof.oneof.indexOf(keys[i]) > -1)
7673
return keys[i];
7774
return undefined;
7875
},
7976
set: function setVirtual(value) {
80-
var keys = oneof.oneof;
81-
for (var i = 0; i < keys.length; ++i)
77+
for (var keys = oneof.oneof, i = 0; i < keys.length; ++i)
8278
if (keys[i] !== value)
8379
delete this[keys[i]];
8480
}
8581
});
8682
});
8783

8884
// Register
89-
type.setCtor(clazz);
85+
type.setCtor(ctor);
9086

9187
return prototype;
9288
};

0 commit comments

Comments
 (0)