Skip to content

Commit

Permalink
add emnapi_get_runtime_version (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Nov 12, 2023
1 parent 3de6220 commit 4ddd5f2
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 48 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion packages/emnapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
12 changes: 8 additions & 4 deletions packages/emnapi/include/emnapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/emnapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
3 changes: 2 additions & 1 deletion packages/emnapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}
},
"scripts": {
"build": "node ./script/build.js"
"build": "node ./script/build.js",
"version": "node ./script/version.js"
},
"repository": {
"type": "git",
Expand Down
21 changes: 21 additions & 0 deletions packages/emnapi/script/version.js
Original file line number Diff line number Diff line change
@@ -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)
})
21 changes: 0 additions & 21 deletions packages/emnapi/src/emnapi.c

This file was deleted.

27 changes: 27 additions & 0 deletions packages/emnapi/src/emnapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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)
21 changes: 8 additions & 13 deletions packages/test/emnapitest/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ 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));
NODE_API_CALL(env, napi_set_element(env, result, 1, minor));
NODE_API_CALL(env, napi_set_element(env, result, 2, patch));
return result;
}
#endif

static void FinalizeCallback(napi_env env,
void* finalize_data,
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 5 additions & 7 deletions packages/test/emnapitest/emnapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})

0 comments on commit 4ddd5f2

Please sign in to comment.