Permalink
Browse files

Skip defining getters and setters on IE8 entirely, automate defining …

…fallbacks
  • Loading branch information...
dcodeIO committed Dec 4, 2016
1 parent 54283d3 commit 95c5538cfaf1daf6b4990f6aa7599779aaacf99f

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +91 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -35,7 +35,7 @@ function Enum(name, values, options) {
this._valuesById = null;
}
Object.defineProperties(EnumPrototype, {
util.props(EnumPrototype, {
/**
* Enum values by id.
@@ -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) {
@@ -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.
@@ -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;
@@ -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]];
@@ -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;
};
@@ -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.
@@ -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));
}
}
@@ -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.
@@ -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;
@@ -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.
@@ -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));
}
}
@@ -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) {
@@ -86,7 +86,7 @@ function Type(name, options) {
this._ctor = null;
}
Object.defineProperties(TypePrototype, {
util.props(TypePrototype, {
/**
* Message fields by id.
@@ -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 = {};
@@ -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);
};
Oops, something went wrong.

0 comments on commit 95c5538

Please sign in to comment.