-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample2.dart
More file actions
42 lines (36 loc) · 1010 Bytes
/
example2.dart
File metadata and controls
42 lines (36 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import 'package:d4rt/d4rt.dart';
enum Status {
success(200),
notFound(404),
error(500);
final int code;
const Status(this.code);
bool get isError => code >= 400;
String describe() => 'Status $name ($code)';
}
final statusEnumBridge = BridgedEnumDefinition<Status>(
name: 'Status',
values: Status.values,
methods: {
'describe': (visitor, target, positionalArgs, namedArgs) =>
(target as Status).describe(),
},
getters: {
'code': (visitor, target) => (target as Status).code,
'isError': (visitor, target) => (target as Status).isError,
},
);
void main() {
final interpreter = D4rt();
interpreter.registerBridgedEnum(
statusEnumBridge, 'package:d4rt_example/example2.dart');
final code = '''
import 'package:d4rt_example/example2.dart';
main() {
var s = Status.error;
return [s.code, s.isError, s.describe()];
}
''';
final result = interpreter.execute(source: code);
print(result); // [500, true, Status error (500)]
}