From 75aa77fbae78df90d095b680ae95ee9da5206e2c Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 23 Nov 2023 00:12:52 +0900 Subject: [PATCH] src: fix compatility with upcoming V8 12.1 APIs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/50709 Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung Reviewed-By: Jiawen Geng Reviewed-By: James M Snell --- src/env-inl.h | 11 +++++-- src/env.h | 10 +++++-- src/js_native_api_v8.cc | 7 ++--- src/node_builtins.cc | 63 +++++++++++++++++++++-------------------- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/env-inl.h b/src/env-inl.h index 793dc72e0dbad8..d5124e73e7c4c6 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -775,13 +775,18 @@ inline void Environment::ThrowRangeError(const char* errmsg) { ThrowError(v8::Exception::RangeError, errmsg); } -inline void Environment::ThrowError( - v8::Local (*fun)(v8::Local), - const char* errmsg) { +inline void Environment::ThrowError(V8ExceptionConstructorOld fun, + const char* errmsg) { v8::HandleScope handle_scope(isolate()); isolate()->ThrowException(fun(OneByteString(isolate(), errmsg))); } +inline void Environment::ThrowError(V8ExceptionConstructorNew fun, + const char* errmsg) { + v8::HandleScope handle_scope(isolate()); + isolate()->ThrowException(fun(OneByteString(isolate(), errmsg), {})); +} + inline void Environment::ThrowErrnoException(int errorno, const char* syscall, const char* message, diff --git a/src/env.h b/src/env.h index afe67d2237ae69..1047f1d794da2a 100644 --- a/src/env.h +++ b/src/env.h @@ -1016,8 +1016,14 @@ class Environment : public MemoryRetainer { }; private: - inline void ThrowError(v8::Local (*fun)(v8::Local), - const char* errmsg); + // V8 has changed the constructor of exceptions, support both APIs before Node + // updates to V8 12.1. + using V8ExceptionConstructorOld = + v8::Local (*)(v8::Local); + using V8ExceptionConstructorNew = + v8::Local (*)(v8::Local, v8::Local); + inline void ThrowError(V8ExceptionConstructorOld fun, const char* errmsg); + inline void ThrowError(V8ExceptionConstructorNew fun, const char* errmsg); void TrackContext(v8::Local context); void UntrackContext(v8::Local context); diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index da6e5c48f15a2d..9b817991e0d59f 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -968,11 +968,8 @@ napi_define_class(napi_env env, env, p->setter, p->data, &setter_tpl)); } - tpl->PrototypeTemplate()->SetAccessorProperty(property_name, - getter_tpl, - setter_tpl, - attributes, - v8::AccessControl::DEFAULT); + tpl->PrototypeTemplate()->SetAccessorProperty( + property_name, getter_tpl, setter_tpl, attributes); } else if (p->method != nullptr) { v8::Local t; STATUS_CALL(v8impl::FunctionCallbackWrapper::NewTemplate( diff --git a/src/node_builtins.cc b/src/node_builtins.cc index 84815969b6d1fa..bafd8d4b8581f0 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -680,37 +680,38 @@ void BuiltinLoader::CreatePerIsolateProperties(IsolateData* isolate_data, Local target) { Isolate* isolate = isolate_data->isolate(); - target->SetAccessor(isolate_data->config_string(), - ConfigStringGetter, - nullptr, - Local(), - DEFAULT, - None, - SideEffectType::kHasNoSideEffect); - - target->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinIds"), - BuiltinIdsGetter, - nullptr, - Local(), - DEFAULT, - None, - SideEffectType::kHasNoSideEffect); - - target->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"), - GetBuiltinCategories, - nullptr, - Local(), - DEFAULT, - None, - SideEffectType::kHasNoSideEffect); - - target->SetAccessor(FIXED_ONE_BYTE_STRING(isolate, "natives"), - GetNatives, - nullptr, - Local(), - DEFAULT, - None, - SideEffectType::kHasNoSideEffect); + target->SetNativeDataProperty(isolate_data->config_string(), + ConfigStringGetter, + nullptr, + Local(), + None, + DEFAULT, + SideEffectType::kHasNoSideEffect); + + target->SetNativeDataProperty(FIXED_ONE_BYTE_STRING(isolate, "builtinIds"), + BuiltinIdsGetter, + nullptr, + Local(), + None, + DEFAULT, + SideEffectType::kHasNoSideEffect); + + target->SetNativeDataProperty( + FIXED_ONE_BYTE_STRING(isolate, "builtinCategories"), + GetBuiltinCategories, + nullptr, + Local(), + None, + DEFAULT, + SideEffectType::kHasNoSideEffect); + + target->SetNativeDataProperty(FIXED_ONE_BYTE_STRING(isolate, "natives"), + GetNatives, + nullptr, + Local(), + None, + DEFAULT, + SideEffectType::kHasNoSideEffect); SetMethod(isolate, target, "getCacheUsage", BuiltinLoader::GetCacheUsage); SetMethod(isolate, target, "compileFunction", BuiltinLoader::CompileFunction);