-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(napi): clear currently registering module slot (#19249)
This commit fixes problem with loading N-API modules that use the "old" way of registration (using "napi_module_register" API). The slot was not cleared after loading modules, causing subsequent calls that use the new way of registration (using "napi_register_module_v1" API) to try and load the previous module. Ref #16460 --------- Co-authored-by: Divy Srivastava <[email protected]>
- Loading branch information
1 parent
7ae55e7
commit 512d533
Showing
9 changed files
with
186 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
package-lock.json | ||
|
||
# Test generated artifacts | ||
.swc | ||
.swc | ||
*.dylib | ||
*.so | ||
*.dll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assert, libSuffix } from "./common.js"; | ||
|
||
const ops = Deno[Deno.internal].core.ops; | ||
|
||
Deno.test("ctr initialization (napi_module_register)", { | ||
ignore: Deno.build.os == "windows", | ||
}, function () { | ||
const path = new URL(`./module.${libSuffix}`, import.meta.url).pathname; | ||
const obj = ops.op_napi_open(path, {}); | ||
assert(obj != null); | ||
assert(typeof obj === "object"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
typedef struct napi_module { | ||
int nm_version; | ||
unsigned int nm_flags; | ||
const char* nm_filename; | ||
void* nm_register_func; | ||
const char* nm_modname; | ||
void* nm_priv; | ||
void* reserved[4]; | ||
} napi_module; | ||
|
||
#ifdef _WIN32 | ||
#define NAPI_EXTERN __declspec(dllexport) | ||
#define NAPI_CDECL __cdecl | ||
#else | ||
#define NAPI_EXTERN __attribute__((visibility("default"))) | ||
#define NAPI_CDECL | ||
#endif | ||
|
||
NAPI_EXTERN void NAPI_CDECL | ||
napi_module_register(napi_module* mod); | ||
|
||
#if defined(_MSC_VER) | ||
#if defined(__cplusplus) | ||
#define NAPI_C_CTOR(fn) \ | ||
static void NAPI_CDECL fn(void); \ | ||
namespace { \ | ||
struct fn##_ { \ | ||
fn##_() { fn(); } \ | ||
} fn##_v_; \ | ||
} \ | ||
static void NAPI_CDECL fn(void) | ||
#else // !defined(__cplusplus) | ||
#pragma section(".CRT$XCU", read) | ||
// The NAPI_C_CTOR macro defines a function fn that is called during CRT | ||
// initialization. | ||
// C does not support dynamic initialization of static variables and this code | ||
// simulates C++ behavior. Exporting the function pointer prevents it from being | ||
// optimized. See for details: | ||
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization?view=msvc-170 | ||
#define NAPI_C_CTOR(fn) \ | ||
static void NAPI_CDECL fn(void); \ | ||
__declspec(dllexport, allocate(".CRT$XCU")) void(NAPI_CDECL * fn##_)(void) = \ | ||
fn; \ | ||
static void NAPI_CDECL fn(void) | ||
#endif // defined(__cplusplus) | ||
#else | ||
#define NAPI_C_CTOR(fn) \ | ||
static void fn(void) __attribute__((constructor)); \ | ||
static void fn(void) | ||
#endif | ||
|
||
#define NAPI_MODULE_TEST(modname, regfunc) \ | ||
static napi_module _module = { \ | ||
1, \ | ||
0, \ | ||
__FILE__, \ | ||
regfunc, \ | ||
#modname, \ | ||
0, \ | ||
{0}, \ | ||
}; \ | ||
NAPI_C_CTOR(_register_##modname) { napi_module_register(&_module); } \ | ||
|
||
void* init(void* env __attribute__((unused)), void* exports) { | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE_TEST(TEST_NAPI_MODULE_NAME, init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters