diff --git a/package.json b/package.json index 65265376..d9cf4f40 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "test": "npm run test -w packages/test", "test:version": "node ./packages/test/script/test-build-all-napi-version.js", "release": "node ./script/release.js", + "bump": "npm version -w packages/emnapi -w packages/runtime -w packages/core -w packages/node", "lint": "eslint \"packages/**/*.ts\"" }, "author": "toyobayashi", diff --git a/packages/emnapi/CMakeLists.txt b/packages/emnapi/CMakeLists.txt index d1e8ec04..3356bbce 100644 --- a/packages/emnapi/CMakeLists.txt +++ b/packages/emnapi/CMakeLists.txt @@ -29,7 +29,6 @@ set(UV_SRC set(ENAPI_BASIC_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/js_native_api.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/node_api.c" - "${CMAKE_CURRENT_SOURCE_DIR}/src/emnapi.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/async_cleanup_hook.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/async_context.c" ) diff --git a/packages/emnapi/include/emnapi.h b/packages/emnapi/include/emnapi.h index 413ffbb5..ab9556d3 100644 --- a/packages/emnapi/include/emnapi.h +++ b/packages/emnapi/include/emnapi.h @@ -5,6 +5,10 @@ #include "js_native_api_types.h" #include "emnapi_common.h" +#define EMNAPI_MAJOR_VERSION 0 +#define EMNAPI_MINOR_VERSION 44 +#define EMNAPI_PATCH_VERSION 0 + typedef enum { emnapi_runtime, emnapi_userland, @@ -41,17 +45,17 @@ EMNAPI_EXTERN napi_status emnapi_get_module_property(napi_env env, const char* utf8name, napi_value* result); +#endif typedef struct { uint32_t major; uint32_t minor; uint32_t patch; -} emnapi_emscripten_version; +} emnapi_runtime_version; EMNAPI_EXTERN -napi_status emnapi_get_emscripten_version(napi_env env, - const emnapi_emscripten_version** version); -#endif +napi_status emnapi_get_runtime_version(napi_env env, + emnapi_runtime_version* version); EMNAPI_EXTERN napi_status emnapi_create_memory_view(napi_env env, diff --git a/packages/emnapi/index.js b/packages/emnapi/index.js index 3e09b92d..05424c61 100644 --- a/packages/emnapi/index.js +++ b/packages/emnapi/index.js @@ -9,7 +9,6 @@ const jsLibrary = path.join(__dirname, './dist/library_napi.js') const sources = [ path.join(__dirname, './src/js_native_api.c'), path.join(__dirname, './src/node_api.c'), - path.join(__dirname, './src/emnapi.c'), path.join(__dirname, './src/async_cleanup_hook.c'), path.join(__dirname, './src/async_context.c'), path.join(__dirname, './src/async_work.c'), diff --git a/packages/emnapi/package.json b/packages/emnapi/package.json index 2ea75474..205e7042 100644 --- a/packages/emnapi/package.json +++ b/packages/emnapi/package.json @@ -12,7 +12,8 @@ } }, "scripts": { - "build": "node ./script/build.js" + "build": "node ./script/build.js", + "version": "node ./script/version.js" }, "repository": { "type": "git", diff --git a/packages/emnapi/script/version.js b/packages/emnapi/script/version.js new file mode 100644 index 00000000..8b6eda78 --- /dev/null +++ b/packages/emnapi/script/version.js @@ -0,0 +1,21 @@ +const fs = require('fs-extra') +const path = require('path') + +async function main () { + const [major, minor, patch] = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8')).version.split('.').map(Number) + const headerPath = path.join(__dirname, '../include/emnapi.h') + const emnapiHeader = fs.readFileSync(headerPath, 'utf8') + fs.writeFileSync( + headerPath, + emnapiHeader + .replace(/#define EMNAPI_MAJOR_VERSION (\d+)/, `#define EMNAPI_MAJOR_VERSION ${major}`) + .replace(/#define EMNAPI_MINOR_VERSION (\d+)/, `#define EMNAPI_MINOR_VERSION ${minor}`) + .replace(/#define EMNAPI_PATCH_VERSION (\d+)/, `#define EMNAPI_PATCH_VERSION ${patch}`), + 'utf8' + ) +} + +main().catch(err => { + console.error(err) + process.exit(1) +}) diff --git a/packages/emnapi/src/emnapi.c b/packages/emnapi/src/emnapi.c deleted file mode 100644 index a8d2e00f..00000000 --- a/packages/emnapi/src/emnapi.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "emnapi_internal.h" - -EXTERN_C_START - -#ifdef __EMSCRIPTEN__ -napi_status -emnapi_get_emscripten_version(napi_env env, - const emnapi_emscripten_version** version) { - CHECK_ENV(env); - CHECK_ARG(env, version); - static emnapi_emscripten_version emscripten_version = { - __EMSCRIPTEN_major__, - __EMSCRIPTEN_minor__, - __EMSCRIPTEN_tiny__ - }; - *version = &emscripten_version; - return napi_clear_last_error(env); -} -#endif - -EXTERN_C_END diff --git a/packages/emnapi/src/emnapi.ts b/packages/emnapi/src/emnapi.ts index 607f27ac..547951e1 100644 --- a/packages/emnapi/src/emnapi.ts +++ b/packages/emnapi/src/emnapi.ts @@ -250,6 +250,31 @@ function emnapi_get_memory_address (env: napi_env, arraybuffer_or_view: napi_val }) } +function emnapi_get_runtime_version (env: napi_env, version: number): napi_status { + $CHECK_ENV!(env) + const envObject = emnapiCtx.envStore.get(env)! + $CHECK_ARG!(envObject, version) + + let runtimeVersion: string + try { + runtimeVersion = emnapiCtx.getRuntimeVersions().version + } catch (_) { + return envObject.setLastError(napi_status.napi_generic_failure) + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const versions = runtimeVersion.split('.') + .map((n: string) => Number(n)) as [number, number, number] + + $from64('version') + + $makeSetValue('version', 0, 'versions[0]', 'u32') + $makeSetValue('version', 4, 'versions[1]', 'u32') + $makeSetValue('version', 8, 'versions[2]', 'u32') + + return envObject.clearLastError() +} + emnapiImplementHelper('$emnapiSyncMemory', undefined, emnapiSyncMemory, ['$emnapiExternalMemory'], 'syncMemory') emnapiImplementHelper('$emnapiGetMemoryAddress', undefined, emnapiGetMemoryAddress, ['$emnapiExternalMemory'], 'getMemoryAddress') @@ -260,3 +285,5 @@ emnapiImplement2('emnapi_is_node_binding_available', 'i', emnapi_is_node_binding emnapiImplement2('emnapi_create_memory_view', 'ipippppp', _emnapi_create_memory_view, ['napi_add_finalizer', '$emnapiExternalMemory']) emnapiImplement2('emnapi_sync_memory', 'ipippp', emnapi_sync_memory, ['$emnapiSyncMemory']) emnapiImplement2('emnapi_get_memory_address', 'ipppp', emnapi_get_memory_address, ['$emnapiGetMemoryAddress']) + +emnapiImplement2('emnapi_get_runtime_version', 'ipp', emnapi_get_runtime_version) diff --git a/packages/test/emnapitest/binding.c b/packages/test/emnapitest/binding.c index 1c7b0739..e5de758e 100644 --- a/packages/test/emnapitest/binding.c +++ b/packages/test/emnapitest/binding.c @@ -38,14 +38,15 @@ static napi_value getModuleProperty(napi_env env, napi_callback_info info) { return result; } +#endif -static napi_value testGetEmscriptenVersion(napi_env env, napi_callback_info info) { - const emnapi_emscripten_version* emscripten_version; +static napi_value testGetRuntimeVersion(napi_env env, napi_callback_info info) { + emnapi_runtime_version runtime_version; napi_value result, major, minor, patch; - NODE_API_CALL(env, emnapi_get_emscripten_version(env, &emscripten_version)); - NODE_API_CALL(env, napi_create_uint32(env, emscripten_version->major, &major)); - NODE_API_CALL(env, napi_create_uint32(env, emscripten_version->minor, &minor)); - NODE_API_CALL(env, napi_create_uint32(env, emscripten_version->patch, &patch)); + NODE_API_CALL(env, emnapi_get_runtime_version(env, &runtime_version)); + NODE_API_CALL(env, napi_create_uint32(env, runtime_version.major, &major)); + NODE_API_CALL(env, napi_create_uint32(env, runtime_version.minor, &minor)); + NODE_API_CALL(env, napi_create_uint32(env, runtime_version.patch, &patch)); NODE_API_CALL(env, napi_create_array_with_length(env, 3, &result)); NODE_API_CALL(env, napi_set_element(env, result, 0, major)); @@ -53,7 +54,6 @@ static napi_value testGetEmscriptenVersion(napi_env env, napi_callback_info info NODE_API_CALL(env, napi_set_element(env, result, 2, patch)); return result; } -#endif static void FinalizeCallback(napi_env env, void* finalize_data, @@ -113,17 +113,12 @@ static napi_value NullArrayBuffer(napi_env env, napi_callback_info info) { EXTERN_C_START napi_value Init(napi_env env, napi_value exports) { -#ifdef __EMSCRIPTEN__ - const emnapi_emscripten_version* emscripten_version; - NODE_API_CALL(env, emnapi_get_emscripten_version(env, &emscripten_version)); - printf("Init: Emscripten v%u.%u.%u\n", emscripten_version->major, emscripten_version->minor, emscripten_version->patch); -#endif napi_property_descriptor descriptors[] = { #ifdef __EMSCRIPTEN__ DECLARE_NODE_API_PROPERTY("getModuleObject", getModuleObject), DECLARE_NODE_API_PROPERTY("getModuleProperty", getModuleProperty), - DECLARE_NODE_API_PROPERTY("testGetEmscriptenVersion", testGetEmscriptenVersion), #endif + DECLARE_NODE_API_PROPERTY("testGetRuntimeVersion", testGetRuntimeVersion), DECLARE_NODE_API_PROPERTY("External", External), DECLARE_NODE_API_PROPERTY("NullArrayBuffer", NullArrayBuffer), DECLARE_NODE_API_PROPERTY("GrowMemory", GrowMemory), diff --git a/packages/test/emnapitest/emnapi.test.js b/packages/test/emnapitest/emnapi.test.js index e6e6f595..3c0f5f1d 100644 --- a/packages/test/emnapitest/emnapi.test.js +++ b/packages/test/emnapitest/emnapi.test.js @@ -40,11 +40,9 @@ module.exports = promise.then(test_typedarray => { } assert.notStrictEqual(buffer.buffer.byteLength, 0) - if (!process.env.EMNAPI_TEST_WASI && !process.env.EMNAPI_TEST_WASM32) { - const [major, minor, patch] = test_typedarray.testGetEmscriptenVersion() - assert.strictEqual(typeof major, 'number') - assert.strictEqual(typeof minor, 'number') - assert.strictEqual(typeof patch, 'number') - console.log(`test: Emscripten v${major}.${minor}.${patch}`) - } + const [major, minor, patch] = test_typedarray.testGetRuntimeVersion() + assert.strictEqual(typeof major, 'number') + assert.strictEqual(typeof minor, 'number') + assert.strictEqual(typeof patch, 'number') + console.log(`@emnapi/runtime: v${major}.${minor}.${patch}`) })