Skip to content

Commit

Permalink
limit change to writePointer
Browse files Browse the repository at this point in the history
Update to avoid changing behaviour for
_writePointer and limit change to writePointer
where objects were already being associated
with the buffer.

Signed-off-by: Michael Dawson <mdawson@devrus.com>
  • Loading branch information
mhdawson committed Mar 11, 2021
1 parent c882e1c commit 4cf8eed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ exports.writeObject = function writeObject (buf, offset, obj) {

exports.writePointer = function writePointer (buf, offset, ptr) {
debug('writing pointer to buffer', buf, offset, ptr);
exports._writePointer(buf, offset, ptr);
exports._writePointer(buf, offset, ptr, true);
exports._attach(buf, ptr);
};

Expand Down
20 changes: 11 additions & 9 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,17 @@ void WritePointer(const CallbackInfo& args) {
if (input.IsNull()) {
*reinterpret_cast<char**>(ptr) = nullptr;
} else {
// create a node-api reference and finalizer to ensure that
// the buffer whoes pointer is written can only be
// collected after the finalizers for the buffer
// to which the pointer was written have already run
Reference<Value>* ref = new Reference<Value>;
*ref = Persistent(args[2]);
args[0].As<Object>().AddFinalizer([](Env env, Reference<Value>* ref) {
delete ref;
}, ref);
if ((args.Length() == 4) && (args[3].As<Boolean>() == true)) {
// create a node-api reference and finalizer to ensure that
// the buffer whoes pointer is written can only be
// collected after the finalizers for the buffer
// to which the pointer was written have already run
Reference<Value>* ref = new Reference<Value>;
*ref = Persistent(args[2]);
args[0].As<Object>().AddFinalizer([](Env env, Reference<Value>* ref) {
delete ref;
}, ref);
}

char* input_ptr = GetBufferData(input);
*reinterpret_cast<char**>(ptr) = input_ptr;
Expand Down

0 comments on commit 4cf8eed

Please sign in to comment.