-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98d3c2d
commit 06f6e3f
Showing
6 changed files
with
73 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |