From a957d7d3b7d7795a9b5baaa74e8c9626f2041742 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 24 Feb 2023 15:07:04 -0800 Subject: [PATCH] Add runtime check for MIN_NODE_VERSION (#18845) See https://github.com/emscripten-core/emsdk/pull/1189 --- src/parseTools.js | 7 +++++++ src/shell.js | 11 +++++++++++ test/test_other.py | 7 +++++++ 3 files changed, 25 insertions(+) diff --git a/src/parseTools.js b/src/parseTools.js index dcb819816d968..931963072f0c7 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -995,3 +995,10 @@ function preJS() { } return result; } + +function formattedMinNodeVersion() { + var major = MIN_NODE_VERSION / 10000 + var minor = (MIN_NODE_VERSION / 100) % 100 + var rev = MIN_NODE_VERSION % 100 + return `v${major}.${minor}.${rev}`; +} diff --git a/src/shell.js b/src/shell.js index c668429c58e74..33763dec4c4b5 100644 --- a/src/shell.js +++ b/src/shell.js @@ -177,6 +177,17 @@ if (ENVIRONMENT_IS_NODE) { #if ENVIRONMENT && ASSERTIONS if (typeof process == 'undefined' || !process.release || process.release.name !== 'node') throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); #endif + +#if ASSERTIONS + var nodeVersion = process.versions.node; + var numericVersion = nodeVersion.split('.').slice(0, 3); + numericVersion = (numericVersion[0] * 10000) + (numericVersion[1] * 100) + numericVersion[2] * 1; + var minVersion = {{{ MIN_NODE_VERSION }}}; + if (numericVersion < {{{ MIN_NODE_VERSION }}}) { + throw new Error('This emscripten-generated code requires node {{{ formattedMinNodeVersion() }}} (detected v' + nodeVersion + ')'); + } +#endif + // `require()` is no-op in an ESM module, use `createRequire()` to construct // the require()` function. This is only necessary for multi-environment // builds, `-sENVIRONMENT=node` emits a static import declaration instead. diff --git a/test/test_other.py b/test/test_other.py index 06168bbccf3db..ddc88812bd68e 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -13026,3 +13026,10 @@ def test_parseTools_legacy(self): self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', 'foo') self.do_runf(test_file('hello_world.c'), '4\nhello, world!', emcc_args=['--post-js=post.js', '--js-library=lib.js']) + + def test_min_node_version(self): + node_version = shared.check_node_version() + node_version = '.'.join(str(x) for x in node_version) + self.set_setting('MIN_NODE_VERSION', 210000) + expected = 'This emscripten-generated code requires node v21.0.0 (detected v%s)' % node_version + self.do_runf(test_file('hello_world.c'), expected, assert_returncode=NON_ZERO)