Dart: dart:builtin

dart:builtin are the runtime libraries available to the DartVM. They provide some of the more system level API you would expect such as streams I/O, filesystem I/O, basic TCP socket I/O, platform information, native process invocation. ¬†Most of these class implementations do call “native”, which from the point of view of the DartVM means invoking native VM code. “native” tells the VM to look up in a list for the entry that maps to the following argument after “native”.

_DirectoryNative

nativedirectory.png

While Dart is still Alpha these builtin may not be written in stone, so try not to depend on them too much. They can and do provide you with some neat access to native functionality you would commonly need when written shell/system applications.

By including #import(‘dart:builtin’); you can view the builtin dart files from DartEditor. Currently the DartEditor might show ¬†warnings when including this module but runtime should not be affected. The truth of the matter is, if you explicitly include ‘dart:builtin’ or not, it is still available to you from implicit inclusion in the DartVM.

dartbuiltin

The following should give an idea of what built in objects are accessible and how they may be used. The examples should not be considered best practices, just an idea of how you could use them and that they exist. The code is taken from a sample that can be found on Github

Directory

    Directory d;
    if (l.length >= 2) {
      d = new Directory(l[1]);
    } else {
      d = new Directory(".");
    }
    d.dirHandler = (var dir) {
      print("${dir}");
    };
    d.fileHandler = (var file) {
      print("${file}");
    };
    d.list(true);

File

    File f = new File(l[0]);
    if (!f.existsSync()) {
      print("Error: file does not exist");
      return;
    }
    RandomAccessFile r = f.openSync(FileMode.READ);
    int length = r.lengthSync();
    List buffer = new List(length);
    int readLength = r.readListSync(buffer, 0, length);
    r.close();
    String s = new String.fromCharCodes(buffer);
    print(s);

ChunkedInputStream

    ChunkedInputStream chunkedInputStream = new ChunkedInputStream(f.openInputStream(), 16);
    List c = chunkedInputStream.read();
    while(c!=null) {
      StringBuffer sb = new StringBuffer();
      c.forEach((int b) {
        sb.add("0x");
        if (b.toRadixString(16).length == 1) {
          sb.add("0");
        }
        sb.add(b.toRadixString(16));
        sb.add(" ");
      });
      print(sb.toString());
      c = chunkedInputStream.read();
    }

InputStream
OutputStream

    File src = new File(arg[1]);
    File dest = new File(arg[2]);
    var srcStream = src.openInputStream();
    var destStream = dest.openOutputStream();
    destStream.write(srcStream.read());
    srcStream.close();
    destStream.close();

StringInputStream
DynamicListInputStream
ListInputStream
ListOutputStream

Platform

 Platform p = new Platform();
 print('{"operatingSystem":${p.operatingSystem()},"numberOfProcessors":${p.numberOfProcessors()},"pathSeparator":${p.pathSeparator()}}');

Process

    Process ps = new Process.start('ps', []);
    StringBuffer messages = new StringBuffer();
    ps.exitHandler = (int status) {
      ps.close();
      print(messages.toString());
    };
    ps.stdout.dataHandler = () => _readAll(ps.stdout, messages);
    ps.stderr.dataHandler = () => _readAll(ps.stderr, messages);

ServerSocket

    // initialize the server
    serverSocket = new ServerSocket(host, 0, 5);
    if (serverSocket == null) {
      throw "can't get server socket";
    }
    serverSocket.connectionHandler = onConnect;
    print("accepting connections on ${host}:${serverSocket.port}");

Socket

    sendSocket = new Socket(host, serverSocket.port);
    if (sendSocket == null) {
      throw "can't get send socket";
    }
    // send first 4 bytes immediately
    for (int i = 0; i < 4; i++) {
      sendByte();
    }
    // send next 4 bytes slowly
    new Timer.repeating((Timer t) {
      sendByte();
      if (bytesSent == bytesTotal) {
        sendSocket.close();
        t.cancel();
        print("finished sending");
      }
    }, 500);

SocketInputStream
SocketOutputStream

Timer

<pre>    if (l.length >= 2) {
      try {
        milliSeconds = (Math.parseInt(l[1]).abs()*milliSeconds);
      } catch (BadNumberFormatException ex) {}
    }
    timer = new Timer((var t) {
      print("\nTime elapsed on Timer");
    }, milliSeconds);

Creating dart class with native access

This document provides a simple example how one could implement a native type in dart type system.

https://docs.google.com/document/pub?id=1zGU2wTXPP4REwCQ-nLmXc_Cqe32RBRxGVL7HNI7F2jk

Follow

Get every new post delivered to your Inbox.