From 06f6e3fd3202b90691695ea5ddb60be7bfd7f34a Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Wed, 24 Jan 2024 21:18:50 +0800 Subject: [PATCH] test cpp and entry --- .github/workflows/main.yml | 3 +++ binding.gyp | 3 ++- index.js | 32 ++++++++++++++++++++++++++++---- src/binding.c | 26 +------------------------- src/common.h | 30 ++++++++++++++++++++++++++++++ src/hello.cpp | 9 +++++++++ 6 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 src/common.h create mode 100644 src/hello.cpp diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 554ab1b..5fe361f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -85,3 +85,6 @@ jobs: - name: NPM Build shell: bash run: npm run build-${{ matrix.target }}${{ (matrix.thread == 'ON' && '-threads') || '' }}:${{ (matrix.os == 'windows-latest' && 'win') || 'unix' }} + + - name: NPM Start + run: npm start diff --git a/binding.gyp b/binding.gyp index 8d06b95..0dcc60c 100644 --- a/binding.gyp +++ b/binding.gyp @@ -3,7 +3,8 @@ { "target_name": "binding", "sources": [ - "src/binding.c" + "src/binding.c", + "src/hello.cpp", ], "conditions": [ [ diff --git a/index.js b/index.js index 0dcabb2..9e8a825 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,38 @@ const path = require('path') +const fs = require('fs') const emnapi = require('@emnapi/runtime') -const ext = path.extname(require.resolve('./build/Release/binding.node')) -const init = require('./build/Release/binding.node') +const entry = (() => { + try { + return require.resolve('./build/Release/binding.node') + } catch (_) { + return require.resolve('./build/Release/binding.wasm') + } +})() + +const ext = path.extname(entry) module.exports = function () { if (ext === '.js') { - return init().then(Module => { + return require(entry).then(Module => { return Module.emnapiInit({ context: emnapi.getDefaultContext() }) }) } - return Promise.resolve().then(() => init) + if (ext === '.node') { + return Promise.resolve().then(() => require(entry)) + } + if (ext === '.wasm') { + const { instantiateNapiModule } = require('@emnapi/core') + return instantiateNapiModule(fs.readFileSync(entry), { + context: emnapi.getDefaultContext(), + wasi: new (require('wasi').WASI)({ version: 'preview1' }), + overwriteImports (imports) { + imports.env.memory = new WebAssembly.Memory({ + initial: 16777216 / 65536, + maximum: 2147483648 / 65536, + shared: true + }) + } + }).then(({ napiModule }) => napiModule.exports) + } } diff --git a/src/binding.c b/src/binding.c index 9e1668b..4b01c33 100644 --- a/src/binding.c +++ b/src/binding.c @@ -1,29 +1,5 @@ #include - -#define NAPI_CALL(env, the_call) \ - do { \ - if ((the_call) != napi_ok) { \ - const napi_extended_error_info *error_info; \ - napi_get_last_error_info((env), &error_info); \ - bool is_pending; \ - const char* err_message = error_info->error_message; \ - napi_is_exception_pending((env), &is_pending); \ - if (!is_pending) { \ - const char* error_message = err_message != NULL ? \ - err_message : \ - "empty error message"; \ - napi_throw_error((env), NULL, error_message); \ - } \ - return NULL; \ - } \ - } while (0) - -static napi_value js_hello(napi_env env, napi_callback_info info) { - napi_value world; - const char* str = "world"; - NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &world)); - return world; -} +#include "./common.h" NAPI_MODULE_INIT() { napi_value hello; diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..510df5a --- /dev/null +++ b/src/common.h @@ -0,0 +1,30 @@ +#ifndef SRC_COMMON_H_ +#define SRC_COMMON_H_ + +#define NAPI_CALL(env, the_call) \ + do { \ + if ((the_call) != napi_ok) { \ + const napi_extended_error_info *error_info; \ + napi_get_last_error_info((env), &error_info); \ + bool is_pending; \ + const char* err_message = error_info->error_message; \ + napi_is_exception_pending((env), &is_pending); \ + if (!is_pending) { \ + const char* error_message = err_message != NULL ? \ + err_message : \ + "empty error message"; \ + napi_throw_error((env), NULL, error_message); \ + } \ + return NULL; \ + } \ + } while (0) + +#endif + +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C +#endif + +EXTERN_C napi_value js_hello(napi_env env, napi_callback_info info); diff --git a/src/hello.cpp b/src/hello.cpp new file mode 100644 index 0000000..622adc3 --- /dev/null +++ b/src/hello.cpp @@ -0,0 +1,9 @@ +#include +#include "./common.h" + +napi_value js_hello(napi_env env, napi_callback_info info) { + napi_value world = nullptr; + const char* str = "world"; + NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &world)); + return world; +}