forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: fix use of Reference with typed arrays
Fixes: nodejs#702 Previously calling Value() on a Reference for a TypedArray that the enderlying object had been collected would result in an error due to a failure in creating the return value. Signed-off-by: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
Showing
6 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
static Reference<Buffer<uint8_t>> weak; | ||
|
||
void CreateWeakArray(const CallbackInfo& info) { | ||
weak = Weak(Buffer<uint8_t>::New(info.Env(), 1)); | ||
weak.SuppressDestruct(); | ||
} | ||
|
||
napi_value AccessWeakArrayEmpty(const CallbackInfo& info) { | ||
Buffer<uint8_t> value = weak.Value(); | ||
return Napi::Boolean::New(info.Env(), value.IsEmpty()); | ||
} | ||
|
||
Object InitReference(Env env) { | ||
Object exports = Object::New(env); | ||
|
||
exports["createWeakArray"] = Function::New(env, CreateWeakArray); | ||
exports["accessWeakArrayEmpty"] = Function::New(env, AccessWeakArrayEmpty); | ||
|
||
return exports; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
|
||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const testUtil = require('./testUtil'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
binding.reference.createWeakArray(); | ||
global.gc(); | ||
assert.strictEqual(true, binding.reference.accessWeakArrayEmpty()); | ||
}; |