Skip to content

Commit 8c36633

Browse files
authored
register source-code enum values (#244)
1 parent c719b27 commit 8c36633

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/src/eval/compiler/compiler.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,14 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
632632
: (declaration as EnumDeclaration).members;
633633

634634
if (declaration is EnumDeclaration) {
635+
_ctx.enumValueIndices[libraryIndex] ??= {};
636+
_ctx.enumValueIndices[libraryIndex]![declaration.name.lexeme] = {};
635637
for (final constant in declaration.constants) {
636638
if (!_topLevelGlobalIndices.containsKey(libraryIndex)) {
637639
_topLevelGlobalIndices[libraryIndex] = {};
638640
_ctx.topLevelGlobalInitializers[libraryIndex] = {};
639641
_ctx.topLevelVariableInferredTypes[libraryIndex] = {};
640642
}
641-
642643
final name = '${declaration.name.lexeme}.${constant.name.lexeme}';
643644
if (_topLevelDeclarationsMap[libraryIndex]!.containsKey(name)) {
644645
throw CompileError(
@@ -649,7 +650,9 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
649650

650651
_topLevelDeclarationsMap[libraryIndex]![name] =
651652
DeclarationOrBridge(libraryIndex, declaration: constant);
652-
_topLevelGlobalIndices[libraryIndex]![name] = _ctx.globalIndex++;
653+
final globalIndex = _ctx.globalIndex++;
654+
_topLevelGlobalIndices[libraryIndex]![name] = globalIndex;
655+
_ctx.enumValueIndices[libraryIndex]![declaration.name.lexeme]![constant.name.lexeme] = globalIndex;
653656
}
654657
}
655658

test/enum_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,32 @@ void main() {
9797
'ShowType', {'Movie': $int(0), 'Series': $int(1)});
9898
runtime.executeLib('package:my_package/main.dart', 'main');
9999
});
100+
101+
test('Enum value index property from imported file', () {
102+
const libSource = '''
103+
enum TestEnum {
104+
alpha,
105+
beta
106+
}
107+
''';
108+
109+
const mainSource = '''
110+
import 'package:my_test_package/lib.dart';
111+
112+
int main() {
113+
return TestEnum.beta.index;
114+
}
115+
''';
116+
117+
final runtime = compiler.compileWriteAndLoad({
118+
'my_test_package': {
119+
'main.dart': mainSource,
120+
'lib.dart': libSource,
121+
}
122+
});
123+
124+
final result = runtime.executeLib('package:my_test_package/main.dart', 'main');
125+
expect(result, 1);
126+
});
100127
});
101128
}

0 commit comments

Comments
 (0)