Skip to content

Commit b11499c

Browse files
committed
crypto: use cppgc to manage Hash
1 parent fceb3ff commit b11499c

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

benchmark/crypto/create-hash.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const { createHash } = require('crypto');
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e5],
9+
});
10+
11+
function main({ n }) {
12+
const array = [];
13+
for (let i = 0; i < n; ++i) {
14+
array.push(null);
15+
}
16+
bench.start();
17+
for (let i = 0; i < n; ++i) {
18+
array[i] = createHash('sha1');
19+
}
20+
bench.end(n);
21+
assert.strictEqual(typeof array[n - 1], 'object')
22+
}

src/crypto/crypto_hash.cc

Lines changed: 10 additions & 3 deletions
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"
@@ -24,9 +25,14 @@ using v8::Object;
2425
using v8::Uint32;
2526
using v8::Value;
2627

28+
#ifdef ASSIGN_OR_RETURN_UNWRAP
29+
#undef ASSIGN_OR_RETURN_UNWRAP
30+
#endif
31+
32+
#define ASSIGN_OR_RETURN_UNWRAP ASSIGN_OR_RETURN_UNWRAP_CPPGC
2733
namespace crypto {
28-
Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) {
29-
MakeWeak();
34+
Hash::Hash(Environment* env, Local<Object> wrap) {
35+
INITIALIZE_CPPGC_OBJECT(env, wrap, this)
3036
}
3137

3238
void Hash::MemoryInfo(MemoryTracker* tracker) const {
@@ -103,7 +109,8 @@ void Hash::New(const FunctionCallbackInfo<Value>& args) {
103109
xof_md_len = Just<unsigned int>(args[1].As<Uint32>()->Value());
104110
}
105111

106-
Hash* hash = new Hash(env, args.This());
112+
Hash* hash = cppgc::MakeGarbageCollected<Hash>(
113+
env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This());
107114
if (md == nullptr || !hash->HashInit(md, xof_md_len)) {
108115
return ThrowCryptoError(env, ERR_get_error(),
109116
"Digest method not supported");

src/crypto/crypto_hash.h

Lines changed: 11 additions & 4 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,8 +12,13 @@
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 CppgcMixin {
1618
public:
19+
CPPGC_MIXIN_METHODS()
20+
DEFAULT_CPPGC_TRACE()
21+
1722
static void Initialize(Environment* env, v8::Local<v8::Object> target);
1823
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
1924

@@ -26,14 +31,16 @@ class Hash final : public BaseObject {
2631

2732
static void GetHashes(const v8::FunctionCallbackInfo<v8::Value>& args);
2833

34+
Hash(Environment* env, v8::Local<v8::Object> wrap);
35+
2936
protected:
3037
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
3138
static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
3239
static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args);
3340

34-
Hash(Environment* env, v8::Local<v8::Object> wrap);
35-
3641
private:
42+
CPPGC_MIXIN_FIELDS()
43+
3744
EVPMDPointer mdctx_ {};
3845
unsigned int md_len_ = 0;
3946
ByteSource digest_;

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"
@@ -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)