dna 0.2.0 copy "dna: ^0.2.0" to clipboard
dna: ^0.2.0 copied to clipboard

outdatedDart 1 only

Dart Native Access.

DNA #

Dart Native Access lets you deal with native libraries from Dart with zero lines of C/C++ code. Just pure Dart.

How to ... #

For instance, you want to call getppid function from libc library on Linux.

  1. Find method signature in documentation:

pid_t getpid(void);

  1. Map parameters types and return type to C date types:

FIXME: Platform specific step

typedef int pid_t;

  1. Map C types to Dart types and DNA type constants. See Type mapping.

  2. Define class Libc with annotation @Lib:

@Lib('libc.so.6')
class Libc {
  
}

libc.so.6 is library name

  1. Define method getppid. Annotate parameters with @Arg and method with @Ret with corresponding type constants:
class Libc {
 
  @Ret(ARG_INT)
  int getpid() { ... }
}
  1. Finish with boilerplate code:

FIXME: This is temporary step for early versions

class Libc {
  dynamic lib = new DynamicLibrary<Libc>();  
  
  int getpid() { return lib.getpid(); }
}

  1. Use it
Libc libc = new Libc();
var processId = libc.getpid();

Type mapping #

FIXME: Decribe pointers and in/out parameters

C Dart DNA In/Out Comments
void void ARG_VOID
char int ARG_CHAR
short int ARG_SHORT
int int ARG_INT
long int ARG_LONG
longlong int ARG_LONGLONG
bool bool ARG_BOOL
float double ARG_FLOAT
double double ARG_DOUBLE
type * int ARG_POINTER if you know raw pointer value
type * TypedData ARG_TYPEDDATA TypeData must be initialized and have expected by callee size
struct * T ARG_TYPEDATA In where T is strut class. See Define structure

|struct *|Ref<T>|ARG_TYPEDATA|Out|where T is strut class. See Define structure| |---|---|---|---| |int *|List<int>|ARG_LISTINT|In|| |int *|Ref<List<int>>|ARG_LISTINT|Out|| |char *|String|ARG_STRING|In|| |char *|Ref<String>|ARG_STRING|Out|| |char **|List<String>|ARG_LISTSTRING|In|| |char **|Ref<List<String>>|ARG_LISTSTRING|Out||

Define method #

@Ret(ARG_XYZ)
T method(@Arg(ARG_XYZ) P1 inParam, @Arg(ARG_XYZ) @Out() Ref<P2> outParam)

Define structure #

@Struct()
class Struct {
    @Field(ARG_XYZ, 0)
    T field;
}

Define library #

@Lib('name')
class Library {
}

FIXME: Symlinks aren't supported yet

Examples #

Linux #

libc library

@Lib('libc.so.6')
class Libc {
  dynamic lib = new DynamicLibrary<Libc>();

  int getpid() { return lib.getpid(); }
  int getuid() { return lib.getuid(); }
            
  void srand(@Arg(ARG_INT) int seed) { return lib.srand(seed); }
  int rand() { return lib.rand(); }
}

void main(){
  Libc libc = new Libc();
  var processId = libc.getpid();
  print('pid $processId');

  var userId = libc.getuid();
  print('user id ${userId}');

  libc.srand(0xDEADBEEF);
  var rand = libc.rand();
  print('random $rand');    
}

Windows #

kenel32 library

@Lib('Kernel32.dll')
class Kernel32 {
  dynamic lib = new DynamicLibrary<Kernel32>();

  int GetCurrentProcess() 
    { return lib.GetCurrentProcess(); }
  int GetProcessId(@Arg(ARG_INT) int process) 
    { return lib.GetProcessId(process); }
  int GetModuleFileNameA(@Arg(ARG_INT) int process, @Arg(ARG_STRING) @Out() Ref<String> imageFileName, @Arg(ARG_INT) int size)  
    { return lib.GetModuleFileNameA(process, imageFileName, size); }
  bool GetProcessWorkingSetSize(@Arg(ARG_INT) int process, @Arg(ARG_POINTER) Ref<int> minimumWorkingSetSize, @Arg(ARG_POINTER) Ref<int> maximumWorkingSetSize) 
    { return lib.GetProcessWorkingSetSize(process, minimumWorkingSetSize, maximumWorkingSetSize); }

  int GetLastError() { return lib.GetLastError(); }
}

void main(){
  Kernel32 kernel32 = new Kernel32();
  var process = kernel32.GetCurrentProcess();
  var processId = kernel32.GetProcessId(process);
  print('pid $processId = ${io.pid}');

  var min = new Ref<int>(0);
  var max = new Ref<int>(0);
  var result = kernel32.GetProcessWorkingSetSize(process, min, max);
  print('working set min ${min.value} max ${max.value}');

  var name = new Ref(new String.fromCharCodes(new List.filled(64, 0)));
  result = kernel32.GetModuleFileNameA(0, name, name.value.length);
  print('module name  ${name.value} result $result error ${kernel32.GetLastError()}');

}

Requirements #

  • Linux 64-bit
  • Windows 32-bit and 64-bit

Trade off #

  • Performance?
  • A lot of things aren't supported yet
2
likes
0
pub points
0%
popularity

Publisher

unverified uploader

Dart Native Access.

Homepage

License

unknown (LICENSE)

More

Packages that depend on dna