diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2019-04-20 16:35:27 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2020-07-18 06:02:58 +0300 |
commit | ef9964f4c183f062ca25337984a542895590141d (patch) | |
tree | 35d1b1b8bc3e4b65aa6572b6e0ab4c0ec022e97d /src/node_external_reference.cc | |
parent | 404302fff5079b5d9faaecbb9a1b2de67f2d86cc (diff) |
src: add an ExternalReferenceRegistry class
Add an ExternalReferenceRegistry class for registering static
external references.
To register the external JS to C++ references created in a binding
(e.g. when a FunctionTemplate is created):
- Add the binding name (same as the id used for `internalBinding()`
and `NODE_MODULE_CONTEXT_AWARE_INTERNAL`) to
`EXTERNAL_REFERENCE_BINDING_LIST` in `src/node_external_reference.h`.
- In the file where the binding is implemented, create a registration
function to register the static C++ references (e.g. the C++
functions in `v8::FunctionCallback` associated with the function
templates), like this:
```c++
void RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(cpp_func_1);
}
```
- At the end of the file where `NODE_MODULE_CONTEXT_AWARE_INTERNAL` is
also usually called, register the registration function with
```
NODE_MODULE_EXTERNAL_REFERENCE(binding_name,
RegisterExternalReferences);
```
PR-URL: https://github.com/nodejs/node/pull/32984
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'src/node_external_reference.cc')
-rw-r--r-- | src/node_external_reference.cc | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/node_external_reference.cc b/src/node_external_reference.cc new file mode 100644 index 00000000000..73e1489865d --- /dev/null +++ b/src/node_external_reference.cc @@ -0,0 +1,22 @@ +#include "node_external_reference.h" +#include <cinttypes> +#include <vector> +#include "util.h" + +namespace node { + +const std::vector<intptr_t>& ExternalReferenceRegistry::external_references() { + CHECK(!is_finalized_); + external_references_.push_back(reinterpret_cast<intptr_t>(nullptr)); + is_finalized_ = true; + return external_references_; +} + +ExternalReferenceRegistry::ExternalReferenceRegistry() { +#define V(modname) _register_external_reference_##modname(this); + EXTERNAL_REFERENCE_BINDING_LIST(V) +#undef V + // TODO(joyeecheung): collect more external references here. +} + +} // namespace node |