Skip to content

Commit d9f3ec8

Browse files
trevnorrisaddaleax
authored andcommitted
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: #12892 Ref: #11883 Ref: #8531 Reviewed-By: Andreas Madsen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 0432c6e commit d9f3ec8

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/env-inl.h

-12
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,6 @@ inline Environment::Environment(IsolateData* isolate_data,
207207
set_binding_cache_object(v8::Object::New(isolate()));
208208
set_module_load_list_array(v8::Array::New(isolate()));
209209

210-
v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(isolate());
211-
fn->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "InternalFieldObject"));
212-
v8::Local<v8::ObjectTemplate> obj = fn->InstanceTemplate();
213-
obj->SetInternalFieldCount(1);
214-
set_generic_internal_field_template(obj);
215-
216210
RB_INIT(&cares_task_list_);
217211
AssignToContext(context);
218212

@@ -473,12 +467,6 @@ inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
473467
t->SetClassName(name_string); // NODE_SET_METHOD() compatibility.
474468
}
475469

476-
inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
477-
v8::MaybeLocal<v8::Object> m_obj =
478-
generic_internal_field_template()->NewInstance(context());
479-
return m_obj.ToLocalChecked();
480-
}
481-
482470
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
483471
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
484472
#define V(TypeName, PropertyName) \

src/env.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,14 @@ namespace node {
262262
V(context, v8::Context) \
263263
V(domain_array, v8::Array) \
264264
V(domains_stack_array, v8::Array) \
265-
V(generic_internal_field_template, v8::ObjectTemplate) \
266265
V(jsstream_constructor_template, v8::FunctionTemplate) \
267266
V(module_load_list_array, v8::Array) \
267+
V(pbkdf2_constructor_template, v8::ObjectTemplate) \
268268
V(pipe_constructor_template, v8::FunctionTemplate) \
269269
V(process_object, v8::Object) \
270270
V(promise_reject_function, v8::Function) \
271271
V(push_values_to_array_function, v8::Function) \
272+
V(randombytes_constructor_template, v8::ObjectTemplate) \
272273
V(script_context_constructor_template, v8::FunctionTemplate) \
273274
V(script_data_constructor_function, v8::Function) \
274275
V(secure_context_constructor_template, v8::FunctionTemplate) \
@@ -534,8 +535,6 @@ class Environment {
534535
const char* name,
535536
v8::FunctionCallback callback);
536537

537-
inline v8::Local<v8::Object> NewInternalFieldObject();
538-
539538
void AtExit(void (*cb)(void* arg), void* arg);
540539
void RunAtExitCallbacks();
541540

src/node_crypto.cc

+19-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ using v8::Maybe;
107107
using v8::MaybeLocal;
108108
using v8::Null;
109109
using v8::Object;
110+
using v8::ObjectTemplate;
110111
using v8::Persistent;
111112
using v8::PropertyAttribute;
112113
using v8::PropertyCallbackInfo;
@@ -5532,7 +5533,8 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
55325533
digest = EVP_sha1();
55335534
}
55345535

5535-
obj = env->NewInternalFieldObject();
5536+
obj = env->pbkdf2_constructor_template()->
5537+
NewInstance(env->context()).ToLocalChecked();
55365538
req = new PBKDF2Request(env,
55375539
obj,
55385540
digest,
@@ -5736,7 +5738,8 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
57365738
if (size < 0 || size > Buffer::kMaxLength)
57375739
return env->ThrowRangeError("size is not a valid Smi");
57385740

5739-
Local<Object> obj = env->NewInternalFieldObject();
5741+
Local<Object> obj = env->randombytes_constructor_template()->
5742+
NewInstance(env->context()).ToLocalChecked();
57405743
char* data = node::Malloc(size);
57415744
RandomBytesRequest* req =
57425745
new RandomBytesRequest(env,
@@ -5774,7 +5777,8 @@ void RandomBytesBuffer(const FunctionCallbackInfo<Value>& args) {
57745777
int64_t offset = args[1]->IntegerValue();
57755778
int64_t size = args[2]->IntegerValue();
57765779

5777-
Local<Object> obj = env->NewInternalFieldObject();
5780+
Local<Object> obj = env->randombytes_constructor_template()->
5781+
NewInstance(env->context()).ToLocalChecked();
57785782
obj->Set(env->context(), env->buffer_string(), args[0]).FromJust();
57795783
char* data = Buffer::Data(args[0]);
57805784
data += offset;
@@ -6251,6 +6255,18 @@ void InitCrypto(Local<Object> target,
62516255
PublicKeyCipher::Cipher<PublicKeyCipher::kPublic,
62526256
EVP_PKEY_verify_recover_init,
62536257
EVP_PKEY_verify_recover>);
6258+
6259+
Local<FunctionTemplate> pb = FunctionTemplate::New(env->isolate());
6260+
pb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "PBKDF2"));
6261+
Local<ObjectTemplate> pbt = pb->InstanceTemplate();
6262+
pbt->SetInternalFieldCount(1);
6263+
env->set_pbkdf2_constructor_template(pbt);
6264+
6265+
Local<FunctionTemplate> rb = FunctionTemplate::New(env->isolate());
6266+
rb->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "RandomBytes"));
6267+
Local<ObjectTemplate> rbt = rb->InstanceTemplate();
6268+
rbt->SetInternalFieldCount(1);
6269+
env->set_randombytes_constructor_template(rbt);
62546270
}
62556271

62566272
} // namespace crypto

0 commit comments

Comments
 (0)