From 9a3b4e921c0cdcc3bd8096f547532866b7c1ade0 Mon Sep 17 00:00:00 2001 From: hackyminer Date: Sun, 6 Sep 2020 01:42:36 +0900 Subject: [PATCH 1/5] support Node 12,14 * use Nan::Call() (revert commit #5341887ef) * use Nan::NewInstance() * fix deprecated IsNearDeath Co-Authored-By: Aleksey Smolenchuk --- README.md | 6 ------ src/weakref.cc | 29 +++-------------------------- test/exports.js | 1 - 3 files changed, 3 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index c153bb5..f1ebe98 100644 --- a/README.md +++ b/README.md @@ -125,12 +125,6 @@ Checks to see if `ref` is a dead reference. Returns `true` if the original Objec has already been GC'd, `false` otherwise. -### Boolean weak.isNearDeath(Weakref ref) - -Checks to see if `ref` is "near death". This will be `true` exactly during the -weak reference callback function, and `false` any other time. - - ### Boolean weak.isWeakRef(Object obj) Checks to see if `obj` is "weak reference" instance. Returns `true` if the diff --git a/src/weakref.cc b/src/weakref.cc index 32f0790..5e95f30 100644 --- a/src/weakref.cc +++ b/src/weakref.cc @@ -140,8 +140,7 @@ NAN_PROPERTY_ENUMERATOR(WeakPropertyEnumerator) { } /** - * Weakref callback function. Invokes the "global" callback function, - * which emits the _CB event on the per-object EventEmitter. + * Weakref callback function. Invokes the "global" callback function. */ static void TargetCallback(const Nan::WeakCallbackInfo &info) { @@ -152,11 +151,7 @@ static void TargetCallback(const Nan::WeakCallbackInfo &info) { Local argv[] = { Nan::New(cont->emitter) }; - // Invoke callback directly, not via Nan::Callback->Call() which uses - // node::MakeCallback() which calls into process._tickCallback() - // too. Those other callbacks are not safe to run from here. - v8::Local globalCallbackDirect = globalCallback->GetFunction(); - globalCallbackDirect->Call(Nan::GetCurrentContext()->Global(), 1, argv); + Nan::Call(*globalCallback, 1, argv); // clean everything up Local proxy = Nan::New(cont->proxy); @@ -177,7 +172,7 @@ NAN_METHOD(Create) { Local _target = info[0].As(); Local _emitter = info[1].As(); - Local proxy = Nan::New(proxyClass)->NewInstance(); + Local proxy = Nan::NewInstance(Nan::New(proxyClass)).ToLocalChecked(); cont->proxy.Reset(proxy); cont->emitter.Reset(_emitter); cont->target.Reset(_target); @@ -224,23 +219,6 @@ NAN_METHOD(Get) { info.GetReturnValue().Set(Unwrap(proxy)); } -/** - * `isNearDeath(weakref)` JS function. - */ - -NAN_METHOD(IsNearDeath) { - WEAKREF_FIRST_ARG - - proxy_container *cont = reinterpret_cast( - Nan::GetInternalFieldPointer(proxy, FIELD_INDEX_CONTAINER) - ); - assert(cont != NULL); - - Local rtn = Nan::New(cont->target.IsNearDeath()); - - info.GetReturnValue().Set(rtn); -} - /** * `isDead(weakref)` JS function. */ @@ -293,7 +271,6 @@ NAN_MODULE_INIT(Initialize) { Nan::SetMethod(target, "get", Get); Nan::SetMethod(target, "isWeakRef", IsWeakRef); - Nan::SetMethod(target, "isNearDeath", IsNearDeath); Nan::SetMethod(target, "isDead", IsDead); Nan::SetMethod(target, "_create", Create); Nan::SetMethod(target, "_getEmitter", GetEmitter); diff --git a/test/exports.js b/test/exports.js index 348cffd..b96740f 100644 --- a/test/exports.js +++ b/test/exports.js @@ -18,7 +18,6 @@ describe('exports', function () { checkFunction('get') checkFunction('create') checkFunction('isWeakRef') - checkFunction('isNearDeath') checkFunction('isDead') checkFunction('callbacks') checkFunction('addCallback') From ab71c5d10d13737de53ee73308b854a8e49f824e Mon Sep 17 00:00:00 2001 From: Jan Stola Date: Fri, 5 Oct 2018 11:27:00 +0200 Subject: [PATCH 2/5] Indexed (resp. named) property enumerator should return numbers (strings and symbols) only. --- src/weakref.cc | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/weakref.cc b/src/weakref.cc index 5e95f30..319d69b 100644 --- a/src/weakref.cc +++ b/src/weakref.cc @@ -128,15 +128,22 @@ NAN_INDEX_DELETER(WeakIndexedPropertyDeleter) { info.GetReturnValue().Set(!dead && Nan::Delete(obj, index).FromJust()); } +NAN_PROPERTY_ENUMERATOR(WeakNamedPropertyEnumerator) { + UNWRAP +#if NODE_MAJOR_VERSION >= 7 + info.GetReturnValue().Set(dead ? Nan::New(0) : obj->GetPropertyNames(Nan::GetCurrentContext(), KeyCollectionMode::kIncludePrototypes, ONLY_ENUMERABLE, IndexFilter::kSkipIndices).ToLocalChecked()); +#else + info.GetReturnValue().Set(dead ? Nan::New(0) : Nan::GetPropertyNames(obj).ToLocalChecked()); +#endif +} -/** - * Only one "enumerator" function needs to be defined. This function is used for - * both the property and indexed enumerator functions. - */ - -NAN_PROPERTY_ENUMERATOR(WeakPropertyEnumerator) { +NAN_INDEX_ENUMERATOR(WeakIndexedPropertyEnumerator) { UNWRAP +#if NODE_MAJOR_VERSION >= 7 + info.GetReturnValue().Set(dead ? Nan::New(0) : obj->GetPropertyNames(Nan::GetCurrentContext(), KeyCollectionMode::kIncludePrototypes, static_cast (ONLY_ENUMERABLE | SKIP_STRINGS | SKIP_SYMBOLS), IndexFilter::kIncludeIndices).ToLocalChecked()); +#else info.GetReturnValue().Set(dead ? Nan::New(0) : Nan::GetPropertyNames(obj).ToLocalChecked()); +#endif } /** @@ -260,13 +267,13 @@ NAN_MODULE_INIT(Initialize) { WeakNamedPropertySetter, WeakNamedPropertyQuery, WeakNamedPropertyDeleter, - WeakPropertyEnumerator); + WeakNamedPropertyEnumerator); Nan::SetIndexedPropertyHandler(p, WeakIndexedPropertyGetter, WeakIndexedPropertySetter, WeakIndexedPropertyQuery, WeakIndexedPropertyDeleter, - WeakPropertyEnumerator); + WeakIndexedPropertyEnumerator); p->SetInternalFieldCount(FIELD_COUNT); Nan::SetMethod(target, "get", Get); From 0a43236c95a506d61fcc49297add6d6f2de09016 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 13 Jan 2022 00:13:09 -0800 Subject: [PATCH 3/5] Test Node 10, 12, 14 --- .travis.yml | 9 +++------ appveyor.yml | 14 ++++---------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index f8138c6..b21d042 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,12 +12,9 @@ addons: language: node_js node_js: - - "4" - - "5" - - "6" - - "7" - - "8" - - "9" + - "10" + - "12" + - "14" install: - npm install diff --git a/appveyor.yml b/appveyor.yml index ba8a361..3441f9a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -4,17 +4,11 @@ environment: # Visual Studio Version MSVS_VERSION: 2013 - # Test against these versions of Node.js and io.js + # Test against these versions of Node.js matrix: - # node.js - - nodejs_version: "0.8" - - nodejs_version: "0.10" - - nodejs_version: "0.12" - # io.js - - nodejs_version: "2" - - nodejs_version: "3" - - nodejs_version: "4" - - nodejs_version: "5" + - nodejs_version: "10" + - nodejs_version: "12" + - nodejs_version: "14" platform: - x86 From 3493f169679fed3484e8a679769da94565832a3e Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 13 Jan 2022 00:16:22 -0800 Subject: [PATCH 4/5] Use GitHub Actions --- .github/workflows/test.yml | 40 +++++++++++++++++++++++++++++++++++ .travis.yml | 27 ------------------------ appveyor.yml | 43 -------------------------------------- 3 files changed, 40 insertions(+), 70 deletions(-) create mode 100644 .github/workflows/test.yml delete mode 100644 .travis.yml delete mode 100644 appveyor.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..81018e0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,40 @@ +name: Node CI + +on: [push] + +jobs: + build: + name: Test Node.js ${{ matrix.node-version }} on ${{ matrix.os }} + + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + node-version: [10.x, 12.x, 14.x, 16.x] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v1 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Print Node.js Version + run: node --version + + - name: Install Dependencies + run: npm install + env: + CI: true + + - name: Run "build" step + run: npm run build --if-present + env: + CI: true + + - name: Run tests + run: npm test + env: + CI: true diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b21d042..0000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -sudo: false - -env: - - CXX=g++-4.8 -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-4.8 - -language: node_js - -node_js: - - "10" - - "12" - - "14" - -install: - - npm install - -script: - # Output useful info for debugging - - node --version - - npm --version - # Run tests - - npm test diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 3441f9a..0000000 --- a/appveyor.yml +++ /dev/null @@ -1,43 +0,0 @@ -# http://www.appveyor.com/docs/appveyor-yml - -# Test against these versions of Node.js. -environment: - # Visual Studio Version - MSVS_VERSION: 2013 - # Test against these versions of Node.js - matrix: - - nodejs_version: "10" - - nodejs_version: "12" - - nodejs_version: "14" - -platform: - - x86 - - x64 - -# Install scripts. (runs after repo cloning) -install: - # Get the latest stable version of Node 0.STABLE.latest - - ps: if($env:nodejs_version -eq "0.8") {Install-Product node $env:nodejs_version} - - ps: if($env:nodejs_version -ne "0.8") {Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)} - # Node 0.8 comes with a too obsolete npm - - IF %nodejs_version% == 0.8 (npm install -g npm@1.4.28) - # Install latest NPM only for node.js versions until built in node-gyp adds io.js support - # Update is required for node.js 0.8 because built in npm(node-gyp) does not know VS2013 - - IF %nodejs_version% LSS 1 (npm install -g npm@2) - - IF %nodejs_version% LSS 1 set PATH=%APPDATA%\npm;%PATH% - # Typical npm stuff. - - npm install --msvs_version=%MSVS_VERSION% - -# Post-install test scripts. -test_script: - # Output useful info for debugging. - - node --version - - npm --version - # run tests - - npm test - -# Don't actually build. -build: off - -# Set build version format here instead of in the admin panel. -version: "{build}" From c9fe85ddb600f24e0247255905b6f1e97498ce2a Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 13 Jan 2022 00:21:04 -0800 Subject: [PATCH 5/5] Use GH Actions badge --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index f1ebe98..a49df79 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ node-weak ========= ### Make weak references to JavaScript Objects. -[![Build Status](https://travis-ci.org/TooTallNate/node-weak.svg?branch=master)](https://travis-ci.org/TooTallNate/node-weak) -[![Build Status](https://ci.appveyor.com/api/projects/status/09lf09d1a5hm24bq?svg=true)](https://ci.appveyor.com/project/TooTallNate/node-weak) +[![Build Status](https://github.com/TooTallNate/node-weak/workflows/Node%20CI/badge.svg)](https://github.com/TooTallNate/node-weak/actions?workflow=Node+CI) On certain rarer occasions, you run into the need to be notified when a JavaScript object is going to be garbage collected. This feature is exposed to V8's C++ API,