Skip to content

Commit bb12986

Browse files
committed
2 parents a6eb0ff + c57c57e commit bb12986

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

lib/src/eval/compiler/compiler.dart

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

649649
if (declaration is EnumDeclaration) {
650+
_ctx.enumValueIndices[libraryIndex] ??= {};
651+
_ctx.enumValueIndices[libraryIndex]![declaration.name.lexeme] = {};
650652
for (final constant in declaration.constants) {
651653
if (!_topLevelGlobalIndices.containsKey(libraryIndex)) {
652654
_topLevelGlobalIndices[libraryIndex] = {};
653655
_ctx.topLevelGlobalInitializers[libraryIndex] = {};
654656
_ctx.topLevelVariableInferredTypes[libraryIndex] = {};
655657
}
656-
657658
final name = '${declaration.name.lexeme}.${constant.name.lexeme}';
658659
if (_topLevelDeclarationsMap[libraryIndex]!.containsKey(name)) {
659660
throw CompileError(
@@ -664,7 +665,9 @@ class Compiler implements BridgeDeclarationRegistry, EvalPluginRegistry {
664665

665666
_topLevelDeclarationsMap[libraryIndex]![name] =
666667
DeclarationOrBridge(libraryIndex, declaration: constant);
667-
_topLevelGlobalIndices[libraryIndex]![name] = _ctx.globalIndex++;
668+
final globalIndex = _ctx.globalIndex++;
669+
_topLevelGlobalIndices[libraryIndex]![name] = globalIndex;
670+
_ctx.enumValueIndices[libraryIndex]![declaration.name.lexeme]![constant.name.lexeme] = globalIndex;
668671
}
669672
}
670673

lib/src/eval/runtime/ops/bridge.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ class Await implements EvcOp {
162162

163163
void _suspend(Runtime runtime, Continuation continuation, $Future future,
164164
Completer completer) async {
165-
final result = await future.$value;
166-
runtime.returnValue = result;
167-
runtime.frameOffset = continuation.frameOffset;
168-
runtime.frame = continuation.frame;
169-
runtime.stack.add(continuation.frame);
170-
runtime.scopeNameStack.add('<asynchronous gap>');
171-
172165
try {
166+
final result = await future.$value;
167+
runtime.returnValue = result;
168+
runtime.frameOffset = continuation.frameOffset;
169+
runtime.frame = continuation.frame;
170+
runtime.stack.add(continuation.frame);
171+
runtime.scopeNameStack.add('<asynchronous gap>');
172+
173173
runtime.bridgeCall(continuation.programOffset);
174174
} catch (e) {
175175
// temporary fix: this isn't correct, we need to reenter the eval loop

test/bridge_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,41 @@ void main() {
259259
.executeLib('package:example/main.dart', 'main', [callback])),
260260
prints('a\nb\n'));
261261
});
262+
263+
test('Should catch bridge future error', () async {
264+
final runtime = compiler.compileWriteAndLoad({
265+
'example': {
266+
'main.dart': '''
267+
import 'dart:async';
268+
269+
void main(Function callback) async {
270+
await callback();
271+
}
272+
'''
273+
}
274+
});
275+
276+
bool callbackExecuted = false;
277+
final callback = $Closure((runtime, target, args) {
278+
callbackExecuted = true;
279+
return $Future.wrap(Future.error(Exception('Bridge error')));
280+
});
281+
282+
Exception? caughtException;
283+
try {
284+
await runtime
285+
.executeLib('package:example/main.dart', 'main', [callback]);
286+
fail('Expected exception was not thrown');
287+
} catch (e) {
288+
caughtException = e as Exception;
289+
}
290+
291+
// Verify callback was executed
292+
expect(callbackExecuted, isTrue);
293+
294+
// Verify correct exception type and message
295+
expect(caughtException, isA<Exception>());
296+
expect(caughtException.toString(), contains('Bridge error'));
297+
});
262298
});
263299
}

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)