From 943b125ec074a8919d7bacdf75541296ac72b4d6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 28 Oct 2024 15:03:12 -0500 Subject: [PATCH 1/2] 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(). --- src/node.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/node.h b/src/node.h index b447d4bf5a4eb4..0502aa7cb516e4 100644 --- a/src/node.h +++ b/src/node.h @@ -1028,8 +1028,8 @@ NODE_DEPRECATED("Use v8::Date::ValueOf() directly", v8::Isolate* isolate = target->GetIsolate(); \ v8::Local context = isolate->GetCurrentContext(); \ v8::Local constant_name = \ - v8::String::NewFromUtf8(isolate, #constant, \ - v8::NewStringType::kInternalized).ToLocalChecked(); \ + v8::String::NewFromUtf8Literal(isolate, #constant, \ + v8::NewStringType::kInternalized); \ v8::Local constant_value = \ v8::Number::New(isolate, static_cast(constant)); \ v8::PropertyAttribute constant_attributes = \ @@ -1046,9 +1046,8 @@ NODE_DEPRECATED("Use v8::Date::ValueOf() directly", v8::Isolate* isolate = target->GetIsolate(); \ v8::Local context = isolate->GetCurrentContext(); \ v8::Local constant_name = \ - v8::String::NewFromUtf8(isolate, #constant, \ - v8::NewStringType::kInternalized) \ - .ToLocalChecked(); \ + v8::String::NewFromUtf8Literal(isolate, #constant, \ + v8::NewStringType::kInternalized); \ v8::Local constant_value = \ v8::Number::New(isolate, static_cast(constant)); \ v8::PropertyAttribute constant_attributes = \ From 49dafdd6715e0517f899784956d6a84142903c8a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 29 Oct 2024 08:59:07 -0500 Subject: [PATCH 2/2] chore: make linter happy --- src/node.h | 69 +++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/src/node.h b/src/node.h index 0502aa7cb516e4..32d346fc4f89f3 100644 --- a/src/node.h +++ b/src/node.h @@ -1023,43 +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::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) +#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,