platform/lib/src/angel_wings.cc

53 lines
1.6 KiB
C++
Raw Normal View History

#include <cstdlib>
#include <iostream>
#include <string.h>
#include <dart_api.h>
2019-04-27 19:12:09 +00:00
#include "angel_wings.h"
// The name of the initialization function is the extension name followed
// by _Init.
2019-04-27 19:12:09 +00:00
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);
2019-04-27 19:12:09 +00:00
if (Dart_IsError(result_code))
return result_code;
return Dart_Null();
}
2019-04-27 19:12:09 +00:00
Dart_Handle HandleError(Dart_Handle handle)
{
if (Dart_IsError(handle))
Dart_PropagateError(handle);
return handle;
}
2019-04-27 19:12:09 +00:00
Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool *auto_setup_scope)
{
// If we fail, we return NULL, and Dart throws an exception.
2019-04-27 19:12:09 +00:00
if (!Dart_IsString(name))
return NULL;
Dart_NativeFunction result = NULL;
2019-04-27 19:12:09 +00:00
const char *cname;
HandleError(Dart_StringToCString(name, &cname));
2019-04-27 19:12:09 +00:00
if (strcmp("Dart_WingsSocket_bindIPv4", cname) == 0)
result = Dart_WingsSocket_bindIPv4;
if (strcmp("Dart_WingsSocket_bindIPv6", cname) == 0)
result = Dart_WingsSocket_bindIPv6;
if (strcmp("Dart_WingsSocket_getPort", cname) == 0)
result = Dart_WingsSocket_getPort;
if (strcmp("Dart_WingsSocket_write", cname) == 0)
result = Dart_WingsSocket_write;
if (strcmp("Dart_WingsSocket_closeDescriptor", cname) == 0)
result = Dart_WingsSocket_closeDescriptor;
if (strcmp("Dart_WingsSocket_close", cname) == 0)
result = Dart_WingsSocket_close;
2019-04-29 05:55:02 +00:00
if (strcmp("Dart_WingsSocket_listen", cname) == 0)
result = Dart_WingsSocket_listen;
return result;
}