summaryrefslogtreecommitdiff
path: root/js/src/Ice/Long.js
blob: 1445b4098e9c282fe443609aea091f4cb9eaf3d8 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

(function(global){
    require("Ice/Class");

    //
    // The Long type represents a signed 64-bit integer as two 32-bit values
    // corresponding to the high and low words.
    //
    var Ice = global.Ice || {};

    var Long = Ice.Class({
        __init__: function(high, low)
        {
            this.high = high;
            this.low = low;
        },
        hashCode: function()
        {
            return this.low;
        },
        equals: function(rhs)
        {
            if(this === rhs)
            {
                return true;
            }
            if(!(rhs instanceof Long))
            {
                return false;
            }
            return this.high === rhs.high && this.low === rhs.low;
        },
        toString: function()
        {
            return this.high + ":" + this.low;
        },
        toNumber: function()
        {
            if((this.high & Long.SIGN_MASK) != 0)
            {
                var low = ~this.low;
                var high = ~this.high;
                if(low < 0xFFFFFFFF)
                {
                    low += 1;
                }
                else
                {
                    low = 0;
                    high += 1;
                    if(high > Long.HIGH_MAX)
                    {
                        return Number.NEGATIVE_INFINITY;
                    }
                }
                return -1 * (high * Long.HIGH_MASK) + low;
            }
            else
            {
                if(this.high > Long.HIGH_MAX)
                {
                    return Number.POSITIVE_INFINITY;
                }
                return (this.high * Long.HIGH_MASK) + this.low;
            }
        }
    });

    //
    // (high & SIGN_MASK) != 0 denotes a negative number;
    // that is, the most significant bit is set.
    //
    Long.SIGN_MASK = 0x80000000;

    //
    // When converting to a JavaScript Number we left shift the
    // high word by 32 bits. As that isn't possible using JavaScript's
    // left shift operator, we multiply the value by 2^32 which will
    // produce the same result.
    //
    Long.HIGH_MASK = 0x100000000;

    //
    // The maximum value for the high word when coverting to
    // a JavaScript Number is 2^21 - 1, in which case all
    // 53 bits are used.
    //
    Long.HIGH_MAX = 0x1FFFFF;

    Ice.Long = Long;
    global.Ice = Ice;
}(typeof (global) === "undefined" ? window : global));