Skip to content

Commit 7988ed1

Browse files
committed
crypto: use cppgc to manage Hash
1 parent 8695c16 commit 7988ed1

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

src/crypto/crypto_hash.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "crypto/crypto_hash.h"
22
#include "async_wrap-inl.h"
33
#include "base_object-inl.h"
4+
#include "cppgc/allocation.h"
5+
#include "cppgc_helpers-inl.h"
46
#include "env-inl.h"
57
#include "memory_tracker-inl.h"
68
#include "string_bytes.h"
@@ -31,9 +33,18 @@ using v8::Object;
3133
using v8::Uint32;
3234
using v8::Value;
3335

36+
#ifdef ASSIGN_OR_RETURN_UNWRAP
37+
#undef ASSIGN_OR_RETURN_UNWRAP
38+
#endif
39+
40+
#define ASSIGN_OR_RETURN_UNWRAP ASSIGN_OR_RETURN_UNWRAP_CPPGC
3441
namespace crypto {
35-
Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
36-
MakeWeak();
42+
Hash::Hash(Environment* env, Local<Object> wrap) {
43+
CppgcMixin::Wrap(this, env, wrap);
44+
}
45+
46+
void Hash::Trace(cppgc::Visitor* visitor) const {
47+
CppgcMixin::Trace(visitor);
3748
}
3849

3950
void Hash::MemoryInfo(MemoryTracker* tracker) const {
@@ -322,7 +333,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
322333
xof_md_len = Just<unsigned int>(args[1].As<Uint32>()->Value());
323334
}
324335

325-
Hash* hash = new Hash(env, args.This());
336+
Hash* hash = cppgc::MakeGarbageCollected<Hash>(
337+
env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This());
326338
if (md == nullptr || !hash->HashInit(md, xof_md_len)) {
327339
return ThrowCryptoError(env, ERR_get_error(),
328340
"Digest method not supported");

src/crypto/crypto_hash.h

Lines changed: 9 additions & 8 deletions
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,29 +12,30 @@
1212

1313
namespace node {
1414
namespace crypto {
15-
class Hash final : public BaseObject {
15+
16+
class Hash final : CPPGC_MIXIN(Hash) {
1617
public:
18+
SET_CPPGC_NAME(Hash)
19+
void Trace(cppgc::Visitor* visitor) const final;
20+
void MemoryInfo(MemoryTracker* tracker) const override;
21+
1722
static void Initialize(Environment* env, v8::Local<v8::Object> target);
1823
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
1924

20-
void MemoryInfo(MemoryTracker* tracker) const override;
21-
SET_MEMORY_INFO_NAME(Hash)
22-
SET_SELF_SIZE(Hash)
23-
2425
bool HashInit(const EVP_MD* md, v8::Maybe<unsigned int> xof_md_len);
2526
bool HashUpdate(const char* data, size_t len);
2627

2728
static void GetHashes(const v8::FunctionCallbackInfo<v8::Value>& args);
2829
static void GetCachedAliases(const v8::FunctionCallbackInfo<v8::Value>& args);
2930
static void OneShotDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3031

32+
Hash(Environment* env, v8::Local<v8::Object> wrap);
33+
3134
protected:
3235
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
3336
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
3437
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3538

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

src/crypto/crypto_util.h

Lines changed: 7 additions & 1 deletion
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"
@@ -73,7 +74,12 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
7374
void (*callback)(T*, const v8::FunctionCallbackInfo<v8::Value>&,
7475
const char*, size_t)) {
7576
T* ctx;
76-
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
77+
if constexpr (std::is_base_of_v<BaseObject, T>) {
78+
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.This());
79+
} else {
80+
ctx = CppgcMixin::Unwrap<T>(args.This());
81+
if (ctx == nullptr) return;
82+
}
7783

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

src/memory_tracker-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ MemoryRetainerNode* MemoryTracker::AddNode(const MemoryRetainer* retainer,
330330
return it->second;
331331
}
332332

333+
// TODO(joyeeheung): deduplicate when it's
333334
MemoryRetainerNode* n = new MemoryRetainerNode(this, retainer);
334335
graph_->AddNode(std::unique_ptr<v8::EmbedderGraph::Node>(n));
335336
seen_[retainer] = n;

0 commit comments

Comments
 (0)