Skip to content

Commit

Permalink
Static target: emit $prototype only if there are any prototype proper…
Browse files Browse the repository at this point in the history
…ties, see #540; utf8 1.0.2
  • Loading branch information
dcodeIO committed Dec 11, 2016
1 parent ecbb4a5 commit 5f07302
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 154 deletions.
8 changes: 5 additions & 3 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ function buildType(ref, type) {
--indent;
push("}");

push("");
push("/** @alias " + fullName + ".prototype */");
push("var $prototype = " + name(type.name) + ".prototype;");
if (type.fieldsArray.length || type.oneofsArray.length) {
push("");
push("/** @alias " + fullName + ".prototype */");
push("var $prototype = " + name(type.name) + ".prototype;");
}

// default values
type.fieldsArray.forEach(function(field) {
Expand Down
113 changes: 51 additions & 62 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@protobufjs/codegen": "1.0.1",
"@protobufjs/eventemitter": "1.0.1",
"@protobufjs/pool": "1.0.1",
"@protobufjs/utf8": "1.0.1"
"@protobufjs/utf8": "1.0.2"
},
"optionalDependencies": {
"long": "^3.2.0"
Expand Down
28 changes: 12 additions & 16 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,36 @@ function Class(type) {
Class.create = function create(type, ctor) {
if (!(type instanceof Type))
throw _TypeError("type", "a Type");
var clazz = ctor;
if (clazz) {
if (typeof clazz !== 'function')
if (ctor) {
if (typeof ctor !== 'function')
throw _TypeError("ctor", "a function");
} else
clazz = (function(MessageCtor) { // eslint-disable-line wrap-iife
ctor = (function(MessageCtor) { // eslint-disable-line wrap-iife
return function Message(properties) {
MessageCtor.call(this, properties);
};
})(Message);

// Let's pretend...
clazz.constructor = Class;
ctor.constructor = Class;

// new Class() -> Message.prototype
var prototype = clazz.prototype = new Message();
prototype.constructor = clazz;
var prototype = ctor.prototype = new Message();
prototype.constructor = ctor;

// Static methods on Message are instance methods on Class and vice versa.
util.merge(clazz, Message, true);
util.merge(ctor, Message, true);

// Classes and messages reference their reflected type
clazz.$type = type;
ctor.$type = type;
prototype.$type = type;

// Messages have non-enumerable default values on their prototype
type.getFieldsArray().forEach(function(field) {
field.resolve();
// objects on the prototype must be immmutable. users must assign a new object instance and
// cannot use Array#push on empty arrays on the prototype for example, as this would modify
// the value on the prototype for ALL messages of this type. Hence, these objects are frozen.
prototype[field.name] = Array.isArray(field.defaultValue)
prototype[field.name] = Array.isArray(field.resolve().defaultValue)
? util.emptyArray
: util.isObject(field.defaultValue)
? util.emptyObject
Expand All @@ -70,23 +68,21 @@ Class.create = function create(type, ctor) {
util.prop(prototype, oneof.resolve().name, {
get: function getVirtual() {
// > If the parser encounters multiple members of the same oneof on the wire, only the last member seen is used in the parsed message.
var keys = Object.keys(this);
for (var i = keys.length - 1; i > -1; --i)
for (var keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
if (oneof.oneof.indexOf(keys[i]) > -1)
return keys[i];
return undefined;
},
set: function setVirtual(value) {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i)
for (var keys = oneof.oneof, i = 0; i < keys.length; ++i)
if (keys[i] !== value)
delete this[keys[i]];
}
});
});

// Register
type.setCtor(clazz);
type.setCtor(ctor);

return prototype;
};
Expand Down
Loading

0 comments on commit 5f07302

Please sign in to comment.