aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/obscuren/otto/value_number.go
blob: badb3a542759e90b82d8fdb15c676ae1ea97e748 (plain) (blame)
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
package otto

import (
    "fmt"
    "math"
    "regexp"
    "strconv"
    "strings"
)

var stringToNumberParseInteger = regexp.MustCompile(`^(?:0[xX])`)

func stringToFloat(value string) float64 {
    value = strings.TrimSpace(value)

    if value == "" {
        return 0
    }

    parseFloat := false
    if strings.IndexRune(value, '.') != -1 {
        parseFloat = true
    } else if stringToNumberParseInteger.MatchString(value) {
        parseFloat = false
    } else {
        parseFloat = true
    }

    if parseFloat {
        number, err := strconv.ParseFloat(value, 64)
        if err != nil && err.(*strconv.NumError).Err != strconv.ErrRange {
            return math.NaN()
        }
        return number
    }

    number, err := strconv.ParseInt(value, 0, 64)
    if err != nil {
        return math.NaN()
    }
    return float64(number)
}

func toNumber(value Value) Value {
    if value._valueType == valueNumber {
        return value
    }
    return Value{valueNumber, toFloat(value)}
}

func toFloat(value Value) float64 {
    switch value._valueType {
    case valueUndefined:
        return math.NaN()
    case valueNull:
        return 0
    }
    switch value := value.value.(type) {
    case bool:
        if value {
            return 1
        }
        return 0
    case int:
        return float64(value)
    case int8:
        return float64(value)
    case int16:
        return float64(value)
    case int32:
        return float64(value)
    case int64:
        return float64(value)
    case uint:
        return float64(value)
    case uint8:
        return float64(value)
    case uint16:
        return float64(value)
    case uint32:
        return float64(value)
    case uint64:
        return float64(value)
    case float64:
        return value
    case string:
        return stringToFloat(value)
    case *_object:
        return toFloat(value.DefaultValue(defaultValueHintNumber))
    }
    panic(fmt.Errorf("toFloat(%T)", value.value))
}

const (
    float_2_64   float64 = 18446744073709551616.0
    float_2_63   float64 = 9223372036854775808.0
    float_2_32   float64 = 4294967296.0
    float_2_31   float64 = 2147483648.0
    float_2_16   float64 = 65536.0
    integer_2_32 int64   = 4294967296
    integer_2_31 int64   = 2146483648
    sqrt1_2      float64 = math.Sqrt2 / 2
)

const (
    maxInt8   = math.MaxInt8
    minInt8   = math.MinInt8
    maxInt16  = math.MaxInt16
    minInt16  = math.MinInt16
    maxInt32  = math.MaxInt32
    minInt32  = math.MinInt32
    maxInt64  = math.MaxInt64
    minInt64  = math.MinInt64
    maxUint8  = math.MaxUint8
    maxUint16 = math.MaxUint16
    maxUint32 = math.MaxUint32
    maxUint64 = math.MaxUint64
    maxUint   = ^uint(0)
    minUint   = 0
    maxInt    = int(^uint(0) >> 1)
    minInt    = -maxInt - 1

    // int64
    int64_maxInt    int64 = int64(maxInt)
    int64_minInt    int64 = int64(minInt)
    int64_maxInt8   int64 = math.MaxInt8
    int64_minInt8   int64 = math.MinInt8
    int64_maxInt16  int64 = math.MaxInt16
    int64_minInt16  int64 = math.MinInt16
    int64_maxInt32  int64 = math.MaxInt32
    int64_minInt32  int64 = math.MinInt32
    int64_maxUint8  int64 = math.MaxUint8
    int64_maxUint16 int64 = math.MaxUint16
    int64_maxUint32 int64 = math.MaxUint32

    // float64
    float_maxInt    float64 = float64(int(^uint(0) >> 1))
    float_minInt    float64 = float64(int(-maxInt - 1))
    float_minUint   float64 = float64(0)
    float_maxUint   float64 = float64(uint(^uint(0)))
    float_minUint64 float64 = float64(0)
    float_maxUint64 float64 = math.MaxUint64
    float_maxInt64  float64 = math.MaxInt64
    float_minInt64  float64 = math.MinInt64
)

func toIntegerFloat(value Value) float64 {
    float := value.toFloat()
    if math.IsInf(float, 0) {
    } else if math.IsNaN(float) {
        float = 0
    } else if float > 0 {
        float = math.Floor(float)
    } else {
        float = math.Ceil(float)
    }
    return float
}

type _integerKind int

const (
    integerValid    _integerKind = iota // 3.0 => 3.0
    integerFloat                        // 3.14159 => 3.0
    integerInfinite                     // Infinity => 2**63-1
    integerInvalid                      // NaN => 0
)

type _integer struct {
    _integerKind
    value int64
}

func (self _integer) valid() bool {
    return self._integerKind == integerValid || self._integerKind == integerFloat
}

func (self _integer) exact() bool {
    return self._integerKind == integerValid
}

func (self _integer) infinite() bool {
    return self._integerKind == integerInfinite
}

func toInteger(value Value) (integer _integer) {
    switch value := value.value.(type) {
    case int8:
        integer.value = int64(value)
        return
    case int16:
        integer.value = int64(value)
        return
    case uint8:
        integer.value = int64(value)
        return
    case uint16:
        integer.value = int64(value)
        return
    case uint32:
        integer.value = int64(value)
        return
    case int:
        integer.value = int64(value)
        return
    case int64:
        integer.value = value
        return
    }
    {
        value := value.toFloat()
        if value == 0 {
            return
        }
        if math.IsNaN(value) {
            integer._integerKind = integerInvalid
            return
        }
        if math.IsInf(value, 0) {
            integer._integerKind = integerInfinite
        }
        if value >= float_maxInt64 {
            integer.value = math.MaxInt64
            return
        }
        if value <= float_minInt64 {
            integer.value = math.MinInt64
            return
        }
        {
            value0 := value
            value1 := float64(0)
            if value0 > 0 {
                value1 = math.Floor(value0)
            } else {
                value1 = math.Ceil(value0)
            }

            if value0 != value1 {
                integer._integerKind = integerFloat
            }
            integer.value = int64(value1)
            return
        }
    }
}

// ECMA 262: 9.5
func toInt32(value Value) int32 {
    {
        switch value := value.value.(type) {
        case int8:
            return int32(value)
        case int16:
            return int32(value)
        case int32:
            return value
        }
    }
    floatValue := value.toFloat()
    if math.IsNaN(floatValue) || math.IsInf(floatValue, 0) {
        return 0
    }
    if floatValue == 0 { // This will work for +0 & -0
        return 0
    }
    remainder := math.Mod(floatValue, float_2_32)
    if remainder > 0 {
        remainder = math.Floor(remainder)
    } else {
        remainder = math.Ceil(remainder) + float_2_32
    }
    if remainder > float_2_31 {
        return int32(remainder - float_2_32)
    }
    return int32(remainder)
}

func toUint32(value Value) uint32 {
    {
        switch value := value.value.(type) {
        case int8:
            return uint32(value)
        case int16:
            return uint32(value)
        case uint8:
            return uint32(value)
        case uint16:
            return uint32(value)
        case uint32:
            return value
        }
    }
    floatValue := value.toFloat()
    if math.IsNaN(floatValue) || math.IsInf(floatValue, 0) {
        return 0
    }
    if floatValue == 0 {
        return 0
    }
    remainder := math.Mod(floatValue, float_2_32)
    if remainder > 0 {
        remainder = math.Floor(remainder)
    } else {
        remainder = math.Ceil(remainder) + float_2_32
    }
    return uint32(remainder)
}

func toUint16(value Value) uint16 {
    {
        switch value := value.value.(type) {
        case int8:
            return uint16(value)
        case uint8:
            return uint16(value)
        case uint16:
            return value
        }
    }
    floatValue := value.toFloat()
    if math.IsNaN(floatValue) || math.IsInf(floatValue, 0) {
        return 0
    }
    if floatValue == 0 {
        return 0
    }
    remainder := math.Mod(floatValue, float_2_16)
    if remainder > 0 {
        remainder = math.Floor(remainder)
    } else {
        remainder = math.Ceil(remainder) + float_2_16
    }
    return uint16(remainder)
}