🌐 Object and Function References
JavaScript 实现了动态内存模型。当对象不再可达时,它们就有资格被后台运行的垃圾回收器回收。
🌐 JavaScript implements a dynamic memory model. When objects are no longer reachable, they become eligible for reclamation by the garbage collector running in the background.
有些情况下,你需要确保由你的 Node-API 代码创建的对象保持分配状态。在这种情况下,你需要显式地创建对它们的引用。这就是 ObjectReference 和 FunctionReference 类的作用。
🌐 There are situations when you need to ensure that objects created by your Node-API code remain allocated. In this case you need to explicitly create a reference to them. This is the purpose of the ObjectReference and FunctionReference classes.
🌐 Persistent Reference
对象和函数引用可以实例化为 Weak 或 Persistent。
🌐 Object and function references can be instantiated as either Weak or Persistent.
Persistent 引用将内部引用计数初始化为一,这可以防止垃圾回收器回收对象的内存。被引用的对象在 Persistent 引用的生命周期内将一直保留在内存中。
🌐 A Persistent reference initializes the internal reference count to one which prevents reclamation of the object's memory by the garbage collector. The referenced object will remain in memory for the life of the Persistent reference.
在已知引用时长的情况下,使用 Persistent 引用是合理的。当 Persistent 引用被删除时,内部引用计数会减少。如果内部引用计数变为零,被引用的对象将有资格被删除。
🌐 Using a Persistent reference makes sense in the case where the duration of the reference is known ahead of time. The internal reference count is decremented when the Persistent reference is deleted. This will make the referenced object eligible for deletion if the internal reference count goes to zero.
使用 Persistent 引用的最常见情况是当你在 Node-API 代码中创建一个 JavaScript 类时,并且你需要确保其构造函数由 JavaScript 运行时引擎分配。
🌐 The most common case for using a Persistent reference is when you create a JavaScript class in your Node-API code and you need to insure its constructor remains allocated by the JavaScript runtime engine.
🌐 Weak Reference
对于更复杂的实现,其中多个 AsyncWorkers 依赖于被引用的对象,使用 Weak 引用可能更合理,因为它将内部引用计数初始化为零。然后可以使用 Reference、Ref 和 Unref 方法来维护引用计数。
🌐 For more complex implementations where multiple AsyncWorkers rely on the referenced object, it may make better sense to use a Weak reference which initializes the internal reference count to zero. The reference count can then be maintained using the Reference Ref and Unref methods.
Weak 引用的最常见用例是当你的 Node-API 代码需要监控你在 Node-API 代码中创建的 JavaScript 对象何时被垃圾回收器释放时。
🌐 The most common use case for a Weak reference is when your Node-API code needs to monitor when a JavaScript object you've created in your Node-API code is released by the garbage collector.
ObjectReference 类继承自 Reference 类。它增加的值是一个由 Get 和 Set 方法组成的集合,这些方法用于操作被引用对象的属性。
🌐 The ObjectReference class inherits from the Reference class. The value it adds is a collection of Get and Set methods that manipulate the referenced object's properties.
像 ObjectReference 一样,FunctionReference 类继承自 Reference 类。虽然 ObjectReference 类增加了 Get 和 Set 方法,但 FunctionReference 类增加了一组 Call 方法,用于实现对函数的调用。
🌐 Like the ObjectReference, the FunctionReference class inherits from the Reference class. While the ObjectReference class adds the Get and Set methods, the FunctionReference class adds a set of Call methods that implement calls to the function.
🌐 Example
此示例代码展示了如何使用 FunctionReference 类。
🌐 This example code shows how to use the FunctionReference class.
native-addon.h 声明了 NativeAddon C++ 类,该类有两个在构造函数中初始化的数据成员:
Napi::FunctionReference jsFnRefNapi::Function jsFn
native-addon.cc 包含 NativeAddon 的实现。构造函数从 JavaScript 调用,接受两个函数参数。第一个参数存储为 Napi::FunctionReference,第二个参数存储为 Napi::Function。
这段代码中有一个故意的错误.
第二个函数存储在 Napi::Function jsFn 数据成员中。这是一个错误,因为第二个参数的生命周期仅限于构造函数的生命周期。构造函数返回后,jsFn 数据成员的值将无效。第一个参数存储在 Napi::FunctionReference jsFnRef 中。由于使用了 Napi::FunctionReference,构造函数返回后,jsFnRef 的值仍将有效。
🌐 The second function is stored in the Napi::Function jsFn data member. This is an error because the lifetime of the second argument is limited to the lifetime of the constructor. The value of the jsFn data member will be invalid after the constructor returns. The first argument is stored in the Napi::FunctionReference jsFnRef. Because of the use of the Napi::FunctionReference, the value of jsFnRef will remain valid after the constructor returns.
NativeAddon 类实现了两个可以从 JavaScript 调用的方法:TryCallByStoredReference 和 TryCallByStoredFunction。注意,Call 方法在 jsFnRef 和 jsFn 数据成员中使用方式相同。
🌐 The NativeAddon class implements two methods which can be called from JavaScript: TryCallByStoredReference and TryCallByStoredFunction. Notice that the Call method is used the same way for both the jsFnRef and jsFn data members.
binding.cc 是一个标准绑定文件,用于将 NativeAddon 类注册到 Node-API。
index.js 展示了如何在 JavaScript 中使用 NativeAddon 类。请注意,对原生 tryCallByStoredFunction 方法的调用失败,因为它依赖的数据成员无效。