Skip to content

Commit

Permalink
crypto: use named FunctionTemplate
Browse files Browse the repository at this point in the history
RandomBytes and PBKDF2 were using the same "generic" ObjectTemplate for
construction. Instead create one for each that is properly named.

PR-URL: #12892
Ref: #11883
Ref: #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>
  • Loading branch information
trevnorris authored and addaleax committed May 10, 2017
1 parent 0432c6e commit d9f3ec8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
12 changes: 0 additions & 12 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,6 @@ inline Environment::Environment(IsolateData* isolate_data,
set_binding_cache_object(v8::Object::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_);
AssignToContext(context);

Expand Down Expand Up @@ -473,12 +467,6 @@ inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
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 VS(PropertyName, StringValue) V(v8::String, PropertyName)
#define V(TypeName, PropertyName) \
Expand Down
5 changes: 2 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,14 @@ namespace node {
V(context, v8::Context) \
V(domain_array, v8::Array) \
V(domains_stack_array, v8::Array) \
V(generic_internal_field_template, v8::ObjectTemplate) \
V(jsstream_constructor_template, v8::FunctionTemplate) \
V(module_load_list_array, v8::Array) \
V(pbkdf2_constructor_template, v8::ObjectTemplate) \
V(pipe_constructor_template, v8::FunctionTemplate) \
V(process_object, v8::Object) \
V(promise_reject_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_data_constructor_function, v8::Function) \
V(secure_context_constructor_template, v8::FunctionTemplate) \
Expand Down Expand Up @@ -534,8 +535,6 @@ class Environment {
const char* name,
v8::FunctionCallback callback);

inline v8::Local<v8::Object> NewInternalFieldObject();

void AtExit(void (*cb)(void* arg), void* arg);
void RunAtExitCallbacks();

Expand Down
22 changes: 19 additions & 3 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ using v8::Maybe;
using v8::MaybeLocal;
using v8::Null;
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
using v8::PropertyAttribute;
using v8::PropertyCallbackInfo;
Expand Down Expand Up @@ -5532,7 +5533,8 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
digest = EVP_sha1();
}

obj = env->NewInternalFieldObject();
obj = env->pbkdf2_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
req = new PBKDF2Request(env,
obj,
digest,
Expand Down Expand Up @@ -5736,7 +5738,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
if (size < 0 || size > Buffer::kMaxLength)
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);
RandomBytesRequest* req =
new RandomBytesRequest(env,
Expand Down Expand Up @@ -5774,7 +5777,8 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
int64_t offset = args[1]->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();
char* data = Buffer::Data(args[0]);
data += offset;
Expand Down Expand Up @@ -6251,6 +6255,18 @@ void InitCrypto(Local<Object> target,
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
EVP_PKEY_verify_recover_init,
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
Expand Down

0 comments on commit d9f3ec8

Please sign in to comment.