Skip to content

Commit

Permalink
Skip defining getters and setters on IE8 entirely, automate defining …
Browse files Browse the repository at this point in the history
…fallbacks
  • Loading branch information
dcodeIO committed Dec 4, 2016
1 parent 54283d3 commit 95c5538
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 49 deletions.
77 changes: 57 additions & 20 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.

4 changes: 2 additions & 2 deletions src/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Enum(name, values, options) {
this._valuesById = null;
}

Object.defineProperties(EnumPrototype, {
util.props(EnumPrototype, {

/**
* Enum values by id.
Expand All @@ -44,7 +44,7 @@ Object.defineProperties(EnumPrototype, {
* @readonly
*/
valuesById: {
get: EnumPrototype.getValuesById = function getValuesById() {
get: function getValuesById() {
if (!this._valuesById) {
this._valuesById = {};
Object.keys(this.values).forEach(function(name) {
Expand Down
2 changes: 1 addition & 1 deletion src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function Field(name, id, type, rule, extend, options) {
this._packed = null;
}

Object.defineProperties(FieldPrototype, {
util.props(FieldPrototype, {

/**
* Determines whether this field is packed. Only relevant when repeated and working with proto2.
Expand Down
13 changes: 6 additions & 7 deletions src/inherits.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function inherits(clazz, type, options) {

}, true);

Object.defineProperties(clazz, classProperties);
util.props(clazz, classProperties);
var prototype = inherits.defineProperties(new Prototype(), type);
clazz.prototype = prototype;
prototype.constructor = clazz;
Expand Down Expand Up @@ -167,9 +167,8 @@ inherits.defineProperties = function defineProperties(prototype, type) {

// Define each oneof with a non-enumerable getter and setter for the present field
type.getOneofsArray().forEach(function(oneof) {
oneof.resolve();
prototypeProperties[oneof.name] = {
get: prototype['get' + oneof.ucName] = function() {
util.prop(prototype, oneof.resolve().name, {
get: function getVirtual() {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i) {
var field = oneof.parent.fields[keys[i]];
Expand All @@ -178,16 +177,16 @@ inherits.defineProperties = function defineProperties(prototype, type) {
}
return undefined;
},
set: prototype['set' + oneof.ucName] = function(value) {
set: function setVirtual(value) {
var keys = oneof.oneof;
for (var i = 0; i < keys.length; ++i) {
if (keys[i] !== value)
delete this[keys[i]];
}
}
};
});
});

Object.defineProperties(prototype, prototypeProperties);
util.props(prototype, prototypeProperties);
return prototype;
};
4 changes: 2 additions & 2 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function clearCache(namespace) {
return namespace;
}

Object.defineProperties(NamespacePrototype, {
util.props(NamespacePrototype, {

/**
* Nested objects of this namespace as an array for iteration.
Expand All @@ -55,7 +55,7 @@ Object.defineProperties(NamespacePrototype, {
* @readonly
*/
nestedArray: {
get: NamespacePrototype.getNestedArray = function getNestedArray() {
get: function getNestedArray() {
return this._nestedArray || (this._nestedArray = util.toArray(this.nested));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function ReflectionObject(name, options) {
/** @alias ReflectionObject.prototype */
var ReflectionObjectPrototype = ReflectionObject.prototype;

Object.defineProperties(ReflectionObjectPrototype, {
util.props(ReflectionObjectPrototype, {

/**
* Reference to the root namespace.
Expand All @@ -59,7 +59,7 @@ Object.defineProperties(ReflectionObjectPrototype, {
* @readonly
*/
root: {
get: ReflectionObjectPrototype.getRoot = function getRoot() {
get: function getRoot() {
var ptr = this;
while (ptr.parent !== null)
ptr = ptr.parent;
Expand Down
6 changes: 3 additions & 3 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Service(name, options) {
this._methodsArray = null;
}

Object.defineProperties(ServicePrototype, {
util.props(ServicePrototype, {

/**
* Methods of this service as an array for iteration.
Expand All @@ -45,7 +45,7 @@ Object.defineProperties(ServicePrototype, {
* @readonly
*/
methodsArray: {
get: ServicePrototype.getMethodsArray = function getMethodsArray() {
get: function getMethodsArray() {
return this._methodsArray || (this._methodsArray = util.toArray(this.methods));
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ ServicePrototype.remove = function remove(object) {
*/
ServicePrototype.create = function create(rpc, requestDelimited, responseDelimited) {
var rpcService = {};
Object.defineProperty(rpcService, "$rpc", {
util.prop(rpcService, "$rpc", {
value: rpc
});
this.getMethodsArray().forEach(function(method) {
Expand Down
4 changes: 2 additions & 2 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function Type(name, options) {
this._ctor = null;
}

Object.defineProperties(TypePrototype, {
util.props(TypePrototype, {

/**
* Message fields by id.
Expand All @@ -95,7 +95,7 @@ Object.defineProperties(TypePrototype, {
* @readonly
*/
fieldsById: {
get: TypePrototype.getFieldsById = function getFieldsById() {
get: function getFieldsById() {
if (this._fieldsById)
return this._fieldsById;
this._fieldsById = {};
Expand Down
38 changes: 38 additions & 0 deletions src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,41 @@ util.longNeq = function longNeq(a, b) {
? (b = LongBits.fromNumber(b)).lo !== a.low || b.hi !== a.high
: a.low !== b.low || a.high !== b.high;
};

/**
* Defines the specified properties on the specified target. Also adds getters and setters for non-ES5 environments.
* @param {Object} target Target object
* @param {Object} descriptors Property descriptors
* @returns {undefined}
*/
util.props = function props(target, descriptors) {
Object.keys(descriptors).forEach(function(key) {
util.prop(target, key, descriptors[key]);
});
};

/**
* Defines the specified property on the specified target. Also adds getters and setters for non-ES5 environments.
* @param {Object} target Target object
* @param {string} key Property name
* @param {Object} descriptor Property descriptor
* @returns {undefined}
*/
util.prop = function prop(target, key, descriptor) {
var ie8 = !-[1,];
var ucKey = key.substring(0, 1).toUpperCase() + key.substring(1);
if (descriptor.get)
target['get' + ucKey] = descriptor.get;
if (descriptor.set)
target['set' + ucKey] = ie8
? function(value) {
descriptor.set.call(this, value);
this[key] = value;
}
: descriptor.set;
if (ie8) {
if (descriptor.value !== undefined)
target[key] = descriptor.value;
} else
Object.defineProperty(target, key, descriptor);
};
Loading

0 comments on commit 95c5538

Please sign in to comment.