-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathdart_builtins.py
More file actions
150 lines (123 loc) · 3.07 KB
/
dart_builtins.py
File metadata and controls
150 lines (123 loc) · 3.07 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# PythonJS builtins for Dart
# by Brett Hartshorn - copyright 2013
# License: "New BSD"
dart_import('dart:collection')
dart_import('dart:math', 'Math')
@dart.extends
class list( ListBase ):
'''
a List in Dart is growable if no size is given in the constructor,
otherwise if size is given it becomes a fixed length list.
Notes:
https://code.google.com/p/dart/issues/detail?id=11201
http://stackoverflow.com/questions/16247045/how-do-i-extend-a-list-in-dart
'''
def __init__(self, items):
self[...] = new( List() )
if instanceof(items, String):
self[...].addAll( items.split("") )
else:
self[...].addAll( items )
@property
def length(self):
return self[...].length
@length.setter
def length(self,n):
self[...].length = n
def __getitem__(self, index):
if index < 0:
index = self.length + index
return self[...][index]
def __setitem__(self, index, value):
if index < 0:
index = self.length + index
self[...][index] = value
def __getslice__(self, start, stop, step):
if stop == null and step == null:
return self[...].sublist( start )
elif stop < 0:
stop = self[...].length + stop
return self[...].sublist(start, stop)
def __add__(self, other):
self[...].addAll( other[...] )
return self
def append(self, item):
self[...].add( item )
def index(self, obj):
return self[...].indexOf(obj)
tuple = list
#@dart.extends
class dict: #( HashMap ):
'''
HashMap can not be extended anymore:
https://groups.google.com/a/dartlang.org/forum/#!msg/announce/Sj3guf3es24/YsPCdT_vb2gJ
'''
def __init__(self, map):
#self[...] = new( Map() )
#self[...].addAll( items )
self[...] = map
@property
def length(self):
return self[...].length
def __getitem__(self, key):
return self[...][key]
def __setitem__(self, key, value):
self[...][key] = value
def contains(self, key):
return self[...].containsKey(key)
def keys(self):
return self[...].keys.toList()
def values(self):
return self[...].values
def items(self):
r = []
for key in self.keys():
value = self[ key ]
r.append( [key,value] )
return r
def range(n):
r = []
i = 0
while i < n:
r.add( i )
i += 1
return r
def len(a):
return a.length
def str(a):
## TODO conversions to string
return a
def isinstance(a, klass):
## this will not work in dart, because 'is' test fails when klass is a variable
#return JS("a is klass")
return a.runtimeType.toString() == klass.toString()
def __getslice__(a, start, stop, step):
if instanceof(a, String):
if start != null and stop != null:
b = a.substring( start, stop )
elif start != null:
b = a.substring( start )
else:
b = a
if step != null:
b = __reverse__(b)
return b
def __reverse__(a):
if instanceof(a, String):
buff = new( StringBuffer() )
n = a.length - 1
while n >= 0:
buff.write( a[n] )
n -= 1
return buff.toString()
def __test_if_true__( ob ):
if instanceof(ob, String):
return ob.length != 0
elif instanceof(ob, Number):
return ob != 0
elif instanceof(ob, list):
return ob.length != 0
elif instanceof(ob, dict):
return ob.length != 0
elif ob != null:
return True