Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 // Utilities used by the node library. | |
| 6 | |
| 7 #library('nodeimpl'); | |
| 8 | |
| 9 // Helpers for read-only collections of native objects | |
| 10 | |
| 11 // Takes a JavaScript object, returns a dart object | |
| 12 // Used to wrap list elements and map values | |
| 13 | |
| 14 typedef var NativeValueWrapper(var v); | |
| 15 | |
| 16 class NativeListBase<E> implements List<E>{ | |
|
Jennifer Messerly
2012/01/21 01:00:31
Do you know what the cases were that required wrap
| |
| 17 var _list; | |
| 18 NativeListBase(this._list); | |
| 19 int get length() => _length(_list); | |
| 20 int _length(var list) native "return list.length;"; | |
| 21 abstract E operator[](int index); | |
| 22 | |
| 23 bool isEmpty() => length == 0; | |
| 24 | |
| 25 void forEach(void f(E e)) { | |
| 26 int len = length; | |
| 27 for (int i = 0; i < len; i++) { | |
| 28 f(this[i]); | |
| 29 } | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 class NativeList<E> extends NativeListBase<E> { | |
| 34 NativeValueWrapper _ctor; | |
| 35 NativeList(var list, this._ctor) : super(list); | |
| 36 E operator[](int index) => _ctor(_at(_list, index)); | |
| 37 var _at(var list, int index) native "return list[index];"; | |
| 38 } | |
| 39 | |
| 40 class NativeListPrimitiveElement<E> extends NativeListBase<E> { | |
| 41 NativeListPrimitiveElement(var list) : super(list); | |
| 42 E operator[](int index) => _at(_list, index); | |
| 43 E _at(var list, int index) native "return list[index];"; | |
| 44 } | |
| 45 | |
| 46 class NativeMapBase<V> implements Map<String, V> { | |
|
Jennifer Messerly
2012/01/21 01:00:31
This is very cool. I've been wanting to test out s
| |
| 47 var _map; | |
| 48 NativeMapBase(this._map); | |
| 49 int get length() => _length(_map); | |
| 50 int _length(var map) native "return map.length;"; | |
| 51 | |
| 52 bool isEmpty() => length == 0; | |
| 53 | |
| 54 abstract V operator[](String key); | |
| 55 | |
| 56 Collection<String> getKeys() { | |
| 57 List<String> keys = new List<String>(); | |
| 58 _forEachKey(_map, (String key) => keys.add(key)); | |
| 59 return keys; | |
| 60 } | |
| 61 | |
| 62 Collection<V> getValues() { | |
| 63 List<V> values = new List<V>(); | |
| 64 _forEachKey(_map, (String key) => values.add(this[key])); | |
| 65 return values; | |
| 66 } | |
| 67 | |
| 68 void forEach(void f(String key, V value)) => | |
| 69 _forEachKey(_map, (String k) => f(k, this[k])); | |
| 70 | |
| 71 void _forEachKey(var map, void f(String key)) | |
| 72 native """ | |
| 73 for (var i in map) { | |
| 74 if (map.hasOwnProperty(i)) { | |
| 75 f(i); | |
| 76 } | |
| 77 } | |
| 78 """; | |
| 79 } | |
| 80 | |
| 81 class NativeMap<V> extends NativeMapBase<V> { | |
| 82 NativeValueWrapper _ctor; | |
| 83 NativeMap(var map, this._ctor) : super(map); | |
| 84 V operator[](String key) => _ctor(_at(_map, key)); | |
| 85 var _at(var map, String key) native "return map[key];"; | |
| 86 } | |
| 87 | |
| 88 class NativeMapPrimitiveValue<V> extends NativeMapBase<V> { | |
| 89 NativeMapPrimitiveValue(map) : super(map); | |
| 90 V operator[](String key) => _at(_map, key); | |
| 91 var _at(var map, String key) native "return map[key];"; | |
| 92 } | |
| 93 | |
| 94 String NativeGetStringProperty(var object, String key) | |
| 95 native "var v = object[key];return v == undefined ? null : v;"; | |
|
Jennifer Messerly
2012/01/21 01:00:31
should this use hasOwnProperty?
| |
| 96 | |
| 97 bool NativeGetBoolProperty(var object, String key) | |
| 98 native "var v = object[key];return v == undefined ? null : v;"; | |
| 99 | |
| 100 int NativeGetIntProperty(var object, String key) | |
| 101 native "var v = object[key];return v == undefined ? null : v;"; | |
| 102 | |
| 103 double NativeGetDoubleProperty(var object, String key) | |
| 104 native "var v = object[key];return v == undefined ? null : v;"; | |
| 105 | |
| 106 | |
| 107 // Fixed size List utilities | |
| 108 | |
| 109 class FixedLists { | |
| 110 static void getRangeCheck(int srcLength, int start, int length) { | |
| 111 if (length < 0) { | |
| 112 throw new IllegalArgumentException(); | |
| 113 } | |
| 114 int end = start + length; | |
| 115 if (start < 0 || start >= srcLength) { | |
| 116 throw new IndexOutOfRangeException(start); | |
| 117 } | |
| 118 if (end < 0 || end > srcLength) { | |
| 119 throw new IndexOutOfRangeException(end); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 static int indexOf(List list, var element, int start) { | |
| 124 if (start == null) { | |
| 125 start = 0; | |
| 126 } | |
| 127 int end = list.length; | |
| 128 for (int i = start; i < end; i++) { | |
| 129 if (list[i] == element) { | |
| 130 return i; | |
| 131 } | |
| 132 } | |
| 133 return -1; | |
| 134 } | |
| 135 | |
| 136 static int lastIndexOf(List list, var element, int start) { | |
| 137 int end = start == null ? list.length : start; | |
| 138 for (int i = end-1; i >= 0; i--) { | |
| 139 if (list[i] == element) { | |
| 140 return i; | |
| 141 } | |
| 142 } | |
| 143 return -1; | |
| 144 } | |
| 145 | |
| 146 static num last(List list) { | |
| 147 int end = list.length; | |
| 148 if (end <= 0) { | |
| 149 throw new IndexOutOfRangeException(0); | |
| 150 } | |
| 151 return list[end-1]; | |
| 152 } | |
| 153 | |
| 154 static void forEach(List list, void f(var element)) { | |
| 155 int len = list.length; | |
| 156 for (int i = 0; i < len; i++) { | |
| 157 f(list[i]); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 static Collection filter(List list, bool f(element), List ctor(int length)) { | |
| 162 // Assume computation is more expensive than allocation, so use a | |
|
Jennifer Messerly
2012/01/21 01:00:31
Agree. This is usually the right assumption.
| |
| 163 // temporary growable list to store the filtered results. | |
| 164 // (The alternative would be to make two passes through the list.) | |
| 165 List filtered = []; | |
| 166 int len = list.length; | |
| 167 for (int i = 0; i < len; i++) { | |
| 168 var e = list[i]; | |
| 169 if (f(e)) { | |
| 170 filtered.add(e); | |
| 171 } | |
| 172 } | |
| 173 int filteredLength = filtered.length; | |
| 174 List result = ctor(filteredLength); | |
| 175 for (int i = 0; i < len; i++) { | |
| 176 result[i] = filtered[i]; | |
| 177 } | |
| 178 return result; | |
| 179 } | |
| 180 | |
| 181 static bool every(List list, bool f(int element)) { | |
| 182 int len = list.length; | |
| 183 for (int i = 0; i < len; i++) { | |
| 184 if (! f(list[i])) { | |
| 185 return false; | |
| 186 } | |
| 187 } | |
| 188 return true; | |
| 189 } | |
| 190 | |
| 191 static Collection map(List list, f(element), List result) { | |
| 192 int len = list.length; | |
| 193 for (int i = 0; i < len; i++) { | |
| 194 result[i] = f(list[i]); | |
| 195 } | |
| 196 return result; | |
| 197 } | |
| 198 | |
| 199 | |
| 200 static bool some(List list, bool f(num element)) { | |
| 201 int len = list.length; | |
| 202 for (int i = 0; i < len; i++) { | |
| 203 if (f(list[i])) { | |
| 204 return true; | |
| 205 } | |
| 206 } | |
| 207 return false; | |
| 208 } | |
| 209 | |
| 210 static bool isEmpty(List list) => list.length == 0; | |
| 211 } | |
| OLD | NEW |