Replies: 3 comments 2 replies
-
Sorry the last few weeks have been busy for me, will try to comment soon. |
Beta Was this translation helpful? Give feedback.
-
Hi @vmoroz, is this JSI implementation public by any chance? Thank you, |
Beta Was this translation helpful? Give feedback.
-
@kisg , yes, the latest code is a part of the react-native-windows repo. See: NodeApiJsiRuntime.h and NodeApiJsiRuntime.cpp . It still uses Node-API extensions. I am considering to create an implementation that can work without these extensions and be in a separate repo. Currently, we have implementation of Node-API for the V8 DLL engine based on Node.js code and for the Hermes engine DLL. They both have the extensions implemented and pass all JSI unit tests against their copy of JSI for Node-API. |
Beta Was this translation helpful? Give feedback.
-
Currently the Node-API offers two types of pointers to JS values:
napi_value
andnapi_ref
.napi_value
points to a temporary value that is typically kept in call stack. It can point to any JavaScript value type includingnull
,undefined
,number
,bool
,string
,object
, etc. It is used by all Node-API functions which manipulate JS values. It is very efficient and offers a virtually direct access to JS engine values. The main drawback is that it cannot be kept around as a field in a native class.napi_ref
is a ref-countednapi_value
holder that is intended to address thenapi_value
transitive nature and to allow keeping it as a field in native classes. But thenapi_ref
tries to do too much and it causes some issues and unexpected behavior:napi_ref
is becoming a weak reference instead of being deleted.napi_ref
could become a weak reference, and weak references work only with objects, thenapi_ref
can only wrap upnapi_value
of object types.napi_ref
explicitly. It can be error prone if the ref count is not zero and some other code still keeps the reference.napi_ref
in another thread.The proposal is to implement new ref-counted
napi_value
holders that are aligned with the behavior found in the C++ standardstd::shared_ptr
andstd::weak_ptr
. These new pointers would have the following features:napi_value
. When this ref count goes to zero, we delete the internal structure pointed by the weak ptr. It mirrors the strong pointer behavior.napi_value
of any type.WeakRef
. It means that it can only wrap objects.These new types could be called
napi_shared_ptr
andnapi_weak_ptr
.As an alternative they could be called
napi_strong_ref
andnapi_weak_ref
.I was playing with a new version of
napi_ref
to support Meta's JSI API implementation on top of the Node-API with the behavior described above. The reference is callednapi_ext_ref
: js_native_ext_api.h . The samenapi_ext_ref
is used for the strong and weak references. It works, but having two different types could be more explicit. Though, both types have the same set of operations: increment/decrement ref count and to get the wrappednapi_value
.Beta Was this translation helpful? Give feedback.
All reactions