|
21 | 21 | #include <aws/common/clock.h> |
22 | 22 | #include <aws/common/environment.h> |
23 | 23 | #include <aws/common/logging.h> |
| 24 | +#include <aws/common/mutex.h> |
24 | 25 | #include <aws/common/ref_count.h> |
25 | 26 | #include <aws/common/system_info.h> |
26 | 27 |
|
|
33 | 34 |
|
34 | 35 | #include <uv.h> |
35 | 36 |
|
36 | | -/* aws-crt-nodejs requires N-API version 4 or above for the threadsafe function API */ |
| 37 | +/* |
| 38 | + * This is a multi-line comment to ensure that the static assert does not collide with the static asserts in |
| 39 | + * aws/common/macro.h. |
| 40 | + * |
| 41 | + * aws-crt-nodejs requires N-API version 4 or above for the threadsafe function API |
| 42 | + */ |
37 | 43 | AWS_STATIC_ASSERT(NAPI_VERSION >= 4); |
38 | 44 |
|
39 | 45 | #define AWS_DEFINE_ERROR_INFO_CRT_NODEJS(CODE, STR) AWS_DEFINE_ERROR_INFO(CODE, STR, "aws-crt-nodejs") |
@@ -578,7 +584,28 @@ static bool s_create_and_register_function( |
578 | 584 | return true; |
579 | 585 | } |
580 | 586 |
|
| 587 | +/* |
| 588 | + * Temporary hack to detect multi-init so we can throw an exception because we haven't figured out the right way |
| 589 | + * to support it yet. Better than a hard crash in native code. |
| 590 | + */ |
| 591 | +static struct aws_mutex s_module_lock = AWS_MUTEX_INIT; |
| 592 | +static bool s_module_initialized = false; |
| 593 | + |
581 | 594 | /* napi_value */ NAPI_MODULE_INIT() /* (napi_env env, napi_value exports) */ { |
| 595 | + |
| 596 | + bool already_initialized = false; |
| 597 | + aws_mutex_lock(&s_module_lock); |
| 598 | + if (s_module_initialized) { |
| 599 | + already_initialized = true; |
| 600 | + } |
| 601 | + s_module_initialized = true; |
| 602 | + aws_mutex_unlock(&s_module_lock); |
| 603 | + |
| 604 | + if (already_initialized) { |
| 605 | + napi_throw_error(env, NULL, "Aws-crt-nodejs does not yet support multi-initialization."); |
| 606 | + return NULL; |
| 607 | + } |
| 608 | + |
582 | 609 | s_install_crash_handler(); |
583 | 610 |
|
584 | 611 | struct aws_allocator *allocator = aws_napi_get_allocator(); |
|
0 commit comments