dna 0.4.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 62

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 data types:

typedef int pid_t;

  1. Map C data types to Dart types and DNA type constants. See Type mapping.
  1. Define class Libc with annotation @Library:
@Library('libc.so.6')
class Libc {
  
}

libc.so.6 is library name

  1. Define method getppid. Annotate parameters with @Param and method with @Method with corresponding type constants:
class Libc {
 
  @Method(C_INT)
  int getpid() { ... }
}

In this example method has no parameters.

  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();

For full code see Examples.

How to define library

@Library('name')
class Library {
    dynamic lib = new DynamicLibrary<Library>(); //It's required yet
}

name is the library name. For instance: libname.so or name.dll or libname.dylib.

Please see documentation for the target OS to know where dynamic linker searches for the library.

FIXME: Symlinks aren't supported yet?

How to define method

@Method(C_XYZ)
T method(@Param(C_XYZ) P1 inParam, @Param(C_XYZ) @Out() Ref<P2> outParam) 
    { return lib.method(inParam, outParam); } //It's required yet

C_XYZ is type constant. See Type mapping.

How to define structure

@Struct()
class Struct {
    @Field(C_XYZ)
    T field;
}

Type mapping

CDartConstantIn/OutComments
voidvoidVOIDIn
charintC_CHARIn
shortintC_SHORTIn
intintC_INTIn
longintC_LONGIn
longlongintC_LONGLONGIn
boolboolC_BOOLIn
floatdoubleC_FLOATIn
doubledoubleC_DOUBLEIn
---------------
$type *intC_POINTERInRaw pointer value.
$type *TypedDataTYPEDDATAIn\OutTypeData must be initialized and have expected by callee size.
---------------
struct *TC_STRUCTInT is strut class. See Define structure.
struct *Ref<T>C_STRUCTOutT is strut class. See Define structure.
------------
char *StringC_STRINGIn
char *Ref<String>C_STRINGOut
int *List<int>LISTINTIn
int *Ref<List<int>>LISTINTOut
char **List<String>LISTSTRINGIn
char **Ref<List<String>>LISTSTRINGOut

Out parameters

Out parameters must be marked as @Param( , out: true)

Dart doesn't support parameters by reference. It's good but it's the causes of one challenge: callee cannot override parameter object. For instance, String parameter cannot be modified inside called method.

Therefore, it's needed to use wrapper object to emulate out parameters: Ref<T>.

FIXME: Describe pointers, in/out parameters, String parameters and TypedData parameters

FAQ

I see Failed assertion: 'libraryPointer != 0

Dynamic linker cannot find the library. Check the library name.

I see Failed assertion: 'methodPointer != 0

Dynamic linker cannot find method in the library. Check the method name. Check that library exports the method.

My library is cross platform and has different name on each platform.

See example.

@Library('libname.so', 'name.dll')
class Library {
    dynamic lib = new DynamicLibrary<Library>();
}

My library doesn't export functions or/and uses custom logic to get function pointer

See example.

@Library('name.dll')
class Library {
  dynamic lib = new DynamicLibrary<Library>(getMethodPointer);

  static int getMethodPointer(DynamicLibrary that, Invocation invocation) {
    return ... //raw function pointer here
  }
}

Why is it needed to define such strange method body{ return lib.name(); }?

Because I don't how to create class at runtime in Dart.

Examples

Linux

libc library

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

  int getpid() { return lib.getpid(); }      
  int getuid() { return lib.getuid(); }       
           
  void srand(@Param(C_UINT) 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

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

  int GetCurrentProcess() { return lib.GetCurrentProcess(); }
  int GetProcessId(@Param(C_INT) int process) { return lib.GetProcessId(process); }
  
  int GetModuleFileNameA(@Param(C_INT) int process, @Param(C_STRING) @Out() Ref<String> imageFileName, 
    @Param(C_INT) int size) { return lib.GetModuleFileNameA(process, imageFileName, size); }
    
  bool GetProcessWorkingSetSize(@Param(C_INT) int process, 
    @Param(C_POINTER) Ref<int> minimumWorkingSetSize,
    @Param(C_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

To Do

  • Improve performance
  • A lot of things aren't supported yet
    • Platform independent types: int8, int16, int32, int64 etc.
    • Function pointers to Dart methods
    • ...
  • Get rid off template code in each library class
  • Mac OS support
  • Refactor and clean up code

Changelog

0.4

  • Refactoring

0.3

  • Nested structure support

0.2

  • Structure support

0.1

  • Initial version

example/example.dart

//  Copyright 2016 root.ext@gmail.com
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

void main(){

}

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dna: "^0.4.0"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dna/dna.dart';
        
Version Uploaded Documentation Archive
0.4.0 Dec 31, 2016 Go to the documentation of dna 0.4.0 Download dna 0.4.0 archive
0.3.0 Dec 29, 2016 Go to the documentation of dna 0.3.0 Download dna 0.3.0 archive
0.2.2 Dec 28, 2016 Go to the documentation of dna 0.2.2 Download dna 0.2.2 archive
0.2.1 Dec 27, 2016 Go to the documentation of dna 0.2.1 Download dna 0.2.1 archive
0.2.0 Dec 26, 2016 Go to the documentation of dna 0.2.0 Download dna 0.2.0 archive
0.1.0 Dec 24, 2016 Go to the documentation of dna 0.1.0 Download dna 0.1.0 archive
0.0.2 Dec 22, 2016 Go to the documentation of dna 0.0.2 Download dna 0.0.2 archive
0.0.1 Dec 19, 2016 Go to the documentation of dna 0.0.1 Download dna 0.0.1 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
29 / 100
Health:
Code health derived from static analysis. [more]
98 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
91 / 100
Overall score:
Weighted score of the above. [more]
62

Platforms

Detected platforms: web, other

Primary library: package:dna/dna.dart with components: mirrors.

Suggestions

  • Fix .analysis_options.

    We were unable to parse .analysis_options.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

  • Enable strong mode analysis.

    Strong mode helps you to detect bugs and potential issues earlier.Start your analysis_options.yaml file with the following:

    analyzer:
      strong-mode: true
    

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <2.0.0
Dev dependencies
test >=0.12.0 <0.13.0