diff options
Diffstat (limited to 'js/src/Ice/StreamHelpers.js')
-rw-r--r-- | js/src/Ice/StreamHelpers.js | 195 |
1 files changed, 103 insertions, 92 deletions
diff --git a/js/src/Ice/StreamHelpers.js b/js/src/Ice/StreamHelpers.js index 956dfb6c0e0..0460ade564d 100644 --- a/js/src/Ice/StreamHelpers.js +++ b/js/src/Ice/StreamHelpers.js @@ -7,32 +7,29 @@ // // ********************************************************************** -var Ice = require("../Ice/ModuleRegistry").Ice; -Ice.__M.require(module, ["../Ice/Class", "../Ice/HashMap", "../Ice/OptionalFormat"]); +const Ice = require("../Ice/OptionalFormat").Ice; -var Class = Ice.Class; -var defineProperty = Object.defineProperty; -var HashMap = Ice.HashMap; -var OptionalFormat = Ice.OptionalFormat; +const defineProperty = Object.defineProperty; +const OptionalFormat = Ice.OptionalFormat; -var StreamHelpers = {}; +const StreamHelpers = {}; StreamHelpers.FSizeOptHelper = function() { - this.writeOpt = function(os, tag, v) + this.writeOptional = function(os, tag, v) { - if(v !== undefined && os.writeOpt(tag, OptionalFormat.FSize)) + if(v !== undefined && os.writeOptional(tag, OptionalFormat.FSize)) { - var pos = os.startSize(); + const pos = os.startSize(); this.write(os, v); os.endSize(pos); } }; - this.readOpt = function(is, tag) + this.readOptional = function(is, tag) { - var v; - if(is.readOpt(tag, OptionalFormat.FSize)) + let v; + if(is.readOptional(tag, OptionalFormat.FSize)) { is.skip(4); v = this.read(is); @@ -43,19 +40,19 @@ StreamHelpers.FSizeOptHelper = function() StreamHelpers.VSizeOptHelper = function() { - this.writeOpt = function(os, tag, v) + this.writeOptional = function(os, tag, v) { - if(v !== undefined && os.writeOpt(tag, OptionalFormat.VSize)) + if(v !== undefined && os.writeOptional(tag, OptionalFormat.VSize)) { os.writeSize(this.minWireSize); this.write(os, v); } }; - this.readOpt = function(is, tag) + this.readOptional = function(is, tag) { - var v; - if(is.readOpt(tag, OptionalFormat.VSize)) + let v; + if(is.readOptional(tag, OptionalFormat.VSize)) { is.skipSize(); v = this.read(is); @@ -66,20 +63,20 @@ StreamHelpers.VSizeOptHelper = function() StreamHelpers.VSizeContainerOptHelper = function(elementSize) { - this.writeOpt = function(os, tag, v) + this.writeOptional = function(os, tag, v) { - if(v !== undefined && os.writeOpt(tag, OptionalFormat.VSize)) + if(v !== undefined && os.writeOptional(tag, OptionalFormat.VSize)) { - var sz = this.size(v); + const sz = this.size(v); os.writeSize(sz > 254 ? sz * elementSize + 5 : sz * elementSize + 1); this.write(os, v); } }; - this.readOpt = function(is, tag) + this.readOptional = function(is, tag) { - var v; - if(is.readOpt(tag, OptionalFormat.VSize)) + let v; + if(is.readOptional(tag, OptionalFormat.VSize)) { is.skipSize(); v = this.read(is); @@ -90,18 +87,18 @@ StreamHelpers.VSizeContainerOptHelper = function(elementSize) StreamHelpers.VSizeContainer1OptHelper = function() { - this.writeOpt = function(os, tag, v) + this.writeOptional = function(os, tag, v) { - if(v !== undefined && os.writeOpt(tag, OptionalFormat.VSize)) + if(v !== undefined && os.writeOptional(tag, OptionalFormat.VSize)) { this.write(os, v); } }; - this.readOpt = function(is, tag) + this.readOptional = function(is, tag) { - var v; - if(is.readOpt(tag, OptionalFormat.VSize)) + let v; + if(is.readOptional(tag, OptionalFormat.VSize)) { v = this.read(is); } @@ -112,8 +109,9 @@ StreamHelpers.VSizeContainer1OptHelper = function() // // Sequence helper to write sequences // -var SequenceHelper = Class({ - write: function(os, v) +class SequenceHelper +{ + write(os, v) { if(v === null || v.length === 0) { @@ -121,38 +119,43 @@ var SequenceHelper = Class({ } else { - var helper = this.elementHelper; + const helper = this.elementHelper; os.writeSize(v.length); - for(var i = 0; i < v.length; ++i) + for(let i = 0; i < v.length; ++i) { helper.write(os, v[i]); } } - }, - read: function(is) + } + + read(is) { - var helper = this.elementHelper; // Cache the element helper. - var sz = is.readAndCheckSeqSize(helper.minWireSize); - var v = []; + const helper = this.elementHelper; // Cache the element helper. + const sz = is.readAndCheckSeqSize(helper.minWireSize); + const v = []; v.length = sz; - for(var i = 0; i < sz; ++i) + for(let i = 0; i < sz; ++i) { v[i] = helper.read(is); } return v; - }, - size: function(v) + } + + size(v) { return (v === null || v === undefined) ? 0 : v.length; } -}); + + get minWireSize() + { + return 1; + } +} + -defineProperty(SequenceHelper.prototype, "minWireSize", { - get: function(){ return 1; } -}); // Speacialization optimized for ByteSeq -var byteSeqHelper = new SequenceHelper(); +const byteSeqHelper = new SequenceHelper(); byteSeqHelper.write = function(os, v) { return os.writeByteSeq(v); }; byteSeqHelper.read = function(is) { return is.readByteSeq(); }; defineProperty(byteSeqHelper, "elementHelper", { @@ -160,21 +163,21 @@ defineProperty(byteSeqHelper, "elementHelper", { }); StreamHelpers.VSizeContainer1OptHelper.call(byteSeqHelper); -// Read method for object sequences -var objectSequenceHelperRead = function(is) +// Read method for value sequences +const valueSequenceHelperRead = function(is) { - var sz = is.readAndCheckSeqSize(1); - var v = []; + const sz = is.readAndCheckSeqSize(1); + const v = []; v.length = sz; - var elementType = this.elementType; - var readObjectAtIndex = function(idx) + const elementType = this.elementType; + const readValueAtIndex = function(idx) { - is.readObject(function(obj) { v[idx] = obj; }, elementType); + is.readValue(obj => v[idx] = obj, elementType); }; - for(var i = 0; i < sz; ++i) + for(let i = 0; i < sz; ++i) { - readObjectAtIndex(i); + readValueAtIndex(i); } return v; }; @@ -186,7 +189,7 @@ StreamHelpers.generateSeqHelper = function(elementHelper, fixed, elementType) return byteSeqHelper; } - var helper = new SequenceHelper(); + const helper = new SequenceHelper(); if(fixed) { if(elementHelper.minWireSize === 1) @@ -212,7 +215,7 @@ StreamHelpers.generateSeqHelper = function(elementHelper, fixed, elementType) defineProperty(helper, "elementType", { get: function(){ return elementType; } }); - helper.read = objectSequenceHelperRead; + helper.read = valueSequenceHelperRead; } return helper; @@ -221,8 +224,9 @@ StreamHelpers.generateSeqHelper = function(elementHelper, fixed, elementType) // // Dictionary helper to write dictionaries // -var DictionaryHelper = Class({ - write: function(os, v) +class DictionaryHelper +{ + write(os, v) { if(v === null || v.size === 0) { @@ -230,63 +234,66 @@ var DictionaryHelper = Class({ } else { - var keyHelper = this.keyHelper; - var valueHelper = this.valueHelper; + const keyHelper = this.keyHelper; + const valueHelper = this.valueHelper; os.writeSize(v.size); - for(var e = v.entries; e !== null; e = e.next) + for(let [key, value] of v) { - keyHelper.write(os, e.key); - valueHelper.write(os, e.value); + keyHelper.write(os, key); + valueHelper.write(os, value); } } - }, - read: function(is) + } + + read(is) { - var mapType = this.mapType; - var v = new mapType(); - var sz = is.readSize(); - var keyHelper = this.keyHelper; - var valueHelper = this.valueHelper; - for(var i = 0; i < sz; ++i) + const mapType = this.mapType; + const v = new mapType(); + const sz = is.readSize(); + const keyHelper = this.keyHelper; + const valueHelper = this.valueHelper; + for(let i = 0; i < sz; ++i) { v.set(keyHelper.read(is), valueHelper.read(is)); } return v; - }, - size: function(v) + } + + size(v) { return (v === null || v === undefined) ? 0 : v.size; } -}); - -Object.defineProperty(DictionaryHelper.prototype, "minWireSize", { - get: function(){ return 1; } -}); + + get minWireSize() + { + return 1; + } +} -// Read method for dictionaries of objects -var objectDictionaryHelperRead = function(is) +// Read method for dictionaries of values +const valueDictionaryHelperRead = function(is) { - var sz = is.readSize(); - var mapType = this.mapType; - var v = new mapType(); - var valueType = this.valueType; + const sz = is.readSize(); + const mapType = this.mapType; + const v = new mapType(); + const valueType = this.valueType; - var readObjectForKey = function(key) + const readValueForKey = function(key) { - is.readObject(function(obj) { v.set(key, obj); }, valueType); + is.readValue(function(obj) { v.set(key, obj); }, valueType); }; - var keyHelper = this.keyHelper; - for(var i = 0; i < sz; ++i) + const keyHelper = this.keyHelper; + for(let i = 0; i < sz; ++i) { - readObjectForKey(keyHelper.read(is)); + readValueForKey(keyHelper.read(is)); } return v; }; StreamHelpers.generateDictHelper = function(keyHelper, valueHelper, fixed, valueType, mapType) { - var helper = new DictionaryHelper(); + const helper = new DictionaryHelper(); if(fixed) { StreamHelpers.VSizeContainerOptHelper.call(helper, keyHelper.minWireSize + valueHelper.minWireSize); @@ -295,12 +302,15 @@ StreamHelpers.generateDictHelper = function(keyHelper, valueHelper, fixed, value { StreamHelpers.FSizeOptHelper.call(helper); } + defineProperty(helper, "mapType", { get: function(){ return mapType; } }); + defineProperty(helper, "keyHelper", { get: function(){ return keyHelper; } }); + defineProperty(helper, "valueHelper", { get: function(){ return valueHelper; } }); @@ -310,7 +320,8 @@ StreamHelpers.generateDictHelper = function(keyHelper, valueHelper, fixed, value defineProperty(helper, "valueType", { get: function(){ return valueType; } }); - helper.read = objectDictionaryHelperRead; + + helper.read = valueDictionaryHelperRead; } return helper; |