From c33c447b2a3e3150b105f561b0aef39e19d6d5c0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 19 Dec 2022 10:18:42 -0800 Subject: [PATCH] src: add KeyObjectHandle::HasInstance In preparation for use by the QUIC implementation. PR-URL: https://github.com/nodejs/node/pull/45912 Reviewed-By: Matteo Collina Reviewed-By: Ben Noordhuis Reviewed-By: Yagiz Nizipli Reviewed-By: Rafael Gonzaga Reviewed-By: Minwoo Jung Reviewed-By: Robert Nagy Reviewed-By: Antoine du Hamel --- src/crypto/crypto_keys.cc | 53 +++++++++++++++++++++------------------ src/crypto/crypto_keys.h | 1 + src/env_properties.h | 2 +- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/crypto/crypto_keys.cc b/src/crypto/crypto_keys.cc index cc2ff2e631ff77..0be2283d6017a4 100644 --- a/src/crypto/crypto_keys.cc +++ b/src/crypto/crypto_keys.cc @@ -889,33 +889,36 @@ size_t KeyObjectData::GetSymmetricKeySize() const { return symmetric_key_.size(); } +bool KeyObjectHandle::HasInstance(Environment* env, Local value) { + Local t = env->crypto_key_object_handle_constructor(); + return !t.IsEmpty() && t->HasInstance(value); +} + v8::Local KeyObjectHandle::Initialize(Environment* env) { - Local templ = env->crypto_key_object_handle_constructor(); - if (!templ.IsEmpty()) { - return templ; + Local templ = env->crypto_key_object_handle_constructor(); + if (templ.IsEmpty()) { + Isolate* isolate = env->isolate(); + templ = NewFunctionTemplate(isolate, New); + templ->InstanceTemplate()->SetInternalFieldCount( + KeyObjectHandle::kInternalFieldCount); + templ->Inherit(BaseObject::GetConstructorTemplate(env)); + + SetProtoMethod(isolate, templ, "init", Init); + SetProtoMethodNoSideEffect( + isolate, templ, "getSymmetricKeySize", GetSymmetricKeySize); + SetProtoMethodNoSideEffect( + isolate, templ, "getAsymmetricKeyType", GetAsymmetricKeyType); + SetProtoMethod(isolate, templ, "export", Export); + SetProtoMethod(isolate, templ, "exportJwk", ExportJWK); + SetProtoMethod(isolate, templ, "initECRaw", InitECRaw); + SetProtoMethod(isolate, templ, "initEDRaw", InitEDRaw); + SetProtoMethod(isolate, templ, "initJwk", InitJWK); + SetProtoMethod(isolate, templ, "keyDetail", GetKeyDetail); + SetProtoMethod(isolate, templ, "equals", Equals); + + env->set_crypto_key_object_handle_constructor(templ); } - Isolate* isolate = env->isolate(); - Local t = NewFunctionTemplate(isolate, New); - t->InstanceTemplate()->SetInternalFieldCount( - KeyObjectHandle::kInternalFieldCount); - t->Inherit(BaseObject::GetConstructorTemplate(env)); - - SetProtoMethod(isolate, t, "init", Init); - SetProtoMethodNoSideEffect( - isolate, t, "getSymmetricKeySize", GetSymmetricKeySize); - SetProtoMethodNoSideEffect( - isolate, t, "getAsymmetricKeyType", GetAsymmetricKeyType); - SetProtoMethod(isolate, t, "export", Export); - SetProtoMethod(isolate, t, "exportJwk", ExportJWK); - SetProtoMethod(isolate, t, "initECRaw", InitECRaw); - SetProtoMethod(isolate, t, "initEDRaw", InitEDRaw); - SetProtoMethod(isolate, t, "initJwk", InitJWK); - SetProtoMethod(isolate, t, "keyDetail", GetKeyDetail); - SetProtoMethod(isolate, t, "equals", Equals); - - auto function = t->GetFunction(env->context()).ToLocalChecked(); - env->set_crypto_key_object_handle_constructor(function); - return function; + return templ->GetFunction(env->context()).ToLocalChecked(); } void KeyObjectHandle::RegisterExternalReferences( diff --git a/src/crypto/crypto_keys.h b/src/crypto/crypto_keys.h index 7b9c8d7e4ff55f..eb4f5222670e89 100644 --- a/src/crypto/crypto_keys.h +++ b/src/crypto/crypto_keys.h @@ -163,6 +163,7 @@ class KeyObjectData : public MemoryRetainer { class KeyObjectHandle : public BaseObject { public: + static bool HasInstance(Environment* env, v8::Local value); static v8::Local Initialize(Environment* env); static void RegisterExternalReferences(ExternalReferenceRegistry* registry); diff --git a/src/env_properties.h b/src/env_properties.h index d621ebbd09425d..3960b0add85d79 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -332,6 +332,7 @@ V(contextify_global_template, v8::ObjectTemplate) \ V(contextify_wrapper_template, v8::ObjectTemplate) \ V(compiled_fn_entry_template, v8::ObjectTemplate) \ + V(crypto_key_object_handle_constructor, v8::FunctionTemplate) \ V(env_proxy_template, v8::ObjectTemplate) \ V(env_proxy_ctor_template, v8::FunctionTemplate) \ V(dir_instance_template, v8::ObjectTemplate) \ @@ -374,7 +375,6 @@ V(async_hooks_promise_resolve_function, v8::Function) \ V(buffer_prototype_object, v8::Object) \ V(crypto_key_object_constructor, v8::Function) \ - V(crypto_key_object_handle_constructor, v8::Function) \ V(crypto_key_object_private_constructor, v8::Function) \ V(crypto_key_object_public_constructor, v8::Function) \ V(crypto_key_object_secret_constructor, v8::Function) \