@@ -417,6 +417,55 @@ void Initialize(Local<Object> target,
417417NODE_MODULE_CONTEXT_AWARE_INTERNAL(cares_wrap, Initialize)
418418```
419419
420+ If the C++ binding is loaded during bootstrap, it needs to be registered
421+ with the utilities in `node_external_reference.h`, like this:
422+
423+ ```cpp
424+ namespace node {
425+ namespace utils {
426+ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
427+ registry->Register(GetHiddenValue);
428+ registry->Register(SetHiddenValue);
429+ // ... register all C++ functions used to create FunctionTemplates.
430+ }
431+ } // namespace util
432+ } // namespace node
433+
434+ // The first argument passed to `NODE_MODULE_EXTERNAL_REFERENCE`,
435+ // which is `util` here, needs to be added to the
436+ // `EXTERNAL_REFERENCE_BINDING_LIST_BASE` list in node_external_reference.h
437+ NODE_MODULE_EXTERNAL_REFERENCE(util, node::util::RegisterExternalReferences)
438+ ```
439+
440+ Otherwise, you might see an error message like this when building the
441+ executables:
442+
443+ ``` console
444+ FAILED: gen/node_snapshot.cc
445+ cd ../../; out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc
446+ Unknown external reference 0x107769200.
447+ <unresolved>
448+ /bin/sh: line 1: 6963 Illegal instruction: 4 out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc
449+ ```
450+
451+ You can try using a debugger to symbolicate the external reference. For example,
452+ with lldb's ` image lookup --address ` command (with gdb it's ` info symbol ` ):
453+
454+ ``` console
455+ $ lldb -- out/Release/node_mksnapshot out/Release/gen/node_snapshot.cc
456+ (lldb) run
457+ Process 7012 launched: '/Users/joyee/projects/node/out/Release/node_mksnapshot' (x86_64)
458+ Unknown external reference 0x1004c8200.
459+ <unresolved>
460+ Process 7012 stopped
461+ (lldb) image lookup --address 0x1004c8200
462+ Address: node_mksnapshot[0x00000001004c8200] (node_mksnapshot.__TEXT.__text + 5009920)
463+ Summary: node_mksnapshot`node::util::GetHiddenValue(v8::FunctionCallbackInfo<v8::Value> const&) at node_util.cc:159
464+ ```
465+
466+ Which explains that the unregistered external reference is
467+ ` node::util::GetHiddenValue ` defined in ` node_util.cc ` .
468+
420469<a id =" per-binding-state " ></a >
421470#### Per-binding state
422471
@@ -467,6 +516,12 @@ void InitializeHttpParser(Local<Object> target,
467516}
468517```
469518
519+ If the binding is loaded during bootstrap, add it to the
520+ `SERIALIZABLE_OBJECT_TYPES` list in `src/node_snapshotable.h` and
521+ inherit from the `SnapshotableObject` class instead. See the comments
522+ of `SnapshotableObject` on how to implement its serialization and
523+ deserialization.
524+
470525<a id="exception-handling"></a>
471526### Exception handling
472527
0 commit comments