All files / src/Ice Struct.js

90.59% Statements 106/117
80% Branches 32/40
100% Functions 7/7
90.59% Lines 106/117

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 11841x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 17407x 17407x 878x 878x 16529x 17407x     16529x 17407x     16529x 17407x 50385x 50385x 50385x 18049x 50379x 10511x 10511x 50385x 6018x 17407x 41x 172x 172x 172x 860x 860x   860x 516x 860x   344x   344x 344x 344x 860x 172x 172x 41x 4046x 4046x   4046x   4046x 4046x 4046x 2944x 4038x 1102x 1102x     4046x 4046x 41x 2023x 2023x 2023x 10115x 10115x 6069x 6069x 4046x 4046x 2023x 2023x 41x 41x 1360x 1360x 1360x 1360x 1360x 1360x 1360x 1360x 400x 400x 1360x 1360x 1360x 3617x 1360x 1360x 1360x 3277x 3212x 3212x 3277x 3277x 1360x 1360x 1360x 1221x 1360x 139x 139x 1360x 1360x 1360x  
// Copyright (c) ZeroC, Inc.
 
import { ArrayUtil } from "./ArrayUtil.js";
import { HashUtil } from "./HashUtil.js";
import { StreamHelpers } from "./StreamHelpers.js";
 
//
// Use generic equality test from ArrayUtil.
//
const eq = ArrayUtil.eq;
 
function equals(other) {
    if (this === other) {
        return true;
    }
 
    if (other === null || other === undefined) {
        return false;
    }
 
    if (this.prototype !== other.prototype) {
        return false;
    }
 
    for (const key in this) {
        const e1 = this[key];
        const e2 = other[key];
        if (typeof e1 == "function") {
            continue; // Don't need to compare functions
        } else if (!eq(e1, e2)) {
            return false;
        }
    }
    return true;
}
 
function clone() {
    const other = new this.constructor();
    for (const key in this) {
        const e = this[key];
        if (e === undefined || e === null) {
            other[key] = e;
        } else if (typeof e == "function") {
            continue;
        } else if (typeof e.clone == "function") {
            other[key] = e.clone();
        } else if (e instanceof Array) {
            other[key] = ArrayUtil.clone(e);
        } else {
            other[key] = e;
        }
    }
    return other;
}
 
function memberHashCode(h, e) {
    if (typeof e.hashCode == "function") {
        return HashUtil.addHashable(h, e);
    } else if (e instanceof Array) {
        return HashUtil.addArray(h, e, memberHashCode);
    } else {
        const t = typeof e;
        if (e instanceof String || t == "string") {
            return HashUtil.addString(h, e);
        } else if (e instanceof Number || t == "number") {
            return HashUtil.addNumber(h, e);
        } else if (e instanceof Boolean || t == "boolean") {
            return HashUtil.addBoolean(h, e);
        }
    }
}
 
function hashCode() {
    let h = 5381;
    for (const key in this) {
        const e = this[key];
        if (e === undefined || e === null || typeof e == "function") {
            continue;
        }
        h = memberHashCode(h, e);
    }
    return h;
}
 
export function defineStruct(obj, legalKeyType, variableLength) {
    obj.prototype.clone = clone;
 
    obj.prototype.equals = equals;
 
    //
    // Only generate hashCode if this structure type is a legal dictionary key type.
    //
    if (legalKeyType) {
        obj.prototype.hashCode = hashCode;
    }
 
    if (obj.prototype._write && obj.prototype._read) {
        obj.write = function (os, v) {
            v._write(os);
        };
 
        obj.read = function (is, v) {
            if (!v || !(v instanceof this)) {
                v = new this();
            }
            v._read(is);
            return v;
        };
 
        if (variableLength) {
            StreamHelpers.FSizeOptHelper.call(obj);
        } else {
            StreamHelpers.VSizeOptHelper.call(obj);
        }
    }
    return obj;
}