2019-03-14 17:24:31 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dart_api.h>
|
2019-04-27 19:12:09 +00:00
|
|
|
#include "angel_wings.h"
|
2019-03-14 17:24:31 +00:00
|
|
|
|
|
|
|
// 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;
|
2019-03-14 17:24:31 +00:00
|
|
|
|
|
|
|
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;
|
2019-03-14 17:24:31 +00:00
|
|
|
|
|
|
|
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-03-14 17:24:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-27 19:12:09 +00:00
|
|
|
Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool *auto_setup_scope)
|
|
|
|
{
|
2019-03-14 17:24:31 +00:00
|
|
|
// 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;
|
2019-03-14 17:24:31 +00:00
|
|
|
Dart_NativeFunction result = NULL;
|
2019-04-27 19:12:09 +00:00
|
|
|
const char *cname;
|
2019-03-14 17:24:31 +00:00
|
|
|
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;
|
2019-03-14 17:24:31 +00:00
|
|
|
return result;
|
|
|
|
}
|