Skip to content

Commit

Permalink
test cpp and entry
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jan 24, 2024
1 parent 98d3c2d commit 06f6e3f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
{
"target_name": "binding",
"sources": [
"src/binding.c"
"src/binding.c",
"src/hello.cpp",
],
"conditions": [
[
Expand Down
32 changes: 28 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
26 changes: 1 addition & 25 deletions src/binding.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
#include <node_api.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)

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;
Expand Down
30 changes: 30 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
@@ -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);
9 changes: 9 additions & 0 deletions src/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <js_native_api.h>
#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;
}

0 comments on commit 06f6e3f

Please sign in to comment.