Book Image

DART Essentials

Book Image

DART Essentials

Overview of this book

Table of Contents (16 chapters)
Dart Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing a minimal native extension


We'll start by writing a very simple extension with just one function that takes Dart's String object as an argument and returns another String object with characters in a reverse order. Let's start by defining the two mandatory C functions, as we mentioned earlier.

The implementation here is based on the official example from https://github.com/dart-lang/bleeding_edge/blob/master/dart/samples/sample_extension/sample_extension.cc:

// Header files are part of Dart SDK.
#include "include/dart_native_api.h"
#include "include/dart_api.h"

Dart_NativeFunction ResolveName(Dart_Handle name,
                                int argc,
                                bool* auto_setup_scope) {

DART_EXPORT Dart_Handle fuzzy_search_Init(
        Dart_Handle parent_library) {
    if (Dart_IsError(parent_library)) {
        return parent_library;
    }
    // Register handler that is called every
    // time Dart's "native" keyword is used.
    Dart_Handle result_code ...