OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * A collection of helper io functions implemented using node.js. | 6 * A collection of helper io functions implemented using node.js. |
7 * | 7 * |
8 * Idea is to clone the node.js API as closely as possible while adding types. | 8 * Idea is to clone the node.js API as closely as possible while adding types. |
9 * Dart libraries on top of this will experiment with different APIs. | 9 * Dart libraries on top of this will experiment with different APIs. |
10 */ | 10 */ |
11 #library('node'); | 11 #library('node'); |
12 | 12 |
13 // The sandbox needs to import the constructor functions for all the non-hidden
native types we use. | 13 #import('dart:coreimpl'); |
| 14 #import('nodeimpl.dart'); |
| 15 #import('fs.dart'); |
| 16 |
| 17 var require(String module) native; |
| 18 |
| 19 // The sandbox needs to import the constructor functions for all the |
| 20 // non-hidden native types we use. |
14 | 21 |
15 var createSandbox() native | 22 var createSandbox() native |
16 """return {'require': require, 'process': process, 'console': console, | 23 """return { |
| 24 'require': require, |
| 25 'process': process, |
| 26 'console': console, |
17 'Buffer' : Buffer, | 27 'Buffer' : Buffer, |
18 'setTimeout': setTimeout, 'clearTimeout': clearTimeout};"""; | 28 'setTimeout': setTimeout, |
| 29 'clearTimeout': clearTimeout, |
| 30 'setInterval': setInterval, |
| 31 'clearInterval': clearInterval, |
| 32 'module' : module, |
| 33 'ArrayBuffer': ArrayBuffer, |
| 34 'Int8Array': Int8Array, |
| 35 'Uint8Array': Uint8Array, |
| 36 'Int16Array': Int16Array, |
| 37 'Uint16Array': Uint16Array, |
| 38 'Int32Array': Int32Array, |
| 39 'Uint32Array': Uint32Array, |
| 40 'Float32Array': Float32Array, |
| 41 'Float64Array': Float64Array, |
| 42 'DataView': DataView |
| 43 };"""; |
19 | 44 |
20 typedef void RequestListener(ServerRequest request, ServerResponse response); | 45 // global console |
21 | |
22 // TODO(nweiz): properly title-case these class names | |
23 | |
24 class http native "require('http')" { | |
25 static Server createServer(RequestListener listener) native; | |
26 } | |
27 | |
28 class Server native "http.Server" { | |
29 void listen(int port, [String hostname, Function callback]) native; | |
30 } | |
31 | |
32 class ServerRequest native "http.IncomingMessage" { | |
33 final String method; | |
34 final String url; | |
35 final Map<String, String> headers; | |
36 final String httpVersion; | |
37 | |
38 void setEncoding([String encoding]) {} | |
39 } | |
40 | |
41 class ServerResponse native "http.ServerResponse" { | |
42 int statusCode; | |
43 | |
44 void setHeader(String name, String value) native; | |
45 | |
46 String getHeader(String name) native; | |
47 | |
48 void removeHeader(String name) native; | |
49 | |
50 void write(String data, [String encoding = 'utf8']) native; | |
51 | |
52 void end([String data, String encoding = 'utf8']) native; | |
53 } | |
54 | 46 |
55 class Console native "Console" { | 47 class Console native "Console" { |
56 // TODO(jimhug): Map node.js's ability to take multiple args to what? | 48 // TODO(jimhug): Map node.js's ability to take multiple args to what? |
57 void log(String text) native; | 49 void log(String text) native; |
58 void info(String text) native; | 50 void info(String text) native; |
59 void warn(String text) native; | 51 void warn(String text) native; |
60 void error(String text) native; | 52 void error(String text) native; |
61 void dir(Object obj) native; | 53 void dir(Object obj) native; |
62 void time(String label) native; | 54 void time(String label) native; |
63 void timeEnd(String label) native; | 55 void timeEnd(String label) native; |
(...skipping 13 matching lines...) Expand all Loading... |
77 // typedef EventListener([arg1, arg2, arg3]); | 69 // typedef EventListener([arg1, arg2, arg3]); |
78 | 70 |
79 interface EventEmitter { | 71 interface EventEmitter { |
80 // void addListener(String event, EventListener listener); | 72 // void addListener(String event, EventListener listener); |
81 // void on(String event, EventListener listener); | 73 // void on(String event, EventListener listener); |
82 // void once(String event, EventListener listener); | 74 // void once(String event, EventListener listener); |
83 // void removeListener(String event, EventListener listener); | 75 // void removeListener(String event, EventListener listener); |
84 // List<EventListener> listeners(String event); | 76 // List<EventListener> listeners(String event); |
85 void removeAllListeners(String event); | 77 void removeAllListeners(String event); |
86 void setMaxListeners(num n); | 78 void setMaxListeners(num n); |
87 // TODO(jackpal): use rest arguments | 79 } |
88 void emit(String event, [var arg1, var arg2, var arg3]); | 80 |
| 81 typedef void StreamErrorListener(Error exception); |
| 82 typedef void StreamCloseListener(); |
| 83 |
| 84 interface CommonStream extends EventEmitter { |
| 85 // Error event |
| 86 void emitError(Error error); |
| 87 void addListenerError(StreamErrorListener listener); |
| 88 void onError(StreamErrorListener listener); |
| 89 void onceError(StreamErrorListener listener); |
| 90 void removeListenerError(StreamErrorListener listener); |
| 91 List<StreamErrorListener> listenersError(); |
| 92 |
| 93 // Close event |
| 94 void emitClose(); |
| 95 void addListenerClose(StreamCloseListener listener); |
| 96 void onClose(StreamCloseListener listener); |
| 97 void onceClose(StreamCloseListener listener); |
| 98 void removeListenerClose(StreamCloseListener listener); |
| 99 List<StreamCloseListener> listenersClose(); |
| 100 |
| 101 void destroy(); |
| 102 void destroySoon(); |
| 103 } |
| 104 |
| 105 // data is either a Buffer or a String, depending upon whether setEncoding |
| 106 // has been called. |
| 107 typedef void ReadableStreamDataListener(var data); |
| 108 typedef void ReadableStreamEndListener(); |
| 109 |
| 110 interface ReadableStream extends CommonStream { |
| 111 // Data event |
| 112 void emitData(var data); |
| 113 void addListenerData(ReadableStreamDataListener listener); |
| 114 void onData(ReadableStreamDataListener listener); |
| 115 void onceData(ReadableStreamDataListener listener); |
| 116 void removeListenerData(ReadableStreamDataListener listener); |
| 117 List<ReadableStreamDataListener> listenersData(); |
| 118 |
| 119 // End event |
| 120 void emitEnd(); |
| 121 void addListenerEnd(ReadableStreamEndListener listener); |
| 122 void onEnd(ReadableStreamEndListener listener); |
| 123 void onceEnd(ReadableStreamEndListener listener); |
| 124 void removeListenerEnd(ReadableStreamEndListener listener); |
| 125 List<ReadableStreamEndListener> listenersEnd(); |
| 126 |
| 127 bool readable; |
| 128 void setEncoding(String encoding); |
| 129 void pause(); |
| 130 void resume(); |
| 131 WritableStream pipe(WritableStream destination, [Map options]); |
| 132 } |
| 133 |
| 134 typedef void WritableStreamDrainListener(); |
| 135 typedef void WritableStreamPipeListener(ReadableStream src); |
| 136 |
| 137 interface WritableStream extends CommonStream { |
| 138 // Drain event |
| 139 void emitDrain(); |
| 140 void addListenerDrain(WritableStreamDrainListener listener); |
| 141 void onDrain(WritableStreamDrainListener listener); |
| 142 void onceDrain(WritableStreamDrainListener listener); |
| 143 void removeListenerDrain(WritableStreamDrainListener listener); |
| 144 List<WritableStreamDrainListener> listenersDrain(); |
| 145 |
| 146 // Pipe event |
| 147 void emitPipe(ReadableStream src); |
| 148 void addListenerPipe(WritableStreamPipeListener listener); |
| 149 void onPipe(WritableStreamPipeListener listener); |
| 150 void oncePipe(WritableStreamPipeListener listener); |
| 151 void removeListenerPipe(WritableStreamPipeListener listener); |
| 152 List<WritableStreamPipeListener> listenersPipe(); |
| 153 |
| 154 bool writable; |
| 155 bool write(String string, [String encoding, int fd]); |
| 156 bool writeBuffer(Buffer buffer); |
| 157 void end([String string, String encoding]); |
| 158 void endBuffer(Buffer buffer); |
| 159 } |
| 160 |
| 161 interface ReadWriteStream extends ReadableStream, WritableStream { |
| 162 // No additional methods. |
| 163 } |
| 164 |
| 165 typedef void FsStreamOpenListener(int fd); |
| 166 |
| 167 interface FsStream { |
| 168 // Open event |
| 169 void emitOpen(int fd); |
| 170 void addListenerOpen(FsStreamOpenListener listener); |
| 171 void onOpen(FsStreamOpenListener listener); |
| 172 void onceOpen(FsStreamOpenListener listener); |
| 173 void removeListenerOpen(FsStreamOpenListener listener); |
| 174 List<FsStreamOpenListener> listenersOpen(); |
89 } | 175 } |
90 | 176 |
91 typedef void ProcessExitListener(); | 177 typedef void ProcessExitListener(); |
92 typedef void ProcessUncaughtExceptionListener(Exception err); | 178 typedef void ProcessUncaughtExceptionListener(Exception err); |
93 typedef void ProcessSignalListener(); | 179 typedef void ProcessSignalListener(); |
94 | 180 |
95 class Process implements EventEmitter native "Process" { | 181 class Process implements EventEmitter native "Process" { |
96 var _process; | 182 var _process; |
97 | 183 |
98 // Note: This is not an exhaustive list of signals. Check with your | 184 // Note: This is not an exhaustive list of signals. Check with your |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 final SIGUSR1='SIGUSR1'; | 217 final SIGUSR1='SIGUSR1'; |
132 final SIGUSR2='SIGUSR2'; | 218 final SIGUSR2='SIGUSR2'; |
133 | 219 |
134 Process(var this._process); | 220 Process(var this._process); |
135 | 221 |
136 // Implement EventEmitter | 222 // Implement EventEmitter |
137 void removeAllListeners(String event) | 223 void removeAllListeners(String event) |
138 native "this._process.removeAllListeners(event);"; | 224 native "this._process.removeAllListeners(event);"; |
139 void setMaxListeners(num n) | 225 void setMaxListeners(num n) |
140 native "this._process.setMaxListeners(n);"; | 226 native "this._process.setMaxListeners(n);"; |
141 void emit(String event, [var arg1, var arg2, var arg3]) | |
142 native "this._process.emit(event, arg1, arg2, arg3)"; | |
143 | 227 |
144 // Exit event | 228 // Exit event |
145 void emitExit() | 229 void emitExit() |
146 native "this._process.emit('exit');"; | 230 native "this._process.emit('exit');"; |
147 void addListenerExit(ProcessExitListener listener) | 231 void addListenerExit(ProcessExitListener listener) |
148 native "this._process.addListener('exit', listener);"; | 232 native "this._process.addListener('exit', listener);"; |
149 void onExit(ProcessExitListener listener) | 233 void onExit(ProcessExitListener listener) |
150 native "this._process.on('exit', listener);"; | 234 native "this._process.on('exit', listener);"; |
151 void onceExit(ProcessExitListener listener) | 235 void onceExit(ProcessExitListener listener) |
152 native "this._process.once('exit', listener);"; | 236 native "this._process.once('exit', listener);"; |
(...skipping 24 matching lines...) Expand all Loading... |
177 native "this._process.addListener(signal, listener);"; | 261 native "this._process.addListener(signal, listener);"; |
178 void onSignal(String signal, ProcessSignalListener listener) | 262 void onSignal(String signal, ProcessSignalListener listener) |
179 native "this._process.on(signal, listener);"; | 263 native "this._process.on(signal, listener);"; |
180 void onceSignal(String signal, ProcessSignalListener listener) | 264 void onceSignal(String signal, ProcessSignalListener listener) |
181 native "this._process.once(signal, listener);"; | 265 native "this._process.once(signal, listener);"; |
182 void removeListenerSignal(String signal, ProcessSignalListener listener) | 266 void removeListenerSignal(String signal, ProcessSignalListener listener) |
183 native "this._process.removeListener(signal, listener);"; | 267 native "this._process.removeListener(signal, listener);"; |
184 List<ProcessSignalListener> listenersSignal(String signal) | 268 List<ProcessSignalListener> listenersSignal(String signal) |
185 native "return this._process.listeners(signal);"; | 269 native "return this._process.listeners(signal);"; |
186 | 270 |
187 WritableStream get stdout() | 271 WriteStream get stdout() |
188 native "return this._process.stdout;"; | 272 native "return this._process.stdout;"; |
189 WritableStream get stderr() | 273 WriteStream get stderr() |
190 native "return this._process.stderr;"; | 274 native "return this._process.stderr;"; |
191 ReadableStream get stdin() | 275 ReadStream get stdin() |
192 native "return this._process.stdin;"; | 276 native "return this._process.stdin;"; |
193 | 277 |
194 List<String> get argv() | 278 List<String> get argv() |
195 native "return this._process.argv;"; | 279 native "return this._process.argv;"; |
196 void set argv(List<String> value) | 280 void set argv(List<String> value) |
197 native "this._process.argv = value;"; | 281 native "this._process.argv = value;"; |
198 String get execPath() | 282 String get execPath() |
199 native "return this._process.execPath;"; | 283 native "return this._process.execPath;"; |
200 String chdir(String directory) | 284 String chdir(String directory) |
201 native "this._process.chdir(directory);"; | 285 native "this._process.chdir(directory);"; |
202 | 286 |
203 String cwd() | 287 String cwd() |
204 native "return this._process.cwd();"; | 288 native "return this._process.cwd();"; |
205 | 289 |
206 EnvMap get env() => new EnvMap(_process); | 290 Map<String,String> get env() |
| 291 => new _EnvMap(_env()); |
| 292 var _env() native "return this._process.env;"; |
207 | 293 |
208 void exit([int code = 0]) | 294 void exit([int code = 0]) |
209 native "this._process.exit(code);"; | 295 native "this._process.exit(code);"; |
210 int getgid() | 296 int getgid() |
211 native "return this._process.getgid();"; | 297 native "return this._process.getgid();"; |
212 void setgid(var gid_or_groupname) | 298 void setgid(var gid_or_groupname) |
213 native "this._process.setgid(uid_or_groupname);"; | 299 native "this._process.setgid(uid_or_groupname);"; |
214 int getuid() | 300 int getuid() |
215 native "return this._process.getuid();"; | 301 native "return this._process.getuid();"; |
216 void setuid(var uid_or_username) | 302 void setuid(var uid_or_username) |
217 native "this._process.setuid(uid_or_groupname);"; | 303 native "this._process.setuid(uid_or_groupname);"; |
218 String get version() | 304 String get version() |
219 native "return this._process.version;"; | 305 native "return this._process.version;"; |
| 306 Map<String,String> get versions() |
| 307 => new _NativeMapPrimitiveValue<String>(_versions()); |
| 308 var _versions() native "return this._process._versions;"; |
220 String get installPrefix() | 309 String get installPrefix() |
221 native "return this._process.installPrefix;"; | 310 native "return this._process.installPrefix;"; |
222 void kill(int pid, [String signal=SIGTERM]) | 311 void kill(int pid, [String signal=SIGTERM]) |
223 native "this._process.kill(pid, signal);"; | 312 native "this._process.kill(pid, signal);"; |
224 int get pid() | 313 int get pid() |
225 native "return this._process.pid;"; | 314 native "return this._process.pid;"; |
226 String get title() | 315 String get title() |
227 native "return this._process.title;"; | 316 native "return this._process.title;"; |
| 317 String get arch() |
| 318 native "return this._process.arch;"; |
228 String get platform() | 319 String get platform() |
229 native "return this._process.platform;"; | 320 native "return this._process.platform;"; |
230 | 321 MemoryUsage memoryUsage() => new MemoryUsage._from(_memoryUsage()); |
231 // TODO(jackpal) implement Map memoryUsage() native; | 322 var _memoryUsage() native "return this._process.memoryUsage()"; |
232 void nextTick(Function callback) | 323 void nextTick(Function callback) |
233 native "return this._process.nextTick(callback);"; | 324 native "return this._process.nextTick(callback);"; |
234 int umask([int mask]) | 325 int umask([int mask]) |
235 native "return this._process.umask(mask);"; | 326 native "return this._process.umask(mask);"; |
| 327 int uptime() |
| 328 native "return this._process.uptime();"; |
236 } | 329 } |
237 | 330 |
238 var get _process() | 331 var get _process() |
239 native "return process;"; | 332 native "return process;"; |
240 | 333 |
241 Process get process() { | 334 Process get process() { |
242 return new Process(_process); | 335 return new Process(_process); |
243 } | 336 } |
244 | 337 |
245 class EnvMap { | 338 class _EnvMap extends NativeMapPrimitiveValue<String>{ |
246 var _process; | 339 _EnvMap(var env) : super(env); |
247 const EnvMap(this._process); | 340 // process.env doesn't implement hasOwnProperty |
248 operator [](key) native "return this._process.env[key];"; | 341 void _forEachKey(var map, void f(String key)) |
249 | 342 native """ |
| 343 for (var i in map) { |
| 344 f(i); |
| 345 } |
| 346 """; |
250 } | 347 } |
251 | 348 |
252 typedef void UtilPumpCallback(var error); | 349 class MemoryUsage { |
253 | 350 MemoryUsage._from(var mu) { |
254 class util native "require('util')" { | 351 rss = NativeGetIntProperty(mu, 'rss'); |
255 static void debug(String string) native; | 352 heapTotal = NativeGetIntProperty(mu, 'heapTotal'); |
256 static void log(String string) native; | 353 heapUsed = NativeGetIntProperty(mu, 'heapUsed'); |
257 static void inspect(var object, [bool showHidden=false, num depth=2]) native; | 354 } |
258 static pump(ReadableStream readableStream, WritableStream writeableStream, | 355 int rss; |
259 [UtilPumpCallback callback]) native; | 356 int heapTotal; |
260 // the method inherits(a,b) doesn't make sense for Dart | 357 int heapUsed; |
261 } | |
262 | |
263 // Object is either a Buffer or a String, depending upon whether setEncoding has
been called. | |
264 typedef void ReadableStreamDataListener(var object); | |
265 typedef void ReadableStreamEndListener(); | |
266 typedef void ReadableStreamErrorListener(Object exception); | |
267 typedef void ReadableStreamCloseListener(); | |
268 | |
269 class ReadableStream implements EventEmitter native "*ReadStream" { | |
270 // EventEmitter | |
271 void removeAllListeners(String event) native; | |
272 void setMaxListeners(num n) native; | |
273 void emit(String event, [var arg1, var arg2, var arg3]) native; | |
274 | |
275 // Data event | |
276 void emitData(var data) | |
277 native "this.emit('data', data);"; | |
278 void addListenerData(ReadableStreamDataListener listener) | |
279 native "this.addListener('data', listener);"; | |
280 void onData(ReadableStreamDataListener listener) | |
281 native "this.on('data', listener);"; | |
282 void onceData(ReadableStreamDataListener listener) | |
283 native "this.once('data', listener);"; | |
284 void removeListenerData(ReadableStreamDataListener listener) | |
285 native "this.removeListener('data', listener);"; | |
286 List<ReadableStreamDataListener> listenersData() | |
287 native "return this._process.listeners('data');"; | |
288 | |
289 // End event | |
290 void emitEnd() | |
291 native "this.emit('end');"; | |
292 void addListenerEnd(ReadableStreamEndListener listener) | |
293 native "this.addListener('end', listener);"; | |
294 void onEnd(ReadableStreamEndListener listener) | |
295 native "this.on('end', listener);"; | |
296 void onceEnd(ReadableStreamEndListener listener) | |
297 native "this.once('end', listener);"; | |
298 void removeListenerEnd(ReadableStreamEndListener listener) | |
299 native "this.removeListener('end', listener);"; | |
300 List<ReadableStreamEndListener> listenersEnd() | |
301 native "return this._process.listeners('end');"; | |
302 | |
303 // Error event | |
304 void emitError(Object exception) | |
305 native "this.emit('error', exception);"; | |
306 void addListenerError(ReadableStreamErrorListener listener) | |
307 native "this.addListener('error', listener);"; | |
308 void onError(ReadableStreamErrorListener listener) | |
309 native "this.on('error', listener);"; | |
310 void onceError(ReadableStreamErrorListener listener) | |
311 native "this.once('error', listener);"; | |
312 void removeListenerError(ReadableStreamErrorListener listener) | |
313 native "this.removeListener('error', listener);"; | |
314 List<ReadableStreamErrorListener> listenersError() | |
315 native "return this._process.listeners('error');"; | |
316 | |
317 // Close event | |
318 void emitClose() | |
319 native "this.emit('close');"; | |
320 void addListenerClose(ReadableStreamCloseListener listener) | |
321 native "this.addListener('close', listener);"; | |
322 void onClose(ReadableStreamCloseListener listener) | |
323 native "this.on('close', listener);"; | |
324 void onceClose(ReadableStreamCloseListener listener) | |
325 native "this.once('close', listener);"; | |
326 void removeListenerClose(ReadableStreamCloseListener listener) | |
327 native "this.removeListener('close', listener);"; | |
328 List<ReadableStreamCloseListener> listenersClose() | |
329 native "return this._process.listeners('close');"; | |
330 | |
331 bool readable; | |
332 void setEncoding(String encoding) native; | |
333 void pause() native; | |
334 void resume() native; | |
335 void destroy() native; | |
336 void destroySoon() native; | |
337 void pipe(WritableStream destination, [bool end=true]) | |
338 native "this.pipe(destination, {'end': end});"; | |
339 } | |
340 | |
341 typedef void WritableStreamDrainListener(); | |
342 typedef void WritableStreamErrorListener(Object exception); | |
343 typedef void WritableStreamCloseListener(); | |
344 typedef void WritableStreamPipeListener(ReadableStream src); | |
345 | |
346 class WritableStream implements EventEmitter native "*WriteStream" { | |
347 // EventEmitter | |
348 void removeAllListeners(String event) native "this._writeStream.removeAllListe
ners(event);"; | |
349 void setMaxListeners(num n) native; | |
350 void emit(String event, [var arg1, var arg2, var arg3]) native; | |
351 | |
352 // Drain event | |
353 void emitDrain() | |
354 native "this.emit('drain');"; | |
355 void addListenerDrain(WritableStreamDrainListener listener) | |
356 native "this.addListener('drain', listener);"; | |
357 void onDrain(WritableStreamDrainListener listener) | |
358 native "this.on('drain', listener);"; | |
359 void onceDrain(WritableStreamDrainListener listener) | |
360 native "this.once('drain', listener);"; | |
361 void removeListenerDrain(WritableStreamDrainListener listener) | |
362 native "this.removeListener('drain', listener);"; | |
363 List<WritableStreamDrainListener> listenersDrain() | |
364 native "return this._process.listeners('drain');"; | |
365 | |
366 // Error event | |
367 void emitError(Object exception) | |
368 native "this.emit('error', exception);"; | |
369 void addListenerError(WritableStreamErrorListener listener) | |
370 native "this.addListener('error', listener);"; | |
371 void onError(WritableStreamErrorListener listener) | |
372 native "this.on('error', listener);"; | |
373 void onceError(WritableStreamErrorListener listener) | |
374 native "this.once('error', listener);"; | |
375 void removeListenerError(WritableStreamErrorListener listener) | |
376 native "this.removeListener('error', listener);"; | |
377 List<WritableStreamErrorListener> listenersError() | |
378 native "return this._process.listeners('error');"; | |
379 | |
380 // Close event | |
381 void emitClose() | |
382 native "this.emit('close');"; | |
383 void addListenerClose(WritableStreamCloseListener listener) | |
384 native "this.addListener('close', listener);"; | |
385 void onClose(WritableStreamCloseListener listener) | |
386 native "this.on('close', listener);"; | |
387 void onceClose(WritableStreamCloseListener listener) | |
388 native "this.once('close', listener);"; | |
389 void removeListenerClose(WritableStreamCloseListener listener) | |
390 native "this.removeListener('close', listener);"; | |
391 List<WritableStreamCloseListener> listenersClose() | |
392 native "return this._process.listeners('close');"; | |
393 | |
394 // Pipe event | |
395 void emitPipe(ReadableStream src) | |
396 native "this.emit('pipe', src);"; | |
397 void addListenerPipe(WritableStreamPipeListener listener) | |
398 native "this.addListener('pipe', listener);"; | |
399 void onPipe(WritableStreamPipeListener listener) | |
400 native "this.on('pipe', listener);"; | |
401 void oncePipe(WritableStreamPipeListener listener) | |
402 native "this.once('pipe', listener);"; | |
403 void removeListenerPipe(WritableStreamPipeListener listener) | |
404 native "this.removeListener('pipe', listener);"; | |
405 List<WritableStreamPipeListener> listenersPipe() | |
406 native "return this._process.listeners('pipe');"; | |
407 | |
408 bool writable; | |
409 bool write(String string, [String encoding='utf8', int fd]) native; | |
410 bool writeBuffer(Buffer buffer) native; | |
411 void end([String string, String encoding='utf8']) native; | |
412 void endBuffer(Buffer buffer) native "this.end(buffer);"; | |
413 void destroy() native; | |
414 void destroySoon() native; | |
415 } | |
416 | |
417 class vm native "require('vm')" { | |
418 static void runInThisContext(String code, [String filename]) native; | |
419 static void runInNewContext(String code, [var sandbox, String filename]) | |
420 native; | |
421 static Script createScript(String code, [String filename]) native; | |
422 static Context createContext([sandbox]) native; | |
423 static runInContext(String code, Context context, [String filename]) native; | |
424 } | |
425 | |
426 interface Context {} | |
427 | |
428 class Script native "vm.Script" { | |
429 void runInThisContext() native; | |
430 void runInNewContext([Map sandbox]) native; | |
431 } | |
432 | |
433 class fs native "require('fs')" { | |
434 static void writeFileSync(String outfile, String text) native; | |
435 | |
436 static String readFileSync(String filename, [String encoding = 'utf8']) | |
437 native; | |
438 | |
439 static String realpathSync(String path) native; | |
440 | |
441 static void mkdirSync(String path, [num mode = 511 /* 0777 octal */]) native; | |
442 static List<String> readdirSync(String path) native; | |
443 static void rmdirSync(String path) native; | |
444 static Stats statSync(String path) native; | |
445 static void unlinkSync(String path) native; | |
446 | |
447 static void writeSync(int fd, String text) native; | |
448 static int openSync(String path, String flags, | |
449 [num mode = 438] /* 0666 octal */) native; | |
450 static void closeSync(int fd) native; | |
451 } | |
452 | |
453 class Stats native "fs.Stats" { | |
454 bool isFile() native; | |
455 bool isDirectory() native; | |
456 bool isBlockDevice() native; | |
457 bool isCharacterDevice() native; | |
458 bool isSymbolicLink() native; | |
459 bool isFIFO() native; | |
460 bool isSocket() native; | |
461 | |
462 // TODO(rnystrom): There are also the other fields we can add here if needed. | |
463 // See: http://nodejs.org/docs/v0.6.1/api/fs.html#fs.Stats. | |
464 } | |
465 | |
466 class path native "require('path')" { | |
467 static bool existsSync(String filename) native; | |
468 static String dirname(String path) native; | |
469 static String basename(String path) native; | |
470 static String extname(String path) native; | |
471 static String normalize(String path) native; | |
472 // TODO(jimhug): Get the right signatures for normalizeArray and join | |
473 } | |
474 | |
475 class Readline native "require('readline')" { | |
476 static ReadlineInterface createInterface(input, output) native; | |
477 } | |
478 | |
479 class ReadlineInterface native "Readline.Interface" { | |
480 void setPrompt(String prompt, [int length]) native; | |
481 void prompt() native; | |
482 void on(String event, Function callback) native; | |
483 } | 358 } |
484 | 359 |
485 interface TimeoutId {} | 360 interface TimeoutId {} |
486 | 361 |
487 TimeoutId setTimeout(Function callback, num delay, [arg]) native; | 362 TimeoutId setTimeout(callback(), int delay, [arg /* ... */]) native; |
488 clearTimeout(TimeoutId id) native; | 363 clearTimeout(TimeoutId timeoutId) native; |
489 | 364 |
490 typedef void ChildProcessExitListener(int code, String signal); | 365 interface IntervalId {} |
491 | 366 |
492 class ChildProcess implements EventEmitter native "ChildProcess" { | 367 IntervalId setInterval(callback(), int delay, [arg /* ... */]) native; |
493 var _childprocess; | 368 clearInterval(IntervalId intervalId) native; |
| 369 |
| 370 // buffer |
| 371 |
| 372 interface Buffer extends List<int> default _BufferImplementation { |
| 373 Buffer(int size); |
| 374 Buffer.fromSize(int size); |
| 375 Buffer.fromList(List<int> list); |
| 376 Buffer.fromString(String string, [String encoding]); |
| 377 |
| 378 int write(String string, int offset, int length, [String encoding]); |
| 379 String toString(String encoding, int start, int end); |
494 | 380 |
495 ChildProcess(this._childprocess); | 381 void copy(Buffer targetBuffer, int targetStart, int sourceStart, int sourceEnd
); |
| 382 Buffer slice(int start, int end); |
| 383 |
| 384 int readUInt8(int offset, [bool noAssert]); |
| 385 int readUInt16LE(int offset, [bool noAssert]); |
| 386 int readUInt16BE(int offset, [bool noAssert]); |
| 387 int readUInt32LE(int offset, [bool noAssert]); |
| 388 int readUInt32BE(int offset, [bool noAssert]); |
| 389 |
| 390 int readInt8(int offset, [bool noAssert]); |
| 391 int readInt16LE(int offset, [bool noAssert]); |
| 392 int readInt16BE(int offset, [bool noAssert]); |
| 393 int readInt32LE(int offset, [bool noAssert]); |
| 394 int readInt32BE(int offset, [bool noAssert]); |
| 395 |
| 396 double readFloatLE(int offset, [bool noAssert]); |
| 397 double readFloatBE(int offset, [bool noAssert]); |
| 398 double readDoubleLE(int offset, [bool noAssert]); |
| 399 double readDoubleBE(int offset, [bool noAssert]); |
| 400 |
| 401 void writeUInt8(int value, int offset, [bool noAssert]); |
| 402 void writeUInt16LE(int value, int offset, [bool noAssert]); |
| 403 void writeUInt16BE(int value, int offset, [bool noAssert]); |
| 404 void writeUInt32LE(int value, int offset, [bool noAssert]); |
| 405 void writeUInt32BE(int value, int offset, [bool noAssert]); |
| 406 |
| 407 void writeInt8(int value, int offset, [bool noAssert]); |
| 408 void writeInt16LE(int value, int offset, [bool noAssert]); |
| 409 void writeInt16BE(int value, int offset, [bool noAssert]); |
| 410 void writeInt32LE(int value, int offset, [bool noAssert]); |
| 411 void writeInt32BE(int value, int offset, [bool noAssert]); |
| 412 |
| 413 void writeFloatLE(double value, int offset, [bool noAssert]); |
| 414 void writeFloatBE(double value, int offset, [bool noAssert]); |
| 415 void writeDoubleLE(double value, int offset, [bool noAssert]); |
| 416 void writeDoubleBE(double value, int offset, [bool noAssert]); |
496 | 417 |
497 // EventEmitter | 418 // end defaults to buffer.length |
498 void removeAllListeners(String event) | 419 void fill(int value, int offset, int end); |
499 native "this._childprocess.removeAllListeners(event);"; | |
500 void setMaxListeners(num n) | |
501 native "this._childprocess.setMaxListeners(n);"; | |
502 void emit(String event, [var arg1, var arg2, var arg3]) | |
503 native "this._childprocess.emit(event, arg1, arg2, arg3);"; | |
504 | |
505 // 'exit' event | |
506 void addListenerExit(ChildProcessExitListener listener) | |
507 native "this._childprocess.addListener('exit', listener);"; | |
508 void onExit(ChildProcessExitListener listener) | |
509 native "this._childprocess.on('exit', listener);"; | |
510 void onceExit(ChildProcessExitListener listener) | |
511 native "this._childprocess.once('exit', listener);"; | |
512 void removeListenerExit(ChildProcessExitListener listener) | |
513 native "this._childprocess.removeListener('exit', listener);"; | |
514 List<ChildProcessExitListener> listenersExit() | |
515 native "return this._childprocess.listeners('exit');"; | |
516 | |
517 WritableStream get stdin() | |
518 native "return this._childprocess.stdin;"; | |
519 | |
520 ReadableStream get stdout() | |
521 native "return this._childprocess.stdout;"; | |
522 ReadableStream get stderr() | |
523 native "return this._childprocess.stderr;"; | |
524 int get pid() | |
525 native "return this._childprocess.pid;"; | |
526 } | 420 } |
527 | 421 |
528 typedef void Child_processCallback(Error error, String stdout, String stderr); | 422 /** Static methods that apply to all buffers */ |
529 | 423 |
530 class Child_process native { | 424 class Buffers { |
531 var _cp; | 425 static int get charsWritten() native "return Buffer._charsWritten;"; |
532 | 426 static bool isBuffer(obj) native "return Buffer.isBuffer(obj);"; |
533 Child_process() { | 427 static int byteLength(String string, [String encoding]) |
534 _cp = _get_child_process(); | 428 native "return Buffer.byteLength(string, encoding);"; |
535 } | 429 static int get INSPECT_MAX_BYTES() native "return Buffer.INSPECT_MAX_BYTES;";
|
536 | 430 static void set INSPECT_MAX_BYTES(int v) native "Buffer.INSPECT_MAX_BYTES = v;
"; |
537 // TODOO(jackpal): translate options into a Javascript dictionary | |
538 ChildProcess spawn(String command, [List<String> args, | |
539 Map<String, Object> options]){ | |
540 return new ChildProcess(_spawn(_cp, command, args, options)); | |
541 } | |
542 | |
543 // TODOO(jackpal): translate options into a Javascript dictionary | |
544 ChildProcess exec(String command, Child_processCallback callback, | |
545 [Map<String, Object> options]) { | |
546 // Note the argument order to exec is different than to _exec, | |
547 // because Dart can't have optional arguments in the middle of | |
548 // an argument list. | |
549 return new ChildProcess(_exec(_cp, command, options, callback)); | |
550 } | |
551 | |
552 static var _spawn(var cp, String command, List<String> args) | |
553 native "return cp.spawn(command, args);"; | |
554 static var _exec(var cp, String command, Map<String, Object> options, | |
555 Child_processCallback callback) | |
556 native "return cp.exec(command, options, callback);"; | |
557 | |
558 static var _get_child_process() | |
559 native "return require('child_process');"; | |
560 } | 431 } |
561 | 432 |
562 var get child_process() { | 433 class _BufferImplementation implements Buffer native "Buffer" { |
563 return new Child_process(); | 434 _BufferImplementation(int size) native; |
564 } | 435 _BufferImplementation.fromSize(int size) |
| 436 native "return new Buffer(size);"; |
| 437 _BufferImplementation.fromList(List<int> list) |
| 438 native "return new Buffer(list);"; |
| 439 _BufferImplementation.fromString(String string, [String encoding='utf8']) |
| 440 native "return new Buffer(string, encoding);"; |
565 | 441 |
566 class Buffer native "Buffer" { | |
567 Buffer(int size) native; | |
568 Buffer.fromSize(int size) | |
569 native "return new Buffer(size);"; | |
570 Buffer.fromList(List<int> list) | |
571 native "return new Buffer(list);"; | |
572 Buffer.fromString(String string, [String encoding='utf8']) | |
573 native "return new Buffer(string, encoding);"; | |
574 // the default length is buffer.length-offset | |
575 int write(String string, int offset, int length, [String encoding='utf8']) | 442 int write(String string, int offset, int length, [String encoding='utf8']) |
576 native; | 443 native; |
577 static int get charsWritten() | 444 static int get charsWritten() |
578 native "return Buffer._charsWritten;"; | 445 native "return Buffer._charsWritten;"; |
579 String toString(String encoding, int start, int end) native; | 446 String toString(String encoding, int start, int end) native; |
| 447 |
| 448 // List<int> protocol |
580 int operator[](int index) native; | 449 int operator[](int index) native; |
581 int operator[]=(int index, int value) native; | 450 int operator[]=(int index, int value) native; |
| 451 |
| 452 void _throwUnsupported() { |
| 453 throw new UnsupportedOperationException('not extendable'); |
| 454 } |
| 455 void add(int value) => _throwUnsupported(); |
| 456 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 457 void addLast(int value) => _throwUnsupported(); |
| 458 void clear() => _throwUnsupported(); |
| 459 List<int> getRange(int start, int length) { |
| 460 FixedLists.getRangeCheck(this.length, start, length); |
| 461 Buffer b = new Buffer(length); |
| 462 this.copy(b, 0, start, start + length); |
| 463 return b; |
| 464 } |
| 465 int indexOf(int element, [int start]) |
| 466 => FixedLists.indexOf(this, element, start); |
| 467 void insertRange(int start, int length, [int initialValue]) |
| 468 => _throwUnsupported(); |
| 469 int last() |
| 470 => FixedLists.last(this); |
| 471 int lastIndexOf(int element, [int start]) |
| 472 => FixedLists.lastIndexOf(this, element, start); |
| 473 void set length(int newLength) => _throwUnsupported(); |
| 474 int removeLast() {_throwUnsupported(); return 0; } |
| 475 void removeRange(int start, int length) => _throwUnsupported(); |
| 476 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 477 => _throwUnsupported(); |
| 478 void sort(int compare(int a, int b)) |
| 479 => DualPivotQuicksort.sort(this, compare); |
| 480 |
| 481 // Collection<int> members: |
| 482 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 483 Buffer filter(bool f(int element)) |
| 484 => FixedLists.filter(this, f, (length) => new Buffer(length)); |
| 485 Buffer map(f(int element)) |
| 486 => FixedLists.map(this, f, new Buffer(this.length)); |
| 487 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 488 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 489 bool isEmpty() => FixedLists.isEmpty(this); |
| 490 |
| 491 // Iterable<int> members: |
| 492 Iterator<int> iterator() => new ListIterator(this); |
| 493 |
582 static bool isBuffer(obj) native; | 494 static bool isBuffer(obj) native; |
583 static int byteLength(String string, [String encoding='utf8']) native; | 495 static int byteLength(String string, [String encoding='utf8']) native; |
584 int length; | 496 int get length() native "return this.length;"; |
585 void copy(Buffer targetBuffer, int targetStart, int sourceStart, int sourceEnd
) native; | 497 void copy(Buffer targetBuffer, int targetStart, int sourceStart, int sourceEnd |
| 498 ) native; |
586 Buffer slice(int start, int end) native; | 499 Buffer slice(int start, int end) native; |
587 | 500 |
588 int readUInt8(int offset, [bool noAssert=false]) native; | 501 int readUInt8(int offset, [bool noAssert=false]) native; |
589 int readUInt16LE(int offset, [bool noAssert=false]) native; | 502 int readUInt16LE(int offset, [bool noAssert=false]) native; |
590 int readUInt16BE(int offset, [bool noAssert=false]) native; | 503 int readUInt16BE(int offset, [bool noAssert=false]) native; |
591 int readUInt32LE(int offset, [bool noAssert=false]) native; | 504 int readUInt32LE(int offset, [bool noAssert=false]) native; |
592 int readUInt32BE(int offset, [bool noAssert=false]) native; | 505 int readUInt32BE(int offset, [bool noAssert=false]) native; |
593 | 506 |
594 int readInt8(int offset, [bool noAssert=false]) native; | 507 int readInt8(int offset, [bool noAssert=false]) native; |
595 int readInt16LE(int offset, [bool noAssert=false]) native; | 508 int readInt16LE(int offset, [bool noAssert=false]) native; |
(...skipping 30 matching lines...) Expand all Loading... |
626 } | 539 } |
627 | 540 |
628 class Error native "Error" { | 541 class Error native "Error" { |
629 var stack; | 542 var stack; |
630 var arguments; | 543 var arguments; |
631 var type; | 544 var type; |
632 String message; | 545 String message; |
633 bool killed; | 546 bool killed; |
634 int code; | 547 int code; |
635 String signal; | 548 String signal; |
636 } | 549 } |
| 550 |
| 551 // TODO: module dgram |
| 552 |
| 553 // TODO: module https |
| 554 |
| 555 // module assert TODO: (or maybe not, isn't there a Dart assert?) |
| 556 |
| 557 // Typed arrays |
| 558 |
| 559 class ArrayBuffer native "ArrayBuffer" { |
| 560 ArrayBuffer(int length) native; |
| 561 int byteLength; |
| 562 } |
| 563 |
| 564 interface ArrayBufferView { |
| 565 final ArrayBuffer buffer; |
| 566 final int byteOffset; |
| 567 final int byteLength; |
| 568 } |
| 569 |
| 570 interface TypedArrayBufferView<E> extends ArrayBufferView, List<E> { |
| 571 final int BYTES_PER_ELEMENT; |
| 572 |
| 573 void set(TypedArrayBufferView<E> array, [int offset]); |
| 574 TypedArrayBufferView<E> subarray(int begin, [int end]); |
| 575 } |
| 576 |
| 577 class Int8Array implements TypedArrayBufferView<int> native "Int8Array" { |
| 578 final ArrayBuffer buffer; |
| 579 final int byteOffset; |
| 580 final int byteLength; |
| 581 |
| 582 final int BYTES_PER_ELEMENT; |
| 583 final int length; |
| 584 |
| 585 Int8Array(int length) native; |
| 586 factory Int8Array.fromArray(Int8Array array) native "return new Int8Array(arra
y);"; |
| 587 factory Int8Array.fromList(List<int> list) native "return new Int8Array(list);
"; |
| 588 factory Int8Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int len
gth]) |
| 589 native """if (byteOffset === undefined) return new Int8Array(buffer); |
| 590 if (length === undefined) return new Int8Array(buffer, byteOffset); |
| 591 return new Int8Array(buffer, byteOffset, length);"""; |
| 592 |
| 593 // List protocol |
| 594 int operator[](int index) native; |
| 595 void operator[]=(int index, int value) native; |
| 596 void _throwUnsupported() { |
| 597 throw new UnsupportedOperationException('not extendable'); |
| 598 } |
| 599 void add(int value) => _throwUnsupported(); |
| 600 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 601 void addLast(int value) => _throwUnsupported(); |
| 602 void clear() => _throwUnsupported(); |
| 603 int indexOf(int element, [int start]) |
| 604 => FixedLists.indexOf(this, element, start); |
| 605 void insertRange(int start, int length, [int initialValue]) |
| 606 => _throwUnsupported(); |
| 607 Int8Array getRange(int start, int length) { |
| 608 FixedLists.getRangeCheck(this.length, start, length); |
| 609 return new Int8Array.fromArray(subarray(start, start+length)); |
| 610 } |
| 611 int last() |
| 612 => FixedLists.last(this); |
| 613 int lastIndexOf(int element, [int start]) |
| 614 => FixedLists.lastIndexOf(this, element, start); |
| 615 int removeLast() => _throwUnsupported(); |
| 616 void removeRange(int start, int length) => _throwUnsupported(); |
| 617 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 618 => _throwUnsupported(); |
| 619 void sort(int compare(int a, int b)) |
| 620 => DualPivotQuicksort.sort(this, compare); |
| 621 |
| 622 // Collection<int> members: |
| 623 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 624 Int8Array filter(bool f(int element)) |
| 625 => FixedLists.filter(this, f, (length) => new Int8Array(length)); |
| 626 Int8Array map(f(int)) |
| 627 => FixedLists.map(this, f, new Int8Array(this.length)); |
| 628 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 629 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 630 bool isEmpty() => FixedLists.isEmpty(this); |
| 631 |
| 632 // Iterable<int> members: |
| 633 Iterator<int> iterator() => new ListIterator(this); |
| 634 |
| 635 void set(Int8Array array, [int offset]) native; |
| 636 Int8Array subarray(int begin, [int end]) |
| 637 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 638 } |
| 639 |
| 640 class Uint8Array implements TypedArrayBufferView<int> native "Uint8Array" { |
| 641 final ArrayBuffer buffer; |
| 642 final int byteOffset; |
| 643 final int byteLength; |
| 644 |
| 645 final int BYTES_PER_ELEMENT; |
| 646 final int length; |
| 647 |
| 648 Uint8Array(int length) native; |
| 649 factory Uint8Array.fromArray(Uint8Array array) native "return new Uint8Array(a
rray);"; |
| 650 factory Uint8Array.fromList(List<int> list) native "return new Uint8Array(list
);"; |
| 651 factory Uint8Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int le
ngth]) |
| 652 native """if (byteOffset === undefined) return new Uint8Array(buffer); |
| 653 if (length === undefined) return new Uint8Array(buffer, byteOffset); |
| 654 return new Uint8Array(buffer, byteOffset, length);"""; |
| 655 |
| 656 // List protocol |
| 657 int operator[](int index) native; |
| 658 void operator[]=(int index, int value) native; |
| 659 void _throwUnsupported() { |
| 660 throw new UnsupportedOperationException('not extendable'); |
| 661 } |
| 662 void add(int value) => _throwUnsupported(); |
| 663 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 664 void addLast(int value) => _throwUnsupported(); |
| 665 void clear() => _throwUnsupported(); |
| 666 int indexOf(int element, [int start]) |
| 667 => FixedLists.indexOf(this, element, start); |
| 668 void insertRange(int start, int length, [int initialValue]) |
| 669 => _throwUnsupported(); |
| 670 Uint8Array getRange(int start, int length) { |
| 671 FixedLists.getRangeCheck(this.length, start, length); |
| 672 return new Uint8Array.fromArray(subarray(start, start+length)); |
| 673 } |
| 674 int last() |
| 675 => FixedLists.last(this); |
| 676 int lastIndexOf(int element, [int start]) |
| 677 => FixedLists.lastIndexOf(this, element, start); |
| 678 int removeLast() => _throwUnsupported(); |
| 679 void removeRange(int start, int length) => _throwUnsupported(); |
| 680 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 681 => _throwUnsupported(); |
| 682 void sort(int compare(int a, int b)) |
| 683 => DualPivotQuicksort.sort(this, compare); |
| 684 |
| 685 // Collection<int> members: |
| 686 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 687 Uint8Array filter(bool f(int element)) |
| 688 => FixedLists.filter(this, f, (length) => new Uint8Array(length)); |
| 689 Uint8Array map(f(int)) |
| 690 => FixedLists.map(this, f, new Uint8Array(this.length)); |
| 691 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 692 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 693 bool isEmpty() => FixedLists.isEmpty(this); |
| 694 |
| 695 // Iterable<int> members: |
| 696 Iterator<int> iterator() => new ListIterator(this); |
| 697 |
| 698 void set(Uint8Array array, [int offset]) native; |
| 699 Uint8Array subarray(int begin, [int end]) |
| 700 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 701 } |
| 702 |
| 703 class Int16Array implements TypedArrayBufferView<int> native "Int16Array" { |
| 704 final ArrayBuffer buffer; |
| 705 final int byteOffset; |
| 706 final int byteLength; |
| 707 |
| 708 final int BYTES_PER_ELEMENT; |
| 709 final int length; |
| 710 |
| 711 Int16Array(int length) native; |
| 712 factory Int16Array.fromArray(Int16Array array) native "return new Int16Array(a
rray);"; |
| 713 factory Int16Array.fromList(List<int> list) native "return new Int16Array(list
);"; |
| 714 factory Int16Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int le
ngth]) |
| 715 native """if (byteOffset === undefined) return new Int16Array(buffer); |
| 716 if (length === undefined) return new Int16Array(buffer, byteOffset); |
| 717 return new Int16Array(buffer, byteOffset, length);"""; |
| 718 |
| 719 // List protocol |
| 720 int operator[](int index) native; |
| 721 void operator[]=(int index, int value) native; |
| 722 void _throwUnsupported() { |
| 723 throw new UnsupportedOperationException('not extendable'); |
| 724 } |
| 725 void add(int value) => _throwUnsupported(); |
| 726 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 727 void addLast(int value) => _throwUnsupported(); |
| 728 void clear() => _throwUnsupported(); |
| 729 int indexOf(int element, [int start]) |
| 730 => FixedLists.indexOf(this, element, start); |
| 731 void insertRange(int start, int length, [int initialValue]) |
| 732 => _throwUnsupported(); |
| 733 Int16Array getRange(int start, int length) { |
| 734 FixedLists.getRangeCheck(this.length, start, length); |
| 735 return new Int16Array.fromArray(subarray(start, start+length)); |
| 736 } |
| 737 int last() |
| 738 => FixedLists.last(this); |
| 739 int lastIndexOf(int element, [int start]) |
| 740 => FixedLists.lastIndexOf(this, element, start); |
| 741 int removeLast() => _throwUnsupported(); |
| 742 void removeRange(int start, int length) => _throwUnsupported(); |
| 743 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 744 => _throwUnsupported(); |
| 745 void sort(int compare(int a, int b)) |
| 746 => DualPivotQuicksort.sort(this, compare); |
| 747 |
| 748 // Collection<int> members: |
| 749 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 750 Int16Array filter(bool f(int element)) |
| 751 => FixedLists.filter(this, f, (length) => new Int16Array(length)); |
| 752 Int16Array map(f(int)) |
| 753 => FixedLists.map(this, f, new Int16Array(this.length)); |
| 754 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 755 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 756 bool isEmpty() => FixedLists.isEmpty(this); |
| 757 |
| 758 // Iterable<int> members: |
| 759 Iterator<int> iterator() => new ListIterator(this); |
| 760 |
| 761 void set(Int16Array array, [int offset]) native; |
| 762 Int16Array subarray(int begin, [int end]) |
| 763 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 764 } |
| 765 |
| 766 class Uint16Array implements TypedArrayBufferView<int> native "Uint16Array" { |
| 767 final ArrayBuffer buffer; |
| 768 final int byteOffset; |
| 769 final int byteLength; |
| 770 |
| 771 final int BYTES_PER_ELEMENT; |
| 772 final int length; |
| 773 |
| 774 Uint16Array(int length) native; |
| 775 factory Uint16Array.fromArray(Uint16Array array) native "return new Uint16Arra
y(array);"; |
| 776 factory Uint16Array.fromList(List<int> list) native "return new Uint16Array(li
st);"; |
| 777 factory Uint16Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int l
ength]) |
| 778 native """if (byteOffset === undefined) return new Uint16Array(buffer); |
| 779 if (length === undefined) return new Uint16Array(buffer, byteOffset); |
| 780 return new Uint16Array(buffer, byteOffset, length);"""; |
| 781 |
| 782 // List protocol |
| 783 int operator[](int index) native; |
| 784 void operator[]=(int index, int value) native; |
| 785 void _throwUnsupported() { |
| 786 throw new UnsupportedOperationException('not extendable'); |
| 787 } |
| 788 void add(int value) => _throwUnsupported(); |
| 789 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 790 void addLast(int value) => _throwUnsupported(); |
| 791 void clear() => _throwUnsupported(); |
| 792 int indexOf(int element, [int start]) |
| 793 => FixedLists.indexOf(this, element, start); |
| 794 void insertRange(int start, int length, [int initialValue]) |
| 795 => _throwUnsupported(); |
| 796 Uint16Array getRange(int start, int length) { |
| 797 FixedLists.getRangeCheck(this.length, start, length); |
| 798 return new Uint16Array.fromArray(subarray(start, start+length)); |
| 799 } |
| 800 int last() |
| 801 => FixedLists.last(this); |
| 802 int lastIndexOf(int element, [int start]) |
| 803 => FixedLists.lastIndexOf(this, element, start); |
| 804 int removeLast() => _throwUnsupported(); |
| 805 void removeRange(int start, int length) => _throwUnsupported(); |
| 806 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 807 => _throwUnsupported(); |
| 808 void sort(int compare(int a, int b)) |
| 809 => DualPivotQuicksort.sort(this, compare); |
| 810 |
| 811 // Collection<int> members: |
| 812 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 813 Uint16Array filter(bool f(int element)) |
| 814 => FixedLists.filter(this, f, (length) => new Uint16Array(length)); |
| 815 Uint16Array map(f(int)) |
| 816 => FixedLists.map(this, f, new Uint16Array(this.length)); |
| 817 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 818 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 819 bool isEmpty() => FixedLists.isEmpty(this); |
| 820 |
| 821 // Iterable<int> members: |
| 822 Iterator<int> iterator() => new ListIterator(this); |
| 823 |
| 824 void set(Uint16Array array, [int offset]) native; |
| 825 Uint16Array subarray(int begin, [int end]) |
| 826 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 827 } |
| 828 |
| 829 class Int32Array implements TypedArrayBufferView<int> native "Int32Array" { |
| 830 final ArrayBuffer buffer; |
| 831 final int byteOffset; |
| 832 final int byteLength; |
| 833 |
| 834 final int BYTES_PER_ELEMENT; |
| 835 final int length; |
| 836 |
| 837 Int32Array(int length) native; |
| 838 factory Int32Array.fromArray(Int32Array array) native "return new Int32Array(a
rray);"; |
| 839 factory Int32Array.fromList(List<int> list) native "return new Int32Array(list
);"; |
| 840 factory Int32Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int le
ngth]) |
| 841 native """if (byteOffset === undefined) return new Int32Array(buffer); |
| 842 if (length === undefined) return new Int32Array(buffer, byteOffset); |
| 843 return new Int32Array(buffer, byteOffset, length);"""; |
| 844 |
| 845 // List protocol |
| 846 int operator[](int index) native; |
| 847 void operator[]=(int index, int value) native; |
| 848 void _throwUnsupported() { |
| 849 throw new UnsupportedOperationException('not extendable'); |
| 850 } |
| 851 void add(int value) => _throwUnsupported(); |
| 852 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 853 void addLast(int value) => _throwUnsupported(); |
| 854 void clear() => _throwUnsupported(); |
| 855 int indexOf(int element, [int start]) |
| 856 => FixedLists.indexOf(this, element, start); |
| 857 void insertRange(int start, int length, [int initialValue]) |
| 858 => _throwUnsupported(); |
| 859 Int32Array getRange(int start, int length) { |
| 860 FixedLists.getRangeCheck(this.length, start, length); |
| 861 return new Int32Array.fromArray(subarray(start, start+length)); |
| 862 } |
| 863 int last() |
| 864 => FixedLists.last(this); |
| 865 int lastIndexOf(int element, [int start]) |
| 866 => FixedLists.lastIndexOf(this, element, start); |
| 867 int removeLast() => _throwUnsupported(); |
| 868 void removeRange(int start, int length) => _throwUnsupported(); |
| 869 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 870 => _throwUnsupported(); |
| 871 void sort(int compare(int a, int b)) |
| 872 => DualPivotQuicksort.sort(this, compare); |
| 873 |
| 874 // Collection<int> members: |
| 875 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 876 Int32Array filter(bool f(int element)) |
| 877 => FixedLists.filter(this, f, (length) => new Int32Array(length)); |
| 878 Int32Array map(f(int)) |
| 879 => FixedLists.map(this, f, new Int32Array(this.length)); |
| 880 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 881 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 882 bool isEmpty() => FixedLists.isEmpty(this); |
| 883 |
| 884 // Iterable<int> members: |
| 885 Iterator<int> iterator() => new ListIterator(this); |
| 886 |
| 887 void set(Int32Array array, [int offset]) native; |
| 888 Int32Array subarray(int begin, [int end]) |
| 889 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 890 } |
| 891 |
| 892 class Uint32Array implements TypedArrayBufferView<int> native "Uint32Array" { |
| 893 final ArrayBuffer buffer; |
| 894 final int byteOffset; |
| 895 final int byteLength; |
| 896 |
| 897 final int BYTES_PER_ELEMENT; |
| 898 final int length; |
| 899 |
| 900 Uint32Array(int length) native; |
| 901 factory Uint32Array.fromArray(Uint32Array array) native "return new Uint32Arra
y(array);"; |
| 902 factory Uint32Array.fromList(List<int> list) native "return new Uint32Array(li
st);"; |
| 903 factory Uint32Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int l
ength]) |
| 904 native """if (byteOffset === undefined) return new Uint32Array(buffer); |
| 905 if (length === undefined) return new Uint32Array(buffer, byteOffset); |
| 906 return new Uint32Array(buffer, byteOffset, length);"""; |
| 907 |
| 908 // List protocol |
| 909 int operator[](int index) native; |
| 910 void operator[]=(int index, int value) native; |
| 911 void _throwUnsupported() { |
| 912 throw new UnsupportedOperationException('not extendable'); |
| 913 } |
| 914 void add(int value) => _throwUnsupported(); |
| 915 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 916 void addLast(int value) => _throwUnsupported(); |
| 917 void clear() => _throwUnsupported(); |
| 918 int indexOf(int element, [int start]) |
| 919 => FixedLists.indexOf(this, element, start); |
| 920 void insertRange(int start, int length, [int initialValue]) |
| 921 => _throwUnsupported(); |
| 922 Uint32Array getRange(int start, int length) { |
| 923 FixedLists.getRangeCheck(this.length, start, length); |
| 924 return new Uint32Array.fromArray(subarray(start, start+length)); |
| 925 } |
| 926 int last() |
| 927 => FixedLists.last(this); |
| 928 int lastIndexOf(int element, [int start]) |
| 929 => FixedLists.lastIndexOf(this, element, start); |
| 930 int removeLast() => _throwUnsupported(); |
| 931 void removeRange(int start, int length) => _throwUnsupported(); |
| 932 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 933 => _throwUnsupported(); |
| 934 void sort(int compare(int a, int b)) |
| 935 => DualPivotQuicksort.sort(this, compare); |
| 936 |
| 937 // Collection<int> members: |
| 938 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 939 Uint32Array filter(bool f(int element)) |
| 940 => FixedLists.filter(this, f, (length) => new Uint32Array(length)); |
| 941 Uint32Array map(f(int)) |
| 942 => FixedLists.map(this, f, new Uint32Array(this.length)); |
| 943 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 944 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 945 bool isEmpty() => FixedLists.isEmpty(this); |
| 946 |
| 947 // Iterable<int> members: |
| 948 Iterator<int> iterator() => new ListIterator(this); |
| 949 |
| 950 void set(Uint32Array array, [int offset]) native; |
| 951 Uint32Array subarray(int begin, [int end]) |
| 952 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 953 } |
| 954 |
| 955 class Float32Array implements TypedArrayBufferView<num> native "Float32Array" { |
| 956 final ArrayBuffer buffer; |
| 957 final int byteOffset; |
| 958 final int byteLength; |
| 959 |
| 960 final int BYTES_PER_ELEMENT; |
| 961 final int length; |
| 962 |
| 963 Float32Array(int length) native; |
| 964 factory Float32Array.fromArray(Float32Array array) native "return new Float32A
rray(array);"; |
| 965 factory Float32Array.fromList(List<double> list) native "return new Float32Arr
ay(list);"; |
| 966 factory Float32Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int
length]) |
| 967 native """if (byteOffset === undefined) return new Float32Array(buffer); |
| 968 if (length === undefined) return new Float32Array(buffer, byteOffset); |
| 969 return new Float32Array(buffer, byteOffset, length);"""; |
| 970 |
| 971 // List protocol |
| 972 int operator[](int index) native; |
| 973 void operator[]=(int index, int value) native; |
| 974 void _throwUnsupported() { |
| 975 throw new UnsupportedOperationException('not extendable'); |
| 976 } |
| 977 void add(int value) => _throwUnsupported(); |
| 978 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 979 void addLast(int value) => _throwUnsupported(); |
| 980 void clear() => _throwUnsupported(); |
| 981 int indexOf(int element, [int start]) |
| 982 => FixedLists.indexOf(this, element, start); |
| 983 void insertRange(int start, int length, [int initialValue]) |
| 984 => _throwUnsupported(); |
| 985 Float32Array getRange(int start, int length) { |
| 986 FixedLists.getRangeCheck(this.length, start, length); |
| 987 return new Float32Array.fromArray(subarray(start, start+length)); |
| 988 } |
| 989 int last() |
| 990 => FixedLists.last(this); |
| 991 int lastIndexOf(int element, [int start]) |
| 992 => FixedLists.lastIndexOf(this, element, start); |
| 993 int removeLast() => _throwUnsupported(); |
| 994 void removeRange(int start, int length) => _throwUnsupported(); |
| 995 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 996 => _throwUnsupported(); |
| 997 void sort(int compare(int a, int b)) |
| 998 => DualPivotQuicksort.sort(this, compare); |
| 999 |
| 1000 // Collection<int> members: |
| 1001 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 1002 Float32Array filter(bool f(int element)) |
| 1003 => FixedLists.filter(this, f, (length) => new Float32Array(length)); |
| 1004 Float32Array map(f(int)) |
| 1005 => FixedLists.map(this, f, new Float32Array(this.length)); |
| 1006 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 1007 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 1008 bool isEmpty() => FixedLists.isEmpty(this); |
| 1009 |
| 1010 // Iterable<int> members: |
| 1011 Iterator<int> iterator() => new ListIterator(this); |
| 1012 |
| 1013 void set(Float32Array array, [int offset]) native; |
| 1014 Float32Array subarray(int begin, [int end]) |
| 1015 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 1016 } |
| 1017 |
| 1018 class Float64Array implements TypedArrayBufferView<num> native "Float64Array" { |
| 1019 final ArrayBuffer buffer; |
| 1020 final int byteOffset; |
| 1021 final int byteLength; |
| 1022 |
| 1023 final int BYTES_PER_ELEMENT; |
| 1024 final int length; |
| 1025 |
| 1026 Float64Array(int length) native; |
| 1027 factory Float64Array.fromArray(Float64Array array) native "return new Float64A
rray(array);"; |
| 1028 factory Float64Array.fromList(List<double> list) native "return new Float64Arr
ay(list);"; |
| 1029 factory Float64Array.fromArrayBuffer(ArrayBuffer buffer, [int byteOffset, int
length]) |
| 1030 native """if (byteOffset === undefined) return new Float64Array(buffer); |
| 1031 if (length === undefined) return new Float64Array(buffer, byteOffset); |
| 1032 return new Float64Array(buffer, byteOffset, length);"""; |
| 1033 |
| 1034 // List protocol |
| 1035 int operator[](int index) native; |
| 1036 void operator[]=(int index, int value) native; |
| 1037 void _throwUnsupported() { |
| 1038 throw new UnsupportedOperationException('not extendable'); |
| 1039 } |
| 1040 void add(int value) => _throwUnsupported(); |
| 1041 void addAll(Collection<int> collection) => _throwUnsupported(); |
| 1042 void addLast(int value) => _throwUnsupported(); |
| 1043 void clear() => _throwUnsupported(); |
| 1044 int indexOf(int element, [int start]) |
| 1045 => FixedLists.indexOf(this, element, start); |
| 1046 void insertRange(int start, int length, [int initialValue]) |
| 1047 => _throwUnsupported(); |
| 1048 Float64Array getRange(int start, int length) { |
| 1049 FixedLists.getRangeCheck(this.length, start, length); |
| 1050 return new Float64Array.fromArray(subarray(start, start+length)); |
| 1051 } |
| 1052 int last() |
| 1053 => FixedLists.last(this); |
| 1054 int lastIndexOf(int element, [int start]) |
| 1055 => FixedLists.lastIndexOf(this, element, start); |
| 1056 int removeLast() => _throwUnsupported(); |
| 1057 void removeRange(int start, int length) => _throwUnsupported(); |
| 1058 void setRange(int start, int length, List<int> from, [int startFrom]) |
| 1059 => _throwUnsupported(); |
| 1060 void sort(int compare(int a, int b)) |
| 1061 => DualPivotQuicksort.sort(this, compare); |
| 1062 |
| 1063 // Collection<int> members: |
| 1064 void forEach(void f(int element)) => FixedLists.forEach(this, f); |
| 1065 Float64Array filter(bool f(int element)) |
| 1066 => FixedLists.filter(this, f, (length) => new Float64Array(length)); |
| 1067 Float64Array map(f(int)) |
| 1068 => FixedLists.map(this, f, new Float64Array(this.length)); |
| 1069 bool every(bool f(int element)) => FixedLists.every(this, f); |
| 1070 bool some(bool f(int element)) => FixedLists.some(this, f); |
| 1071 bool isEmpty() => FixedLists.isEmpty(this); |
| 1072 |
| 1073 // Iterable<int> members: |
| 1074 Iterator<int> iterator() => new ListIterator(this); |
| 1075 void set(Float64Array array, [int offset]) native; |
| 1076 Float64Array subarray(int begin, [int end]) |
| 1077 native "return end == null ? this.subarray(begin) : this.subarray(begin, e
nd);"; |
| 1078 } |
| 1079 |
| 1080 class DataView implements ArrayBufferView native "DataView" { |
| 1081 final ArrayBuffer buffer; |
| 1082 final int byteOffset; |
| 1083 final int byteLength; |
| 1084 |
| 1085 DataView.fromArray(ArrayBuffer buffer, [int byteOffset, int byteLength]) |
| 1086 native """if (byteOffset === undefined) return new DataView(buffer); |
| 1087 if (length === undefined) return new DataView(buffer, byteOffset); |
| 1088 return new DataView(buffer, byteOffset, length);"""; |
| 1089 |
| 1090 int getInt8(int byteOffset) native; |
| 1091 int getUint8(int byteOffset) native; |
| 1092 int getInt16(int byteOffset, [bool littleEndian=false]) native; |
| 1093 int getUint16(int byteOffset, [bool littleEndian=false]) native; |
| 1094 int getInt32(int byteOffset, [bool littleEndian=false]) native; |
| 1095 int getUint32(int byteOffset, [bool littleEndian=false]) native; |
| 1096 num getFloat32(int byteOffset, [bool littleEndian=false]) native; |
| 1097 num getFloat64(int byteOffset, [bool littleEndian=false]) native; |
| 1098 |
| 1099 void setInt8(int byteOffset, int value) native; |
| 1100 void setUint8(int byteOffset, int value) native; |
| 1101 void setInt16(int byteOffset, int value, [bool littleEndian=false]) native; |
| 1102 void setUint16(int byteOffset, int value, [bool littleEndian=false]) native; |
| 1103 void setInt32(int byteOffset, int value, [bool littleEndian=false]) native; |
| 1104 void setUint32(int byteOffset, int value, [bool littleEndian=false]) native; |
| 1105 void setFloat32(int byteOffset, num value, [bool littleEndian=false]) native; |
| 1106 void setFloat64(int byteOffset, num value, [bool littleEndian=false]) native; |
| 1107 } |
OLD | NEW |