Skip to content

Commit

Permalink
update headers
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Oct 6, 2023
1 parent c553223 commit 3bbbe79
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
14 changes: 10 additions & 4 deletions packages/emnapi/include/emnapi_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
#define EMNAPI_INCLUDE_COMMON_H_

#ifdef __EMSCRIPTEN__
#define NAPI_EXTERN __attribute__((__import_module__("env")))

#define EMNAPI_EXTERN __attribute__((__import_module__("env")))
#else
#define NAPI_EXTERN __attribute__((__import_module__("napi")))

#define EMNAPI_EXTERN __attribute__((__import_module__("emnapi")))
#endif

#define EMNAPI_INTERNAL_EXTERN __attribute__((__import_module__("env")))

#ifdef __cplusplus
#ifndef EXTERN_C_START
#define EXTERN_C_START extern "C" {
#endif

#ifndef EXTERN_C_END
#define EXTERN_C_END }
#endif
#else
#ifndef EXTERN_C_START
#define EXTERN_C_START
#endif

#ifndef EXTERN_C_END
#define EXTERN_C_END
#endif
#endif

#endif
8 changes: 1 addition & 7 deletions packages/emnapi/include/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,29 @@

#include "js_native_api_types.h"

#ifdef EMNAPI_UNMODIFIED_UPSTREAM
// If you need __declspec(dllimport), either include <node_api.h> instead, or
// define NAPI_EXTERN as __declspec(dllimport) on the compiler's command line.
#ifndef NAPI_EXTERN
#ifdef _WIN32
#define NAPI_EXTERN __declspec(dllexport)
#elif defined(__wasm32__)
#elif defined(__wasm__)
#define NAPI_EXTERN \
__attribute__((visibility("default"))) \
__attribute__((__import_module__("napi")))
#else
#define NAPI_EXTERN __attribute__((visibility("default")))
#endif
#endif
#else
#include "emnapi_common.h"
#endif

#define NAPI_AUTO_LENGTH SIZE_MAX

#ifdef EMNAPI_UNMODIFIED_UPSTREAM
#ifdef __cplusplus
#define EXTERN_C_START extern "C" {
#define EXTERN_C_END }
#else
#define EXTERN_C_START
#define EXTERN_C_END
#endif
#endif

EXTERN_C_START

Expand Down
26 changes: 5 additions & 21 deletions packages/emnapi/include/node_api.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#ifndef SRC_NODE_API_H_
#define SRC_NODE_API_H_

#ifdef EMNAPI_UNMODIFIED_UPSTREAM
#ifdef BUILDING_NODE_EXTENSION
#if defined(BUILDING_NODE_EXTENSION) && !defined(NAPI_EXTERN)
#ifdef _WIN32
// Building native addon against node
#define NAPI_EXTERN __declspec(dllimport)
#elif defined(__wasm32__)
#elif defined(__wasm__)
#define NAPI_EXTERN __attribute__((__import_module__("napi")))
#endif
#endif
#endif
#include "js_native_api.h"
#include "node_api_types.h"

Expand All @@ -19,16 +17,13 @@ struct uv_loop_s; // Forward declaration.
#ifdef _WIN32
#define NAPI_MODULE_EXPORT __declspec(dllexport)
#else
#ifdef EMNAPI_UNMODIFIED_UPSTREAM
#define NAPI_MODULE_EXPORT __attribute__((visibility("default")))
#else
#ifdef __EMSCRIPTEN__
#define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) __attribute__((used))
#define NAPI_MODULE_EXPORT \
__attribute__((visibility("default"))) __attribute__((used))
#else
#define NAPI_MODULE_EXPORT __attribute__((visibility("default")))
#endif
#endif
#endif

#if defined(__GNUC__)
#define NAPI_NO_RETURN __attribute__((noreturn))
Expand Down Expand Up @@ -59,19 +54,11 @@ typedef struct napi_module {
NAPI_MODULE_INITIALIZER_X_HELPER(base, version)
#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version

#ifdef EMNAPI_UNMODIFIED_UPSTREAM
#ifdef __wasm32__
#define NAPI_MODULE_INITIALIZER_BASE napi_register_wasm_v
#else
#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
#endif
#else
#ifdef __wasm__
#define NAPI_MODULE_INITIALIZER_BASE napi_register_wasm_v
#else
#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
#endif
#endif

#define NODE_API_MODULE_GET_API_VERSION_BASE node_api_module_get_api_version_v

Expand Down Expand Up @@ -160,7 +147,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_get_buffer_info(napi_env env,
napi_value value,
void** data,
size_t* length);
#if !defined(EMNAPI_UNMODIFIED_UPSTREAM) || (defined(EMNAPI_UNMODIFIED_UPSTREAM) && !defined(__wasm32__))

// Methods to manage simple async operations
NAPI_EXTERN napi_status NAPI_CDECL
napi_create_async_work(napi_env env,
Expand All @@ -176,7 +163,6 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_queue_async_work(napi_env env,
napi_async_work work);
NAPI_EXTERN napi_status NAPI_CDECL napi_cancel_async_work(napi_env env,
napi_async_work work);
#endif // __wasm32__

// version management
NAPI_EXTERN napi_status NAPI_CDECL
Expand Down Expand Up @@ -214,7 +200,6 @@ napi_close_callback_scope(napi_env env, napi_callback_scope scope);

#if NAPI_VERSION >= 4

#if !defined(EMNAPI_UNMODIFIED_UPSTREAM) || (defined(EMNAPI_UNMODIFIED_UPSTREAM) && !defined(__wasm32__))
// Calling into JS from other threads
NAPI_EXTERN napi_status NAPI_CDECL
napi_create_threadsafe_function(napi_env env,
Expand Down Expand Up @@ -248,7 +233,6 @@ napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);

NAPI_EXTERN napi_status NAPI_CDECL
napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
#endif // __wasm32__

#endif // NAPI_VERSION >= 4

Expand Down
3 changes: 3 additions & 0 deletions packages/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ endif()

if(IS_WASM)
set(EMNAPI_FIND_NODE_ADDON_API ON)
if(IS_EMSCRIPTEN)
add_compile_definitions("NAPI_EXTERN=__attribute__((__import_module__(\"env\")))")
endif()
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../emnapi" "${CMAKE_CURRENT_BINARY_DIR}/emnapi")
endif()

Expand Down

0 comments on commit 3bbbe79

Please sign in to comment.