From 48a43dedf0c9b1169ed9ec764e1779fbc3fb93c0 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 15:29:44 +0100 Subject: [PATCH 1/6] chore(deps): bump the all group across 1 directory with 3 updates (#1582) Bumps the all group with 3 updates in the / directory: [actions/checkout](https://github.com/actions/checkout), [actions/setup-node](https://github.com/actions/setup-node) and [github/codeql-action](https://github.com/github/codeql-action). Updates `actions/checkout` from 4.1.7 to 4.2.0 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/692973e3d937129bcbf40652eb9f2f61becf3332...d632683dd7b4114ad314bca15554477dd762a938) Updates `actions/setup-node` from 4.0.3 to 4.0.4 - [Release notes](https://github.com/actions/setup-node/releases) - [Commits](https://github.com/actions/setup-node/compare/1e60f620b9541d16bece96c5465dc8ee9832be0b...0a44ba7841725637a19e28fa30b79a866c81b0a6) Updates `github/codeql-action` from 3.26.7 to 3.26.9 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/8214744c546c1e5c8f03dde8fab3a7353211988d...461ef6c76dfe95d5c364de2f431ddbd31a417628) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-minor dependency-group: all - dependency-name: actions/setup-node dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/ci-win.yml | 4 ++-- .github/workflows/ci.yml | 4 ++-- .github/workflows/codeql.yml | 8 ++++---- .github/workflows/coverage-linux.yml | 4 ++-- .github/workflows/dependency-review.yml | 2 +- .github/workflows/linter.yml | 4 ++-- .github/workflows/release-please.yml | 4 ++-- .github/workflows/scorecards.yml | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci-win.yml b/.github/workflows/ci-win.yml index e091988fd..e42f13e2d 100644 --- a/.github/workflows/ci-win.yml +++ b/.github/workflows/ci-win.yml @@ -32,13 +32,13 @@ jobs: with: egress-policy: audit - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 - name: Set up Python ${{ env.PYTHON_VERSION }} uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0 with: python-version: ${{ env.PYTHON_VERSION }} - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: node-version: ${{ matrix.node-version }} architecture: ${{ matrix.architecture }} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 596946b70..2627be989 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,13 +37,13 @@ jobs: with: egress-policy: audit - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 - name: Set up Python ${{ env.PYTHON_VERSION }} uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0 with: python-version: ${{ env.PYTHON_VERSION }} - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: node-version: ${{ matrix.node-version }} - name: Check Node.js installation diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 442adf1bd..577644889 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -46,11 +46,11 @@ jobs: egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs - name: Checkout repository - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7 + uses: github/codeql-action/init@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -70,7 +70,7 @@ jobs: - name: Use Node.js v18.x if: matrix.language == 'cpp' - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: node-version: 18.x @@ -80,6 +80,6 @@ jobs: npx node-gyp rebuild -C test - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7 + uses: github/codeql-action/analyze@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9 with: category: "/language:${{matrix.language}}" diff --git a/.github/workflows/coverage-linux.yml b/.github/workflows/coverage-linux.yml index ae837297c..d54805285 100644 --- a/.github/workflows/coverage-linux.yml +++ b/.github/workflows/coverage-linux.yml @@ -39,7 +39,7 @@ jobs: with: egress-policy: audit - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 with: persist-credentials: false - name: Set up Python ${{ env.PYTHON_VERSION }} @@ -47,7 +47,7 @@ jobs: with: python-version: ${{ env.PYTHON_VERSION }} - name: Use Node.js ${{ env.NODE_VERSION }} - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: node-version: ${{ env.NODE_VERSION }} - name: Environment Information diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 4e488882a..456f9621f 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -22,6 +22,6 @@ jobs: egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs - name: 'Checkout Repository' - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 - name: 'Dependency Review' uses: actions/dependency-review-action@5a2ce3f5b92ee19cbb1541a4984c76d921601d7c # v4.3.4 diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 732862b7e..668cd918a 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -20,12 +20,12 @@ jobs: with: egress-policy: audit - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 with: fetch-depth: 0 - run: git branch -a - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index 01d678ec7..63e3d3b2a 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -36,8 +36,8 @@ jobs: with: egress-policy: audit - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3 + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 + - uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: node-version: lts/* registry-url: 'https://registry.npmjs.org' diff --git a/.github/workflows/scorecards.yml b/.github/workflows/scorecards.yml index bdbf90cf2..65a5749cd 100644 --- a/.github/workflows/scorecards.yml +++ b/.github/workflows/scorecards.yml @@ -36,7 +36,7 @@ jobs: egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs - name: "Checkout code" - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 with: persist-credentials: false @@ -71,6 +71,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7 + uses: github/codeql-action/upload-sarif@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9 with: sarif_file: results.sarif From d8523a708030a0a3abb9d7832051c70e2dafac3d Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Thu, 3 Oct 2024 10:42:53 +0100 Subject: [PATCH 2/6] src: enable type cast checks in tests (#1581) --- doc/value.md | 13 +++++ napi-inl.h | 22 ++++++-- napi.h | 6 +++ test/async_worker.cc | 6 +-- test/binding.gyp | 3 +- test/function.js | 2 +- .../global_object_delete_property.cc | 8 +-- .../global_object_get_property.cc | 8 +-- .../global_object_has_own_property.cc | 6 +-- .../global_object_set_property.cc | 10 ++-- test/name.cc | 18 ++++--- test/object/delete_property.cc | 10 ++-- test/object/get_property.cc | 10 ++-- test/object/has_own_property.cc | 8 +-- test/object/has_property.cc | 10 ++-- test/object/object.cc | 2 +- test/object/set_property.cc | 10 ++-- test/object/subscript_operator.cc | 6 +-- test/object_reference.cc | 36 ++++++------- test/run_script.cc | 2 +- test/type_taggable.cc | 2 +- test/typedarray.cc | 52 ++++++++++--------- 22 files changed, 146 insertions(+), 104 deletions(-) diff --git a/doc/value.md b/doc/value.md index 90e358236..f19532fa5 100644 --- a/doc/value.md +++ b/doc/value.md @@ -86,6 +86,19 @@ In order to enforce expected type, use `Napi::Value::Is*()` methods to check the type before calling `Napi::Value::As()`, or compile with definition `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` to enforce type checks. +### UnsafeAs + +```cpp +template T Napi::Value::UnsafeAs() const; +``` + +Casts to another type of `Napi::Value`, when the actual type is known or +assumed. + +This conversion does not coerce the type. This does not check the type even if +`NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` is defined. This indicates intentional +unsafe type cast. Use `Napi::Value::As()` if possible. + ### Env ```cpp diff --git a/napi-inl.h b/napi-inl.h index e7e21a7b3..8631b9f9b 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -896,6 +896,16 @@ inline T Value::As() const { return T(_env, _value); } +template +inline T Value::UnsafeAs() const { + return T(_env, _value); +} + +// static +inline void Value::CheckCast(napi_env /* env */, napi_value value) { + NAPI_CHECK(value != nullptr, "Value::CheckCast", "empty value"); +} + inline MaybeOrValue Value::ToBoolean() const { napi_value result; napi_status status = napi_coerce_to_bool(_env, _value, &result); @@ -1303,12 +1313,15 @@ inline Symbol Symbol::New(napi_env env, napi_value description) { inline MaybeOrValue Symbol::WellKnown(napi_env env, const std::string& name) { + // No need to check if the return value is a symbol or undefined. + // Well known symbols are definite and it is an develop time error + // if the symbol does not exist. #if defined(NODE_ADDON_API_ENABLE_MAYBE) Value symbol_obj; Value symbol_value; if (Napi::Env(env).Global().Get("Symbol").UnwrapTo(&symbol_obj) && symbol_obj.As().Get(name).UnwrapTo(&symbol_value)) { - return Just(symbol_value.As()); + return Just(symbol_value.UnsafeAs()); } return Nothing(); #else @@ -1317,7 +1330,7 @@ inline MaybeOrValue Symbol::WellKnown(napi_env env, .Get("Symbol") .As() .Get(name) - .As(); + .UnsafeAs(); #endif } @@ -1535,7 +1548,10 @@ inline void Object::CheckCast(napi_env env, napi_value value) { napi_valuetype type; napi_status status = napi_typeof(env, value, &type); NAPI_CHECK(status == napi_ok, "Object::CheckCast", "napi_typeof failed"); - NAPI_INTERNAL_CHECK_EQ(type, napi_object, "%d", "Object::CheckCast"); + NAPI_INTERNAL_CHECK(type == napi_object || type == napi_function, + "Object::CheckCast", + "Expect napi_object or napi_function, but got %d.", + type); } inline Object::Object() : TypeTaggable() {} diff --git a/napi.h b/napi.h index edec5111e..ac896080d 100644 --- a/napi.h +++ b/napi.h @@ -461,6 +461,8 @@ class Value { template static Value From(napi_env env, const T& value); + static void CheckCast(napi_env env, napi_value value); + /// Converts to a Node-API value primitive. /// /// If the instance is _empty_, this returns `nullptr`. @@ -527,6 +529,10 @@ class Value { template T As() const; + // Unsafe Value::As(), should be avoided. + template + T UnsafeAs() const; + MaybeOrValue ToBoolean() const; ///< Coerces a value to a JavaScript boolean. MaybeOrValue ToNumber() diff --git a/test/async_worker.cc b/test/async_worker.cc index c0a20cb5c..34044e9c8 100644 --- a/test/async_worker.cc +++ b/test/async_worker.cc @@ -20,14 +20,14 @@ class TestWorkerWithUserDefRecv : public AsyncWorker { static void DoWorkWithAsyncRes(const CallbackInfo& info) { Object recv = info[0].As(); Function cb = info[1].As(); - Object resource = info[2].As(); + Value resource = info[2]; TestWorkerWithUserDefRecv* worker = nullptr; if (resource == info.Env().Null()) { worker = new TestWorkerWithUserDefRecv(recv, cb, "TestResource"); } else { - worker = - new TestWorkerWithUserDefRecv(recv, cb, "TestResource", resource); + worker = new TestWorkerWithUserDefRecv( + recv, cb, "TestResource", resource.As()); } worker->Queue(); diff --git a/test/binding.gyp b/test/binding.gyp index 28de3fe96..df8d5e84a 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -106,7 +106,8 @@ { 'target_name': 'binding', 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], - 'sources': ['>@(build_sources)'] + 'sources': ['>@(build_sources)'], + 'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS'] }, { 'target_name': 'binding_noexcept', diff --git a/test/function.js b/test/function.js index 04b020394..7cd8c04e2 100644 --- a/test/function.js +++ b/test/function.js @@ -89,7 +89,7 @@ function test (binding) { assert.deepStrictEqual(args, [7, 8, 9]); assert.throws(() => { - binding.callWithInvalidReceiver(); + binding.callWithInvalidReceiver(() => {}); }, /Invalid (pointer passed as )?argument/); obj = binding.callConstructorWithArgs(testConstructor, 5, 6, 7); diff --git a/test/globalObject/global_object_delete_property.cc b/test/globalObject/global_object_delete_property.cc index 295ed3f36..70738c2f4 100644 --- a/test/globalObject/global_object_delete_property.cc +++ b/test/globalObject/global_object_delete_property.cc @@ -5,27 +5,27 @@ using namespace Napi; Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New( info.Env(), MaybeUnwrap(globalObject.Delete(key.Utf8Value().c_str()))); } Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key.Utf8Value()))); } Value DeletePropertyWithInt32AsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Number key = info[0].As(); + Number key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key.Uint32Value()))); } Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key))); } diff --git a/test/globalObject/global_object_get_property.cc b/test/globalObject/global_object_get_property.cc index dd112043c..81f727d91 100644 --- a/test/globalObject/global_object_get_property.cc +++ b/test/globalObject/global_object_get_property.cc @@ -5,25 +5,25 @@ using namespace Napi; Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); return MaybeUnwrap(globalObject.Get(key)); } Value GetPropertyWithInt32AsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Number key = info[0].As(); + Number key = info[0].UnsafeAs(); return MaybeUnwrapOr(globalObject.Get(key.Uint32Value()), Value()); } Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String cStrkey = info[0].As(); + String cStrkey = info[0].UnsafeAs(); return MaybeUnwrapOr(globalObject.Get(cStrkey.Utf8Value().c_str()), Value()); } Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String cppStrKey = info[0].As(); + String cppStrKey = info[0].UnsafeAs(); return MaybeUnwrapOr(globalObject.Get(cppStrKey.Utf8Value()), Value()); } diff --git a/test/globalObject/global_object_has_own_property.cc b/test/globalObject/global_object_has_own_property.cc index 89c299913..388788d97 100644 --- a/test/globalObject/global_object_has_own_property.cc +++ b/test/globalObject/global_object_has_own_property.cc @@ -5,7 +5,7 @@ using namespace Napi; Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New( info.Env(), MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value().c_str()), @@ -14,7 +14,7 @@ Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); return Boolean::New( info.Env(), MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value()), false)); @@ -22,7 +22,7 @@ Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); return Boolean::New(info.Env(), MaybeUnwrap(globalObject.HasOwnProperty(key))); } diff --git a/test/globalObject/global_object_set_property.cc b/test/globalObject/global_object_set_property.cc index 7065bee56..06da6315a 100644 --- a/test/globalObject/global_object_set_property.cc +++ b/test/globalObject/global_object_set_property.cc @@ -4,28 +4,28 @@ using namespace Napi; void SetPropertyWithCStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key.Utf8Value().c_str(), value); } void SetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - String key = info[0].As(); + String key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key.Utf8Value(), value); } void SetPropertyWithInt32AsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Number key = info[0].As(); + Number key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key.Uint32Value(), value); } void SetPropertyWithNapiValueAsKey(const CallbackInfo& info) { Object globalObject = info.Env().Global(); - Name key = info[0].As(); + Name key = info[0].UnsafeAs(); Value value = info[1]; globalObject.Set(key, value); -} \ No newline at end of file +} diff --git a/test/name.cc b/test/name.cc index d8556e154..27bab9312 100644 --- a/test/name.cc +++ b/test/name.cc @@ -21,19 +21,21 @@ Value EchoString(const CallbackInfo& info) { Value CreateString(const CallbackInfo& info) { String encoding = info[0].As(); - Number length = info[1].As(); + Value length = info[1]; if (encoding.Utf8Value() == "utf8") { if (length.IsUndefined()) { return String::New(info.Env(), testValueUtf8); } else { - return String::New(info.Env(), testValueUtf8, length.Uint32Value()); + return String::New( + info.Env(), testValueUtf8, length.As().Uint32Value()); } } else if (encoding.Utf8Value() == "utf16") { if (length.IsUndefined()) { return String::New(info.Env(), testValueUtf16); } else { - return String::New(info.Env(), testValueUtf16, length.Uint32Value()); + return String::New( + info.Env(), testValueUtf16, length.As().Uint32Value()); } } else { Error::New(info.Env(), "Invalid encoding.").ThrowAsJavaScriptException(); @@ -44,12 +46,12 @@ Value CreateString(const CallbackInfo& info) { Value CheckString(const CallbackInfo& info) { String value = info[0].As(); String encoding = info[1].As(); - Number length = info[2].As(); + Value length = info[2]; if (encoding.Utf8Value() == "utf8") { std::string testValue = testValueUtf8; if (!length.IsUndefined()) { - testValue = testValue.substr(0, length.Uint32Value()); + testValue = testValue.substr(0, length.As().Uint32Value()); } std::string stringValue = value; @@ -57,7 +59,7 @@ Value CheckString(const CallbackInfo& info) { } else if (encoding.Utf8Value() == "utf16") { std::u16string testValue = testValueUtf16; if (!length.IsUndefined()) { - testValue = testValue.substr(0, length.Uint32Value()); + testValue = testValue.substr(0, length.As().Uint32Value()); } std::u16string stringValue = value; @@ -69,10 +71,10 @@ Value CheckString(const CallbackInfo& info) { } Value CreateSymbol(const CallbackInfo& info) { - String description = info[0].As(); + Value description = info[0]; if (!description.IsUndefined()) { - return Symbol::New(info.Env(), description); + return Symbol::New(info.Env(), description.As()); } else { return Symbol::New(info.Env()); } diff --git a/test/object/delete_property.cc b/test/object/delete_property.cc index ca69e5387..b05af20cc 100644 --- a/test/object/delete_property.cc +++ b/test/object/delete_property.cc @@ -4,13 +4,13 @@ using namespace Napi; Value DeletePropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrap(obj.Delete(key.Uint32Value()))); } Value DeletePropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New( info.Env(), @@ -18,20 +18,20 @@ Value DeletePropertyWithNapiValue(const CallbackInfo& info) { } Value DeletePropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(key), false)); } Value DeletePropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New( info.Env(), MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value().c_str()), false)); } Value DeletePropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value()), false)); diff --git a/test/object/get_property.cc b/test/object/get_property.cc index 523f99199..2791ad2aa 100644 --- a/test/object/get_property.cc +++ b/test/object/get_property.cc @@ -4,31 +4,31 @@ using namespace Napi; Value GetPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return MaybeUnwrapOr(obj.Get(static_cast(key)), Value()); } Value GetPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return MaybeUnwrapOr(obj.Get(key), Value()); } Value GetPropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number key = info[1].As(); return MaybeUnwrap(obj.Get(key.Uint32Value())); } Value GetPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value().c_str()), Value()); } Value GetPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value()), Value()); } diff --git a/test/object/has_own_property.cc b/test/object/has_own_property.cc index d7fbde98b..b566fefbb 100644 --- a/test/object/has_own_property.cc +++ b/test/object/has_own_property.cc @@ -4,7 +4,7 @@ using namespace Napi; Value HasOwnPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New( info.Env(), @@ -12,14 +12,14 @@ Value HasOwnPropertyWithNapiValue(const CallbackInfo& info) { } Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(key), false)); } Value HasOwnPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New( info.Env(), @@ -27,7 +27,7 @@ Value HasOwnPropertyWithCStyleString(const CallbackInfo& info) { } Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New( info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(jsKey.Utf8Value()), false)); diff --git a/test/object/has_property.cc b/test/object/has_property.cc index fa410833f..46c13de30 100644 --- a/test/object/has_property.cc +++ b/test/object/has_property.cc @@ -4,34 +4,34 @@ using namespace Napi; Value HasPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New( info.Env(), MaybeUnwrapOr(obj.Has(static_cast(key)), false)); } Value HasPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(key), false)); } Value HasPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(jsKey.Utf8Value().c_str()), false)); } Value HasPropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(jsKey.Uint32Value()), false)); } Value HasPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Has(jsKey.Utf8Value()), false)); diff --git a/test/object/object.cc b/test/object/object.cc index f6f0bd98b..be0bcadde 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -338,7 +338,7 @@ void Increment(const CallbackInfo& info) { #endif // NAPI_CPP_EXCEPTIONS Value InstanceOf(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Function constructor = info[1].As(); return Boolean::New(info.Env(), MaybeUnwrap(obj.InstanceOf(constructor))); } diff --git a/test/object/set_property.cc b/test/object/set_property.cc index 8171568d6..da8c93bbd 100644 --- a/test/object/set_property.cc +++ b/test/object/set_property.cc @@ -4,7 +4,7 @@ using namespace Napi; Value SetPropertyWithNapiValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); Value value = info[2]; return Boolean::New( @@ -13,14 +13,14 @@ Value SetPropertyWithNapiValue(const CallbackInfo& info) { } Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Name key = info[1].As(); Value value = info[2]; return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Set(key, value), false)); } Value SetPropertyWithUint32(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); Number key = info[1].As(); Value value = info[2]; return Boolean::New(info.Env(), @@ -28,7 +28,7 @@ Value SetPropertyWithUint32(const CallbackInfo& info) { } Value SetPropertyWithCStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); Value value = info[2]; return Boolean::New( @@ -37,7 +37,7 @@ Value SetPropertyWithCStyleString(const CallbackInfo& info) { } Value SetPropertyWithCppStyleString(const CallbackInfo& info) { - Object obj = info[0].As(); + Object obj = info[0].UnsafeAs(); String jsKey = info[1].As(); Value value = info[2]; return Boolean::New(info.Env(), diff --git a/test/object/subscript_operator.cc b/test/object/subscript_operator.cc index 7f94b2dd3..15bb74620 100644 --- a/test/object/subscript_operator.cc +++ b/test/object/subscript_operator.cc @@ -8,7 +8,7 @@ Value SubscriptGetWithCStyleString(const CallbackInfo& info) { // make sure const case compiles const Object obj2 = info[0].As(); - MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As(); + MaybeUnwrap(obj2[jsKey.Utf8Value().c_str()]).As(); Object obj = info[0].As(); return obj[jsKey.Utf8Value().c_str()]; @@ -19,7 +19,7 @@ Value SubscriptGetWithCppStyleString(const CallbackInfo& info) { // make sure const case compiles const Object obj2 = info[0].As(); - MaybeUnwrap(obj2[jsKey.Utf8Value()]).As(); + MaybeUnwrap(obj2[jsKey.Utf8Value()]).As(); Object obj = info[0].As(); return obj[jsKey.Utf8Value()]; @@ -30,7 +30,7 @@ Value SubscriptGetAtIndex(const CallbackInfo& info) { // make sure const case compiles const Object obj2 = info[0].As(); - MaybeUnwrap(obj2[index]).As(); + MaybeUnwrap(obj2[index]).As(); Object obj = info[0].As(); return obj[index]; diff --git a/test/object_reference.cc b/test/object_reference.cc index 2d3fc4807..e74b14b2c 100644 --- a/test/object_reference.cc +++ b/test/object_reference.cc @@ -224,13 +224,13 @@ void SetCastedObjects(const CallbackInfo& info) { Value GetFromValue(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { return weak.Value(); } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { return persistent.Value(); } else { return reference.Value(); @@ -290,7 +290,7 @@ Value GetFromGetters(const CallbackInfo& info) { Value GetFromGetter(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { @@ -300,7 +300,7 @@ Value GetFromGetter(const CallbackInfo& info) { return MaybeUnwrap(weak.Get(info[1].As().Uint32Value())); } } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { if (info[1].IsString()) { return MaybeUnwrap(persistent.Get(info[1].As().Utf8Value())); } else if (info[1].IsNumber()) { @@ -322,13 +322,13 @@ Value GetFromGetter(const CallbackInfo& info) { Value GetCastedFromValue(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (casted_weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { return casted_weak.Value(); } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { return casted_persistent.Value(); } else { return casted_reference.Value(); @@ -341,13 +341,13 @@ Value GetCastedFromValue(const CallbackInfo& info) { Value GetCastedFromGetter(const CallbackInfo& info) { Env env = info.Env(); - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { if (casted_weak.IsEmpty()) { return String::New(env, "No Referenced Value"); } else { return MaybeUnwrap(casted_weak.Get(info[1].As())); } - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { return MaybeUnwrap(casted_persistent.Get(info[1].As())); } else { return MaybeUnwrap(casted_reference.Get(info[1].As())); @@ -360,15 +360,15 @@ Number UnrefObjects(const CallbackInfo& info) { Env env = info.Env(); uint32_t num; - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { num = weak.Unref(); - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { num = persistent.Unref(); - } else if (info[0].As() == String::New(env, "references")) { + } else if (info[0] == String::New(env, "references")) { num = reference.Unref(); - } else if (info[0].As() == String::New(env, "casted weak")) { + } else if (info[0] == String::New(env, "casted weak")) { num = casted_weak.Unref(); - } else if (info[0].As() == String::New(env, "casted persistent")) { + } else if (info[0] == String::New(env, "casted persistent")) { num = casted_persistent.Unref(); } else { num = casted_reference.Unref(); @@ -383,15 +383,15 @@ Number RefObjects(const CallbackInfo& info) { Env env = info.Env(); uint32_t num; - if (info[0].As() == String::New(env, "weak")) { + if (info[0] == String::New(env, "weak")) { num = weak.Ref(); - } else if (info[0].As() == String::New(env, "persistent")) { + } else if (info[0] == String::New(env, "persistent")) { num = persistent.Ref(); - } else if (info[0].As() == String::New(env, "references")) { + } else if (info[0] == String::New(env, "references")) { num = reference.Ref(); - } else if (info[0].As() == String::New(env, "casted weak")) { + } else if (info[0] == String::New(env, "casted weak")) { num = casted_weak.Ref(); - } else if (info[0].As() == String::New(env, "casted persistent")) { + } else if (info[0] == String::New(env, "casted persistent")) { num = casted_persistent.Ref(); } else { num = casted_reference.Ref(); diff --git a/test/run_script.cc b/test/run_script.cc index cc7a74b71..e957df9eb 100644 --- a/test/run_script.cc +++ b/test/run_script.cc @@ -18,7 +18,7 @@ Value RunStdString(const CallbackInfo& info) { Value RunJsString(const CallbackInfo& info) { Env env = info.Env(); - return MaybeUnwrapOr(env.RunScript(info[0].As()), Value()); + return MaybeUnwrapOr(env.RunScript(info[0].UnsafeAs()), Value()); } Value RunWithContext(const CallbackInfo& info) { diff --git a/test/type_taggable.cc b/test/type_taggable.cc index 2320dd81c..ac58f9281 100644 --- a/test/type_taggable.cc +++ b/test/type_taggable.cc @@ -26,7 +26,7 @@ class TestTypeTaggable { static Value CheckTypeTag(const CallbackInfo& info) { uint32_t type_index = info[0].As().Int32Value(); - TypeTaggable instance = info[1].As(); + TypeTaggable instance = info[1].UnsafeAs(); return Boolean::New(info.Env(), instance.CheckTypeTag(&type_tags[type_index])); diff --git a/test/typedarray.cc b/test/typedarray.cc index 3ee9f3f2a..0b9e0970e 100644 --- a/test/typedarray.cc +++ b/test/typedarray.cc @@ -21,7 +21,7 @@ namespace { Value CreateTypedArray(const CallbackInfo& info) { std::string arrayType = info[0].As(); size_t length = info[1].As().Uint32Value(); - ArrayBuffer buffer = info[2].As(); + Value buffer = info[2]; size_t bufferOffset = info[3].IsUndefined() ? 0 : info[3].As().Uint32Value(); @@ -32,7 +32,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Int8Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_int8_array); } else if (arrayType == "uint8") { @@ -42,7 +42,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Uint8Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint8_array); } else if (arrayType == "uint8_clamped") { @@ -50,7 +50,7 @@ Value CreateTypedArray(const CallbackInfo& info) { ? Uint8Array::New(info.Env(), length, napi_uint8_clamped_array) : Uint8Array::New(info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint8_clamped_array); } else if (arrayType == "int16") { @@ -60,7 +60,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Int16Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_int16_array); } else if (arrayType == "uint16") { @@ -70,7 +70,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Uint16Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint16_array); } else if (arrayType == "int32") { @@ -80,7 +80,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Int32Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_int32_array); } else if (arrayType == "uint32") { @@ -90,7 +90,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Uint32Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_uint32_array); } else if (arrayType == "float32") { @@ -100,7 +100,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Float32Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_float32_array); } else if (arrayType == "float64") { @@ -110,7 +110,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(Float64Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_float64_array); #if (NAPI_VERSION > 5) @@ -121,7 +121,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(BigInt64Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_bigint64_array); } else if (arrayType == "biguint64") { @@ -131,7 +131,7 @@ Value CreateTypedArray(const CallbackInfo& info) { : NAPI_TYPEDARRAY_NEW_BUFFER(BigUint64Array, info.Env(), length, - buffer, + buffer.As(), bufferOffset, napi_biguint64_array); #endif @@ -208,8 +208,8 @@ Value CheckBufferContent(const CallbackInfo& info) { case napi_uint8_array: return Boolean::New( info.Env(), - TypedArrayDataIsEquivalent(info[0].As(), - info[1].As())); + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); case napi_uint8_clamped_array: return Boolean::New( @@ -335,35 +335,39 @@ Value GetTypedArrayElement(const CallbackInfo& info) { void SetTypedArrayElement(const CallbackInfo& info) { TypedArray array = info[0].As(); size_t index = info[1].As().Uint32Value(); - Number value = info[2].As(); + Value value = info[2]; switch (array.TypedArrayType()) { case napi_int8_array: - array.As()[index] = static_cast(value.Int32Value()); + array.As()[index] = + static_cast(value.As().Int32Value()); break; case napi_uint8_array: - array.As()[index] = static_cast(value.Uint32Value()); + array.As()[index] = + static_cast(value.As().Uint32Value()); break; case napi_uint8_clamped_array: - array.As()[index] = static_cast(value.Uint32Value()); + array.As()[index] = + static_cast(value.As().Uint32Value()); break; case napi_int16_array: - array.As()[index] = static_cast(value.Int32Value()); + array.As()[index] = + static_cast(value.As().Int32Value()); break; case napi_uint16_array: array.As()[index] = - static_cast(value.Uint32Value()); + static_cast(value.As().Uint32Value()); break; case napi_int32_array: - array.As()[index] = value.Int32Value(); + array.As()[index] = value.As().Int32Value(); break; case napi_uint32_array: - array.As()[index] = value.Uint32Value(); + array.As()[index] = value.As().Uint32Value(); break; case napi_float32_array: - array.As()[index] = value.FloatValue(); + array.As()[index] = value.As().FloatValue(); break; case napi_float64_array: - array.As()[index] = value.DoubleValue(); + array.As()[index] = value.As().DoubleValue(); break; #if (NAPI_VERSION > 5) case napi_bigint64_array: { From f12f2f570235515805cc17375793d46edcd23549 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Mon, 7 Oct 2024 08:50:26 +0100 Subject: [PATCH 3/6] doc: amend outdated doc about multiple environments (#1586) --- napi.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/napi.h b/napi.h index ac896080d..4710c66ba 100644 --- a/napi.h +++ b/napi.h @@ -307,8 +307,7 @@ using MaybeOrValue = T; /// Node-API operations within the callback. (Many methods infer the /// environment from the `this` instance that the method is called on.) /// -/// In the future, multiple environments per process may be supported, -/// although current implementations only support one environment per process. +/// Multiple environments may co-exist in a single process or a thread. /// /// In the V8 JavaScript engine, a Node-API environment approximately /// corresponds to an Isolate. From f3eaae6026edb8706c4d10b0cca480b06a5fe295 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:24:24 -0400 Subject: [PATCH 4/6] chore(deps): bump the all group with 2 updates (#1587) Bumps the all group with 2 updates: [github/codeql-action](https://github.com/github/codeql-action) and [codecov/codecov-action](https://github.com/codecov/codecov-action). Updates `github/codeql-action` from 3.26.9 to 3.26.11 - [Release notes](https://github.com/github/codeql-action/releases) - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/github/codeql-action/compare/461ef6c76dfe95d5c364de2f431ddbd31a417628...6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea) Updates `codecov/codecov-action` from 4.5.0 to 4.6.0 - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/e28ff129e5465c2c0dcc6f003fc735cb6ae0c673...b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238) --- updated-dependencies: - dependency-name: github/codeql-action dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor dependency-group: all ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql.yml | 4 ++-- .github/workflows/coverage-linux.yml | 2 +- .github/workflows/scorecards.yml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 577644889..6f89ce734 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -50,7 +50,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9 + uses: github/codeql-action/init@6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea # v3.26.11 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -80,6 +80,6 @@ jobs: npx node-gyp rebuild -C test - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9 + uses: github/codeql-action/analyze@6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea # v3.26.11 with: category: "/language:${{matrix.language}}" diff --git a/.github/workflows/coverage-linux.yml b/.github/workflows/coverage-linux.yml index d54805285..6294aa5ff 100644 --- a/.github/workflows/coverage-linux.yml +++ b/.github/workflows/coverage-linux.yml @@ -63,7 +63,7 @@ jobs: run: | npm run report-coverage-xml - name: Upload - uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0 + uses: codecov/codecov-action@b9fd7d16f6d7d1b5d2bec1a2887e65ceed900238 # v4.6.0 with: directory: ./coverage-xml token: ${{ secrets.CODECOV_TOKEN }} # To bypass public API rate limiting, see https://github.com/codecov/codecov-action/issues/557 diff --git a/.github/workflows/scorecards.yml b/.github/workflows/scorecards.yml index 65a5749cd..1f037be55 100644 --- a/.github/workflows/scorecards.yml +++ b/.github/workflows/scorecards.yml @@ -71,6 +71,6 @@ jobs: # Upload the results to GitHub's code scanning dashboard. - name: "Upload to code-scanning" - uses: github/codeql-action/upload-sarif@461ef6c76dfe95d5c364de2f431ddbd31a417628 # v3.26.9 + uses: github/codeql-action/upload-sarif@6db8d6351fd0be61f9ed8ebd12ccd35dcec51fea # v3.26.11 with: sarif_file: results.sarif From 6ba3891954d8b56215d133e54a86cb621e476b9e Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Wed, 9 Oct 2024 08:20:41 +0900 Subject: [PATCH 5/6] fix: missing node_api_nogc_env definition (#1585) --- .github/workflows/node-api-headers.yml | 71 ++++++++++++++++++++++++++ napi-inl.h | 31 +++++------ napi.h | 19 +++++-- test/binding.gyp | 7 ++- 4 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/node-api-headers.yml diff --git a/.github/workflows/node-api-headers.yml b/.github/workflows/node-api-headers.yml new file mode 100644 index 000000000..b64002026 --- /dev/null +++ b/.github/workflows/node-api-headers.yml @@ -0,0 +1,71 @@ +name: Node.js CI with node-api-headers + +on: [push, pull_request] + +env: + PYTHON_VERSION: '3.11' + +permissions: + contents: read + +jobs: + test: + timeout-minutes: 30 + strategy: + fail-fast: false + matrix: + api_version: + - '9' + node-version: + - 22.x + node-api-headers-version: + - '1.1.0' + - '1.2.0' + - '1.3.0' + os: + - ubuntu-latest + compiler: + - gcc + - clang + runs-on: ${{ matrix.os }} + steps: + - name: Harden Runner + uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1 + with: + egress-policy: audit + + - uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 # v4.2.0 + - name: Set up Python ${{ env.PYTHON_VERSION }} + uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3 # v5.2.0 + with: + python-version: ${{ env.PYTHON_VERSION }} + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 + with: + node-version: ${{ matrix.node-version }} + - name: Check Node.js installation + run: | + node --version + npm --version + - name: Install dependencies + run: | + npm install + npm install "node-api-headers@${{ matrix.node-api-headers-version }}" + - name: npm test + run: | + export NAPI_VERSION=${{ matrix.api_version }} + if [ "${{ matrix.compiler }}" = "gcc" ]; then + export CC="gcc" CXX="g++" + fi + if [ "${{ matrix.compiler }}" = "clang" ]; then + export CC="clang" CXX="clang++" + fi + echo "CC=\"$CC\" CXX=\"$CXX\"" + echo "$CC --version" + $CC --version + echo "$CXX --version" + $CXX --version + export CFLAGS="$CFLAGS -O3 --coverage" LDFLAGS="$LDFLAGS --coverage" + export use_node_api_headers=true + echo "CFLAGS=\"$CFLAGS\" LDFLAGS=\"$LDFLAGS\"" + npm run pretest -- --verbose diff --git a/napi-inl.h b/napi-inl.h index 8631b9f9b..c3034e299 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -35,7 +35,7 @@ namespace details { constexpr int napi_no_external_buffers_allowed = 22; template -inline void default_basic_finalizer(node_api_nogc_env /*env*/, +inline void default_basic_finalizer(node_addon_api_basic_env /*env*/, void* data, void* /*hint*/) { delete static_cast(data); @@ -45,8 +45,9 @@ inline void default_basic_finalizer(node_api_nogc_env /*env*/, // garbage-collected. // TODO: Replace this code with `napi_add_finalizer()` whenever it becomes // available on all supported versions of Node.js. -template > +template < + typename FreeType, + node_addon_api_basic_finalize finalizer = default_basic_finalizer> inline napi_status AttachData(napi_env env, napi_value obj, FreeType* data, @@ -192,10 +193,10 @@ template struct FinalizeData { #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER template >> + typename = std::enable_if_t< + std::is_invocable_v>> #endif - static inline void Wrapper(node_api_nogc_env env, + static inline void Wrapper(node_addon_api_basic_env env, void* data, void* finalizeHint) NAPI_NOEXCEPT { WrapVoidCallback([&] { @@ -208,9 +209,9 @@ struct FinalizeData { #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER template >, + !std::is_invocable_v>, typename = void> - static inline void Wrapper(node_api_nogc_env env, + static inline void Wrapper(node_addon_api_basic_env env, void* data, void* finalizeHint) NAPI_NOEXCEPT { #ifdef NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS @@ -228,9 +229,9 @@ struct FinalizeData { #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER template >> + std::is_invocable_v>> #endif - static inline void WrapperWithHint(node_api_nogc_env env, + static inline void WrapperWithHint(node_addon_api_basic_env env, void* data, void* finalizeHint) NAPI_NOEXCEPT { WrapVoidCallback([&] { @@ -243,9 +244,9 @@ struct FinalizeData { #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER template >, + !std::is_invocable_v>, typename = void> - static inline void WrapperWithHint(node_api_nogc_env env, + static inline void WrapperWithHint(node_addon_api_basic_env env, void* data, void* finalizeHint) NAPI_NOEXCEPT { #ifdef NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS @@ -576,9 +577,9 @@ inline Maybe Just(const T& t) { // BasicEnv / Env class //////////////////////////////////////////////////////////////////////////////// -inline BasicEnv::BasicEnv(node_api_nogc_env env) : _env(env) {} +inline BasicEnv::BasicEnv(node_addon_api_basic_env env) : _env(env) {} -inline BasicEnv::operator node_api_nogc_env() const { +inline BasicEnv::operator node_addon_api_basic_env() const { return _env; } @@ -5034,7 +5035,7 @@ inline napi_value ObjectWrap::StaticSetterCallbackWrapper( } template -inline void ObjectWrap::FinalizeCallback(node_api_nogc_env env, +inline void ObjectWrap::FinalizeCallback(node_addon_api_basic_env env, void* data, void* /*hint*/) { // If the child class does not override _any_ Finalize() method, `env` will be diff --git a/napi.h b/napi.h index 4710c66ba..6e1343e51 100644 --- a/napi.h +++ b/napi.h @@ -299,6 +299,14 @@ template using MaybeOrValue = T; #endif +#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER +using node_addon_api_basic_env = node_api_nogc_env; +using node_addon_api_basic_finalize = node_api_nogc_finalize; +#else +using node_addon_api_basic_env = napi_env; +using node_addon_api_basic_finalize = napi_finalize; +#endif + /// Environment for Node-API values and operations. /// /// All Node-API values and operations must be associated with an environment. @@ -313,7 +321,7 @@ using MaybeOrValue = T; /// corresponds to an Isolate. class BasicEnv { private: - node_api_nogc_env _env; + node_addon_api_basic_env _env; #if NAPI_VERSION > 5 template static void DefaultFini(Env, T* data); @@ -321,8 +329,9 @@ class BasicEnv { static void DefaultFiniWithHint(Env, DataType* data, HintType* hint); #endif // NAPI_VERSION > 5 public: - BasicEnv(node_api_nogc_env env); - operator node_api_nogc_env() const; + BasicEnv(node_addon_api_basic_env env); + + operator node_addon_api_basic_env() const; // Without these operator overloads, the error: // @@ -2469,7 +2478,9 @@ class ObjectWrap : public InstanceWrap, public Reference { napi_callback_info info); static napi_value StaticSetterCallbackWrapper(napi_env env, napi_callback_info info); - static void FinalizeCallback(node_api_nogc_env env, void* data, void* hint); + static void FinalizeCallback(node_addon_api_basic_env env, + void* data, + void* hint); static void PostFinalizeCallback(napi_env env, void* data, void* hint); diff --git a/test/binding.gyp b/test/binding.gyp index df8d5e84a..3dd861d0b 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -89,6 +89,7 @@ 'value_type_cast.cc' ], 'want_coverage': ' Date: Fri, 11 Oct 2024 11:06:33 -0400 Subject: [PATCH 6/6] chore: release v8.2.1 (#1589) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .release-please-manifest.json | 2 +- CHANGELOG.md | 8 ++++++++ README.md | 2 +- package.json | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.release-please-manifest.json b/.release-please-manifest.json index f59c11ca8..7d53cf127 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "8.2.0" + ".": "8.2.1" } diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a745ffe8..0b3d1ce54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # node-addon-api Changelog +## [8.2.1](https://github.com/nodejs/node-addon-api/compare/v8.2.0...v8.2.1) (2024-10-09) + + +### Bug Fixes + +* failed type cast checks in Symbol::WellKnown ([#1581](https://github.com/nodejs/node-addon-api/issues/1581)) ([d8523a7](https://github.com/nodejs/node-addon-api/commit/d8523a708030a0a3abb9d7832051c70e2dafac3d)) +* missing node_api_nogc_env definition ([#1585](https://github.com/nodejs/node-addon-api/issues/1585)) ([6ba3891](https://github.com/nodejs/node-addon-api/commit/6ba3891954d8b56215d133e54a86cb621e476b9e)) + ## [8.2.0](https://github.com/nodejs/node-addon-api/compare/v8.1.0...v8.2.0) (2024-09-19) diff --git a/README.md b/README.md index 1ce351de7..4c52acb1b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ and exception handling semantics with low overhead. API references are available in the [doc](doc/README.md) directory. -## Current version: 8.2.0 +## Current version: 8.2.1 (See [CHANGELOG.md](CHANGELOG.md) for complete Changelog) diff --git a/package.json b/package.json index 93ec78495..16ffcf180 100644 --- a/package.json +++ b/package.json @@ -478,7 +478,7 @@ "lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix" }, "pre-commit": "lint", - "version": "8.2.0", + "version": "8.2.1", "support": true, "engines": { "node": "^18 || ^20 || >= 21"