aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/robertkrimen/otto/otto_.go
blob: 304a831508ba275d191ee91e3f8943cf844e0d72 (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
package otto

import (
    "fmt"
    "regexp"
    runtime_ "runtime"
    "strconv"
    "strings"
)

var isIdentifier_Regexp *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z\$][a-zA-Z0-9\$]*$`)

func isIdentifier(string_ string) bool {
    return isIdentifier_Regexp.MatchString(string_)
}

func (self *_runtime) toValueArray(arguments ...interface{}) []Value {
    length := len(arguments)
    if length == 1 {
        if valueArray, ok := arguments[0].([]Value); ok {
            return valueArray
        }
        return []Value{self.toValue(arguments[0])}
    }

    valueArray := make([]Value, length)
    for index, value := range arguments {
        valueArray[index] = self.toValue(value)
    }

    return valueArray
}

func stringToArrayIndex(name string) int64 {
    index, err := strconv.ParseInt(name, 10, 64)
    if err != nil {
        return -1
    }
    if index < 0 {
        return -1
    }
    if index >= maxUint32 {
        // The value 2^32 (or above) is not a valid index because
        // you cannot store a uint32 length for an index of uint32
        return -1
    }
    return index
}

func isUint32(value int64) bool {
    return value >= 0 && value <= maxUint32
}

func arrayIndexToString(index int64) string {
    return strconv.FormatInt(index, 10)
}

func valueOfArrayIndex(array []Value, index int) Value {
    value, _ := getValueOfArrayIndex(array, index)
    return value
}

func getValueOfArrayIndex(array []Value, index int) (Value, bool) {
    if index >= 0 && index < len(array) {
        value := array[index]
        if !value.isEmpty() {
            return value, true
        }
    }
    return Value{}, false
}

// A range index can be anything from 0 up to length. It is NOT safe to use as an index
// to an array, but is useful for slicing and in some ECMA algorithms.
func valueToRangeIndex(indexValue Value, length int64, negativeIsZero bool) int64 {
    index := indexValue.number().int64
    if negativeIsZero {
        if index < 0 {
            index = 0
        }
        // minimum(index, length)
        if index >= length {
            index = length
        }
        return index
    }

    if index < 0 {
        index += length
        if index < 0 {
            index = 0
        }
    } else {
        if index > length {
            index = length
        }
    }
    return index
}

func rangeStartEnd(array []Value, size int64, negativeIsZero bool) (start, end int64) {
    start = valueToRangeIndex(valueOfArrayIndex(array, 0), size, negativeIsZero)
    if len(array) == 1 {
        // If there is only the start argument, then end = size
        end = size
        return
    }

    // Assuming the argument is undefined...
    end = size
    endValue := valueOfArrayIndex(array, 1)
    if !endValue.IsUndefined() {
        // Which it is not, so get the value as an array index
        end = valueToRangeIndex(endValue, size, negativeIsZero)
    }
    return
}

func rangeStartLength(source []Value, size int64) (start, length int64) {
    start = valueToRangeIndex(valueOfArrayIndex(source, 0), size, false)

    // Assume the second argument is missing or undefined
    length = int64(size)
    if len(source) == 1 {
        // If there is only the start argument, then length = size
        return
    }

    lengthValue := valueOfArrayIndex(source, 1)
    if !lengthValue.IsUndefined() {
        // Which it is not, so get the value as an array index
        length = lengthValue.number().int64
    }
    return
}

func boolFields(input string) (result map[string]bool) {
    result = map[string]bool{}
    for _, word := range strings.Fields(input) {
        result[word] = true
    }
    return result
}

func hereBeDragons(arguments ...interface{}) string {
    pc, _, _, _ := runtime_.Caller(1)
    name := runtime_.FuncForPC(pc).Name()
    message := fmt.Sprintf("Here be dragons -- %s", name)
    if len(arguments) > 0 {
        message += ": "
        argument0 := fmt.Sprintf("%s", arguments[0])
        if len(arguments) == 1 {
            message += argument0
        } else {
            message += fmt.Sprintf(argument0, arguments[1:]...)
        }
    } else {
        message += "."
    }
    return message
}

func throwHereBeDragons(arguments ...interface{}) {
    panic(hereBeDragons(arguments...))
}

func eachPair(list []interface{}, fn func(_0, _1 interface{})) {
    for len(list) > 0 {
        var _0, _1 interface{}
        _0 = list[0]
        list = list[1:] // Pop off first
        if len(list) > 0 {
            _1 = list[0]
            list = list[1:] // Pop off second
        }
        fn(_0, _1)
    }
}