Browse Source

crypto: use named FunctionTemplate

RandomBytes and PBKDF2 were using the same "generic" ObjectTemplate for
construction. Instead create one for each that is properly named.

PR-URL: https://github.com/nodejs/node/pull/12892
Ref: https://github.com/nodejs/node/pull/11883
Ref: https://github.com/nodejs/node/pull/8531
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
v6
Trevor Norris 8 years ago
committed by Anna Henningsen
parent
commit
d9f3ec8e09
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 12
      src/env-inl.h
  2. 5
      src/env.h
  3. 22
      src/node_crypto.cc

12
src/env-inl.h

@ -207,12 +207,6 @@ inline Environment::Environment(IsolateData* isolate_data,
set_binding_cache_object(v8::Object::New(isolate())); set_binding_cache_object(v8::Object::New(isolate()));
set_module_load_list_array(v8::Array::New(isolate())); set_module_load_list_array(v8::Array::New(isolate()));
v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(isolate());
fn->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "InternalFieldObject"));
v8::Local<v8::ObjectTemplate> obj = fn->InstanceTemplate();
obj->SetInternalFieldCount(1);
set_generic_internal_field_template(obj);
RB_INIT(&cares_task_list_); RB_INIT(&cares_task_list_);
AssignToContext(context); AssignToContext(context);
@ -473,12 +467,6 @@ inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
t->SetClassName(name_string); // NODE_SET_METHOD() compatibility. t->SetClassName(name_string); // NODE_SET_METHOD() compatibility.
} }
inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
v8::MaybeLocal<v8::Object> m_obj =
generic_internal_field_template()->NewInstance(context());
return m_obj.ToLocalChecked();
}
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName) #define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName) #define VS(PropertyName, StringValue) V(v8::String, PropertyName)
#define V(TypeName, PropertyName) \ #define V(TypeName, PropertyName) \

5
src/env.h

@ -262,13 +262,14 @@ namespace node {
V(context, v8::Context) \ V(context, v8::Context) \
V(domain_array, v8::Array) \ V(domain_array, v8::Array) \
V(domains_stack_array, v8::Array) \ V(domains_stack_array, v8::Array) \
V(generic_internal_field_template, v8::ObjectTemplate) \
V(jsstream_constructor_template, v8::FunctionTemplate) \ V(jsstream_constructor_template, v8::FunctionTemplate) \
V(module_load_list_array, v8::Array) \ V(module_load_list_array, v8::Array) \
V(pbkdf2_constructor_template, v8::ObjectTemplate) \
V(pipe_constructor_template, v8::FunctionTemplate) \ V(pipe_constructor_template, v8::FunctionTemplate) \
V(process_object, v8::Object) \ V(process_object, v8::Object) \
V(promise_reject_function, v8::Function) \ V(promise_reject_function, v8::Function) \
V(push_values_to_array_function, v8::Function) \ V(push_values_to_array_function, v8::Function) \
V(randombytes_constructor_template, v8::ObjectTemplate) \
V(script_context_constructor_template, v8::FunctionTemplate) \ V(script_context_constructor_template, v8::FunctionTemplate) \
V(script_data_constructor_function, v8::Function) \ V(script_data_constructor_function, v8::Function) \
V(secure_context_constructor_template, v8::FunctionTemplate) \ V(secure_context_constructor_template, v8::FunctionTemplate) \
@ -534,8 +535,6 @@ class Environment {
const char* name, const char* name,
v8::FunctionCallback callback); v8::FunctionCallback callback);
inline v8::Local<v8::Object> NewInternalFieldObject();
void AtExit(void (*cb)(void* arg), void* arg); void AtExit(void (*cb)(void* arg), void* arg);
void RunAtExitCallbacks(); void RunAtExitCallbacks();

22
src/node_crypto.cc

@ -107,6 +107,7 @@ using v8::Maybe;
using v8::MaybeLocal; using v8::MaybeLocal;
using v8::Null; using v8::Null;
using v8::Object; using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent; using v8::Persistent;
using v8::PropertyAttribute; using v8::PropertyAttribute;
using v8::PropertyCallbackInfo; using v8::PropertyCallbackInfo;
@ -5532,7 +5533,8 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
digest = EVP_sha1(); digest = EVP_sha1();
} }
obj = env->NewInternalFieldObject(); obj = env->pbkdf2_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
req = new PBKDF2Request(env, req = new PBKDF2Request(env,
obj, obj,
digest, digest,
@ -5736,7 +5738,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
if (size < 0 || size > Buffer::kMaxLength) if (size < 0 || size > Buffer::kMaxLength)
return env->ThrowRangeError("size is not a valid Smi"); return env->ThrowRangeError("size is not a valid Smi");
Local<Object> obj = env->NewInternalFieldObject(); Local<Object> obj = env->randombytes_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
char* data = node::Malloc(size); char* data = node::Malloc(size);
RandomBytesRequest* req = RandomBytesRequest* req =
new RandomBytesRequest(env, new RandomBytesRequest(env,
@ -5774,7 +5777,8 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
int64_t offset = args[1]->IntegerValue(); int64_t offset = args[1]->IntegerValue();
int64_t size = args[2]->IntegerValue(); int64_t size = args[2]->IntegerValue();
Local<Object> obj = env->NewInternalFieldObject(); Local<Object> obj = env->randombytes_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
obj->Set(env->context(), env->buffer_string(), args[0]).FromJust(); obj->Set(env->context(), env->buffer_string(), args[0]).FromJust();
char* data = Buffer::Data(args[0]); char* data = Buffer::Data(args[0]);
data += offset; data += offset;
@ -6251,6 +6255,18 @@ void InitCrypto(Local<Object> target,
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic, PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_verify_recover_init, EVP_PKEY_verify_recover_init,
EVP_PKEY_verify_recover>); EVP_PKEY_verify_recover>);
Local<FunctionTemplate> pb = FunctionTemplate::New(env->isolate());
pb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PBKDF2"));
Local<ObjectTemplate> pbt = pb->InstanceTemplate();
pbt->SetInternalFieldCount(1);
env->set_pbkdf2_constructor_template(pbt);
Local<FunctionTemplate> rb = FunctionTemplate::New(env->isolate());
rb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "RandomBytes"));
Local<ObjectTemplate> rbt = rb->InstanceTemplate();
rbt->SetInternalFieldCount(1);
env->set_randombytes_constructor_template(rbt);
} }
} // namespace crypto } // namespace crypto

Loading…
Cancel
Save