OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 library fletchc.fletch_selector; | |
6 | |
7 enum SelectorKind { | |
8 Method, | |
9 Getter, | |
10 Setter, | |
11 } | |
12 | |
13 class FletchSelector { | |
14 final int encodedSelector; | |
15 | |
16 const FletchSelector(this.encodedSelector); | |
17 | |
18 int get id => decodeId(encodedSelector); | |
19 | |
20 SelectorKind get kind => SelectorKind.values[encodedSelector >> 8 & 3]; | |
21 | |
22 int get arity => encodedSelector & 255; | |
23 | |
24 static const MAX_ARITY = (1 << 8) - 1; | |
25 static const MAX_UNIQUE_SELECTORS = (1 << 22) - 1; | |
26 static const ID_SHIFT = 10; | |
27 | |
28 // Encode a fletch selector. The result is a 32bit integer with the following | |
29 // layout (lower to higher): | |
30 // - 8 bit arity | |
31 // - 2 bit kind | |
32 // - 22 bit id | |
33 static int encode(int id, SelectorKind kind, int arity) { | |
34 if (arity > MAX_ARITY) throw "Only arity up to 255 is supported"; | |
35 if (id > MAX_UNIQUE_SELECTORS) { | |
36 throw "Only ${MAX_UNIQUE_SELECTORS + 1} unique identifiers is supported"; | |
37 } | |
38 return arity | (kind.index << 8) | (id << ID_SHIFT); | |
39 } | |
40 | |
41 static int encodeMethod(int id, int arity) { | |
42 return encode(id, SelectorKind.Method, arity); | |
43 } | |
44 | |
45 static int encodeGetter(int id) => encode(id, SelectorKind.Getter, 0); | |
46 | |
47 static int encodeSetter(int id) => encode(id, SelectorKind.Setter, 1); | |
48 | |
49 static int decodeId(int selector) => selector >> ID_SHIFT; | |
50 } | |
OLD | NEW |