BabylonJS / BabylonJS/BabylonNative

Napi-JSI ObjectWrap should extend Reference<Object>

Open
#1,030 1 comment 1 reaction 0 assignees View on GitHub
infrastructure
Dominant language
C++
Stars
919
Forks
162
Avg merge
1d 15h
Merged PRs (30d)
19

Description

**Is your feature request related to a problem? Please describe.**
Currently the ObjectWrap implementation does not extend Reference, which leads to API differences between Napi-JSI and Napi-Direct implementations. For example you cannot call Value() to get access to the JS Object from the context of the C++ class.

**Describe the solution you'd like**
We should change ObjectWrap to extend Reference to match the implementation in Napi-Direct. Here is an example of the proposed change: https://github.com/BabylonJS/BabylonNative/pull/1029/commits/b213beae15094393963c4b04d5b266f4c413af3b

This change needs to be updated to either make Reference to use a weak reference via JSI::WeakObject or otherwise handle cleanup to allow for GC to clean up stale JS objects.

**Describe alternatives you've considered**
Currently we can set properties via the InstanceAccessor or by storing a persistent reference to the object in the constructor. The first approach has performance issues for heavily accessed code paths as it requires marshalling to and from the JS context repeatedly, and the second requires managing the lifetime of the reference natively and does not allow JS GC to run.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.