Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <aws/common/clock.h>
#include <aws/common/environment.h>
#include <aws/common/logging.h>
#include <aws/common/mutex.h>
#include <aws/common/ref_count.h>
#include <aws/common/system_info.h>

Expand All @@ -33,7 +34,12 @@

#include <uv.h>

/* aws-crt-nodejs requires N-API version 4 or above for the threadsafe function API */
/*
* This is a multi-line comment to ensure that the static assert does not collide with the static asserts in
* aws/common/macro.h.
*
* aws-crt-nodejs requires N-API version 4 or above for the threadsafe function API
*/
AWS_STATIC_ASSERT(NAPI_VERSION >= 4);

#define AWS_DEFINE_ERROR_INFO_CRT_NODEJS(CODE, STR) AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-crt-nodejs")
Expand Down Expand Up @@ -578,7 +584,28 @@ static bool s_create_and_register_function(
return true;
}

/*
* Temporary hack to detect multi-init so we can throw an exception because we haven't figured out the right way
* to support it yet. Better than a hard crash in native code.
*/
static struct aws_mutex s_module_lock = AWS_MUTEX_INIT;
static bool s_module_initialized = false;

/* napi_value */ NAPI_MODULE_INIT() /* (napi_env env, napi_value exports) */ {

bool already_initialized = false;
aws_mutex_lock(&s_module_lock);
if (s_module_initialized) {
already_initialized = true;
}
s_module_initialized = true;
aws_mutex_unlock(&s_module_lock);

if (already_initialized) {
napi_throw_error(env, NULL, "Aws-crt-nodejs does not yet support multi-initialization.");
return NULL;
}

s_install_crash_handler();

struct aws_allocator *allocator = aws_napi_get_allocator();
Expand Down