All files / src/Ice StreamHelpers.js

97.98% Statements 389/397
98.11% Branches 104/106
93.75% Functions 45/48
97.98% Lines 389/397

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 39841x 41x 41x 41x 41x 41x 41x 41x 41x 41x 328x 328x 328x 25347x 25347x 328x 328x 24165x 24165x 328x 328x 325x 325x 328x 328x 299x 299x 328x 328x 5147x 5147x 328x 328x 328x 205x 104x 205x 205x 328x 328x 328x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 17x 17x 17x 17x 9x 17x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 17x 17x 17x 17x 9x 17x 41x 41x 41x 41x 41x 13x 41x 41x 41x 41x 41x 678x 678x 41x 41x             41x 41x     41x 41x 41x 41x 41x 2954x 222x 91x 91x 91x 91x 2954x 2954x 2954x 204x 204x 83x 83x 83x 204x 2954x 41x 41x 41x 139x 63x 29x 29x 29x 139x 139x 139x 54x 54x 25x 25x 25x 54x 139x 41x 41x 41x 263x 165x 68x 68x 68x 68x 263x 263x 263x 144x 144x 61x 61x 61x 144x 263x 41x 41x 41x 94x 90x 30x 30x 94x 94x 94x 81x 81x 27x 27x 81x 94x 41x 41x 41x 41x 41x 41x 41x 4875x 3640x 4871x 1235x 1235x 1235x 26062x 26062x 1235x 4875x 41x 41x 4890x 4890x 4890x 4890x 4890x 24194x 24194x 4890x 4890x 41x 41x 48x 48x 41x 41x 128x 128x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 30x 30x 30x 30x 30x 52x 44x 52x 30x 30x 30x 52x 52x 30x 41x 41x 41x 1559x 52x 52x 1507x 1507x 1559x 298x 53x 298x 245x 245x 1559x 1209x 1209x 1507x 1507x 1507x 1559x 174x 174x 174x 174x 174x 1507x 1507x 41x 41x 41x 41x 41x 41x 41x 4050x 3535x 4050x 515x 515x 515x 515x 1967x 1967x 1967x 515x 4050x 41x 41x 453x 453x 453x 453x 453x 453x 1746x 1746x 453x 453x 41x 41x 20x 20x 41x 41x 84x 84x 41x 41x 41x 23x 23x 23x 23x 23x 23x 23x 42x 23x 23x 23x 23x 42x 42x 23x 23x 41x 41x 542x 542x 18x 542x 524x 524x 542x 542x 542x 542x 542x 542x 542x 542x 542x 542x 542x 542x 542x 542x 13x 13x 13x 13x 13x 13x 542x 542x 41x  
// Copyright (c) ZeroC, Inc.
 
import { OptionalFormat } from "./OptionalFormat.js";
import { InputStream } from "./InputStream.js";
import { OutputStream } from "./OutputStream.js";
import { TypeRegistry } from "./TypeRegistry.js";
import { Value } from "./Value.js";
 
const defineProperty = Object.defineProperty;
 
function defineBuiltinHelper(write, read, sz, format, min, max) {
    const helper = class {
        static write(os, v) {
            return write.call(os, v);
        }
 
        static read(is) {
            return read.call(is);
        }
 
        static writeOptional(os, tag, v) {
            os.writeOptionalHelper(tag, format, write, v);
        }
 
        static readOptional(is, tag) {
            return is.readOptionalHelper(tag, format, read);
        }
 
        static get minWireSize() {
            return sz;
        }
    };
 
    if (min !== undefined && max !== undefined) {
        helper.validate = function (v) {
            return v >= min && v <= max;
        };
    }
 
    return helper;
}
 
const istr = InputStream.prototype;
const ostr = OutputStream.prototype;
 
//
// Constants to use in number type range checks.
//
const MIN_UINT8_VALUE = 0x0;
const MAX_UINT8_VALUE = 0xff;
 
const MIN_INT16_VALUE = -0x8000;
const MAX_INT16_VALUE = 0x7fff;
 
const MIN_INT32_VALUE = -0x80000000;
const MAX_INT32_VALUE = 0x7fffffff;
 
const MIN_FLOAT32_VALUE = -3.4028234664e38;
const MAX_FLOAT32_VALUE = 3.4028234664e38;
 
export const ByteHelper = defineBuiltinHelper(
    ostr.writeByte,
    istr.readByte,
    1,
    OptionalFormat.F1,
    MIN_UINT8_VALUE,
    MAX_UINT8_VALUE,
);
 
export const ShortHelper = defineBuiltinHelper(
    ostr.writeShort,
    istr.readShort,
    2,
    OptionalFormat.F2,
    MIN_INT16_VALUE,
    MAX_INT16_VALUE,
);
 
export const IntHelper = defineBuiltinHelper(
    ostr.writeInt,
    istr.readInt,
    4,
    OptionalFormat.F4,
    MIN_INT32_VALUE,
    MAX_INT32_VALUE,
);
 
export const FloatHelper = defineBuiltinHelper(
    ostr.writeFloat,
    istr.readFloat,
    4,
    OptionalFormat.F4,
    MIN_FLOAT32_VALUE,
    MAX_FLOAT32_VALUE,
);
FloatHelper.validate = function (v) {
    return (
        Number.isNaN(v) ||
        v == Number.POSITIVE_INFINITY ||
        v == Number.NEGATIVE_INFINITY ||
        (v >= MIN_FLOAT32_VALUE && v <= MAX_FLOAT32_VALUE)
    );
};
 
export const DoubleHelper = defineBuiltinHelper(
    ostr.writeDouble,
    istr.readDouble,
    8,
    OptionalFormat.F8,
    -Number.MAX_VALUE,
    Number.MAX_VALUE,
);
DoubleHelper.validate = function (v) {
    return (
        Number.isNaN(v) ||
        v == Number.POSITIVE_INFINITY ||
        v == Number.NEGATIVE_INFINITY ||
        (v >= -Number.MAX_VALUE && v <= Number.MAX_VALUE)
    );
};
 
export const BoolHelper = defineBuiltinHelper(ostr.writeBool, istr.readBool, 1, OptionalFormat.F1);
export const LongHelper = defineBuiltinHelper(ostr.writeLong, istr.readLong, 8, OptionalFormat.F8);
LongHelper.validate = function (v) {
    return typeof v == "number" || (typeof v == "bigint" && BigInt.asIntN(64, v) === v);
};
 
export const StringHelper = defineBuiltinHelper(ostr.writeString, istr.readString, 1, OptionalFormat.VSize);
export const ObjectHelper = class {
    static write(os, v) {
        os.writeValue(v);
    }
 
    static read(is) {
        let o;
        is.readValue(v => {
            o = v;
        }, Value);
        return o;
    }
 
    static get minWireSize() {
        return 1;
    }
};
 
export const StreamHelpers = {};
 
StreamHelpers.FSizeOptHelper = function () {
    this.writeOptional = function (os, tag, v) {
        if (v !== undefined && v !== null && os.writeOptional(tag, OptionalFormat.FSize)) {
            const pos = os.startSize();
            this.write(os, v);
            os.endSize(pos);
        }
    };
 
    this.readOptional = function (is, tag) {
        let v;
        if (is.readOptional(tag, OptionalFormat.FSize)) {
            is.skip(4);
            v = this.read(is);
        }
        return v;
    };
};
 
StreamHelpers.VSizeOptHelper = function () {
    this.writeOptional = function (os, tag, v) {
        if (v !== undefined && v !== null && os.writeOptional(tag, OptionalFormat.VSize)) {
            os.writeSize(this.minWireSize);
            this.write(os, v);
        }
    };
 
    this.readOptional = function (is, tag) {
        let v;
        if (is.readOptional(tag, OptionalFormat.VSize)) {
            is.skipSize();
            v = this.read(is);
        }
        return v;
    };
};
 
StreamHelpers.VSizeContainerOptHelper = function (elementSize) {
    this.writeOptional = function (os, tag, v) {
        if (v !== undefined && v !== null && os.writeOptional(tag, OptionalFormat.VSize)) {
            const sz = this.size(v);
            os.writeSize(sz > 254 ? sz * elementSize + 5 : sz * elementSize + 1);
            this.write(os, v);
        }
    };
 
    this.readOptional = function (is, tag) {
        let v;
        if (is.readOptional(tag, OptionalFormat.VSize)) {
            is.skipSize();
            v = this.read(is);
        }
        return v;
    };
};
 
StreamHelpers.VSizeContainer1OptHelper = function () {
    this.writeOptional = function (os, tag, v) {
        if (v !== undefined && v !== null && os.writeOptional(tag, OptionalFormat.VSize)) {
            this.write(os, v);
        }
    };
 
    this.readOptional = function (is, tag) {
        let v;
        if (is.readOptional(tag, OptionalFormat.VSize)) {
            v = this.read(is);
        }
        return v;
    };
};
 
//
// Sequence helper to write sequences
//
class SequenceHelper {
    write(os, v) {
        if (v === null || v === undefined || v.length === 0) {
            os.writeSize(0);
        } else {
            const helper = this.elementHelper;
            os.writeSize(v.length);
            for (let i = 0; i < v.length; ++i) {
                helper.write(os, v[i]);
            }
        }
    }
 
    read(is) {
        const helper = this.elementHelper; // Cache the element helper.
        const sz = is.readAndCheckSeqSize(helper.minWireSize);
        const v = [];
        v.length = sz;
        for (let i = 0; i < sz; ++i) {
            v[i] = helper.read(is);
        }
        return v;
    }
 
    size(v) {
        return v === null || v === undefined ? 0 : v.length;
    }
 
    get minWireSize() {
        return 1;
    }
}
 
// Specialization optimized for ByteSeq
const byteSeqHelper = new SequenceHelper();
byteSeqHelper.write = (os, v) => os.writeByteSeq(v);
byteSeqHelper.read = is => is.readByteSeq();
 
defineProperty(byteSeqHelper, "elementHelper", { get: () => ByteHelper });
StreamHelpers.VSizeContainer1OptHelper.call(byteSeqHelper);
 
// Read method for value sequences
const valueSequenceHelperRead = function (is) {
    const sz = is.readAndCheckSeqSize(1);
    const v = [];
    v.length = sz;
    const elementType = this.elementType;
    const readValueAtIndex = function (idx) {
        is.readValue(obj => {
            v[idx] = obj;
        }, elementType);
    };
 
    for (let i = 0; i < sz; ++i) {
        readValueAtIndex(i);
    }
    return v;
};
 
StreamHelpers.generateSeqHelper = function (elementHelper, fixed, elementType) {
    if (elementHelper === ByteHelper) {
        return byteSeqHelper;
    }
 
    const helper = new SequenceHelper();
    if (fixed) {
        if (elementHelper.minWireSize === 1) {
            StreamHelpers.VSizeContainer1OptHelper.call(helper);
        } else {
            StreamHelpers.VSizeContainerOptHelper.call(helper, elementHelper.minWireSize);
        }
    } else {
        StreamHelpers.FSizeOptHelper.call(helper);
    }
 
    defineProperty(helper, "elementHelper", { get: () => elementHelper });
 
    if (elementHelper == ObjectHelper) {
        defineProperty(helper, "elementType", {
            get: () => TypeRegistry.getValueType(elementType),
        });
        helper.read = valueSequenceHelperRead;
    }
 
    return helper;
};
 
//
// Dictionary helper to write dictionaries
//
class DictionaryHelper {
    write(os, v) {
        if (v === null || v == undefined || v.size === 0) {
            os.writeSize(0);
        } else {
            const keyHelper = this.keyHelper;
            const valueHelper = this.valueHelper;
            os.writeSize(v.size);
            for (const [key, value] of v) {
                keyHelper.write(os, key);
                valueHelper.write(os, value);
            }
        }
    }
 
    read(is) {
        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(v) {
        return v === null || v === undefined ? 0 : v.size;
    }
 
    get minWireSize() {
        return 1;
    }
}
 
// Read method for dictionaries of values
function valueDictionaryHelperRead(is) {
    const sz = is.readSize();
    const mapType = this.mapType;
    const v = new mapType();
    const valueType = this.valueType;
 
    const readValueForKey = function (key) {
        is.readValue(obj => v.set(key, obj), valueType);
    };
 
    const keyHelper = this.keyHelper;
    for (let i = 0; i < sz; ++i) {
        readValueForKey(keyHelper.read(is));
    }
    return v;
}
 
StreamHelpers.generateDictHelper = function (keyHelper, valueHelper, fixed, valueType, mapType) {
    const helper = new DictionaryHelper();
    if (fixed) {
        StreamHelpers.VSizeContainerOptHelper.call(helper, keyHelper.minWireSize + valueHelper.minWireSize);
    } else {
        StreamHelpers.FSizeOptHelper.call(helper);
    }
 
    defineProperty(helper, "mapType", {
        get: () => mapType,
    });
 
    defineProperty(helper, "keyHelper", {
        get: () => keyHelper,
    });
 
    defineProperty(helper, "valueHelper", {
        get: () => valueHelper,
    });
 
    if (valueHelper == ObjectHelper) {
        defineProperty(helper, "valueType", {
            get: () => TypeRegistry.getValueType(valueType),
        });
 
        helper.read = valueDictionaryHelperRead;
    }
 
    return helper;
};