-
Notifications
You must be signed in to change notification settings - Fork 21
/
external_class_generic.dart
84 lines (75 loc) · 1.98 KB
/
external_class_generic.dart
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import 'package:hetu_script/hetu_script.dart';
import 'package:hetu_script/binding.dart';
class Generic<T> {
T value;
Generic(this.value);
}
extension GenericBinding on Generic {
dynamic htFetch(String id) {
switch (id) {
case 'value':
return value;
default:
throw HTError.undefined(id);
}
}
dynamic htAssign(String id, dynamic value) {
switch (id) {
case 'value':
this.value = value;
default:
throw HTError.undefined(id);
}
}
}
class GenericClassBinding extends HTExternalClass {
GenericClassBinding() : super('Generic');
@override
dynamic memberGet(String id, {String? from}) {
switch (id) {
case 'Generic':
return (HTEntity entity,
{List<dynamic> positionalArgs = const [],
Map<String, dynamic> namedArgs = const {},
List<HTType> typeArgs = const []}) {
final typeString = typeArgs[0].toString();
switch (typeString) {
case 'num':
return Generic<num>(positionalArgs[0]);
case 'int':
return Generic<int>(positionalArgs[0]);
case 'float':
return Generic<double>(positionalArgs[0]);
case 'str':
return Generic<String>(positionalArgs[0]);
default:
return Generic(positionalArgs[0]);
}
};
default:
throw HTError.undefined(id);
}
}
@override
dynamic instanceMemberGet(dynamic object, String id) {
var i = object as Generic;
return i.htFetch(id);
}
@override
dynamic instanceMemberSet(dynamic object, String id, dynamic value) {
var i = object as Generic;
return i.htAssign(id, value);
}
}
void main() {
final hetu = Hetu();
hetu.init(externalClasses: [GenericClassBinding()]);
hetu.eval('''
external class Generic {
constructor (value)
final value
}
var obj = Generic<str>('hello world')
print(obj.value)
''');
}