• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / brs / #294

24 Apr 2023 07:40PM UTC coverage: 91.705% (+5.4%) from 86.275%
#294

push

web-flow
Merge pull request #1 from rokucommunity/adoption

refactor in preparation for adoption the project

1736 of 2013 branches covered (86.24%)

Branch coverage included in aggregate %.

5152 of 5498 relevant lines covered (93.71%)

8882.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.26
/src/brsTypes/Int32.ts
1
import { ValueKind, Comparable, BrsBoolean } from "./BrsType";
138✔
2
import { BrsNumber, Numeric } from "./BrsNumber";
3
import { BrsType } from "./";
4
import { Boxable } from "./Boxing";
5
import { Float } from "./Float";
138✔
6
import { Double } from "./Double";
138✔
7
import { Int64 } from "./Int64";
138✔
8
import { roInt } from "./components/RoInt";
138✔
9
import Long from "long";
138✔
10

11
export class Int32 implements Numeric, Comparable, Boxable {
138✔
12
    readonly kind = ValueKind.Int32;
5,686✔
13
    private readonly value: number;
14

15
    getValue(): number {
16
        return this.value;
3,214✔
17
    }
18

19
    /**
20
     * Creates a new BrightScript 32-bit integer value representing the provided `value`.
21
     * @param value the value to store in the BrightScript number, truncated to a 32-bit
22
     *              integer.
23
     */
24
    constructor(value: number | Long) {
25
        if (value instanceof Long) {
5,686✔
26
            // RBI ignores the 32 most significant bits when converting a 64-bit int to a 32-bit int, effectively
27
            // performing a bitwise AND with `0x00000000FFFFFFFF`.  Since Long already tracks the lower and upper
28
            // portions as separate 32-bit values, we can simply extract the least-significant portion.
29
            value = value.low;
9✔
30
        }
31
        this.value = Math.trunc(value);
5,686✔
32
    }
33

34
    /**
35
     * Creates a new BrightScript 32-bit integer value representing the integer contained in
36
     * `asString`.
37
     * @param asString the string representation of the value to store in the BrightScript 32-bit
38
     *                 int. Will be truncated to a 32-bit integer.
39
     * @returns a BrightScript 32-bit integer value representing `asString`.
40
     */
41
    static fromString(asString: string): Int32 {
42
        if (asString.toLowerCase().startsWith("&h")) {
1,953✔
43
            asString = asString.slice(2); // remove "&h" from the string representation
3✔
44
            return new Int32(Number.parseInt(asString, 16));
3✔
45
        }
46
        return new Int32(Number.parseFloat(asString));
1,950✔
47
    }
48

49
    add(rhs: BrsNumber): BrsNumber {
50
        switch (rhs.kind) {
533✔
51
            case ValueKind.Int32:
52
                return new Int32(this.getValue() + rhs.getValue());
529✔
53
            case ValueKind.Int64:
54
                return new Int64(rhs.getValue().add(this.getValue()));
1✔
55
            case ValueKind.Float:
56
                return new Float(this.getValue() + rhs.getValue());
2✔
57
            case ValueKind.Double:
58
                return new Double(this.getValue() + rhs.getValue());
1✔
59
        }
60
    }
61

62
    subtract(rhs: BrsNumber): BrsNumber {
63
        switch (rhs.kind) {
29✔
64
            case ValueKind.Int32:
65
                return new Int32(this.getValue() - rhs.getValue());
24✔
66
            case ValueKind.Int64:
67
                return new Int64(this.getValue()).subtract(rhs);
1✔
68
            case ValueKind.Float:
69
                return new Float(this.getValue() - rhs.getValue());
3✔
70
            case ValueKind.Double:
71
                return new Double(this.getValue() - rhs.getValue());
1✔
72
        }
73
    }
74

75
    multiply(rhs: BrsNumber): BrsNumber {
76
        switch (rhs.kind) {
29✔
77
            case ValueKind.Int32:
78
                return new Int32(this.getValue() * rhs.getValue());
25✔
79
            case ValueKind.Int64:
80
                return new Int64(rhs.getValue().multiply(this.getValue()));
1✔
81
            case ValueKind.Float:
82
                return new Float(this.getValue() * rhs.getValue());
2✔
83
            case ValueKind.Double:
84
                return new Double(this.getValue() * rhs.getValue());
1✔
85
        }
86
    }
87

88
    divide(rhs: BrsNumber): Float | Double {
89
        switch (rhs.kind) {
8✔
90
            case ValueKind.Int32:
91
                return new Float(this.getValue() / rhs.getValue());
4✔
92
            case ValueKind.Int64:
93
                return new Float(this.getValue() / rhs.getValue().toNumber());
1✔
94
            case ValueKind.Float:
95
                return new Float(this.getValue() / rhs.getValue());
2✔
96
            case ValueKind.Double:
97
                return new Double(this.getValue() / rhs.getValue());
1✔
98
        }
99
    }
100

101
    modulo(rhs: BrsNumber): BrsNumber {
102
        switch (rhs.kind) {
5✔
103
            case ValueKind.Int32:
104
                return new Int32(this.getValue() % rhs.getValue());
1✔
105
            case ValueKind.Float:
106
                return new Float(this.getValue() % rhs.getValue());
2✔
107
            case ValueKind.Double:
108
                return new Double(this.getValue() % rhs.getValue());
1✔
109
            case ValueKind.Int64:
110
                return new Int64(this.getValue()).modulo(rhs);
1✔
111
        }
112
    }
113

114
    intDivide(rhs: BrsNumber): Int32 | Int64 {
115
        switch (rhs.kind) {
6✔
116
            case ValueKind.Int32:
117
            case ValueKind.Float:
118
            case ValueKind.Double:
119
                // TODO: Is 32-bit precision enough here?
120
                return new Int32(Math.trunc(this.getValue() / rhs.getValue()));
5✔
121
            case ValueKind.Int64:
122
                return new Int64(Math.trunc(this.getValue() / rhs.getValue().toNumber()));
1✔
123
        }
124
    }
125

126
    leftShift(rhs: BrsNumber): Int32 {
127
        switch (rhs.kind) {
5!
128
            case ValueKind.Int32:
129
            case ValueKind.Float:
130
            case ValueKind.Double:
131
                return new Int32(this.getValue() << Math.trunc(rhs.getValue()));
5✔
132
            case ValueKind.Int64:
133
                return new Int32(this.getValue() << rhs.getValue().toNumber());
×
134
        }
135
    }
136

137
    rightShift(rhs: BrsNumber): Int32 {
138
        switch (rhs.kind) {
5!
139
            case ValueKind.Int32:
140
            case ValueKind.Float:
141
            case ValueKind.Double:
142
                return new Int32(this.getValue() >>> Math.trunc(rhs.getValue()));
5✔
143
            case ValueKind.Int64:
144
                return new Int32(this.getValue() >>> rhs.getValue().toNumber());
×
145
        }
146
    }
147

148
    pow(exponent: BrsNumber): BrsNumber {
149
        switch (exponent.kind) {
12✔
150
            case ValueKind.Int32:
151
                return new Float(Math.pow(this.getValue(), exponent.getValue()));
5✔
152
            case ValueKind.Int64:
153
                return new Int64(this.getValue()).pow(exponent);
2✔
154
            case ValueKind.Float:
155
                return new Float(Math.pow(this.getValue(), exponent.getValue()));
3✔
156
            case ValueKind.Double:
157
                return new Double(Math.pow(this.getValue(), exponent.getValue()));
2✔
158
        }
159
    }
160

161
    and(rhs: BrsNumber): BrsNumber {
162
        switch (rhs.kind) {
5✔
163
            case ValueKind.Int32:
164
                return new Int32(this.getValue() & rhs.getValue());
2✔
165
            case ValueKind.Int64:
166
                return new Int64(this.getValue()).and(rhs);
1✔
167
            case ValueKind.Float:
168
                return new Int32(this.getValue() & rhs.getValue());
1✔
169
            case ValueKind.Double:
170
                return new Int32(this.getValue() & rhs.getValue());
1✔
171
        }
172
    }
173

174
    or(rhs: BrsNumber): BrsNumber {
175
        switch (rhs.kind) {
5✔
176
            case ValueKind.Int32:
177
                return new Int32(this.getValue() | rhs.getValue());
1✔
178
            case ValueKind.Int64:
179
                return new Int64(this.getValue()).or(rhs);
1✔
180
            case ValueKind.Float:
181
                return new Int32(this.getValue() | rhs.getValue());
2✔
182
            case ValueKind.Double:
183
                return new Int32(this.getValue() | rhs.getValue());
1✔
184
        }
185
    }
186

187
    lessThan(other: BrsType): BrsBoolean {
188
        switch (other.kind) {
251✔
189
            case ValueKind.Int32:
190
                return BrsBoolean.from(this.getValue() < other.getValue());
240✔
191
            case ValueKind.Int64:
192
                return new Int64(this.getValue()).lessThan(other);
2✔
193
            case ValueKind.Float:
194
                return new Float(this.getValue()).lessThan(other);
2✔
195
            case ValueKind.Double:
196
                return new Double(this.getValue()).lessThan(other);
2✔
197
            default:
198
                return BrsBoolean.False;
5✔
199
        }
200
    }
201

202
    greaterThan(other: BrsType): BrsBoolean {
203
        switch (other.kind) {
386✔
204
            case ValueKind.Int32:
205
                return BrsBoolean.from(this.getValue() > other.getValue());
375✔
206
            case ValueKind.Int64:
207
                return new Int64(this.getValue()).greaterThan(other);
2✔
208
            case ValueKind.Float:
209
                return new Float(this.getValue()).greaterThan(other);
2✔
210
            case ValueKind.Double:
211
                return new Double(this.getValue()).greaterThan(other);
2✔
212
            default:
213
                return BrsBoolean.False;
5✔
214
        }
215
    }
216

217
    equalTo(other: BrsType): BrsBoolean {
218
        switch (other.kind) {
45✔
219
            case ValueKind.Int32:
220
                return BrsBoolean.from(this.getValue() === other.getValue());
30✔
221
            case ValueKind.Int64:
222
                return new Int64(this.getValue()).equalTo(other);
2✔
223
            case ValueKind.Float:
224
                return new Float(this.getValue()).equalTo(other);
2✔
225
            case ValueKind.Double:
226
                return new Double(this.getValue()).equalTo(other);
2✔
227
            default:
228
                return BrsBoolean.False;
9✔
229
        }
230
    }
231

232
    toString(parent?: BrsType): string {
233
        return this.value.toString();
259✔
234
    }
235

236
    box() {
237
        return new roInt(this);
26✔
238
    }
239
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc