Skip to content

Replace the running process, with a POSIX execve system call.

License

Notifications You must be signed in to change notification settings

shiguanghuxian/dexecve

 
 

Repository files navigation

dexecve

Pub Version .github/workflows/main.yml semantic-release Conventional Commits KeepAChangelog License

Replace the running process, with a POSIX execve system call.

Usage

import 'package:dexecve/dexecve.dart';

void main() {
  dexecve('ping', ['1.1.1.1']);
}

see ./example/main.dart for more details

Dart FFI & Golang

This project is a working example of how to integrate dartlang and golang using dart's c interop.

Steps basically look like this:

  1. Create a golang c-shared library.

    hello.go

    package main
    
    import "C"
    
    import (
    	"fmt"
    )
    
    //export HelloWorld
    func HelloWorld() {
    	fmt.Println("hello world")
    }
    
    func main() {}
  2. Compile the library. eg: go build -o libhello.so -buildmode=c-shared hello.go

    You may want to cross compile the library for multiple platforms. Cross compilation of a static golang binary is relatively straight forward these days however it's not as easy to cross compile a library as cgo is required which normally is disabled for cross compilation.

    I have found xgo very helpful for this.

  3. Write the dart ffi code to interface with the library. This feels almost like writing TypeScript typings for a JavaScript library.

    hello.dart

    import 'dart:ffi';
    
    DynamicLibrary _lib = DynamicLibrary.open('libhello.so');
    
    typedef HelloWorld = void Function();
    typedef HelloWorld_func = Void Function();
    final HelloWorld helloWorld = _lib.lookup<NativeFunction<HelloWorld_func>>('HelloWorld').asFunction();
  4. Consume the function in your dart code and profit :)

    main.dart

    import 'hello.dart';
    
    void main() {
      helloWorld();
    }

Essentially all this project does is call golang's syscall.Exec() function.

I understand this might be overkill and a much smaller package could be created if I used C directly. One day I might do just that...

In my opinion this creates a very powerful tool chain, dartlang feels very familiar with Classes, Generics, Extension Methods, Exceptions, Async/Await and many other concepts that other more main stream languages have had for a very long time.

While golang offers a very powerful concurrency model, handy tools like defer, a simplified programming model, native performance and a larger ecosystem which can help to fill any gaps in the current dart ecosystem.

Anything I can do in Go and I can do in Dart!

Further Resources

About

Replace the running process, with a POSIX execve system call.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 94.5%
  • Go 4.1%
  • JavaScript 1.4%