Skip to content

Commit e7027cb

Browse files
committed
src: add KeyObjectHandle::HasInstance
In preparation for use by the QUIC implementation.
1 parent 261a840 commit e7027cb

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/crypto/crypto_keys.cc

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -893,33 +893,36 @@ size_t KeyObjectData::GetSymmetricKeySize() const {
893893
return symmetric_key_.size();
894894
}
895895

896+
bool KeyObjectHandle::HasInstance(Environment* env, Local<Value> value) {
897+
Local<FunctionTemplate> t = env->crypto_key_object_handle_constructor();
898+
return !t.IsEmpty() && t->HasInstance(value);
899+
}
900+
896901
v8::Local<v8::Function> KeyObjectHandle::Initialize(Environment* env) {
897-
Local<Function> templ = env->crypto_key_object_handle_constructor();
898-
if (!templ.IsEmpty()) {
899-
return templ;
902+
Local<FunctionTemplate> templ = env->crypto_key_object_handle_constructor();
903+
if (templ.IsEmpty()) {
904+
Isolate* isolate = env->isolate();
905+
templ = NewFunctionTemplate(isolate, New);
906+
templ->InstanceTemplate()->SetInternalFieldCount(
907+
KeyObjectHandle::kInternalFieldCount);
908+
templ->Inherit(BaseObject::GetConstructorTemplate(env));
909+
910+
SetProtoMethod(isolate, templ, "init", Init);
911+
SetProtoMethodNoSideEffect(
912+
isolate, templ, "getSymmetricKeySize", GetSymmetricKeySize);
913+
SetProtoMethodNoSideEffect(
914+
isolate, templ, "getAsymmetricKeyType", GetAsymmetricKeyType);
915+
SetProtoMethod(isolate, templ, "export", Export);
916+
SetProtoMethod(isolate, templ, "exportJwk", ExportJWK);
917+
SetProtoMethod(isolate, templ, "initECRaw", InitECRaw);
918+
SetProtoMethod(isolate, templ, "initEDRaw", InitEDRaw);
919+
SetProtoMethod(isolate, templ, "initJwk", InitJWK);
920+
SetProtoMethod(isolate, templ, "keyDetail", GetKeyDetail);
921+
SetProtoMethod(isolate, templ, "equals", Equals);
922+
923+
env->set_crypto_key_object_handle_constructor(templ);
900924
}
901-
Isolate* isolate = env->isolate();
902-
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New);
903-
t->InstanceTemplate()->SetInternalFieldCount(
904-
KeyObjectHandle::kInternalFieldCount);
905-
t->Inherit(BaseObject::GetConstructorTemplate(env));
906-
907-
SetProtoMethod(isolate, t, "init", Init);
908-
SetProtoMethodNoSideEffect(
909-
isolate, t, "getSymmetricKeySize", GetSymmetricKeySize);
910-
SetProtoMethodNoSideEffect(
911-
isolate, t, "getAsymmetricKeyType", GetAsymmetricKeyType);
912-
SetProtoMethod(isolate, t, "export", Export);
913-
SetProtoMethod(isolate, t, "exportJwk", ExportJWK);
914-
SetProtoMethod(isolate, t, "initECRaw", InitECRaw);
915-
SetProtoMethod(isolate, t, "initEDRaw", InitEDRaw);
916-
SetProtoMethod(isolate, t, "initJwk", InitJWK);
917-
SetProtoMethod(isolate, t, "keyDetail", GetKeyDetail);
918-
SetProtoMethod(isolate, t, "equals", Equals);
919-
920-
auto function = t->GetFunction(env->context()).ToLocalChecked();
921-
env->set_crypto_key_object_handle_constructor(function);
922-
return function;
925+
return templ->GetFunction(env->context()).ToLocalChecked();
923926
}
924927

925928
void KeyObjectHandle::RegisterExternalReferences(

src/crypto/crypto_keys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class KeyObjectData : public MemoryRetainer {
163163

164164
class KeyObjectHandle : public BaseObject {
165165
public:
166+
static bool HasInstance(Environment* env, v8::Local<v8::Value> value);
166167
static v8::Local<v8::Function> Initialize(Environment* env);
167168
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
168169

src/env_properties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
V(contextify_global_template, v8::ObjectTemplate) \
333333
V(contextify_wrapper_template, v8::ObjectTemplate) \
334334
V(compiled_fn_entry_template, v8::ObjectTemplate) \
335+
V(crypto_key_object_handle_constructor, v8::FunctionTemplate) \
335336
V(env_proxy_template, v8::ObjectTemplate) \
336337
V(env_proxy_ctor_template, v8::FunctionTemplate) \
337338
V(dir_instance_template, v8::ObjectTemplate) \
@@ -374,7 +375,6 @@
374375
V(async_hooks_promise_resolve_function, v8::Function) \
375376
V(buffer_prototype_object, v8::Object) \
376377
V(crypto_key_object_constructor, v8::Function) \
377-
V(crypto_key_object_handle_constructor, v8::Function) \
378378
V(crypto_key_object_private_constructor, v8::Function) \
379379
V(crypto_key_object_public_constructor, v8::Function) \
380380
V(crypto_key_object_secret_constructor, v8::Function) \

0 commit comments

Comments
 (0)