platform/lib/src/angel_wings.cc

42 lines
1.3 KiB
C++
Raw Normal View History

#include <cstdlib>
#include <iostream>
#include <string.h>
#include <dart_api.h>
// Forward declaration of ResolveName function.
Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool* auto_setup_scope);
// The name of the initialization function is the extension name followed
// by _Init.
DART_EXPORT Dart_Handle angel_wings_Init(Dart_Handle parent_library) {
if (Dart_IsError(parent_library)) return parent_library;
Dart_Handle result_code =
Dart_SetNativeResolver(parent_library, ResolveName, NULL);
if (Dart_IsError(result_code)) return result_code;
return Dart_Null();
}
Dart_Handle HandleError(Dart_Handle handle) {
if (Dart_IsError(handle)) Dart_PropagateError(handle);
return handle;
}
// Native functions get their arguments in a Dart_NativeArguments structure
// and return their results with Dart_SetReturnValue.
void SayHello(Dart_NativeArguments arguments) {
std::cout << "Hello, native world!" << std::endl;
Dart_SetReturnValue(arguments, Dart_Null());
}
Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool* auto_setup_scope) {
// If we fail, we return NULL, and Dart throws an exception.
if (!Dart_IsString(name)) return NULL;
Dart_NativeFunction result = NULL;
const char* cname;
HandleError(Dart_StringToCString(name, &cname));
if (strcmp("SayHello", cname) == 0) result = SayHello;
return result;
}