diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..5e6a762 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,63 @@ +name: Node-API Build Matrix + +on: + push: + pull_request: + release: + types: [published] + +permissions: + contents: write # upload release assets + +jobs: + build: + name: ${{ matrix.name }} + runs-on: ${{ matrix.os }} + container: ${{ matrix.container }} + strategy: + fail-fast: false + matrix: + include: + - { name: linux-x64-gnu, os: ubuntu-24.04 } + - { name: linux-arm64-gnu, os: ubuntu-24.04-arm } + - { name: linux-x64-musl, os: ubuntu-24.04, container: "node:22-alpine" } + - { name: darwin-universal, os: macos-latest, cmake: "'-DCMAKE_OSX_ARCHITECTURES=arm64;x86_64' -DCMAKE_OSX_DEPLOYMENT_TARGET=11.0" } + - { name: win32-x64-msvc, os: windows-latest, cmake: "-A x64" } + - { name: win32-ia32-msvc, os: windows-latest, cmake: "-A Win32", notest: true } + - { name: win32-arm64-msvc, os: windows-11-arm, cmake: "-A ARM64" } + - { name: win32-x64-mingw, os: windows-latest, msys2: CLANG64, cmake: "-G Ninja" } + defaults: + run: + shell: ${{ matrix.msys2 && 'msys2 {0}' || 'bash' }} + steps: + - uses: actions/checkout@v5 + - if: matrix.container + shell: sh + run: apk add --no-cache bash cmake make gcc musl-dev tar github-cli + - if: matrix.msys2 + uses: msys2/setup-msys2@v2 + with: + msystem: ${{ matrix.msys2 }} + pacboy: cc:p cmake:p ninja:p + path-type: inherit # makes the node installed by setup-node visible to ctest + - if: ${{ !matrix.container }} + uses: actions/setup-node@v5 + with: { node-version: 22 } + - name: Configure + run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/dist" ${{ matrix.cmake }} + - name: Build + run: cmake --build build --config Release + - name: Test (addons loaded by node) + if: ${{ !matrix.notest }} + run: ctest --test-dir build -C Release --output-on-failure + - name: Install and package + run: | + cmake --install build --config Release + tar -czf node-api-stub-${{ matrix.name }}.tar.gz -C dist . + - uses: actions/upload-artifact@v6 + with: + name: node-api-stub-${{ matrix.name }} + path: node-api-stub-${{ matrix.name }}.tar.gz + - if: github.event_name == 'release' + env: { GH_TOKEN: "${{ github.token }}" } + run: gh release upload "${{ github.event.release.tag_name }}" node-api-stub-${{ matrix.name }}.tar.gz --clobber diff --git a/.github/workflows/node-api.yml b/.github/workflows/node-api.yml deleted file mode 100644 index 645b953..0000000 --- a/.github/workflows/node-api.yml +++ /dev/null @@ -1,151 +0,0 @@ -name: Node-API Build Matrix - -on: [push, pull_request, release] - -jobs: - build: - name: ${{ matrix.config.name }} - runs-on: ${{ matrix.config.os }} - strategy: - fail-fast: false - matrix: - config: - - { - name: "Windows", - os: "windows-latest", - artifact: "libnode_api-windows", - build_type: "Release", - generator: "CodeBlocks - Unix Makefiles", - compiler: { - name: "tdm-gcc", - version: "9.2.0" - }, - archiver: "7z" - } - - { - name: "Ubuntu 20.04", - os: "ubuntu-20.04", - artifact: "libnode_api-ubuntu-20_04", - build_type: "Release", - generator: "Unix Makefiles", - archiver: "7z" - } - - { - name: "Ubuntu 18.04", - os: "ubuntu-18.04", - artifact: "libnode_api-ubuntu-18_04", - build_type: "Release", - generator: "Unix Makefiles", - archiver: "7z" - } - - { - name: "Ubuntu 16.04", - os: "ubuntu-16.04", - artifact: "libnode_api-ubuntu-16_04", - build_type: "Release", - generator: "Unix Makefiles", - archiver: "7z" - } - - { - name: "macOS", - os: "macos-latest", - artifact: "libnode_api-macOS", - build_type: "Release", - generator: "Unix Makefiles", - archiver: "7z" - } - steps: - - uses: actions/checkout@v2 - - name: Print environment - run: | - echo "Name: ${{ matrix.config.name }}" - echo "OS: ${{ matrix.config.os }}" - echo "Generator: ${{ matrix.config.generator }}" - echo "Archiver: ${{ matrix.config.archiver }}" - echo "Artifact: ${{ matrix.config.artifact }}" - - name: Install dependencies on Windows - if: startsWith(matrix.config.os, 'windows') - run: | - echo "Install for windows ..." - choco install cmake - cmake --version - make --version - choco install wget - wget --version - choco install 7zip.install - ${{ matrix.config.archiver }} - - name: Install compiler on Windows - if: startsWith(matrix.config.os, 'windows') - shell: bash - run: | - wget -O ${{ matrix.config.compiler.name }}.zip https://github.com/napi-bindings/tdm-gcc/archive/v${{ matrix.config.compiler.version }}.zip - ${{ matrix.config.archiver }} x tdm-gcc.zip -o/ - - name: Install dependencies on Linux - if: startsWith(matrix.config.os, 'ubuntu') - run: | - echo "Install for Linux ..." - sudo apt-get update - sudo apt-get install build-essential -y - sudo apt-get install cmake -y - sudo apt-get install p7zip-full -y - gcc --version - cmake --version - make --version - - name: Install dependencies on macOS - if: startsWith(matrix.config.os, 'macos') - run: | - echo "Install for macOs ..." - brew install p7zip cmake - cmake --version - make --version - echo "Set MACOSX_DEPLOYMENT_TARGET to 10.10" - export MACOSX_DEPLOYMENT_TARGET=10.10 - - name: Configure on Unix system - if: startsWith(matrix.config.os, 'macos') || startsWith(matrix.config.os, 'ubuntu') - shell: bash - run: | - mkdir ./build - mkdir ./node_api - cmake \ - -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \ - -G "${{ matrix.config.generator }}" \ - -S ./ \ - -B ./build - - name: Configure on Windows - if: startsWith(matrix.config.os, 'windows') - shell: bash - run: | - mkdir ./build - mkdir ./node_api - cmake \ - -DCMAKE_MAKE_PROGRAM=/${{ matrix.config.compiler.name }}-${{ matrix.config.compiler.version }}/bin/mingw32-make.exe \ - -DCMAKE_C_COMPILER=/${{ matrix.config.compiler.name }}-${{ matrix.config.compiler.version }}/bin/gcc.exe \ - -DCMAKE_CXX_COMPILER=/${{ matrix.config.compiler.name }}-${{ matrix.config.compiler.version }}/bin/g++.exe \ - -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} \ - -G "${{ matrix.config.generator }}" \ - -S ./ \ - -B ./build - - name: Build - shell: bash - run: cmake --build ./build --config ${{ matrix.config.build_type }} - - name: "Prepare" - shell: bash - run: | - cp ./build/libnode_api.a ./node_api/libnode_api.a - cd ./node_api - ${{ matrix.config.archiver }} a -tzip ../${{ matrix.config.artifact }}.zip . - - name: Upload artifact - uses: actions/upload-artifact@v2 - with: - path: ./${{ matrix.config.artifact }}.zip - name: ${{ matrix.config.artifact }} - - name: Release - if: github.event_name == 'release' && (github.event.action == 'published') - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ github.event.release.upload_url }} - asset_path: ./${{ matrix.config.artifact }}.zip - asset_name: ${{ matrix.config.artifact }}.zip - asset_content_type: application/zip \ No newline at end of file diff --git a/.gitignore b/.gitignore index d76b74e..d1d33a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ -build +build/ +build-*/ +dist/ +.tmp-node-api-headers/ .DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md index f9fa600..4728091 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,32 @@ # node-api-stub Changelog +## Unreleased Version 10.0.0 + +### Notable changes + +- Updated to Node-API version 10. Headers and the `.def` files are vendored + unmodified from `node-api-headers` 1.9.0 (155 symbols); 9 symbols were missing + in 8.0.0. +- New `NodeApi::NodeApi` CMake target that applies the right link mechanism for + each platform: import library for `NODE.EXE` with a delay-load hook on + Windows (MSVC and MinGW), `-U` for each Node-API symbol on macOS, nothing on + ELF platforms. +- The project can be consumed with `FetchContent`, `add_subdirectory` and + `find_package(NodeApi)` (exported targets and package config files). +- The stub source is generated from the `.def` file. Static and shared stub + libraries are optional and abort with a message when called, instead of + returning `napi_ok` with uninitialised results. +- New tests that load the addons in Node.js, including the `-Bsymbolic` case. +- New build matrix: Linux x64/arm64 (glibc and musl), macOS universal, Windows + x64/x86/arm64 (MSVC) and x64 (MinGW). + +### Breaking changes + +- Headers moved from the repository root to `include/`; `node_api.c` removed. +- The `node_api` target and `libnode_api.a` are replaced by `NodeApi::NodeApi` + and, for the static stub, `NodeApi::stub` / `libnode_api_stub.a` + (`NodeApi::legacy` is an alias of it). + ## 2021-03-27 Version 8.0.0, @NickNaso ### Notable changes diff --git a/CMakeLists.txt b/CMakeLists.txt index 95b9a70..b709e89 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,160 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.16...3.31) -project(libnode_api LANGUAGES C) +# Major version = latest Node-API version covered (same scheme as 7.0.0 and 8.0.0). +project(node-api-stub VERSION 10.0.0 LANGUAGES C + DESCRIPTION "Node-API headers, link-time stubs and import libraries") +file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/def/NODE_API_HEADERS_VERSION" NODE_API_HEADERS_VERSION LIMIT_COUNT 1) -add_library(node_api STATIC node_api.c) -install(TARGETS node_api RUNTIME) +if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) + set(NODE_API_IS_TOP_LEVEL ON) +else() + set(NODE_API_IS_TOP_LEVEL OFF) +endif() + +# --- Options ----------------------------------------------------------------- +# Stub libraries are only useful on ELF platforms. On Windows and macOS a stub +# with function bodies ends up being called instead of Node-API (see README), +# so they are off by default there. +if(WIN32 OR APPLE) + set(_stub_default OFF) +else() + set(_stub_default ON) +endif() + +set(NODE_API_VERSION "8" CACHE STRING + "NAPI_VERSION defined for consumers (1-10 or 'experimental'); empty = do not define it") +option(NODE_API_BUILD_STUB_STATIC "Build the static stub (libnode_api_stub.a)" ${_stub_default}) +option(NODE_API_BUILD_STUB_SHARED "Build the shared stub (libnode_api_stub.so/.dylib/.dll)" ${_stub_default}) +option(NODE_API_ELF_ALLOW_UNDEFINED "ELF: add -z undefs to consumers (overrides -z defs / --no-undefined)" ON) +set(NODE_API_MACOS_UNDEFINED "symbols" CACHE STRING + "macOS: 'symbols' (-U for each Node-API symbol) or 'dynamic_lookup'") +set_property(CACHE NODE_API_MACOS_UNDEFINED PROPERTY STRINGS symbols dynamic_lookup) +option(NODE_API_WIN_DELAY_LOAD "Windows/MSVC: delay-load node.exe and redirect it to the host executable (Electron, renamed node)" ON) +option(NODE_API_INSTALL "Generate install/export rules" ${NODE_API_IS_TOP_LEVEL}) +option(NODE_API_BUILD_TESTS "Build tests (addon loaded by node)" ${NODE_API_IS_TOP_LEVEL}) + +include(GNUInstallDirs) +include(cmake/NodeApiStub.cmake) + +node_api_read_symbols("${CMAKE_CURRENT_SOURCE_DIR}/def/node_api.def" NODE_API_SYMBOLS) +list(LENGTH NODE_API_SYMBOLS _n) +message(STATUS "node-api-stub ${PROJECT_VERSION} (node-api-headers ${NODE_API_HEADERS_VERSION}): ${_n} Node-API symbols") + +set(_gen "${CMAKE_CURRENT_BINARY_DIR}/generated") + +# --- NodeApi::headers -------------------------------------------------------- +add_library(node_api_headers INTERFACE) +add_library(NodeApi::headers ALIAS node_api_headers) +set_target_properties(node_api_headers PROPERTIES EXPORT_NAME headers) +target_include_directories(node_api_headers INTERFACE + $ + $) +if(NODE_API_VERSION STREQUAL "experimental") + target_compile_definitions(node_api_headers INTERFACE NAPI_EXPERIMENTAL) +elseif(NOT NODE_API_VERSION STREQUAL "") + target_compile_definitions(node_api_headers INTERFACE NAPI_VERSION=${NODE_API_VERSION}) +endif() +# On Windows this turns NAPI_EXTERN into __declspec(dllimport), which is what +# the import library expects. +target_compile_definitions(node_api_headers INTERFACE BUILDING_NODE_EXTENSION) + +# --- NodeApi::NodeApi: the target consumers should use --------------------- +# Picks the right link mechanism for the platform; no stub is linked. +add_library(node_api INTERFACE) +add_library(NodeApi::NodeApi ALIAS node_api) +set_target_properties(node_api PROPERTIES EXPORT_NAME NodeApi) +target_link_libraries(node_api INTERFACE node_api_headers) + +set(_install_files "") +if(WIN32) + # Import library for NODE.EXE generated from the official .def file. + node_api_generate_import_library("${CMAKE_CURRENT_SOURCE_DIR}/def/node_api.def" "${_gen}" _implib) + get_filename_component(_implib_name "${_implib}" NAME) + target_link_libraries(node_api INTERFACE + $ + $/${CMAKE_INSTALL_LIBDIR}/${_implib_name}>) + list(APPEND _install_files "${_implib}") + if(NODE_API_WIN_DELAY_LOAD AND MSVC) + target_link_options(node_api INTERFACE "LINKER:/DELAYLOAD:node.exe") + target_link_libraries(node_api INTERFACE delayimp) + target_sources(node_api INTERFACE + $ + $/${CMAKE_INSTALL_DATADIR}/node-api-stub/win_delay_load_hook.c>) + endif() +elseif(APPLE) + if(NODE_API_MACOS_UNDEFINED STREQUAL "dynamic_lookup") + target_link_options(node_api INTERFACE "LINKER:-undefined,dynamic_lookup") + else() + # Only Node-API symbols may stay undefined: every other link error is still an error. + foreach(_s IN LISTS NODE_API_SYMBOLS) + target_link_options(node_api INTERFACE "LINKER:-U,_${_s}") + endforeach() + endif() +else() + # ELF: undefined symbols are allowed in shared objects by default. + # -z undefs is only needed when the toolchain enforces -z defs + # (e.g. Android NDK, Meson b_lundef). + if(NODE_API_ELF_ALLOW_UNDEFINED) + target_link_options(node_api INTERFACE "LINKER:-z,undefs") + endif() +endif() + +# --- Stub libraries (optional) ----------------------------------------------- +node_api_generate_stub_source("${_gen}/node_api_stub.c" "${NODE_API_SYMBOLS}") +set(_stub_targets "") +if(NODE_API_BUILD_STUB_STATIC) + add_library(node_api_stub STATIC "${_gen}/node_api_stub.c") + add_library(NodeApi::stub ALIAS node_api_stub) + set_target_properties(node_api_stub PROPERTIES EXPORT_NAME stub POSITION_INDEPENDENT_CODE ON) + target_compile_definitions(node_api_stub PRIVATE NODE_API_STUB_STATIC) + target_link_libraries(node_api_stub INTERFACE node_api_headers) + list(APPEND _stub_targets node_api_stub) +endif() +if(NODE_API_BUILD_STUB_SHARED) + add_library(node_api_stub_shared SHARED "${_gen}/node_api_stub.c") + add_library(NodeApi::stub_shared ALIAS node_api_stub_shared) + set_target_properties(node_api_stub_shared PROPERTIES EXPORT_NAME stub_shared + OUTPUT_NAME node_api_stub C_VISIBILITY_PRESET hidden) + target_link_libraries(node_api_stub_shared INTERFACE node_api_headers) + list(APPEND _stub_targets node_api_stub_shared) +endif() +# As a dependency (add_subdirectory/FetchContent) the stubs are built only if +# something links them. +if(NOT NODE_API_IS_TOP_LEVEL AND _stub_targets) + set_target_properties(${_stub_targets} PROPERTIES EXCLUDE_FROM_ALL ON) +endif() +# Migration aid for projects that linked the old `node_api` target (libnode_api.a). +if(TARGET node_api_stub) + add_library(NodeApi::legacy ALIAS node_api_stub) +endif() + +# --- Install / export -------------------------------------------------------- +if(NODE_API_INSTALL) + include(CMakePackageConfigHelpers) + install(TARGETS node_api_headers node_api ${_stub_targets} EXPORT NodeApiTargets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/node-api) + install(FILES def/node_api.def def/js_native_api.def def/LICENSE-node-api-headers + DESTINATION ${CMAKE_INSTALL_DATADIR}/node-api-stub) + install(FILES src/win_delay_load_hook.c DESTINATION ${CMAKE_INSTALL_DATADIR}/node-api-stub) + if(_install_files) + install(FILES ${_install_files} DESTINATION ${CMAKE_INSTALL_LIBDIR}) + endif() + install(EXPORT NodeApiTargets NAMESPACE NodeApi:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/NodeApi) + configure_package_config_file(cmake/NodeApiConfig.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/NodeApiConfig.cmake" + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/NodeApi) + write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/NodeApiConfigVersion.cmake" + COMPATIBILITY SameMajorVersion) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/NodeApiConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/NodeApiConfigVersion.cmake" + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/NodeApi) +endif() + +if(NODE_API_BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() diff --git a/README.md b/README.md index 622f698..ecd3c47 100644 --- a/README.md +++ b/README.md @@ -3,127 +3,159 @@ ![Node-API Build Matrix](https://github.com/napi-bindings/node-api-stub/workflows/Node-API%20Build%20Matrix/badge.svg) - [Introduction](#introduction) -- [Building](#building) +- [How it works](#how-it-works) +- [Using it from CMake](#using-it-from-cmake) +- [Targets and options](#targets-and-options) +- [Stub libraries](#stub-libraries) +- [Building and testing](#building-and-testing) +- [Updating the Node-API headers](#updating-the-node-api-headers) +- [Migrating from 8.x](#migrating-from-8x) - [Code of conduct](CODE_OF_CONDUCT.md) - [Changelog](CHANGELOG.md) -- [Team](#team) -- [Acknowledgments](#acknowledgements) +- [Team](#the-team) +- [Acknowledgements](#acknowledgements) - [License](#license) ## Introduction -This repository contains the stub for Node-API used to avoid linker error on -building native addon for Node.js using programming languages different from -C and C++. - -## Building - -The main purpose of this reposistory is to build Node-API static library that -you could include on your project. On release you can find the build for the -following operating systems: - -- **Windows** -- **Ubuntu 20.04** -- **Ubuntu 18.04** -- **Ubuntu 16.04** -- **macOS** - -If you want to build the Node-API library on your own the first step is to -clone this repository: - -`> git clone https://github.com/napi-bindings/node-api-stub.git` - -### Building on Windows - -You will need to install **GCC** for Windows and **CMake**. - -#### Install GCC compiler - -- Download the right version for your system from this **[link](https://jmeubank.github.io/tdm-gcc/download/)**. -- Be sure that **gcc** in in your path of execution. - -#### Install CMake - -- Download the right version for your system from this **[link](https://cmake.org/download/)**. -- Be sure that **cmake** is in your path of execution. - -#### Configure and build - -Open your shell and execute the commands reported below: - +A native addon for Node.js calls the Node-API functions (`napi_*`, `node_api_*`) +exported by the host process (`node`, `node.exe`, Electron, ...). When the addon +is built, those symbols do not exist yet, and some toolchains refuse to link: +Windows always needs an import library, macOS rejects undefined symbols by +default, and on Linux some build systems add `-z defs`. + +This project gives CMake projects (and any other build system) everything they +need to link an addon on every platform, without Node.js installed: + +- the official Node-API headers, vendored from + [node-api-headers](https://github.com/nodejs/node-api-headers) (Node-API 10); +- a `NodeApi::NodeApi` target that applies the right link mechanism for the + platform; +- optional static and shared stub libraries for the cases that need them. + +## How it works + +The addon must always end up calling the Node-API functions of the host +process. A library with function bodies linked into the addon can be called +instead of them, so the right mechanism depends on the platform: + +| Platform | What `NodeApi::NodeApi` does | +|---|---| +| Windows, MSVC / clang-cl (x64, x86, arm64) | links `node_api.lib`, an import library for `NODE.EXE` generated from the official `.def` file, plus `/DELAYLOAD:node.exe` and a delay-load hook so that the addon also loads in hosts with another name (Electron, a renamed `node.exe`) | +| Windows, MinGW / llvm-mingw | links `libnode_api.dll.a`, generated with `dlltool` | +| macOS | `-Wl,-U,_` for each Node-API symbol: only those may stay undefined (or `-undefined dynamic_lookup`, see options) | +| Linux, BSD, Android (ELF) | nothing is linked; adds `-z undefs` so that `-z defs` does not reject the Node-API symbols | + +The import libraries are generated at configure time, so the project builds +from source anywhere a C compiler is available; prebuilt archives are also +attached to each release. + +> **Why not a stub everywhere?** A stub linked into the addon is called instead +> of Node-API when the linker binds the calls inside the addon: always on +> Windows and macOS, and on Linux when the addon is linked with `-Bsymbolic`. +> `go build -buildmode=c-shared` always passes `-Bsymbolic`, so a Go addon +> linked with the static stub calls the empty functions. The tests in +> [`tests/`](tests/CMakeLists.txt) reproduce this case. + +## Using it from CMake + +```cmake +# A) from source (recommended) +include(FetchContent) +FetchContent_Declare(node_api_stub + GIT_REPOSITORY https://github.com/napi-bindings/node-api-stub.git + GIT_TAG v10.0.0) +FetchContent_MakeAvailable(node_api_stub) + +# B) from an installed package or a release archive +# find_package(NodeApi 10 REQUIRED CONFIG) + +add_library(my_addon MODULE addon.c) +set_target_properties(my_addon PROPERTIES PREFIX "" SUFFIX ".node") +target_link_libraries(my_addon PRIVATE NodeApi::NodeApi) ``` -mkdir ./build -cmake \ --DCMAKE_MAKE_PROGRAM=mingw32-make.exe \ --DCMAKE_C_COMPILER=gcc.exe \ --DCMAKE_CXX_COMPILER=g++.exe \ --DCMAKE_BUILD_TYPE=Release \ --G "CodeBlocks - Unix Makefiles" \ --S ./ \ --B ./build -cmake --build ./build -``` - -`-DCMAKE_BUILD_TYPE` could be set with one of the following two values: - -- Release -- Debug - -### Building on Unix-like platform - -You will need to install **C / C++** compiler and **CMake**. - -#### Prerequisistes to build on Linux - -These steps were tested on: -- **Ubuntu 16.04** -- **Ubuntu 18.04** -- **Ubuntu 20.04** - -First, you will need to install dependencies: +`add_subdirectory(node-api-stub)` works the same way as `FetchContent`. + +## Targets and options + +| Target | Content | +|---|---| +| `NodeApi::NodeApi` | **use this one.** Headers, `NAPI_VERSION`, `BUILDING_NODE_EXTENSION` and the platform link mechanism | +| `NodeApi::headers` | headers and compile definitions only | +| `NodeApi::stub` | static stub, position-independent | +| `NodeApi::stub_shared` | shared stub | +| `NodeApi::legacy` | alias of `NodeApi::stub`, for projects that used the old `node_api` target | + +| Option | Default | Effect | +|---|---|---| +| `NODE_API_VERSION` | `8` | `NAPI_VERSION` defined for consumers; `experimental` defines `NAPI_EXPERIMENTAL`; empty to leave it to the consumer | +| `NODE_API_BUILD_STUB_STATIC` | ON on ELF, OFF on Windows/macOS | build `NodeApi::stub` | +| `NODE_API_BUILD_STUB_SHARED` | ON on ELF, OFF on Windows/macOS | build `NodeApi::stub_shared` | +| `NODE_API_ELF_ALLOW_UNDEFINED` | ON | add `-z undefs` on ELF | +| `NODE_API_MACOS_UNDEFINED` | `symbols` | `symbols` (`-U` per Node-API symbol) or `dynamic_lookup` | +| `NODE_API_WIN_DELAY_LOAD` | ON | MSVC: delay-load `node.exe` and redirect it to the host executable | +| `NODE_API_INSTALL` | ON when top level | install and export rules | +| `NODE_API_BUILD_TESTS` | ON when top level | tests that load the addons in Node.js | + +When the project is used as a dependency, the stub libraries are excluded from +`all` and are built only if a target links them. + +## Stub libraries + +The stubs are generated from `def/node_api.def`: each function prints its own +name and calls `abort()`. They must never run; if they do, the addon fails +loudly instead of carrying on with uninitialised values. + +- **Static (`libnode_api_stub.a`)**: for toolchains that need an archive to + link and do not accept linker options. On ELF it works only through symbol + interposition: do not use it with `-Bsymbolic`, and therefore not with Go + `-buildmode=c-shared`. +- **Shared (`libnode_api_stub.so`)**: correct even with `-Bsymbolic`, but the + addon records a `DT_NEEDED` entry for it: remove it after linking + (`patchelf --remove-needed libnode_api_stub.so addon.node`) or ship the + library next to the addon. +- **Windows and macOS**: not recommended; built only on explicit request. + +Other options that avoid the link problem entirely are resolving the Node-API +functions at run time (`dlsym(RTLD_DEFAULT, ...)` / `GetProcAddress`), as +[napi-rs](https://napi.rs) does, or declaring them as weak references on ELF +(`#pragma weak napi_create_object`). + +## Building and testing + +You need CMake 3.16 or later and a C compiler. Node.js is optional and is used +only by the tests, which load the test addons and check that their calls reach +the real Node-API. ``` -sudo apt-get update -sudo apt-get install build-essential -y -sudo apt-get install cmake -y +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release +cmake --build build --config Release +ctest --test-dir build -C Release --output-on-failure +cmake --install build --config Release --prefix ./dist ``` -#### Prerequisites to build on macOS - -These steps were tested on macOS Catalina (10.15). - -First, you will need to install dependencies: -- Install [Xcode](https://developer.apple.com/xcode/download/) - - You also need to install the `XCode Command Line Tools` by running - `xcode-select --install`. Alternatively, if you already have the full Xcode - installed, you can find them under the menu - `Xcode -> Open Developer Tool -> More Developer Tools...`. This step will - install `clang`, `clang++`, and `make`. -- Install [Homebrew](https://brew.sh/) -- Install and `cmake` running the following command: -``` -brew install cmake -``` +On Windows use the Visual Studio generator (`-A x64`, `-A Win32`, `-A ARM64`) +or MinGW / llvm-mingw with `-G Ninja`. -#### Configure and build +## Updating the Node-API headers -Open your shell and execute the commands reported below: +`include/` and `def/` come unmodified from the +[node-api-headers](https://www.npmjs.com/package/node-api-headers) npm package. +To update them (Node.js and npm are not needed): ``` -mkdir ./build -cmake \ --DCMAKE_BUILD_TYPE=Release \ --G "Unix Makefiles" \ --S ./ \ --B ./build -cmake --build ./build +cmake -DVERSION=1.9.0 -P scripts/update-node-api-headers.cmake ``` -`-DCMAKE_BUILD_TYPE` could be set with one of the following two values: +## Migrating from 8.x -- Release -- Debug +- The headers moved from the repository root to `include/`; `node_api.c` is + now generated. +- Replace the `node_api` target with `NodeApi::NodeApi`. If you really need the + old static archive, link `NodeApi::legacy` (on ELF only). +- The static library is now called `libnode_api_stub.a`, and its functions + abort instead of returning `napi_ok`. ## The Team @@ -141,4 +173,8 @@ Thank you to all people that encourage me every day. ## License -Licensed under [Apache license V2](./LICENSE) \ No newline at end of file +Licensed under [Apache license V2](./LICENSE). + +The files in `include/` and `def/` come from +[node-api-headers](https://github.com/nodejs/node-api-headers) and are +licensed under the MIT license (see `def/LICENSE-node-api-headers`). diff --git a/cmake/NodeApiConfig.cmake.in b/cmake/NodeApiConfig.cmake.in new file mode 100644 index 0000000..264d84f --- /dev/null +++ b/cmake/NodeApiConfig.cmake.in @@ -0,0 +1,13 @@ +@PACKAGE_INIT@ + +# Exported targets: +# NodeApi::NodeApi headers + the right link mechanism for the platform (recommended) +# NodeApi::headers Node-API headers only +# NodeApi::stub static stub (if built) +# NodeApi::stub_shared shared stub (if built) +include("${CMAKE_CURRENT_LIST_DIR}/NodeApiTargets.cmake") + +set(NodeApi_HEADERS_VERSION "@NODE_API_HEADERS_VERSION@") +set(NodeApi_NAPI_VERSION "@NODE_API_VERSION@") + +check_required_components(NodeApi) diff --git a/cmake/NodeApiStub.cmake b/cmake/NodeApiStub.cmake new file mode 100644 index 0000000..987070e --- /dev/null +++ b/cmake/NodeApiStub.cmake @@ -0,0 +1,113 @@ +# Helper functions for node-api-stub. +# Single source of truth for symbols: def/node_api.def from node-api-headers +# (the union of every Node-API version). + +# Reads the exported names from a .def file (skips NAME/EXPORTS/comments). +function(node_api_read_symbols def_file out_var) + file(STRINGS "${def_file}" _lines) + set(_syms "") + foreach(_l IN LISTS _lines) + string(STRIP "${_l}" _l) + if(_l STREQUAL "" OR _l MATCHES "^(NAME|LIBRARY|EXPORTS)" OR _l MATCHES "^;") + continue() + endif() + list(APPEND _syms "${_l}") + endforeach() + list(REMOVE_DUPLICATES _syms) + set(${out_var} "${_syms}" PARENT_SCOPE) +endfunction() + +# Generates the stub source: every function prints its own name and aborts. +# The headers are not included on purpose: the linker only needs the names +# (C has no name mangling) and a stub must never run. If it does, the failure +# has to be loud instead of returning napi_ok with uninitialised results. +function(node_api_generate_stub_source out_file symbols) + set(_body "") + foreach(_s IN LISTS symbols) + string(APPEND _body "NODE_API_STUB(${_s})\n") + endforeach() + set(_content [=[/* Generated by node-api-stub. Do not edit. */ +#include +#include + +#if defined(_WIN32) && !defined(NODE_API_STUB_STATIC) +# define NODE_API_STUB_EXPORT __declspec(dllexport) +#elif defined(_WIN32) +# define NODE_API_STUB_EXPORT +#else +/* Default visibility: with the static stub on ELF the reference stays + interposable, so the dynamic loader picks node's definition (unless the + addon is linked with -Bsymbolic). */ +# define NODE_API_STUB_EXPORT __attribute__((visibility("default"))) +#endif + +static void node_api_stub_trap(const char* name) { + fprintf(stderr, + "node-api-stub: '%s' was called. The addon is bound to the link-time " + "stub instead of the host's Node-API (static stub with -Bsymbolic, " + "macOS or Windows?).\n", + name); + fflush(stderr); + abort(); +} + +#define NODE_API_STUB(name) \ + NODE_API_STUB_EXPORT int name(void) { node_api_stub_trap(#name); return 9; } + +]=]) + string(APPEND _content "${_body}") + # Rewrite the file only when it changes, so reconfiguring does not force a rebuild. + file(WRITE "${out_file}.tmp" "${_content}") + configure_file("${out_file}.tmp" "${out_file}" COPYONLY) +endfunction() + +# Generates (at configure time) the NODE.EXE import library for the current toolchain: +# MSVC / clang-cl -> node_api.lib (lib.exe or llvm-lib) +# MinGW (GNU/LLVM) -> libnode_api.dll.a (dlltool or llvm-dlltool) +function(node_api_generate_import_library def_file out_dir out_var) + file(MAKE_DIRECTORY "${out_dir}") + if(MSVC OR CMAKE_C_SIMULATE_ID STREQUAL "MSVC") + set(_arch "${CMAKE_C_COMPILER_ARCHITECTURE_ID}") + string(TOUPPER "${_arch}" _arch) + if(_arch STREQUAL "X64" OR _arch STREQUAL "AMD64") + set(_machine X64) + elseif(_arch STREQUAL "X86") + set(_machine X86) + elseif(_arch STREQUAL "ARM64") + set(_machine ARM64) + else() + message(FATAL_ERROR "node-api-stub: unsupported MSVC architecture '${_arch}'") + endif() + set(_out "${out_dir}/node_api.lib") + execute_process( + COMMAND "${CMAKE_AR}" /nologo "/def:${def_file}" "/out:${_out}" "/machine:${_machine}" /name:node.exe + RESULT_VARIABLE _rc OUTPUT_VARIABLE _o ERROR_VARIABLE _e) + else() + if(CMAKE_DLLTOOL) + set(_dlltool "${CMAKE_DLLTOOL}") + else() + find_program(_dlltool NAMES llvm-dlltool dlltool x86_64-w64-mingw32-dlltool) + if(NOT _dlltool) + message(FATAL_ERROR "node-api-stub: dlltool not found; set CMAKE_DLLTOOL") + endif() + endif() + string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" _p) + if(_p MATCHES "^(x86_64|amd64)$" OR (CMAKE_SIZEOF_VOID_P EQUAL 8 AND _p MATCHES "86")) + set(_machine i386:x86-64) + elseif(_p MATCHES "^(aarch64|arm64)$") + set(_machine arm64) + elseif(_p MATCHES "^(i.86|x86)$") + set(_machine i386) + else() + message(FATAL_ERROR "node-api-stub: unsupported MinGW processor '${CMAKE_SYSTEM_PROCESSOR}'") + endif() + set(_out "${out_dir}/libnode_api.dll.a") + execute_process( + COMMAND "${_dlltool}" -d "${def_file}" -l "${_out}" -D node.exe -m ${_machine} + RESULT_VARIABLE _rc OUTPUT_VARIABLE _o ERROR_VARIABLE _e) + endif() + if(NOT _rc EQUAL 0) + message(FATAL_ERROR "node-api-stub: import library generation failed (${_rc})\n${_o}\n${_e}") + endif() + set(${out_var} "${_out}" PARENT_SCOPE) +endfunction() diff --git a/def/LICENSE-node-api-headers b/def/LICENSE-node-api-headers new file mode 100644 index 0000000..a32c171 --- /dev/null +++ b/def/LICENSE-node-api-headers @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Node.js + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/def/NODE_API_HEADERS_VERSION b/def/NODE_API_HEADERS_VERSION new file mode 100644 index 0000000..f8e233b --- /dev/null +++ b/def/NODE_API_HEADERS_VERSION @@ -0,0 +1 @@ +1.9.0 diff --git a/def/js_native_api.def b/def/js_native_api.def new file mode 100644 index 0000000..99a5422 --- /dev/null +++ b/def/js_native_api.def @@ -0,0 +1,125 @@ +NAME NODE.EXE +EXPORTS +napi_adjust_external_memory +napi_call_function +napi_close_escapable_handle_scope +napi_close_handle_scope +napi_coerce_to_bool +napi_coerce_to_number +napi_coerce_to_object +napi_coerce_to_string +napi_create_array +napi_create_array_with_length +napi_create_arraybuffer +napi_create_dataview +napi_create_double +napi_create_error +napi_create_external +napi_create_external_arraybuffer +napi_create_function +napi_create_int32 +napi_create_int64 +napi_create_object +napi_create_promise +napi_create_range_error +napi_create_reference +napi_create_string_latin1 +napi_create_string_utf16 +napi_create_string_utf8 +napi_create_symbol +napi_create_type_error +napi_create_typedarray +napi_create_uint32 +napi_define_class +napi_define_properties +napi_delete_element +napi_delete_property +napi_delete_reference +napi_escape_handle +napi_get_and_clear_last_exception +napi_get_array_length +napi_get_arraybuffer_info +napi_get_boolean +napi_get_cb_info +napi_get_dataview_info +napi_get_element +napi_get_global +napi_get_last_error_info +napi_get_named_property +napi_get_new_target +napi_get_null +napi_get_property +napi_get_property_names +napi_get_prototype +napi_get_reference_value +napi_get_typedarray_info +napi_get_undefined +napi_get_value_bool +napi_get_value_double +napi_get_value_external +napi_get_value_int32 +napi_get_value_int64 +napi_get_value_string_latin1 +napi_get_value_string_utf16 +napi_get_value_string_utf8 +napi_get_value_uint32 +napi_get_version +napi_has_element +napi_has_named_property +napi_has_own_property +napi_has_property +napi_instanceof +napi_is_array +napi_is_arraybuffer +napi_is_dataview +napi_is_error +napi_is_exception_pending +napi_is_promise +napi_is_typedarray +napi_new_instance +napi_open_escapable_handle_scope +napi_open_handle_scope +napi_reference_ref +napi_reference_unref +napi_reject_deferred +napi_remove_wrap +napi_resolve_deferred +napi_run_script +napi_set_element +napi_set_named_property +napi_set_property +napi_strict_equals +napi_throw +napi_throw_error +napi_throw_range_error +napi_throw_type_error +napi_typeof +napi_unwrap +napi_wrap +napi_add_finalizer +napi_create_date +napi_get_date_value +napi_is_date +napi_create_bigint_int64 +napi_create_bigint_uint64 +napi_create_bigint_words +napi_get_all_property_names +napi_get_instance_data +napi_get_value_bigint_int64 +napi_get_value_bigint_uint64 +napi_get_value_bigint_words +napi_set_instance_data +napi_detach_arraybuffer +napi_is_detached_arraybuffer +napi_check_object_type_tag +napi_object_freeze +napi_object_seal +napi_type_tag_object +node_api_create_syntax_error +node_api_symbol_for +node_api_throw_syntax_error +node_api_create_external_string_latin1 +node_api_create_external_string_utf16 +node_api_create_property_key_latin1 +node_api_create_property_key_utf16 +node_api_create_property_key_utf8 \ No newline at end of file diff --git a/def/node_api.def b/def/node_api.def new file mode 100644 index 0000000..1c53ce0 --- /dev/null +++ b/def/node_api.def @@ -0,0 +1,157 @@ +NAME NODE.EXE +EXPORTS +napi_async_destroy +napi_async_init +napi_cancel_async_work +napi_create_async_work +napi_create_buffer +napi_create_buffer_copy +napi_create_external_buffer +napi_delete_async_work +napi_fatal_error +napi_get_buffer_info +napi_get_node_version +napi_is_buffer +napi_make_callback +napi_module_register +napi_queue_async_work +napi_adjust_external_memory +napi_call_function +napi_close_escapable_handle_scope +napi_close_handle_scope +napi_coerce_to_bool +napi_coerce_to_number +napi_coerce_to_object +napi_coerce_to_string +napi_create_array +napi_create_array_with_length +napi_create_arraybuffer +napi_create_dataview +napi_create_double +napi_create_error +napi_create_external +napi_create_external_arraybuffer +napi_create_function +napi_create_int32 +napi_create_int64 +napi_create_object +napi_create_promise +napi_create_range_error +napi_create_reference +napi_create_string_latin1 +napi_create_string_utf16 +napi_create_string_utf8 +napi_create_symbol +napi_create_type_error +napi_create_typedarray +napi_create_uint32 +napi_define_class +napi_define_properties +napi_delete_element +napi_delete_property +napi_delete_reference +napi_escape_handle +napi_get_and_clear_last_exception +napi_get_array_length +napi_get_arraybuffer_info +napi_get_boolean +napi_get_cb_info +napi_get_dataview_info +napi_get_element +napi_get_global +napi_get_last_error_info +napi_get_named_property +napi_get_new_target +napi_get_null +napi_get_property +napi_get_property_names +napi_get_prototype +napi_get_reference_value +napi_get_typedarray_info +napi_get_undefined +napi_get_value_bool +napi_get_value_double +napi_get_value_external +napi_get_value_int32 +napi_get_value_int64 +napi_get_value_string_latin1 +napi_get_value_string_utf16 +napi_get_value_string_utf8 +napi_get_value_uint32 +napi_get_version +napi_has_element +napi_has_named_property +napi_has_own_property +napi_has_property +napi_instanceof +napi_is_array +napi_is_arraybuffer +napi_is_dataview +napi_is_error +napi_is_exception_pending +napi_is_promise +napi_is_typedarray +napi_new_instance +napi_open_escapable_handle_scope +napi_open_handle_scope +napi_reference_ref +napi_reference_unref +napi_reject_deferred +napi_remove_wrap +napi_resolve_deferred +napi_run_script +napi_set_element +napi_set_named_property +napi_set_property +napi_strict_equals +napi_throw +napi_throw_error +napi_throw_range_error +napi_throw_type_error +napi_typeof +napi_unwrap +napi_wrap +napi_get_uv_event_loop +napi_add_env_cleanup_hook +napi_close_callback_scope +napi_fatal_exception +napi_open_callback_scope +napi_remove_env_cleanup_hook +napi_acquire_threadsafe_function +napi_call_threadsafe_function +napi_create_threadsafe_function +napi_get_threadsafe_function_context +napi_ref_threadsafe_function +napi_release_threadsafe_function +napi_unref_threadsafe_function +napi_add_finalizer +napi_create_date +napi_get_date_value +napi_is_date +napi_create_bigint_int64 +napi_create_bigint_uint64 +napi_create_bigint_words +napi_get_all_property_names +napi_get_instance_data +napi_get_value_bigint_int64 +napi_get_value_bigint_uint64 +napi_get_value_bigint_words +napi_set_instance_data +napi_detach_arraybuffer +napi_is_detached_arraybuffer +napi_add_async_cleanup_hook +napi_remove_async_cleanup_hook +napi_check_object_type_tag +napi_object_freeze +napi_object_seal +napi_type_tag_object +node_api_get_module_file_name +node_api_create_syntax_error +node_api_symbol_for +node_api_throw_syntax_error +node_api_create_buffer_from_arraybuffer +node_api_create_external_string_latin1 +node_api_create_external_string_utf16 +node_api_create_property_key_latin1 +node_api_create_property_key_utf16 +node_api_create_property_key_utf8 \ No newline at end of file diff --git a/include/js_native_api.h b/include/js_native_api.h new file mode 100644 index 0000000..8e91d6c --- /dev/null +++ b/include/js_native_api.h @@ -0,0 +1,591 @@ +#ifndef SRC_JS_NATIVE_API_H_ +#define SRC_JS_NATIVE_API_H_ + +// This file needs to be compatible with C compilers. +#include // NOLINT(modernize-deprecated-headers) +#include // NOLINT(modernize-deprecated-headers) + +#include "js_native_api_types.h" + +// If you need __declspec(dllimport), either include 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(__wasm__) +#define NAPI_EXTERN \ + __attribute__((visibility("default"))) \ + __attribute__((__import_module__("napi"))) +#else +#define NAPI_EXTERN __attribute__((visibility("default"))) +#endif +#endif + +#define NAPI_AUTO_LENGTH SIZE_MAX + +#ifdef __cplusplus +#define EXTERN_C_START extern "C" { +#define EXTERN_C_END } +#else +#define EXTERN_C_START +#define EXTERN_C_END +#endif + +EXTERN_C_START + +NAPI_EXTERN napi_status NAPI_CDECL napi_get_last_error_info( + node_api_basic_env env, const napi_extended_error_info** result); + +// Getters for defined singletons +NAPI_EXTERN napi_status NAPI_CDECL napi_get_undefined(napi_env env, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_null(napi_env env, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_global(napi_env env, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_boolean(napi_env env, + bool value, + napi_value* result); + +// Methods to create Primitive types/Objects +NAPI_EXTERN napi_status NAPI_CDECL napi_create_object(napi_env env, + napi_value* result); + +NAPI_EXTERN napi_status NAPI_CDECL napi_create_array(napi_env env, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_array_with_length(napi_env env, size_t length, napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_double(napi_env env, + double value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_int32(napi_env env, + int32_t value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_uint32(napi_env env, + uint32_t value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_int64(napi_env env, + int64_t value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_string_latin1( + napi_env env, const char* str, size_t length, napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_string_utf8(napi_env env, + const char* str, + size_t length, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_string_utf16(napi_env env, + const char16_t* str, + size_t length, + napi_value* result); +#if NAPI_VERSION >= 10 +NAPI_EXTERN napi_status NAPI_CDECL node_api_create_external_string_latin1( + napi_env env, + char* str, + size_t length, + node_api_basic_finalize finalize_callback, + void* finalize_hint, + napi_value* result, + bool* copied); +NAPI_EXTERN napi_status NAPI_CDECL +node_api_create_external_string_utf16(napi_env env, + char16_t* str, + size_t length, + node_api_basic_finalize finalize_callback, + void* finalize_hint, + napi_value* result, + bool* copied); + +NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_latin1( + napi_env env, const char* str, size_t length, napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf8( + napi_env env, const char* str, size_t length, napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL node_api_create_property_key_utf16( + napi_env env, const char16_t* str, size_t length, napi_value* result); +#endif // NAPI_VERSION >= 10 + +NAPI_EXTERN napi_status NAPI_CDECL napi_create_symbol(napi_env env, + napi_value description, + napi_value* result); +#if NAPI_VERSION >= 9 +NAPI_EXTERN napi_status NAPI_CDECL +node_api_symbol_for(napi_env env, + const char* utf8description, + size_t length, + napi_value* result); +#endif // NAPI_VERSION >= 9 +NAPI_EXTERN napi_status NAPI_CDECL napi_create_function(napi_env env, + const char* utf8name, + size_t length, + napi_callback cb, + void* data, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_error(napi_env env, + napi_value code, + napi_value msg, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_type_error(napi_env env, + napi_value code, + napi_value msg, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_range_error(napi_env env, + napi_value code, + napi_value msg, + napi_value* result); +#if NAPI_VERSION >= 9 +NAPI_EXTERN napi_status NAPI_CDECL node_api_create_syntax_error( + napi_env env, napi_value code, napi_value msg, napi_value* result); +#endif // NAPI_VERSION >= 9 + +// Methods to get the native napi_value from Primitive type +NAPI_EXTERN napi_status NAPI_CDECL napi_typeof(napi_env env, + napi_value value, + napi_valuetype* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_double(napi_env env, + napi_value value, + double* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_int32(napi_env env, + napi_value value, + int32_t* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_uint32(napi_env env, + napi_value value, + uint32_t* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_int64(napi_env env, + napi_value value, + int64_t* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bool(napi_env env, + napi_value value, + bool* result); + +// Copies LATIN-1 encoded bytes from a string into a buffer. +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_string_latin1( + napi_env env, napi_value value, char* buf, size_t bufsize, size_t* result); + +// Copies UTF-8 encoded bytes from a string into a buffer. +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_string_utf8( + napi_env env, napi_value value, char* buf, size_t bufsize, size_t* result); + +// Copies UTF-16 encoded bytes from a string into a buffer. +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_string_utf16(napi_env env, + napi_value value, + char16_t* buf, + size_t bufsize, + size_t* result); + +// Methods to coerce values +// These APIs may execute user scripts +NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_bool(napi_env env, + napi_value value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_number(napi_env env, + napi_value value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_object(napi_env env, + napi_value value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_string(napi_env env, + napi_value value, + napi_value* result); + +// Methods to work with Objects +NAPI_EXTERN napi_status NAPI_CDECL napi_get_prototype(napi_env env, + napi_value object, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_property_names(napi_env env, + napi_value object, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_set_property(napi_env env, + napi_value object, + napi_value key, + napi_value value); +NAPI_EXTERN napi_status NAPI_CDECL napi_has_property(napi_env env, + napi_value object, + napi_value key, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_property(napi_env env, + napi_value object, + napi_value key, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_delete_property(napi_env env, + napi_value object, + napi_value key, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_has_own_property(napi_env env, + napi_value object, + napi_value key, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_set_named_property(napi_env env, + napi_value object, + const char* utf8name, + napi_value value); +NAPI_EXTERN napi_status NAPI_CDECL napi_has_named_property(napi_env env, + napi_value object, + const char* utf8name, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_named_property(napi_env env, + napi_value object, + const char* utf8name, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_set_element(napi_env env, + napi_value object, + uint32_t index, + napi_value value); +NAPI_EXTERN napi_status NAPI_CDECL napi_has_element(napi_env env, + napi_value object, + uint32_t index, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_element(napi_env env, + napi_value object, + uint32_t index, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_delete_element(napi_env env, + napi_value object, + uint32_t index, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_define_properties(napi_env env, + napi_value object, + size_t property_count, + const napi_property_descriptor* properties); + +// Methods to work with Arrays +NAPI_EXTERN napi_status NAPI_CDECL napi_is_array(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_array_length(napi_env env, + napi_value value, + uint32_t* result); + +// Methods to compare values +NAPI_EXTERN napi_status NAPI_CDECL napi_strict_equals(napi_env env, + napi_value lhs, + napi_value rhs, + bool* result); + +// Methods to work with Functions +NAPI_EXTERN napi_status NAPI_CDECL napi_call_function(napi_env env, + napi_value recv, + napi_value func, + size_t argc, + const napi_value* argv, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_new_instance(napi_env env, + napi_value constructor, + size_t argc, + const napi_value* argv, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_instanceof(napi_env env, + napi_value object, + napi_value constructor, + bool* result); + +// Methods to work with napi_callbacks + +// Gets all callback info in a single call. (Ugly, but faster.) +NAPI_EXTERN napi_status NAPI_CDECL napi_get_cb_info( + napi_env env, // [in] Node-API environment handle + napi_callback_info cbinfo, // [in] Opaque callback-info handle + size_t* argc, // [in-out] Specifies the size of the provided argv array + // and receives the actual count of args. + napi_value* argv, // [out] Array of values + napi_value* this_arg, // [out] Receives the JS 'this' arg for the call + void** data); // [out] Receives the data pointer for the callback. + +NAPI_EXTERN napi_status NAPI_CDECL napi_get_new_target( + napi_env env, napi_callback_info cbinfo, napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_define_class(napi_env env, + const char* utf8name, + size_t length, + napi_callback constructor, + void* data, + size_t property_count, + const napi_property_descriptor* properties, + napi_value* result); + +// Methods to work with external data objects +NAPI_EXTERN napi_status NAPI_CDECL +napi_wrap(napi_env env, + napi_value js_object, + void* native_object, + node_api_basic_finalize finalize_cb, + void* finalize_hint, + napi_ref* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_unwrap(napi_env env, + napi_value js_object, + void** result); +NAPI_EXTERN napi_status NAPI_CDECL napi_remove_wrap(napi_env env, + napi_value js_object, + void** result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_external(napi_env env, + void* data, + node_api_basic_finalize finalize_cb, + void* finalize_hint, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_external(napi_env env, + napi_value value, + void** result); + +// Methods to control object lifespan + +// Set initial_refcount to 0 for a weak reference, >0 for a strong reference. +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_reference(napi_env env, + napi_value value, + uint32_t initial_refcount, + napi_ref* result); + +// Deletes a reference. The referenced value is released, and may +// be GC'd unless there are other references to it. +NAPI_EXTERN napi_status NAPI_CDECL napi_delete_reference(node_api_basic_env env, + napi_ref ref); + +// Increments the reference count, optionally returning the resulting count. +// After this call the reference will be a strong reference because its +// refcount is >0, and the referenced object is effectively "pinned". +// Calling this when the refcount is 0 and the object is unavailable +// results in an error. +NAPI_EXTERN napi_status NAPI_CDECL napi_reference_ref(napi_env env, + napi_ref ref, + uint32_t* result); + +// Decrements the reference count, optionally returning the resulting count. +// If the result is 0 the reference is now weak and the object may be GC'd +// at any time if there are no other references. Calling this when the +// refcount is already 0 results in an error. +NAPI_EXTERN napi_status NAPI_CDECL napi_reference_unref(napi_env env, + napi_ref ref, + uint32_t* result); + +// Attempts to get a referenced value. If the reference is weak, +// the value might no longer be available, in that case the call +// is still successful but the result is NULL. +NAPI_EXTERN napi_status NAPI_CDECL napi_get_reference_value(napi_env env, + napi_ref ref, + napi_value* result); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_open_handle_scope(napi_env env, napi_handle_scope* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_close_handle_scope(napi_env env, napi_handle_scope scope); +NAPI_EXTERN napi_status NAPI_CDECL napi_open_escapable_handle_scope( + napi_env env, napi_escapable_handle_scope* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_close_escapable_handle_scope( + napi_env env, napi_escapable_handle_scope scope); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_escape_handle(napi_env env, + napi_escapable_handle_scope scope, + napi_value escapee, + napi_value* result); + +// Methods to support error handling +NAPI_EXTERN napi_status NAPI_CDECL napi_throw(napi_env env, napi_value error); +NAPI_EXTERN napi_status NAPI_CDECL napi_throw_error(napi_env env, + const char* code, + const char* msg); +NAPI_EXTERN napi_status NAPI_CDECL napi_throw_type_error(napi_env env, + const char* code, + const char* msg); +NAPI_EXTERN napi_status NAPI_CDECL napi_throw_range_error(napi_env env, + const char* code, + const char* msg); +#if NAPI_VERSION >= 9 +NAPI_EXTERN napi_status NAPI_CDECL node_api_throw_syntax_error(napi_env env, + const char* code, + const char* msg); +#endif // NAPI_VERSION >= 9 +NAPI_EXTERN napi_status NAPI_CDECL napi_is_error(napi_env env, + napi_value value, + bool* result); + +// Methods to support catching exceptions +NAPI_EXTERN napi_status NAPI_CDECL napi_is_exception_pending(napi_env env, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_and_clear_last_exception(napi_env env, napi_value* result); + +// Methods to work with array buffers and typed arrays +NAPI_EXTERN napi_status NAPI_CDECL napi_is_arraybuffer(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_create_arraybuffer(napi_env env, + size_t byte_length, + void** data, + napi_value* result); +#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_external_arraybuffer(napi_env env, + void* external_data, + size_t byte_length, + node_api_basic_finalize finalize_cb, + void* finalize_hint, + napi_value* result); +#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED +NAPI_EXTERN napi_status NAPI_CDECL napi_get_arraybuffer_info( + napi_env env, napi_value arraybuffer, void** data, size_t* byte_length); +NAPI_EXTERN napi_status NAPI_CDECL napi_is_typedarray(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_typedarray(napi_env env, + napi_typedarray_type type, + size_t length, + napi_value arraybuffer, + size_t byte_offset, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_typedarray_info(napi_env env, + napi_value typedarray, + napi_typedarray_type* type, + size_t* length, + void** data, + napi_value* arraybuffer, + size_t* byte_offset); + +NAPI_EXTERN napi_status NAPI_CDECL napi_create_dataview(napi_env env, + size_t length, + napi_value arraybuffer, + size_t byte_offset, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_is_dataview(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_dataview_info(napi_env env, + napi_value dataview, + size_t* bytelength, + void** data, + napi_value* arraybuffer, + size_t* byte_offset); + + +// version management +NAPI_EXTERN napi_status NAPI_CDECL napi_get_version(node_api_basic_env env, + uint32_t* result); + +// Promises +NAPI_EXTERN napi_status NAPI_CDECL napi_create_promise(napi_env env, + napi_deferred* deferred, + napi_value* promise); +NAPI_EXTERN napi_status NAPI_CDECL napi_resolve_deferred(napi_env env, + napi_deferred deferred, + napi_value resolution); +NAPI_EXTERN napi_status NAPI_CDECL napi_reject_deferred(napi_env env, + napi_deferred deferred, + napi_value rejection); +NAPI_EXTERN napi_status NAPI_CDECL napi_is_promise(napi_env env, + napi_value value, + bool* is_promise); + +// Running a script +NAPI_EXTERN napi_status NAPI_CDECL napi_run_script(napi_env env, + napi_value script, + napi_value* result); + +// Memory management +NAPI_EXTERN napi_status NAPI_CDECL napi_adjust_external_memory( + node_api_basic_env env, int64_t change_in_bytes, int64_t* adjusted_value); + +#if NAPI_VERSION >= 5 + +// Dates +NAPI_EXTERN napi_status NAPI_CDECL napi_create_date(napi_env env, + double time, + napi_value* result); + +NAPI_EXTERN napi_status NAPI_CDECL napi_is_date(napi_env env, + napi_value value, + bool* is_date); + +NAPI_EXTERN napi_status NAPI_CDECL napi_get_date_value(napi_env env, + napi_value value, + double* result); + +// Add finalizer for pointer +NAPI_EXTERN napi_status NAPI_CDECL +napi_add_finalizer(napi_env env, + napi_value js_object, + void* finalize_data, + node_api_basic_finalize finalize_cb, + void* finalize_hint, + napi_ref* result); + +#endif // NAPI_VERSION >= 5 + + +#if NAPI_VERSION >= 6 + +// BigInt +NAPI_EXTERN napi_status NAPI_CDECL napi_create_bigint_int64(napi_env env, + int64_t value, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_bigint_uint64(napi_env env, uint64_t value, napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_bigint_words(napi_env env, + int sign_bit, + size_t word_count, + const uint64_t* words, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bigint_int64(napi_env env, + napi_value value, + int64_t* result, + bool* lossless); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bigint_uint64( + napi_env env, napi_value value, uint64_t* result, bool* lossless); +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_value_bigint_words(napi_env env, + napi_value value, + int* sign_bit, + size_t* word_count, + uint64_t* words); + +// Object +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_all_property_names(napi_env env, + napi_value object, + napi_key_collection_mode key_mode, + napi_key_filter key_filter, + napi_key_conversion key_conversion, + napi_value* result); + +// Instance data +NAPI_EXTERN napi_status NAPI_CDECL +napi_set_instance_data(node_api_basic_env env, + void* data, + napi_finalize finalize_cb, + void* finalize_hint); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_instance_data(node_api_basic_env env, void** data); +#endif // NAPI_VERSION >= 6 + +#if NAPI_VERSION >= 7 +// ArrayBuffer detaching +NAPI_EXTERN napi_status NAPI_CDECL +napi_detach_arraybuffer(napi_env env, napi_value arraybuffer); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_is_detached_arraybuffer(napi_env env, napi_value value, bool* result); +#endif // NAPI_VERSION >= 7 + +#if NAPI_VERSION >= 8 +// Type tagging +NAPI_EXTERN napi_status NAPI_CDECL napi_type_tag_object( + napi_env env, napi_value value, const napi_type_tag* type_tag); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_check_object_type_tag(napi_env env, + napi_value value, + const napi_type_tag* type_tag, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_object_freeze(napi_env env, + napi_value object); +NAPI_EXTERN napi_status NAPI_CDECL napi_object_seal(napi_env env, + napi_value object); +#endif // NAPI_VERSION >= 8 + +EXTERN_C_END + +#endif // SRC_JS_NATIVE_API_H_ diff --git a/js_native_api_types.h b/include/js_native_api_types.h similarity index 56% rename from js_native_api_types.h rename to include/js_native_api_types.h index 6aba066..dc4922e 100644 --- a/js_native_api_types.h +++ b/include/js_native_api_types.h @@ -1,19 +1,63 @@ #ifndef SRC_JS_NATIVE_API_TYPES_H_ #define SRC_JS_NATIVE_API_TYPES_H_ +// Use INT_MAX, this should only be consumed by the pre-processor anyway. +#define NAPI_VERSION_EXPERIMENTAL 2147483647 +#ifndef NAPI_VERSION +// The baseline version for Node-API. +// NAPI_VERSION controls which version is used by default when compiling +// a native addon. If the addon developer wants to use functions from a +// newer Node-API version not yet available in all LTS versions, they can +// set NAPI_VERSION to explicitly depend on that version. +#define NAPI_VERSION 8 +#endif + + // This file needs to be compatible with C compilers. // This is a public include file, and these includes have essentially -// became part of it's API. +// become part of its API. #include // NOLINT(modernize-deprecated-headers) #include // NOLINT(modernize-deprecated-headers) #if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900) - typedef uint16_t char16_t; +typedef uint16_t char16_t; +#endif + +#ifndef NAPI_CDECL +#ifdef _WIN32 +#define NAPI_CDECL __cdecl +#else +#define NAPI_CDECL +#endif #endif // JSVM API types are all opaque pointers for ABI stability // typedef undefined structs instead of void* for compile time type safety typedef struct napi_env__* napi_env; + +// We need to mark APIs which can be called during garbage collection (GC), +// meaning that they do not affect the state of the JS engine, and can +// therefore be called synchronously from a finalizer that itself runs +// synchronously during GC. Such APIs can receive either a `napi_env` or a +// `node_api_basic_env` as their first parameter, because we should be able to +// also call them during normal, non-garbage-collecting operations, whereas +// APIs that affect the state of the JS engine can only receive a `napi_env` as +// their first parameter, because we must not call them during GC. In lieu of +// inheritance, we use the properties of the const qualifier to accomplish +// this, because both a const and a non-const value can be passed to an API +// expecting a const value, but only a non-const value can be passed to an API +// expecting a non-const value. +// +// In conjunction with appropriate CFLAGS to warn us if we're passing a const +// (basic) environment into an API that expects a non-const environment, and +// the definition of basic finalizer function pointer types below, which +// receive a basic environment as their first parameter, and can thus only call +// basic APIs (unless the user explicitly casts the environment), we achieve +// the ability to ensure at compile time that we do not call APIs that affect +// the state of the JS engine from a synchronous (basic) finalizer. +typedef struct napi_env__* node_api_nogc_env; +typedef node_api_nogc_env node_api_basic_env; + typedef struct napi_value__* napi_value; typedef struct napi_ref__* napi_ref; typedef struct napi_handle_scope__* napi_handle_scope; @@ -36,9 +80,7 @@ typedef enum { napi_default_method = napi_writable | napi_configurable, // Default for object properties, like in JS obj[prop]. - napi_default_jsproperty = napi_writable | - napi_enumerable | - napi_configurable, + napi_default_jsproperty = napi_writable | napi_enumerable | napi_configurable, #endif // NAPI_VERSION >= 8 } napi_property_attributes; @@ -68,6 +110,8 @@ typedef enum { napi_float64_array, napi_bigint64_array, napi_biguint64_array, +#define NODE_API_HAS_FLOAT16_ARRAY + napi_float16_array, } napi_typedarray_type; typedef enum { @@ -92,7 +136,9 @@ typedef enum { napi_date_expected, napi_arraybuffer_expected, napi_detachable_arraybuffer_expected, - napi_would_deadlock // unused + napi_would_deadlock, // unused + napi_no_external_buffers_allowed, + napi_cannot_run_js, } napi_status; // Note: when adding a new enum value to `napi_status`, please also update // * `const int last_status` in the definition of `napi_get_last_error_info()' @@ -102,11 +148,18 @@ typedef enum { // * the definition of `napi_status` in doc/api/n-api.md to reflect the newly // added value(s). -typedef napi_value (*napi_callback)(napi_env env, - napi_callback_info info); -typedef void (*napi_finalize)(napi_env env, - void* finalize_data, - void* finalize_hint); +typedef napi_value(NAPI_CDECL* napi_callback)(napi_env env, + napi_callback_info info); +typedef void(NAPI_CDECL* napi_finalize)(napi_env env, + void* finalize_data, + void* finalize_hint); + +typedef napi_finalize node_api_nogc_finalize; +typedef node_api_nogc_finalize node_api_basic_finalize; + +// A finalizer that can be called from any thread and at any time. +typedef void(NAPI_CDECL* node_api_noenv_finalize)(void* finalize_data, + void* finalize_hint); typedef struct { // One of utf8name or name should be NULL. diff --git a/include/node_api.h b/include/node_api.h new file mode 100644 index 0000000..46dbb02 --- /dev/null +++ b/include/node_api.h @@ -0,0 +1,265 @@ +#ifndef SRC_NODE_API_H_ +#define SRC_NODE_API_H_ + +#if defined(BUILDING_NODE_EXTENSION) && !defined(NAPI_EXTERN) +#ifdef _WIN32 +// Building native addon against node +#define NAPI_EXTERN __declspec(dllimport) +#elif defined(__wasm__) +#define NAPI_EXTERN __attribute__((__import_module__("napi"))) +#endif +#endif +#include "js_native_api.h" +#include "node_api_types.h" + +struct uv_loop_s; // Forward declaration. + +#ifdef _WIN32 +#define NAPI_MODULE_EXPORT __declspec(dllexport) +#else +#ifdef __EMSCRIPTEN__ +#define NAPI_MODULE_EXPORT \ + __attribute__((visibility("default"))) __attribute__((used)) +#else +#define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) +#endif +#endif + +#if defined(__GNUC__) +#define NAPI_NO_RETURN __attribute__((noreturn)) +#elif defined(_WIN32) +#define NAPI_NO_RETURN __declspec(noreturn) +#else +#define NAPI_NO_RETURN +#endif + +// Used by deprecated registration method napi_module_register. +typedef struct napi_module { + int nm_version; + unsigned int nm_flags; + const char* nm_filename; + napi_addon_register_func nm_register_func; + const char* nm_modname; + void* nm_priv; + void* reserved[4]; +} napi_module; + +#define NAPI_MODULE_VERSION 1 + +#define NAPI_MODULE_INITIALIZER_X(base, version) \ + NAPI_MODULE_INITIALIZER_X_HELPER(base, version) +#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version + +#ifdef __wasm__ +#define NAPI_MODULE_INITIALIZER_BASE napi_register_wasm_v +#else +#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v +#endif + +#define NODE_API_MODULE_GET_API_VERSION_BASE node_api_module_get_api_version_v + +#define NAPI_MODULE_INITIALIZER \ + NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, NAPI_MODULE_VERSION) + +#define NODE_API_MODULE_GET_API_VERSION \ + NAPI_MODULE_INITIALIZER_X(NODE_API_MODULE_GET_API_VERSION_BASE, \ + NAPI_MODULE_VERSION) + +#define NAPI_MODULE_INIT() \ + EXTERN_C_START \ + NAPI_MODULE_EXPORT int32_t NODE_API_MODULE_GET_API_VERSION(void) { \ + return NAPI_VERSION; \ + } \ + NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \ + napi_value exports); \ + EXTERN_C_END \ + napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports) + +#define NAPI_MODULE(modname, regfunc) \ + NAPI_MODULE_INIT() { return regfunc(env, exports); } + +// Deprecated. Use NAPI_MODULE. +#define NAPI_MODULE_X(modname, regfunc, priv, flags) \ + NAPI_MODULE(modname, regfunc) + +EXTERN_C_START + +// Deprecated. Replaced by symbol-based registration defined by NAPI_MODULE +// and NAPI_MODULE_INIT macros. +NAPI_EXTERN void NAPI_CDECL +napi_module_register(napi_module* mod); + +NAPI_EXTERN NAPI_NO_RETURN void NAPI_CDECL +napi_fatal_error(const char* location, + size_t location_len, + const char* message, + size_t message_len); + +// Methods for custom handling of async operations +NAPI_EXTERN napi_status NAPI_CDECL +napi_async_init(napi_env env, + napi_value async_resource, + napi_value async_resource_name, + napi_async_context* result); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_async_destroy(napi_env env, napi_async_context async_context); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_make_callback(napi_env env, + napi_async_context async_context, + napi_value recv, + napi_value func, + size_t argc, + const napi_value* argv, + napi_value* result); + +// Methods to provide node::Buffer functionality with napi types +NAPI_EXTERN napi_status NAPI_CDECL napi_create_buffer(napi_env env, + size_t length, + void** data, + napi_value* result); +#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_external_buffer(napi_env env, + size_t length, + void* data, + node_api_basic_finalize finalize_cb, + void* finalize_hint, + napi_value* result); +#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED + +#if NAPI_VERSION >= 10 + +NAPI_EXTERN napi_status NAPI_CDECL +node_api_create_buffer_from_arraybuffer(napi_env env, + napi_value arraybuffer, + size_t byte_offset, + size_t byte_length, + napi_value* result); +#endif // NAPI_VERSION >= 10 + +NAPI_EXTERN napi_status NAPI_CDECL napi_create_buffer_copy(napi_env env, + size_t length, + const void* data, + void** result_data, + napi_value* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_is_buffer(napi_env env, + napi_value value, + bool* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_get_buffer_info(napi_env env, + napi_value value, + void** data, + size_t* length); + +// Methods to manage simple async operations +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_async_work(napi_env env, + napi_value async_resource, + napi_value async_resource_name, + napi_async_execute_callback execute, + napi_async_complete_callback complete, + void* data, + napi_async_work* result); +NAPI_EXTERN napi_status NAPI_CDECL napi_delete_async_work(napi_env env, + napi_async_work work); +NAPI_EXTERN napi_status NAPI_CDECL napi_queue_async_work(node_api_basic_env env, + napi_async_work work); +NAPI_EXTERN napi_status NAPI_CDECL +napi_cancel_async_work(node_api_basic_env env, napi_async_work work); + +// version management +NAPI_EXTERN napi_status NAPI_CDECL napi_get_node_version( + node_api_basic_env env, const napi_node_version** version); + +#if NAPI_VERSION >= 2 + +// Return the current libuv event loop for a given environment +NAPI_EXTERN napi_status NAPI_CDECL +napi_get_uv_event_loop(node_api_basic_env env, struct uv_loop_s** loop); + +#endif // NAPI_VERSION >= 2 + +#if NAPI_VERSION >= 3 + +NAPI_EXTERN napi_status NAPI_CDECL napi_fatal_exception(napi_env env, + napi_value err); + +NAPI_EXTERN napi_status NAPI_CDECL napi_add_env_cleanup_hook( + node_api_basic_env env, napi_cleanup_hook fun, void* arg); + +NAPI_EXTERN napi_status NAPI_CDECL napi_remove_env_cleanup_hook( + node_api_basic_env env, napi_cleanup_hook fun, void* arg); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_open_callback_scope(napi_env env, + napi_value resource_object, + napi_async_context context, + napi_callback_scope* result); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_close_callback_scope(napi_env env, napi_callback_scope scope); + +#endif // NAPI_VERSION >= 3 + +#if NAPI_VERSION >= 4 + +// Calling into JS from other threads +NAPI_EXTERN napi_status NAPI_CDECL +napi_create_threadsafe_function(napi_env env, + napi_value func, + napi_value async_resource, + napi_value async_resource_name, + size_t max_queue_size, + size_t initial_thread_count, + void* thread_finalize_data, + napi_finalize thread_finalize_cb, + void* context, + napi_threadsafe_function_call_js call_js_cb, + napi_threadsafe_function* result); + +NAPI_EXTERN napi_status NAPI_CDECL napi_get_threadsafe_function_context( + napi_threadsafe_function func, void** result); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_call_threadsafe_function(napi_threadsafe_function func, + void* data, + napi_threadsafe_function_call_mode is_blocking); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_acquire_threadsafe_function(napi_threadsafe_function func); + +NAPI_EXTERN napi_status NAPI_CDECL napi_release_threadsafe_function( + napi_threadsafe_function func, napi_threadsafe_function_release_mode mode); + +NAPI_EXTERN napi_status NAPI_CDECL napi_unref_threadsafe_function( + node_api_basic_env env, napi_threadsafe_function func); + +NAPI_EXTERN napi_status NAPI_CDECL napi_ref_threadsafe_function( + node_api_basic_env env, napi_threadsafe_function func); + +#endif // NAPI_VERSION >= 4 + +#if NAPI_VERSION >= 8 + +NAPI_EXTERN napi_status NAPI_CDECL +napi_add_async_cleanup_hook(node_api_basic_env env, + napi_async_cleanup_hook hook, + void* arg, + napi_async_cleanup_hook_handle* remove_handle); + +NAPI_EXTERN napi_status NAPI_CDECL +napi_remove_async_cleanup_hook(napi_async_cleanup_hook_handle remove_handle); + +#endif // NAPI_VERSION >= 8 + +#if NAPI_VERSION >= 9 + +NAPI_EXTERN napi_status NAPI_CDECL +node_api_get_module_file_name(node_api_basic_env env, const char** result); + +#endif // NAPI_VERSION >= 9 + +EXTERN_C_END + +#endif // SRC_NODE_API_H_ diff --git a/node_api_types.h b/include/node_api_types.h similarity index 50% rename from node_api_types.h rename to include/node_api_types.h index 58ffc61..79123f0 100644 --- a/node_api_types.h +++ b/include/node_api_types.h @@ -3,9 +3,20 @@ #include "js_native_api_types.h" +typedef napi_value(NAPI_CDECL* napi_addon_register_func)(napi_env env, + napi_value exports); +// False positive: https://github.com/cpplint/cpplint/issues/409 +// NOLINTNEXTLINE (readability/casting) +typedef int32_t(NAPI_CDECL* node_api_addon_get_api_version_func)(void); + typedef struct napi_callback_scope__* napi_callback_scope; typedef struct napi_async_context__* napi_async_context; typedef struct napi_async_work__* napi_async_work; + +#if NAPI_VERSION >= 3 +typedef void(NAPI_CDECL* napi_cleanup_hook)(void* arg); +#endif // NAPI_VERSION >= 3 + #if NAPI_VERSION >= 4 typedef struct napi_threadsafe_function__* napi_threadsafe_function; #endif // NAPI_VERSION >= 4 @@ -22,16 +33,13 @@ typedef enum { } napi_threadsafe_function_call_mode; #endif // NAPI_VERSION >= 4 -typedef void (*napi_async_execute_callback)(napi_env env, - void* data); -typedef void (*napi_async_complete_callback)(napi_env env, - napi_status status, - void* data); +typedef void(NAPI_CDECL* napi_async_execute_callback)(napi_env env, void* data); +typedef void(NAPI_CDECL* napi_async_complete_callback)(napi_env env, + napi_status status, + void* data); #if NAPI_VERSION >= 4 -typedef void (*napi_threadsafe_function_call_js)(napi_env env, - napi_value js_callback, - void* context, - void* data); +typedef void(NAPI_CDECL* napi_threadsafe_function_call_js)( + napi_env env, napi_value js_callback, void* context, void* data); #endif // NAPI_VERSION >= 4 typedef struct { @@ -43,8 +51,8 @@ typedef struct { #if NAPI_VERSION >= 8 typedef struct napi_async_cleanup_hook_handle__* napi_async_cleanup_hook_handle; -typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle, - void* data); +typedef void(NAPI_CDECL* napi_async_cleanup_hook)( + napi_async_cleanup_hook_handle handle, void* data); #endif // NAPI_VERSION >= 8 #endif // SRC_NODE_API_TYPES_H_ diff --git a/js_native_api.h b/js_native_api.h deleted file mode 100644 index e804d1d..0000000 --- a/js_native_api.h +++ /dev/null @@ -1,561 +0,0 @@ -#ifndef SRC_JS_NATIVE_API_H_ -#define SRC_JS_NATIVE_API_H_ - -// This file needs to be compatible with C compilers. -#include // NOLINT(modernize-deprecated-headers) -#include // NOLINT(modernize-deprecated-headers) - -// Use INT_MAX, this should only be consumed by the pre-processor anyway. -#define NAPI_VERSION_EXPERIMENTAL 2147483647 -#ifndef NAPI_VERSION -#ifdef NAPI_EXPERIMENTAL -#define NAPI_VERSION NAPI_VERSION_EXPERIMENTAL -#else -// The baseline version for N-API. -// The NAPI_VERSION controls which version will be used by default when -// compilling a native addon. If the addon developer specifically wants to use -// functions available in a new version of N-API that is not yet ported in all -// LTS versions, they can set NAPI_VERSION knowing that they have specifically -// depended on that version. -#define NAPI_VERSION 8 -#endif -#endif - -#include "js_native_api_types.h" - -// If you need __declspec(dllimport), either include 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__) - #define NAPI_EXTERN __attribute__((visibility("default"))) \ - __attribute__((__import_module__("napi"))) - #else - #define NAPI_EXTERN __attribute__((visibility("default"))) - #endif -#endif - -#define NAPI_AUTO_LENGTH SIZE_MAX - -#ifdef __cplusplus -#define EXTERN_C_START extern "C" { -#define EXTERN_C_END } -#else -#define EXTERN_C_START -#define EXTERN_C_END -#endif - -EXTERN_C_START - -NAPI_EXTERN napi_status -napi_get_last_error_info(napi_env env, - const napi_extended_error_info** result); - -// Getters for defined singletons -NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result); -NAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value* result); -NAPI_EXTERN napi_status napi_get_global(napi_env env, napi_value* result); -NAPI_EXTERN napi_status napi_get_boolean(napi_env env, - bool value, - napi_value* result); - -// Methods to create Primitive types/Objects -NAPI_EXTERN napi_status napi_create_object(napi_env env, napi_value* result); -NAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value* result); -NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env, - size_t length, - napi_value* result); -NAPI_EXTERN napi_status napi_create_double(napi_env env, - double value, - napi_value* result); -NAPI_EXTERN napi_status napi_create_int32(napi_env env, - int32_t value, - napi_value* result); -NAPI_EXTERN napi_status napi_create_uint32(napi_env env, - uint32_t value, - napi_value* result); -NAPI_EXTERN napi_status napi_create_int64(napi_env env, - int64_t value, - napi_value* result); -NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env, - const char* str, - size_t length, - napi_value* result); -NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env, - const char* str, - size_t length, - napi_value* result); -NAPI_EXTERN napi_status napi_create_string_utf16(napi_env env, - const char16_t* str, - size_t length, - napi_value* result); -NAPI_EXTERN napi_status napi_create_symbol(napi_env env, - napi_value description, - napi_value* result); -NAPI_EXTERN napi_status napi_create_function(napi_env env, - const char* utf8name, - size_t length, - napi_callback cb, - void* data, - napi_value* result); -NAPI_EXTERN napi_status napi_create_error(napi_env env, - napi_value code, - napi_value msg, - napi_value* result); -NAPI_EXTERN napi_status napi_create_type_error(napi_env env, - napi_value code, - napi_value msg, - napi_value* result); -NAPI_EXTERN napi_status napi_create_range_error(napi_env env, - napi_value code, - napi_value msg, - napi_value* result); - -// Methods to get the native napi_value from Primitive type -NAPI_EXTERN napi_status napi_typeof(napi_env env, - napi_value value, - napi_valuetype* result); -NAPI_EXTERN napi_status napi_get_value_double(napi_env env, - napi_value value, - double* result); -NAPI_EXTERN napi_status napi_get_value_int32(napi_env env, - napi_value value, - int32_t* result); -NAPI_EXTERN napi_status napi_get_value_uint32(napi_env env, - napi_value value, - uint32_t* result); -NAPI_EXTERN napi_status napi_get_value_int64(napi_env env, - napi_value value, - int64_t* result); -NAPI_EXTERN napi_status napi_get_value_bool(napi_env env, - napi_value value, - bool* result); - -// Copies LATIN-1 encoded bytes from a string into a buffer. -NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env, - napi_value value, - char* buf, - size_t bufsize, - size_t* result); - -// Copies UTF-8 encoded bytes from a string into a buffer. -NAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env, - napi_value value, - char* buf, - size_t bufsize, - size_t* result); - -// Copies UTF-16 encoded bytes from a string into a buffer. -NAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env, - napi_value value, - char16_t* buf, - size_t bufsize, - size_t* result); - -// Methods to coerce values -// These APIs may execute user scripts -NAPI_EXTERN napi_status napi_coerce_to_bool(napi_env env, - napi_value value, - napi_value* result); -NAPI_EXTERN napi_status napi_coerce_to_number(napi_env env, - napi_value value, - napi_value* result); -NAPI_EXTERN napi_status napi_coerce_to_object(napi_env env, - napi_value value, - napi_value* result); -NAPI_EXTERN napi_status napi_coerce_to_string(napi_env env, - napi_value value, - napi_value* result); - -// Methods to work with Objects -NAPI_EXTERN napi_status napi_get_prototype(napi_env env, - napi_value object, - napi_value* result); -NAPI_EXTERN napi_status napi_get_property_names(napi_env env, - napi_value object, - napi_value* result); -NAPI_EXTERN napi_status napi_set_property(napi_env env, - napi_value object, - napi_value key, - napi_value value); -NAPI_EXTERN napi_status napi_has_property(napi_env env, - napi_value object, - napi_value key, - bool* result); -NAPI_EXTERN napi_status napi_get_property(napi_env env, - napi_value object, - napi_value key, - napi_value* result); -NAPI_EXTERN napi_status napi_delete_property(napi_env env, - napi_value object, - napi_value key, - bool* result); -NAPI_EXTERN napi_status napi_has_own_property(napi_env env, - napi_value object, - napi_value key, - bool* result); -NAPI_EXTERN napi_status napi_set_named_property(napi_env env, - napi_value object, - const char* utf8name, - napi_value value); -NAPI_EXTERN napi_status napi_has_named_property(napi_env env, - napi_value object, - const char* utf8name, - bool* result); -NAPI_EXTERN napi_status napi_get_named_property(napi_env env, - napi_value object, - const char* utf8name, - napi_value* result); -NAPI_EXTERN napi_status napi_set_element(napi_env env, - napi_value object, - uint32_t index, - napi_value value); -NAPI_EXTERN napi_status napi_has_element(napi_env env, - napi_value object, - uint32_t index, - bool* result); -NAPI_EXTERN napi_status napi_get_element(napi_env env, - napi_value object, - uint32_t index, - napi_value* result); -NAPI_EXTERN napi_status napi_delete_element(napi_env env, - napi_value object, - uint32_t index, - bool* result); -NAPI_EXTERN napi_status -napi_define_properties(napi_env env, - napi_value object, - size_t property_count, - const napi_property_descriptor* properties); - -// Methods to work with Arrays -NAPI_EXTERN napi_status napi_is_array(napi_env env, - napi_value value, - bool* result); -NAPI_EXTERN napi_status napi_get_array_length(napi_env env, - napi_value value, - uint32_t* result); - -// Methods to compare values -NAPI_EXTERN napi_status napi_strict_equals(napi_env env, - napi_value lhs, - napi_value rhs, - bool* result); - -// Methods to work with Functions -NAPI_EXTERN napi_status napi_call_function(napi_env env, - napi_value recv, - napi_value func, - size_t argc, - const napi_value* argv, - napi_value* result); -NAPI_EXTERN napi_status napi_new_instance(napi_env env, - napi_value constructor, - size_t argc, - const napi_value* argv, - napi_value* result); -NAPI_EXTERN napi_status napi_instanceof(napi_env env, - napi_value object, - napi_value constructor, - bool* result); - -// Methods to work with napi_callbacks - -// Gets all callback info in a single call. (Ugly, but faster.) -NAPI_EXTERN napi_status napi_get_cb_info( - napi_env env, // [in] NAPI environment handle - napi_callback_info cbinfo, // [in] Opaque callback-info handle - size_t* argc, // [in-out] Specifies the size of the provided argv array - // and receives the actual count of args. - napi_value* argv, // [out] Array of values - napi_value* this_arg, // [out] Receives the JS 'this' arg for the call - void** data); // [out] Receives the data pointer for the callback. - -NAPI_EXTERN napi_status napi_get_new_target(napi_env env, - napi_callback_info cbinfo, - napi_value* result); -NAPI_EXTERN napi_status -napi_define_class(napi_env env, - const char* utf8name, - size_t length, - napi_callback constructor, - void* data, - size_t property_count, - const napi_property_descriptor* properties, - napi_value* result); - -// Methods to work with external data objects -NAPI_EXTERN napi_status napi_wrap(napi_env env, - napi_value js_object, - void* native_object, - napi_finalize finalize_cb, - void* finalize_hint, - napi_ref* result); -NAPI_EXTERN napi_status napi_unwrap(napi_env env, - napi_value js_object, - void** result); -NAPI_EXTERN napi_status napi_remove_wrap(napi_env env, - napi_value js_object, - void** result); -NAPI_EXTERN napi_status napi_create_external(napi_env env, - void* data, - napi_finalize finalize_cb, - void* finalize_hint, - napi_value* result); -NAPI_EXTERN napi_status napi_get_value_external(napi_env env, - napi_value value, - void** result); - -// Methods to control object lifespan - -// Set initial_refcount to 0 for a weak reference, >0 for a strong reference. -NAPI_EXTERN napi_status napi_create_reference(napi_env env, - napi_value value, - uint32_t initial_refcount, - napi_ref* result); - -// Deletes a reference. The referenced value is released, and may -// be GC'd unless there are other references to it. -NAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref); - -// Increments the reference count, optionally returning the resulting count. -// After this call the reference will be a strong reference because its -// refcount is >0, and the referenced object is effectively "pinned". -// Calling this when the refcount is 0 and the object is unavailable -// results in an error. -NAPI_EXTERN napi_status napi_reference_ref(napi_env env, - napi_ref ref, - uint32_t* result); - -// Decrements the reference count, optionally returning the resulting count. -// If the result is 0 the reference is now weak and the object may be GC'd -// at any time if there are no other references. Calling this when the -// refcount is already 0 results in an error. -NAPI_EXTERN napi_status napi_reference_unref(napi_env env, - napi_ref ref, - uint32_t* result); - -// Attempts to get a referenced value. If the reference is weak, -// the value might no longer be available, in that case the call -// is still successful but the result is NULL. -NAPI_EXTERN napi_status napi_get_reference_value(napi_env env, - napi_ref ref, - napi_value* result); - -NAPI_EXTERN napi_status napi_open_handle_scope(napi_env env, - napi_handle_scope* result); -NAPI_EXTERN napi_status napi_close_handle_scope(napi_env env, - napi_handle_scope scope); -NAPI_EXTERN napi_status -napi_open_escapable_handle_scope(napi_env env, - napi_escapable_handle_scope* result); -NAPI_EXTERN napi_status -napi_close_escapable_handle_scope(napi_env env, - napi_escapable_handle_scope scope); - -NAPI_EXTERN napi_status napi_escape_handle(napi_env env, - napi_escapable_handle_scope scope, - napi_value escapee, - napi_value* result); - -// Methods to support error handling -NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error); -NAPI_EXTERN napi_status napi_throw_error(napi_env env, - const char* code, - const char* msg); -NAPI_EXTERN napi_status napi_throw_type_error(napi_env env, - const char* code, - const char* msg); -NAPI_EXTERN napi_status napi_throw_range_error(napi_env env, - const char* code, - const char* msg); -NAPI_EXTERN napi_status napi_is_error(napi_env env, - napi_value value, - bool* result); - -// Methods to support catching exceptions -NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result); -NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env, - napi_value* result); - -// Methods to work with array buffers and typed arrays -NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env, - napi_value value, - bool* result); -NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env, - size_t byte_length, - void** data, - napi_value* result); -NAPI_EXTERN napi_status -napi_create_external_arraybuffer(napi_env env, - void* external_data, - size_t byte_length, - napi_finalize finalize_cb, - void* finalize_hint, - napi_value* result); -NAPI_EXTERN napi_status napi_get_arraybuffer_info(napi_env env, - napi_value arraybuffer, - void** data, - size_t* byte_length); -NAPI_EXTERN napi_status napi_is_typedarray(napi_env env, - napi_value value, - bool* result); -NAPI_EXTERN napi_status napi_create_typedarray(napi_env env, - napi_typedarray_type type, - size_t length, - napi_value arraybuffer, - size_t byte_offset, - napi_value* result); -NAPI_EXTERN napi_status napi_get_typedarray_info(napi_env env, - napi_value typedarray, - napi_typedarray_type* type, - size_t* length, - void** data, - napi_value* arraybuffer, - size_t* byte_offset); - -NAPI_EXTERN napi_status napi_create_dataview(napi_env env, - size_t length, - napi_value arraybuffer, - size_t byte_offset, - napi_value* result); -NAPI_EXTERN napi_status napi_is_dataview(napi_env env, - napi_value value, - bool* result); -NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env, - napi_value dataview, - size_t* bytelength, - void** data, - napi_value* arraybuffer, - size_t* byte_offset); - -// version management -NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result); - -// Promises -NAPI_EXTERN napi_status napi_create_promise(napi_env env, - napi_deferred* deferred, - napi_value* promise); -NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env, - napi_deferred deferred, - napi_value resolution); -NAPI_EXTERN napi_status napi_reject_deferred(napi_env env, - napi_deferred deferred, - napi_value rejection); -NAPI_EXTERN napi_status napi_is_promise(napi_env env, - napi_value value, - bool* is_promise); - -// Running a script -NAPI_EXTERN napi_status napi_run_script(napi_env env, - napi_value script, - napi_value* result); - -// Memory management -NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env, - int64_t change_in_bytes, - int64_t* adjusted_value); - -#if NAPI_VERSION >= 5 - -// Dates -NAPI_EXTERN napi_status napi_create_date(napi_env env, - double time, - napi_value* result); - -NAPI_EXTERN napi_status napi_is_date(napi_env env, - napi_value value, - bool* is_date); - -NAPI_EXTERN napi_status napi_get_date_value(napi_env env, - napi_value value, - double* result); - -// Add finalizer for pointer -NAPI_EXTERN napi_status napi_add_finalizer(napi_env env, - napi_value js_object, - void* native_object, - napi_finalize finalize_cb, - void* finalize_hint, - napi_ref* result); - -#endif // NAPI_VERSION >= 5 - -#if NAPI_VERSION >= 6 - -// BigInt -NAPI_EXTERN napi_status napi_create_bigint_int64(napi_env env, - int64_t value, - napi_value* result); -NAPI_EXTERN napi_status napi_create_bigint_uint64(napi_env env, - uint64_t value, - napi_value* result); -NAPI_EXTERN napi_status napi_create_bigint_words(napi_env env, - int sign_bit, - size_t word_count, - const uint64_t* words, - napi_value* result); -NAPI_EXTERN napi_status napi_get_value_bigint_int64(napi_env env, - napi_value value, - int64_t* result, - bool* lossless); -NAPI_EXTERN napi_status napi_get_value_bigint_uint64(napi_env env, - napi_value value, - uint64_t* result, - bool* lossless); -NAPI_EXTERN napi_status napi_get_value_bigint_words(napi_env env, - napi_value value, - int* sign_bit, - size_t* word_count, - uint64_t* words); - -// Object -NAPI_EXTERN napi_status -napi_get_all_property_names(napi_env env, - napi_value object, - napi_key_collection_mode key_mode, - napi_key_filter key_filter, - napi_key_conversion key_conversion, - napi_value* result); - -// Instance data -NAPI_EXTERN napi_status napi_set_instance_data(napi_env env, - void* data, - napi_finalize finalize_cb, - void* finalize_hint); - -NAPI_EXTERN napi_status napi_get_instance_data(napi_env env, - void** data); -#endif // NAPI_VERSION >= 6 - -#if NAPI_VERSION >= 7 -// ArrayBuffer detaching -NAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env, - napi_value arraybuffer); - -NAPI_EXTERN napi_status napi_is_detached_arraybuffer(napi_env env, - napi_value value, - bool* result); -#endif // NAPI_VERSION >= 7 - -#if NAPI_VERSION >= 8 -// Type tagging -NAPI_EXTERN napi_status napi_type_tag_object(napi_env env, - napi_value value, - const napi_type_tag* type_tag); - -NAPI_EXTERN napi_status -napi_check_object_type_tag(napi_env env, - napi_value value, - const napi_type_tag* type_tag, - bool* result); -NAPI_EXTERN napi_status napi_object_freeze(napi_env env, - napi_value object); -NAPI_EXTERN napi_status napi_object_seal(napi_env env, - napi_value object); -#endif // NAPI_VERSION >= 8 - -EXTERN_C_END - -#endif // SRC_JS_NATIVE_API_H_ diff --git a/node_api.c b/node_api.c deleted file mode 100644 index 9c89060..0000000 --- a/node_api.c +++ /dev/null @@ -1,1351 +0,0 @@ -#include "node_api.h" - -// ############################################################################## -// Stub for JavaScript API -// ############################################################################## - -NAPI_EXTERN napi_status -napi_get_last_error_info( - napi_env env, - const napi_extended_error_info** result) { - return napi_ok; -} - -// Getters for defined singletons - -NAPI_EXTERN napi_status -napi_get_undefined( - napi_env env, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_null( - napi_env env, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_global( - napi_env env, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_boolean( - napi_env env, - bool value, - napi_value* result) { - return napi_ok; -} - -// Methods to create Primitive types/Objects - -NAPI_EXTERN napi_status -napi_create_object( - napi_env env, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_array( - napi_env env, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_array_with_length( - napi_env env, - size_t length, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_double( - napi_env env, - double value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_int32( - napi_env env, - int32_t value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_uint32( - napi_env env, - uint32_t value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_int64( - napi_env env, - int64_t value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_string_latin1( - napi_env env, - const char* str, - size_t length, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_string_utf8( - napi_env env, - const char* str, - size_t length, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_string_utf16( - napi_env env, - const char16_t* str, - size_t length, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_symbol( - napi_env env, - napi_value description, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_function( - napi_env env, - const char* utf8name, - size_t length, - napi_callback cb, - void* data, napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_error( - napi_env env, - napi_value code, - napi_value msg, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_type_error( - napi_env env, - napi_value code, - napi_value msg, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_range_error( - napi_env env, - napi_value code, - napi_value msg, - napi_value* result) { - return napi_ok; -} - -// Methods to get the native napi_value from Primitive type - -NAPI_EXTERN napi_status -napi_typeof( - napi_env env, - napi_value value, - napi_valuetype* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_double( - napi_env env, - napi_value value, - double* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_int32( - napi_env env, - napi_value value, - int32_t* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_uint32( - napi_env env, - napi_value value, - uint32_t* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_int64( - napi_env env, - napi_value value, - int64_t* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_bool( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -// Copies LATIN-1 encoded bytes from a string into a buffer. - -NAPI_EXTERN napi_status -napi_get_value_string_latin1( - napi_env env, - napi_value value, - char* buf, - size_t bufsize, - size_t* result) { - return napi_ok; -} - -// Copies UTF-8 encoded bytes from a string into a buffer. - -NAPI_EXTERN napi_status -napi_get_value_string_utf8( - napi_env env, - napi_value value, - char* buf, - size_t bufsize, - size_t* result) { - return napi_ok; -} - -// Copies UTF-16 encoded bytes from a string into a buffer. - -NAPI_EXTERN napi_status -napi_get_value_string_utf16( - napi_env env, - napi_value value, - char16_t* buf, - size_t bufsize, - size_t* result) { - return napi_ok; -} - -// Methods to coerce values -// These APIs may execute user scripts - -NAPI_EXTERN napi_status -napi_coerce_to_bool( - napi_env env, - napi_value value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_coerce_to_number( - napi_env env, - napi_value value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_coerce_to_object( - napi_env env, - napi_value value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_coerce_to_string( - napi_env env, - napi_value value, - napi_value* result) { - return napi_ok; -} - -// Methods to work with Objects - -NAPI_EXTERN napi_status -napi_get_prototype( - napi_env env, - napi_value object, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_property_names( - napi_env env, - napi_value object, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_set_property( - napi_env env, - napi_value object, - napi_value key, - napi_value value) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_has_property( - napi_env env, - napi_value object, - napi_value key, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_property( - napi_env env, - napi_value object, - napi_value key, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_delete_property( - napi_env env, - napi_value object, - napi_value key, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_has_own_property( - napi_env env, - napi_value object, - napi_value key, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_set_named_property( - napi_env env, - napi_value object, - const char* utf8name, - napi_value value) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_has_named_property( - napi_env env, - napi_value object, - const char* utf8name, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_named_property( - napi_env env, - napi_value object, - const char* utf8name, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_set_element( - napi_env env, - napi_value object, - uint32_t index, - napi_value value) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_has_element( - napi_env env, - napi_value object, - uint32_t index, - bool* result) { - return napi_ok; -} -NAPI_EXTERN napi_status -napi_get_element( - napi_env env, - napi_value object, - uint32_t index, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_delete_element( - napi_env env, - napi_value object, - uint32_t index, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_define_properties( - napi_env env, - napi_value object, - size_t property_count, - const napi_property_descriptor* properties) { - return napi_ok; -} - -// Methods to work with Arrays - -NAPI_EXTERN napi_status -napi_is_array( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_array_length( - napi_env env, - napi_value value, - uint32_t* result) { - return napi_ok; -} - -// Methods to compare values - -NAPI_EXTERN napi_status -napi_strict_equals( - napi_env env, - napi_value lhs, - napi_value rhs, - bool* result) { - return napi_ok; -} - -// Methods to work with Functions - -NAPI_EXTERN napi_status -napi_call_function( - napi_env env, - napi_value recv, - napi_value func, - size_t argc, - const napi_value* argv, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_new_instance( - napi_env env, - napi_value constructor, - size_t argc, - const napi_value* argv, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_instanceof( - napi_env env, - napi_value object, - napi_value constructor, - bool* result) { - return napi_ok; -} - -// Methods to work with napi_callbacks - -// Gets all callback info in a single call. (Ugly, but faster.) -NAPI_EXTERN napi_status -napi_get_cb_info( - napi_env env, - napi_callback_info cbinfo, - size_t* argc, - napi_value* argv, - napi_value* this_arg, - void** data) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_new_target( - napi_env env, - napi_callback_info cbinfo, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_define_class( - napi_env env, - const char* utf8name, - size_t length, - napi_callback constructor, - void* data, - size_t property_count, - const napi_property_descriptor* properties, - napi_value* result) { - return napi_ok; -} - -// Methods to work with external data objects - -NAPI_EXTERN napi_status -napi_wrap( - napi_env env, - napi_value js_object, - void* native_object, - napi_finalize finalize_cb, - void* finalize_hint, - napi_ref* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_unwrap( - napi_env env, - napi_value js_object, - void** result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_remove_wrap( - napi_env env, - napi_value js_object, - void** result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_external( - napi_env env, - void* data, - napi_finalize finalize_cb, - void* finalize_hint, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_external( - napi_env env, - napi_value value, - void** result) { - return napi_ok; -} - -// Set initial_refcount to 0 for a weak reference, >0 for a strong reference. - -NAPI_EXTERN napi_status -napi_create_reference( - napi_env env, - napi_value value, - uint32_t initial_refcount, - napi_ref* result) { - return napi_ok; -} - -// Deletes a reference. The referenced value is released, and may -// be GC'd unless there are other references to it. - -NAPI_EXTERN napi_status -napi_delete_reference( - napi_env env, - napi_ref ref) { - return napi_ok; -} - -// Increments the reference count, optionally returning the resulting count. -// After this call the reference will be a strong reference because its -// refcount is >0, and the referenced object is effectively "pinned". -// Calling this when the refcount is 0 and the object is unavailable -// results in an error. - -NAPI_EXTERN napi_status -napi_reference_ref( - napi_env env, - napi_ref ref, - uint32_t* result) { - return napi_ok; -} - -// Decrements the reference count, optionally returning the resulting count. -// If the result is 0 the reference is now weak and the object may be GC'd -// at any time if there are no other references. Calling this when the -// refcount is already 0 results in an error. - -NAPI_EXTERN napi_status -napi_reference_unref( - napi_env env, - napi_ref ref, - uint32_t* result) { - return napi_ok; -} - -// Attempts to get a referenced value. If the reference is weak, -// the value might no longer be available, in that case the call -// is still successful but the result is NULL. - -NAPI_EXTERN napi_status -napi_get_reference_value( - napi_env env, - napi_ref ref, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_open_handle_scope( - napi_env env, - napi_handle_scope* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_close_handle_scope( - napi_env env, - napi_handle_scope scope) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_open_escapable_handle_scope( - napi_env env, - napi_escapable_handle_scope* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_close_escapable_handle_scope( - napi_env env, - napi_escapable_handle_scope scope) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_escape_handle( - napi_env env, - napi_escapable_handle_scope scope, - napi_value escapee, - napi_value* result) { - return napi_ok; -} - -// Methods to support error handling - -NAPI_EXTERN napi_status -napi_throw( - napi_env env, - napi_value error) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_throw_error( - napi_env env, - const char* code, - const char* msg) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_throw_type_error( - napi_env env, - const char* code, - const char* msg) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_throw_range_error( - napi_env env, - const char* code, - const char* msg) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_error( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -// Methods to support catching exceptions - -NAPI_EXTERN napi_status -napi_is_exception_pending( - napi_env env, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_and_clear_last_exception( - napi_env env, - napi_value* result) { - return napi_ok; -} - -// Methods to work with array buffers and typed arrays - -NAPI_EXTERN napi_status -napi_is_arraybuffer( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_arraybuffer( - napi_env env, - size_t byte_length, - void** data, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_external_arraybuffer( - napi_env env, - void* external_data, - size_t byte_length, - napi_finalize finalize_cb, - void* finalize_hint, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_arraybuffer_info( - napi_env env, - napi_value arraybuffer, - void** data, - size_t* byte_length) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_typedarray( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_typedarray( - napi_env env, - napi_typedarray_type type, - size_t length, - napi_value arraybuffer, - size_t byte_offset, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_typedarray_info( - napi_env env, - napi_value typedarray, - napi_typedarray_type* type, - size_t* length, - void** data, - napi_value* arraybuffer, - size_t* byte_offset) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_dataview( - napi_env env, - size_t length, - napi_value arraybuffer, - size_t byte_offset, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_dataview( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_dataview_info( - napi_env env, - napi_value dataview, - size_t* bytelength, - void** data, - napi_value* arraybuffer, - size_t* byte_offset) { - return napi_ok; -} - -// Version management - -NAPI_EXTERN napi_status -napi_get_version( - napi_env env, - uint32_t* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_node_version( - napi_env env, - const napi_node_version** version) { - return napi_ok; -} - -// Promises - -NAPI_EXTERN napi_status -napi_create_promise( - napi_env env, - napi_deferred* deferred, - napi_value* promise) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_resolve_deferred( - napi_env env, - napi_deferred deferred, - napi_value resolution) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_reject_deferred( - napi_env env, - napi_deferred deferred, - napi_value rejection) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_promise( - napi_env env, - napi_value promise, - bool* is_promise) { - return napi_ok; -} - -// Running a script - -NAPI_EXTERN napi_status -napi_run_script( - napi_env env, - napi_value script, - napi_value* result) { - return napi_ok; -} - -// Memory management - -NAPI_EXTERN napi_status -napi_adjust_external_memory( - napi_env env, - int64_t change_in_bytes, - int64_t* adjusted_value) { - return napi_ok; -} - -#if NAPI_VERSION >= 5 - -// Dates - -NAPI_EXTERN napi_status -napi_create_date( - napi_env env, - double time, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_date( - napi_env env, - napi_value value, - bool* is_date) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_date_value( - napi_env env, - napi_value value, - double* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_add_finalizer( - napi_env env, - napi_value js_object, - void* native_object, - napi_finalize finalize_cb, - void* finalize_hint, - napi_ref* result) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 5 - -#if NAPI_VERSION >= 6 - -NAPI_EXTERN napi_status -napi_create_bigint_int64( - napi_env env, - int64_t value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_bigint_uint64( - napi_env env, - uint64_t value, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_bigint_words( - napi_env env, - int sign_bit, - size_t word_count, - const uint64_t* words, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_bigint_int64( - napi_env env, - napi_value value, - int64_t* result, - bool* lossless) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_bigint_uint64( - napi_env env, - napi_value value, - uint64_t* result, - bool* lossless) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_value_bigint_words( - napi_env env, - napi_value value, - int* sign_bit, - size_t* word_count, - uint64_t* words) { - return napi_ok; -} - -// Instance data -NAPI_EXTERN napi_status -napi_set_instance_data( - napi_env env, - void* data, - napi_finalize finalize_cb, - void* finalize_hint) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_instance_data( - napi_env env, - void** data) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_all_property_names( - napi_env env, - napi_value object, - napi_key_collection_mode key_mode, - napi_key_filter key_filter, - napi_key_conversion key_conversion, - napi_value* result) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 6 - -#if NAPI_VERSION >= 7 - -NAPI_EXTERN napi_status -napi_detach_arraybuffer( - napi_env env, - napi_value arraybuffer) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_detached_arraybuffer( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 7 - -#if NAPI_VERSION >= 8 - -// Type tagging -NAPI_EXTERN napi_status -napi_type_tag_object( - napi_env env, - napi_value value, - const napi_type_tag* type_tag) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_check_object_type_tag( - napi_env env, - napi_value value, - const napi_type_tag* type_tag, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_object_freeze( - napi_env env, - napi_value object) { - return napi_ok; - -} -NAPI_EXTERN napi_status -napi_object_seal( - napi_env env, - napi_value object) { - return napi_ok; -} -#endif // NAPI_VERSION >= 8 - -// ############################################################################## -// Stub for runtime API -// ############################################################################## - -NAPI_EXTERN void napi_module_register(napi_module* mod) {} - -void exit() __attribute__((noreturn)); - -NAPI_EXTERN NAPI_NO_RETURN -void napi_fatal_error( - const char* location, - size_t location_len, - const char* message, - size_t message_len) { - exit(0); -} - -// Methods for custom handling of async operations - -NAPI_EXTERN napi_status -napi_async_init( - napi_env env, - napi_value async_resource, - napi_value async_resource_name, - napi_async_context* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_async_destroy( - napi_env env, - napi_async_context async_context) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_make_callback( - napi_env env, - napi_async_context async_context, - napi_value recv, - napi_value func, - size_t argc, - const napi_value* argv, - napi_value* result) { - return napi_ok; -} - -// Methods to provide node::Buffer functionality with napi types - -NAPI_EXTERN napi_status -napi_create_buffer( - napi_env env, - size_t length, - void** data, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_external_buffer( - napi_env env, - size_t length, - void* data, - napi_finalize finalize_cb, - void* finalize_hint, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_create_buffer_copy( - napi_env env, - size_t length, - const void* data, - void** result_data, - napi_value* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_is_buffer( - napi_env env, - napi_value value, - bool* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_buffer_info( - napi_env env, - napi_value value, - void** data, - size_t* length) { - return napi_ok; -} - -// Methods to manage simple async operations - -NAPI_EXTERN napi_status -napi_create_async_work( - napi_env env, - napi_value async_resource, - napi_value async_resource_name, - napi_async_execute_callback execute, - napi_async_complete_callback complete, - void* data, - napi_async_work* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_delete_async_work( - napi_env env, - napi_async_work work) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_queue_async_work( - napi_env env, - napi_async_work work) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_cancel_async_work( - napi_env env, - napi_async_work work) { - return napi_ok; -} - -#if NAPI_VERSION >= 2 - -// Return the current libuv event loop for a given environment - -NAPI_EXTERN napi_status -napi_get_uv_event_loop( - napi_env env, - struct uv_loop_s** loop) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 2 - -#if NAPI_VERSION >= 3 - -NAPI_EXTERN napi_status -napi_fatal_exception(napi_env env, napi_value err) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_add_env_cleanup_hook( - napi_env env, - void (*fun)(void* arg), - void* arg) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_remove_env_cleanup_hook( - napi_env env, - void (*fun)(void* arg), - void* arg) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_open_callback_scope( - napi_env env, - napi_value resource_object, - napi_async_context context, - napi_callback_scope* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_close_callback_scope( - napi_env env, - napi_callback_scope scope) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 3 - -#if NAPI_VERSION >= 4 - -// Calling into JS from other threads - -NAPI_EXTERN napi_status -napi_create_threadsafe_function( - napi_env env, - napi_value func, - napi_value async_resource, - napi_value async_resource_name, - size_t max_queue_size, - size_t initial_thread_count, - void* thread_finalize_data, - napi_finalize thread_finalize_cb, - void* context, - napi_threadsafe_function_call_js call_js_cb, - napi_threadsafe_function* result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_get_threadsafe_function_context( - napi_threadsafe_function func, - void** result) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_call_threadsafe_function( - napi_threadsafe_function func, - void* data, - napi_threadsafe_function_call_mode is_blocking) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_acquire_threadsafe_function( - napi_threadsafe_function func) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_release_threadsafe_function( - napi_threadsafe_function func, - napi_threadsafe_function_release_mode mode) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_unref_threadsafe_function( - napi_env env, - napi_threadsafe_function func) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_ref_threadsafe_function( - napi_env env, - napi_threadsafe_function func) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 4 - -#if NAPI_VERSION >= 8 - -NAPI_EXTERN napi_status -napi_add_async_cleanup_hook( - napi_env env, - napi_async_cleanup_hook hook, - void* arg, - napi_async_cleanup_hook_handle* remove_handle) { - return napi_ok; -} - -NAPI_EXTERN napi_status -napi_remove_async_cleanup_hook( - napi_async_cleanup_hook_handle remove_handle) { - return napi_ok; -} - -#endif // NAPI_VERSION >= 8 - -#ifdef NAPI_EXPERIMENTAL - -NAPI_EXTERN napi_status -node_api_get_module_file_name( - napi_env env, const char** result) { - return napi_ok; -} - -#endif // NAPI_EXPERIMENTAL \ No newline at end of file diff --git a/node_api.h b/node_api.h deleted file mode 100644 index 1772c67..0000000 --- a/node_api.h +++ /dev/null @@ -1,275 +0,0 @@ -#ifndef SRC_NODE_API_H_ -#define SRC_NODE_API_H_ - -#ifdef BUILDING_NODE_EXTENSION - #ifdef _WIN32 - // Building native module against node - #define NAPI_EXTERN __declspec(dllimport) - #elif defined(__wasm32__) - #define NAPI_EXTERN __attribute__((__import_module__("napi"))) - #endif -#endif -#include "js_native_api.h" -#include "node_api_types.h" - -struct uv_loop_s; // Forward declaration. - -#ifdef _WIN32 -# define NAPI_MODULE_EXPORT __declspec(dllexport) -#else -# define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) -#endif - -#if defined(__GNUC__) -# define NAPI_NO_RETURN __attribute__((noreturn)) -#elif defined(_WIN32) -# define NAPI_NO_RETURN __declspec(noreturn) -#else -# define NAPI_NO_RETURN -#endif - -typedef napi_value (*napi_addon_register_func)(napi_env env, - napi_value exports); - -typedef struct napi_module { - int nm_version; - unsigned int nm_flags; - const char* nm_filename; - napi_addon_register_func nm_register_func; - const char* nm_modname; - void* nm_priv; - void* reserved[4]; -} napi_module; - -#define NAPI_MODULE_VERSION 1 - -#if defined(_MSC_VER) -#pragma section(".CRT$XCU", read) -#define NAPI_C_CTOR(fn) \ - static void __cdecl fn(void); \ - __declspec(dllexport, allocate(".CRT$XCU")) void(__cdecl * fn##_)(void) = \ - fn; \ - static void __cdecl fn(void) -#else -#define NAPI_C_CTOR(fn) \ - static void fn(void) __attribute__((constructor)); \ - static void fn(void) -#endif - -#define NAPI_MODULE_X(modname, regfunc, priv, flags) \ - EXTERN_C_START \ - static napi_module _module = \ - { \ - NAPI_MODULE_VERSION, \ - flags, \ - __FILE__, \ - regfunc, \ - #modname, \ - priv, \ - {0}, \ - }; \ - NAPI_C_CTOR(_register_ ## modname) { \ - napi_module_register(&_module); \ - } \ - EXTERN_C_END - -#define NAPI_MODULE_INITIALIZER_X(base, version) \ - NAPI_MODULE_INITIALIZER_X_HELPER(base, version) -#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version - -#ifdef __wasm32__ -#define NAPI_WASM_INITIALIZER \ - NAPI_MODULE_INITIALIZER_X(napi_register_wasm_v, NAPI_MODULE_VERSION) -#define NAPI_MODULE(modname, regfunc) \ - EXTERN_C_START \ - NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \ - napi_value exports) { \ - return regfunc(env, exports); \ - } \ - EXTERN_C_END -#else -#define NAPI_MODULE(modname, regfunc) \ - NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage) -#endif - -#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v - -#define NAPI_MODULE_INITIALIZER \ - NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, \ - NAPI_MODULE_VERSION) - -#define NAPI_MODULE_INIT() \ - EXTERN_C_START \ - NAPI_MODULE_EXPORT napi_value \ - NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports); \ - EXTERN_C_END \ - NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \ - napi_value NAPI_MODULE_INITIALIZER(napi_env env, \ - napi_value exports) - -EXTERN_C_START - -NAPI_EXTERN void napi_module_register(napi_module* mod); - -NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, - size_t location_len, - const char* message, - size_t message_len); - -// Methods for custom handling of async operations -NAPI_EXTERN napi_status napi_async_init(napi_env env, - napi_value async_resource, - napi_value async_resource_name, - napi_async_context* result); - -NAPI_EXTERN napi_status napi_async_destroy(napi_env env, - napi_async_context async_context); - -NAPI_EXTERN napi_status napi_make_callback(napi_env env, - napi_async_context async_context, - napi_value recv, - napi_value func, - size_t argc, - const napi_value* argv, - napi_value* result); - -// Methods to provide node::Buffer functionality with napi types -NAPI_EXTERN napi_status napi_create_buffer(napi_env env, - size_t length, - void** data, - napi_value* result); -NAPI_EXTERN napi_status napi_create_external_buffer(napi_env env, - size_t length, - void* data, - napi_finalize finalize_cb, - void* finalize_hint, - napi_value* result); -NAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env, - size_t length, - const void* data, - void** result_data, - napi_value* result); -NAPI_EXTERN napi_status napi_is_buffer(napi_env env, - napi_value value, - bool* result); -NAPI_EXTERN napi_status napi_get_buffer_info(napi_env env, - napi_value value, - void** data, - size_t* length); - -// Methods to manage simple async operations -NAPI_EXTERN -napi_status napi_create_async_work(napi_env env, - napi_value async_resource, - napi_value async_resource_name, - napi_async_execute_callback execute, - napi_async_complete_callback complete, - void* data, - napi_async_work* result); -NAPI_EXTERN napi_status napi_delete_async_work(napi_env env, - napi_async_work work); -NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, - napi_async_work work); -NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, - napi_async_work work); - -// version management -NAPI_EXTERN -napi_status napi_get_node_version(napi_env env, - const napi_node_version** version); - -#if NAPI_VERSION >= 2 - -// Return the current libuv event loop for a given environment -NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, - struct uv_loop_s** loop); - -#endif // NAPI_VERSION >= 2 - -#if NAPI_VERSION >= 3 - -NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err); - -NAPI_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env, - void (*fun)(void* arg), - void* arg); - -NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env, - void (*fun)(void* arg), - void* arg); - -NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env, - napi_value resource_object, - napi_async_context context, - napi_callback_scope* result); - -NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env, - napi_callback_scope scope); - -#endif // NAPI_VERSION >= 3 - -#if NAPI_VERSION >= 4 - -#ifndef __wasm32__ -// Calling into JS from other threads -NAPI_EXTERN napi_status -napi_create_threadsafe_function(napi_env env, - napi_value func, - napi_value async_resource, - napi_value async_resource_name, - size_t max_queue_size, - size_t initial_thread_count, - void* thread_finalize_data, - napi_finalize thread_finalize_cb, - void* context, - napi_threadsafe_function_call_js call_js_cb, - napi_threadsafe_function* result); - -NAPI_EXTERN napi_status -napi_get_threadsafe_function_context(napi_threadsafe_function func, - void** result); - -NAPI_EXTERN napi_status -napi_call_threadsafe_function(napi_threadsafe_function func, - void* data, - napi_threadsafe_function_call_mode is_blocking); - -NAPI_EXTERN napi_status -napi_acquire_threadsafe_function(napi_threadsafe_function func); - -NAPI_EXTERN napi_status -napi_release_threadsafe_function(napi_threadsafe_function func, - napi_threadsafe_function_release_mode mode); - -NAPI_EXTERN napi_status -napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func); - -NAPI_EXTERN napi_status -napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func); -#endif // __wasm32__ - -#endif // NAPI_VERSION >= 4 - -#if NAPI_VERSION >= 8 - -NAPI_EXTERN napi_status napi_add_async_cleanup_hook( - napi_env env, - napi_async_cleanup_hook hook, - void* arg, - napi_async_cleanup_hook_handle* remove_handle); - -NAPI_EXTERN napi_status napi_remove_async_cleanup_hook( - napi_async_cleanup_hook_handle remove_handle); - -#endif // NAPI_VERSION >= 8 - -#ifdef NAPI_EXPERIMENTAL - -NAPI_EXTERN napi_status -node_api_get_module_file_name(napi_env env, const char** result); - -#endif // NAPI_EXPERIMENTAL - -EXTERN_C_END - -#endif // SRC_NODE_API_H_ diff --git a/scripts/update-node-api-headers.cmake b/scripts/update-node-api-headers.cmake new file mode 100644 index 0000000..b965968 --- /dev/null +++ b/scripts/update-node-api-headers.cmake @@ -0,0 +1,40 @@ +# Refreshes include/ and def/ from the official node-api-headers npm package. +# cmake -DVERSION=1.9.0 -P scripts/update-node-api-headers.cmake +# Needs neither Node.js nor npm: the tarball is downloaded from the registry. +cmake_minimum_required(VERSION 3.18) + +if(NOT VERSION) + message(FATAL_ERROR "Pass -DVERSION= of node-api-headers") +endif() +get_filename_component(_root "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE) +set(_tmp "${_root}/.tmp-node-api-headers") +file(REMOVE_RECURSE "${_tmp}") +file(MAKE_DIRECTORY "${_tmp}") + +set(_url "https://registry.npmjs.org/node-api-headers/-/node-api-headers-${VERSION}.tgz") +file(DOWNLOAD "${_url}" "${_tmp}/pkg.tgz" STATUS _st TLS_VERIFY ON) +list(GET _st 0 _code) +if(NOT _code EQUAL 0) + file(REMOVE_RECURSE "${_tmp}") + message(FATAL_ERROR "Download failed: ${_url}\n${_st}") +endif() +file(ARCHIVE_EXTRACT INPUT "${_tmp}/pkg.tgz" DESTINATION "${_tmp}") + +# Check the package layout before touching include/ and def/: if anything is +# missing the repository is left unchanged. +foreach(_f include/node_api.h include/node_api_types.h include/js_native_api.h + include/js_native_api_types.h def/node_api.def def/js_native_api.def LICENSE) + if(NOT EXISTS "${_tmp}/package/${_f}") + file(REMOVE_RECURSE "${_tmp}") + message(FATAL_ERROR "node-api-headers ${VERSION}: '${_f}' not found in the package; nothing was changed") + endif() +endforeach() + +file(REMOVE_RECURSE "${_root}/include" "${_root}/def") +file(COPY "${_tmp}/package/include/" DESTINATION "${_root}/include") +file(COPY "${_tmp}/package/def/" DESTINATION "${_root}/def") +file(COPY "${_tmp}/package/LICENSE" DESTINATION "${_root}/def") +file(RENAME "${_root}/def/LICENSE" "${_root}/def/LICENSE-node-api-headers") +file(WRITE "${_root}/def/NODE_API_HEADERS_VERSION" "${VERSION}\n") +file(REMOVE_RECURSE "${_tmp}") +message(STATUS "node-api-headers ${VERSION} vendored into include/ and def/") diff --git a/src/win_delay_load_hook.c b/src/win_delay_load_hook.c new file mode 100644 index 0000000..be8c833 --- /dev/null +++ b/src/win_delay_load_hook.c @@ -0,0 +1,31 @@ +/* + * Delay-load hook for Node-API addons on Windows (MSVC / clang-cl). + * + * The import library declares the Node-API symbols as imported from "node.exe". + * When the host process has a different name (electron.exe, a renamed node, + * ...) normal loading would fail: with /DELAYLOAD:node.exe this hook returns + * the module of the current executable (or libnode.dll when present), and the + * loader resolves the symbols from it with GetProcAddress. + * C equivalent of node-gyp's win_delay_load_hook.cc. + */ +#if defined(_MSC_VER) + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include +#include + +static FARPROC WINAPI node_api_delay_load_hook(unsigned int event, PDelayLoadInfo info) { + HMODULE m; + if (event != dliNotePreLoadLibrary) return NULL; + if (_stricmp(info->szDll, "node.exe") != 0) return NULL; + m = GetModuleHandleA("libnode.dll"); /* Node built as a shared library */ + if (m == NULL) m = GetModuleHandleA(NULL); /* the host executable */ + return (FARPROC)m; +} + +const PfnDliHook __pfnDliNotifyHook2 = node_api_delay_load_hook; + +#endif /* _MSC_VER */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..dc586fb --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,61 @@ +find_program(NODE_EXECUTABLE node) +if(NOT NODE_EXECUTABLE) + message(WARNING "node-api-stub: node not found, the addons are built but not loaded") +endif() + +function(node_api_test_addon name) + add_library(${name} MODULE addon/addon.c) + set_target_properties(${name} PROPERTIES PREFIX "" SUFFIX ".node" C_VISIBILITY_PRESET hidden) + target_link_libraries(${name} PRIVATE ${ARGN}) +endfunction() + +set(_expect_real "const r=require(process.argv[1]); if(r!=='real-node-api'){console.error('got',r);process.exit(1)}") + +# 1. Recommended target, with the most hostile link options on ELF: +# -z defs (rejects undefined symbols) and -Bsymbolic (as go build -buildmode=c-shared). +node_api_test_addon(addon_nodeapi NodeApi::NodeApi) +if(NOT WIN32 AND NOT APPLE) + target_link_options(addon_nodeapi PRIVATE "LINKER:-z,defs" "LINKER:-Bsymbolic") +endif() +if(NODE_EXECUTABLE) + add_test(NAME load_nodeapi COMMAND ${NODE_EXECUTABLE} -e "${_expect_real}" $) +endif() + +# 2. Windows: the addon must also load from a host that is not called node.exe +# (delay-load hook). The node executable is copied under another name. +if(WIN32 AND MSVC AND NODE_API_WIN_DELAY_LOAD AND NODE_EXECUTABLE) + set(_renamed "${CMAKE_CURRENT_BINARY_DIR}/renamed-host.exe") + configure_file("${NODE_EXECUTABLE}" "${_renamed}" COPYONLY) + add_test(NAME load_nodeapi_renamed_host COMMAND "${_renamed}" -e "${_expect_real}" $) +endif() + +if(TARGET node_api_stub AND NOT WIN32 AND NOT APPLE) + # 3. Static stub + -Bsymbolic: must fail loudly (abort with a message). + node_api_test_addon(addon_stub_bsymbolic NodeApi::stub) + target_link_options(addon_stub_bsymbolic PRIVATE "LINKER:-Bsymbolic") + # 4. Static stub without -Bsymbolic: works through ELF symbol interposition. + node_api_test_addon(addon_stub_interpose NodeApi::stub) + if(NODE_EXECUTABLE) + add_test(NAME stub_bsymbolic_traps + COMMAND ${NODE_EXECUTABLE} -e "const c=require('child_process').spawnSync(process.execPath,['-e','require(process.argv[1])',process.argv[1]],{encoding:'utf8'});process.stderr.write(c.stderr);process.exit(c.signal==='SIGABRT'&&/node-api-stub: 'napi_create_string_utf8' was called/.test(c.stderr)?0:1)" + $) + add_test(NAME load_stub_interpose COMMAND ${NODE_EXECUTABLE} -e "${_expect_real}" $) + endif() +endif() + +if(TARGET node_api_stub_shared AND NOT WIN32 AND NOT APPLE) + # 5. Shared stub: correct even with -Bsymbolic, as long as the .so can be found at load time. + node_api_test_addon(addon_stub_shared NodeApi::stub_shared) + target_link_options(addon_stub_shared PRIVATE "LINKER:-Bsymbolic") + set_target_properties(addon_stub_shared PROPERTIES BUILD_RPATH "$") + if(NODE_EXECUTABLE) + add_test(NAME load_stub_shared COMMAND ${NODE_EXECUTABLE} -e "${_expect_real}" $) + endif() +endif() + +# The stub must export exactly the symbols listed in the .def file. +if(TARGET node_api_stub AND CMAKE_NM) + add_test(NAME stub_exports_all_symbols + COMMAND ${CMAKE_COMMAND} -DNM=${CMAKE_NM} -DLIB=$ + -DDEF=${PROJECT_SOURCE_DIR}/def/node_api.def -P ${CMAKE_CURRENT_SOURCE_DIR}/check_symbols.cmake) +endif() diff --git a/tests/addon/addon.c b/tests/addon/addon.c new file mode 100644 index 0000000..e732b93 --- /dev/null +++ b/tests/addon/addon.c @@ -0,0 +1,10 @@ +#include + +/* Returns "real-node-api" when the calls reach the host's Node-API. + If they reach a stub instead, the value stays NULL and `exports` is returned. */ +NAPI_MODULE_INIT() { + napi_value v = NULL; + if (napi_create_string_utf8(env, "real-node-api", NAPI_AUTO_LENGTH, &v) != napi_ok || v == NULL) + return exports; + return v; +} diff --git a/tests/check_symbols.cmake b/tests/check_symbols.cmake new file mode 100644 index 0000000..1e78398 --- /dev/null +++ b/tests/check_symbols.cmake @@ -0,0 +1,28 @@ +# Checks that every symbol in DEF is defined in LIB (nm output) and vice versa. +# cmake -DNM= -DLIB= -DDEF= -P check_symbols.cmake +include("${CMAKE_CURRENT_LIST_DIR}/../cmake/NodeApiStub.cmake") +node_api_read_symbols("${DEF}" _expected) + +execute_process(COMMAND "${NM}" -g --defined-only "${LIB}" + OUTPUT_VARIABLE _out RESULT_VARIABLE _rc) +if(NOT _rc EQUAL 0) + message(FATAL_ERROR "nm failed on ${LIB}") +endif() +string(REGEX MATCHALL "[ \t][Tt][ \t]+_?((napi|node_api)_[A-Za-z0-9_]+)" _matches "${_out}") +set(_defined "") +foreach(_m IN LISTS _matches) + string(REGEX REPLACE ".*[Tt][ \t]+_?" "" _s "${_m}") + list(APPEND _defined "${_s}") +endforeach() +list(REMOVE_DUPLICATES _defined) + +set(_missing ${_expected}) +list(REMOVE_ITEM _missing ${_defined}) +set(_extra ${_defined}) +list(REMOVE_ITEM _extra ${_expected}) +list(LENGTH _expected _ne) +list(LENGTH _defined _nd) +if(_missing OR _extra) + message(FATAL_ERROR "stub symbols mismatch (expected ${_ne}, defined ${_nd})\nmissing: ${_missing}\nextra: ${_extra}") +endif() +message(STATUS "stub defines all ${_ne} Node-API symbols")