Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix weak reference handling in runtime object map #334

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/NodeApi/Interop/JSRuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,10 @@ private JSValue GetOrCreateObjectWrapper<T>(T obj, Func<JSValue> createWrapper)
if (_objectMap.TryGetValue(obj, out JSReference? wrapperWeakRef) &&
!wrapperWeakRef.IsDisposed)
{
JSValue? existingWrapper = wrapperWeakRef.GetValue();
if (existingWrapper.HasValue)
if (wrapperWeakRef.TryGetValue(out JSValue existingWrapper))
{
// Return the JS wrapper that was found in the map.
return existingWrapper.Value;
return existingWrapper;
}
else
{
Expand Down
28 changes: 28 additions & 0 deletions test/TestCases/napi-dotnet/object_map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// Test the JSRuntimeContext "object map" which keeps track of JS wrapper objects
// for corresponding .NET objects.

const assert = require('assert');

/** @type {import('./napi-dotnet')} */
const binding = require('../common').binding;

const ComplexTypes = binding.ComplexTypes;
assert.strictEqual(typeof ComplexTypes, 'object');

let obj1 = ComplexTypes.classObject;
assert(obj1);

// The same JS wrapper instance should be returned every time.
let obj2 = ComplexTypes.classObject;
assert.strictEqual(obj1, obj2);

// Force the JS wrapper object to be collected.
obj1 = obj2 = undefined;
global.gc();

// A new JS wrapper object should be created.
let obj3 = ComplexTypes.classObject;
assert(obj3);
Loading