Skip to content

Commit 86862d9

Browse files
committed
crypto: use cppgc to manage Hash
1 parent 6ee4c09 commit 86862d9

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

Diff for: src/crypto/crypto_hash.cc

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "crypto/crypto_hash.h"
22
#include "async_wrap-inl.h"
33
#include "base_object-inl.h"
4+
#include "cppgc/allocation.h"
45
#include "env-inl.h"
56
#include "memory_tracker-inl.h"
67
#include "string_bytes.h"
@@ -26,9 +27,14 @@ using v8::Object;
2627
using v8::Uint32;
2728
using v8::Value;
2829

30+
#ifdef ASSIGN_OR_RETURN_UNWRAP
31+
#undef ASSIGN_OR_RETURN_UNWRAP
32+
#endif
33+
34+
#define ASSIGN_OR_RETURN_UNWRAP ASSIGN_OR_RETURN_UNWRAP_CPPGC
2935
namespace crypto {
30-
Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
31-
MakeWeak();
36+
Hash::Hash(Environment* env, Local<Object> wrap) {
37+
InitializeCppgc(this, env, wrap);
3238
}
3339

3440
void Hash::MemoryInfo(MemoryTracker* tracker) const {
@@ -321,7 +327,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
321327
xof_md_len = Just<unsigned int>(args[1].As<Uint32>()->Value());
322328
}
323329

324-
Hash* hash = new Hash(env, args.This());
330+
Hash* hash = cppgc::MakeGarbageCollected<Hash>(
331+
env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This());
325332
if (md == nullptr || !hash->HashInit(md, xof_md_len)) {
326333
return ThrowCryptoError(env, ERR_get_error(),
327334
"Digest method not supported");

Diff for: src/crypto/crypto_hash.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6-
#include "base_object.h"
6+
#include "cppgc_helpers.h"
77
#include "crypto/crypto_keys.h"
88
#include "crypto/crypto_util.h"
99
#include "env.h"
@@ -12,14 +12,21 @@
1212

1313
namespace node {
1414
namespace crypto {
15-
class Hash final : public BaseObject {
15+
class Hash final : public cppgc::GarbageCollected<Hash>,
16+
public MemoryRetainer,
17+
public cppgc::NameProvider,
18+
public CppgcMixin {
1619
public:
1720
static void Initialize(Environment* env, v8::Local<v8::Object> target);
1821
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
1922

2023
void MemoryInfo(MemoryTracker* tracker) const override;
2124
SET_MEMORY_INFO_NAME(Hash)
2225
SET_SELF_SIZE(Hash)
26+
void Trace(cppgc::Visitor* visitor) const final {
27+
CppgcMixin::Trace(visitor);
28+
}
29+
const char* GetHumanReadableName() const final { return "Node / Hash"; }
2330

2431
bool HashInit(const EVP_MD* md, v8::Maybe<unsigned int> xof_md_len);
2532
bool HashUpdate(const char* data, size_t len);
@@ -28,13 +35,13 @@ class Hash final : public BaseObject {
2835
static void GetCachedAliases(const v8::FunctionCallbackInfo<v8::Value>& args);
2936
static void OneShotDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3037

38+
Hash(Environment* env, v8::Local<v8::Object> wrap);
39+
3140
protected:
3241
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
3342
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
3443
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3544

36-
Hash(Environment* env, v8::Local<v8::Object> wrap);
37-
3845
private:
3946
EVPMDCtxPointer mdctx_{};
4047
unsigned int md_len_ = 0;

Diff for: src/crypto/crypto_util.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "async_wrap.h"
7+
#include "cppgc_helpers.h"
78
#include "env.h"
89
#include "node_errors.h"
910
#include "node_external_reference.h"
@@ -136,7 +137,12 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
136137
void (*callback)(T*, const v8::FunctionCallbackInfo<v8::Value>&,
137138
const char*, size_t)) {
138139
T* ctx;
139-
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
140+
if constexpr (std::is_base_of_v<BaseObject, T>) {
141+
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
142+
} else {
143+
ctx = CppgcMixin::Unwrap<T>(args.Holder());
144+
if (ctx == nullptr) return;
145+
}
140146

141147
if (args[0]->IsString()) {
142148
StringBytes::InlineDecoder decoder;

0 commit comments

Comments
 (0)