UInt: Add UInt#_update for creating subclasses that have dependent state.

This commit is contained in:
Stefan Thomas
2014-01-25 11:30:58 -08:00
parent 0d4357232c
commit fa07601a2a
2 changed files with 36 additions and 0 deletions

View File

@@ -15,6 +15,8 @@ var Base = require('./base').Base;
var UInt = function () { var UInt = function () {
// Internal form: NaN or BigInteger // Internal form: NaN or BigInteger
this._value = NaN; this._value = NaN;
this._update();
}; };
UInt.json_rewrite = function (j, opts) { UInt.json_rewrite = function (j, opts) {
@@ -96,6 +98,8 @@ UInt.prototype.clone = function () {
UInt.prototype.copyTo = function (d) { UInt.prototype.copyTo = function (d) {
d._value = this._value; d._value = this._value;
if ("function" === typeof d._update) d._update();
return d; return d;
}; };
@@ -111,6 +115,20 @@ UInt.prototype.is_zero = function () {
return this._value.equals(BigInteger.ZERO); return this._value.equals(BigInteger.ZERO);
}; };
/**
* Update any derivative values.
*
* This allows subclasses to maintain caches of any data that they derive from
* the main _value. For example, the Currency class keeps the currency type, the
* currency code and other information about the currency cached.
*
* The reason for keeping this mechanism in this class is so every subclass can
* call it whenever it modifies the internal state.
*/
UInt.prototype._update = function () {
// Nothing to do by default. Subclasses will override this.
};
// value = NaN on error. // value = NaN on error.
UInt.prototype.parse_generic = function (j) { UInt.prototype.parse_generic = function (j) {
// Canonicalize and validate // Canonicalize and validate
@@ -150,6 +168,8 @@ UInt.prototype.parse_generic = function (j) {
} }
} }
this._update();
return this; return this;
}; };
@@ -161,6 +181,8 @@ UInt.prototype.parse_hex = function (j) {
this._value = NaN; this._value = NaN;
} }
this._update();
return this; return this;
}; };
@@ -172,6 +194,8 @@ UInt.prototype.parse_bits = function (j) {
this.parse_bytes(bytes); this.parse_bytes(bytes);
} }
this._update();
return this; return this;
}; };
@@ -183,6 +207,8 @@ UInt.prototype.parse_bytes = function (j) {
this._value = new BigInteger([0].concat(j), 256); this._value = new BigInteger([0].concat(j), 256);
} }
this._update();
return this; return this;
}; };
@@ -198,6 +224,8 @@ UInt.prototype.parse_bn = function (j) {
this._value = NaN; this._value = NaN;
} }
this._update();
return this; return this;
}; };
@@ -211,6 +239,8 @@ UInt.prototype.parse_number = function (j) {
this._value = new BigInteger(""+j); this._value = new BigInteger(""+j);
} }
this._update();
return this; return this;
}; };

View File

@@ -16,6 +16,8 @@ var UInt160 = extend(function () {
// Internal form: NaN or BigInteger // Internal form: NaN or BigInteger
this._value = NaN; this._value = NaN;
this._version_byte = void(0); this._version_byte = void(0);
this._update();
}, UInt); }, UInt);
UInt160.width = 20; UInt160.width = 20;
@@ -61,6 +63,8 @@ UInt160.prototype.parse_json = function (j) {
this.parse_hex(j); this.parse_hex(j);
} }
this._update();
return this; return this;
}; };
@@ -73,6 +77,8 @@ UInt160.prototype.parse_generic = function (j) {
} }
} }
this._update();
return this; return this;
}; };