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