From 467618418a187c075633c05198f3e431a61b9143 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 30 Oct 2024 23:48:58 -0500 Subject: [PATCH] src: use NewFromUtf8Literal in NODE_DEFINE_CONSTANT Small efficiency improvement over NewFromUtf8(): the literal's length is known at compile time, so V8 doesn't have to call strlen() or ToLocalChecked(). PR-URL: https://github.com/nodejs/node/pull/55581 Reviewed-By: Yagiz Nizipli Reviewed-By: Richard Lau --- src/node.h | 70 +++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/src/node.h b/src/node.h index b447d4bf5a4eb4..32d346fc4f89f3 100644 --- a/src/node.h +++ b/src/node.h @@ -1023,44 +1023,38 @@ NODE_DEPRECATED("Use v8::Date::ValueOf() directly", }) #define NODE_V8_UNIXTIME node::NODE_V8_UNIXTIME -#define NODE_DEFINE_CONSTANT(target, constant) \ - do { \ - v8::Isolate* isolate = target->GetIsolate(); \ - v8::Local context = isolate->GetCurrentContext(); \ - v8::Local constant_name = \ - v8::String::NewFromUtf8(isolate, #constant, \ - v8::NewStringType::kInternalized).ToLocalChecked(); \ - v8::Local constant_value = \ - v8::Number::New(isolate, static_cast(constant)); \ - v8::PropertyAttribute constant_attributes = \ - static_cast(v8::ReadOnly | v8::DontDelete); \ - (target)->DefineOwnProperty(context, \ - constant_name, \ - constant_value, \ - constant_attributes).Check(); \ - } \ - while (0) - -#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant) \ - do { \ - v8::Isolate* isolate = target->GetIsolate(); \ - v8::Local context = isolate->GetCurrentContext(); \ - v8::Local constant_name = \ - v8::String::NewFromUtf8(isolate, #constant, \ - v8::NewStringType::kInternalized) \ - .ToLocalChecked(); \ - v8::Local constant_value = \ - v8::Number::New(isolate, static_cast(constant)); \ - v8::PropertyAttribute constant_attributes = \ - static_cast(v8::ReadOnly | \ - v8::DontDelete | \ - v8::DontEnum); \ - (target)->DefineOwnProperty(context, \ - constant_name, \ - constant_value, \ - constant_attributes).Check(); \ - } \ - while (0) +#define NODE_DEFINE_CONSTANT(target, constant) \ + do { \ + v8::Isolate* isolate = target->GetIsolate(); \ + v8::Local context = isolate->GetCurrentContext(); \ + v8::Local constant_name = v8::String::NewFromUtf8Literal( \ + isolate, #constant, v8::NewStringType::kInternalized); \ + v8::Local constant_value = \ + v8::Number::New(isolate, static_cast(constant)); \ + v8::PropertyAttribute constant_attributes = \ + static_cast(v8::ReadOnly | v8::DontDelete); \ + (target) \ + ->DefineOwnProperty( \ + context, constant_name, constant_value, constant_attributes) \ + .Check(); \ + } while (0) + +#define NODE_DEFINE_HIDDEN_CONSTANT(target, constant) \ + do { \ + v8::Isolate* isolate = target->GetIsolate(); \ + v8::Local context = isolate->GetCurrentContext(); \ + v8::Local constant_name = v8::String::NewFromUtf8Literal( \ + isolate, #constant, v8::NewStringType::kInternalized); \ + v8::Local constant_value = \ + v8::Number::New(isolate, static_cast(constant)); \ + v8::PropertyAttribute constant_attributes = \ + static_cast(v8::ReadOnly | v8::DontDelete | \ + v8::DontEnum); \ + (target) \ + ->DefineOwnProperty( \ + context, constant_name, constant_value, constant_attributes) \ + .Check(); \ + } while (0) // Used to be a macro, hence the uppercase name. inline void NODE_SET_METHOD(v8::Local recv,