From 9e2b036ee75e5f016696171248f3a7f1782034e0 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 11 Feb 2023 17:47:46 +0100 Subject: [PATCH] src: add SetFastMethodNoSideEffect() The original SetFastMethod() uses v8::SideEffectType::kHasNoSideEffect by default, which is different from SetMethod(). Follow the previous convention and add a new SetFastMethodNoSideEffect() instead. --- src/node_process_methods.cc | 5 +++-- src/util.cc | 21 +++++++++++++++++++++ src/util.h | 5 +++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index 06fd7313d7f01c..778e4cd9dd966e 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -481,8 +481,9 @@ v8::CFunction BindingData::fast_bigint_(v8::CFunction::Make(FastBigInt)); void BindingData::AddMethods() { Local ctx = env()->context(); - SetFastMethod(ctx, object(), "hrtime", SlowNumber, &fast_number_); - SetFastMethod(ctx, object(), "hrtimeBigInt", SlowBigInt, &fast_bigint_); + SetFastMethodNoSideEffect(ctx, object(), "hrtime", SlowNumber, &fast_number_); + SetFastMethodNoSideEffect( + ctx, object(), "hrtimeBigInt", SlowBigInt, &fast_bigint_); } void BindingData::RegisterExternalReferences( diff --git a/src/util.cc b/src/util.cc index 006eb068982d75..a50f780b73664f 100644 --- a/src/util.cc +++ b/src/util.cc @@ -398,6 +398,27 @@ void SetFastMethod(Local context, v8::FunctionCallback slow_callback, const v8::CFunction* c_function) { Isolate* isolate = context->GetIsolate(); + Local function = + NewFunctionTemplate(isolate, + slow_callback, + Local(), + v8::ConstructorBehavior::kThrow, + v8::SideEffectType::kHasSideEffect, + c_function) + ->GetFunction(context) + .ToLocalChecked(); + const v8::NewStringType type = v8::NewStringType::kInternalized; + Local name_string = + v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked(); + that->Set(context, name_string, function).Check(); +} + +void SetFastMethodNoSideEffect(Local context, + Local that, + const char* name, + v8::FunctionCallback slow_callback, + const v8::CFunction* c_function) { + Isolate* isolate = context->GetIsolate(); Local function = NewFunctionTemplate(isolate, slow_callback, diff --git a/src/util.h b/src/util.h index d0e4bd41ec92c8..f12d585d33afc8 100644 --- a/src/util.h +++ b/src/util.h @@ -894,6 +894,11 @@ void SetFastMethod(v8::Local context, const char* name, v8::FunctionCallback slow_callback, const v8::CFunction* c_function); +void SetFastMethodNoSideEffect(v8::Local context, + v8::Local that, + const char* name, + v8::FunctionCallback slow_callback, + const v8::CFunction* c_function); void SetProtoMethod(v8::Isolate* isolate, v8::Local that,