Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: use v8::Isolate::GetDefaultLocale() to compute navigator.language #54279

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.13',
'v8_embedder_string': '-node.14',

##### V8 defaults for Node.js #####

Expand Down
7 changes: 7 additions & 0 deletions deps/v8/include/v8-isolate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdint.h>

#include <memory>
#include <string>
#include <utility>

#include "cppgc/common.h"
Expand Down Expand Up @@ -1717,6 +1718,12 @@ class V8_EXPORT Isolate {
*/
void LocaleConfigurationChangeNotification();

/**
* Returns the default locale in a string if Intl support is enabled.
* Otherwise returns an empty string.
*/
std::string GetDefaultLocale();

Isolate() = delete;
~Isolate() = delete;
Isolate(const Isolate&) = delete;
Expand Down
11 changes: 11 additions & 0 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10768,6 +10768,17 @@ void v8::Isolate::LocaleConfigurationChangeNotification() {
#endif // V8_INTL_SUPPORT
}

std::string Isolate::GetDefaultLocale() {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);

#ifdef V8_INTL_SUPPORT
return i_isolate->DefaultLocale();
#else
return std::string();
#endif
}

#if defined(V8_OS_WIN) && defined(V8_ENABLE_ETW_STACK_WALKING)
void Isolate::SetFilterETWSessionByURLCallback(
FilterETWSessionByURLCallback callback) {
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const {
} = require('internal/process/per_thread');

const {
language,
getDefaultLocale,
} = internalBinding('config');

/**
Expand Down Expand Up @@ -104,7 +104,9 @@ class Navigator {
* @return {string}
*/
get language() {
return language;
// The default locale might be changed dynamically, so always invoke the
// binding.
return getDefaultLocale() || 'en-US';
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
5 changes: 0 additions & 5 deletions lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const {
globalThis,
} = primordials;

const {
Intl,
} = globalThis;

const {
getOptionValue,
refreshOptions,
Expand Down Expand Up @@ -333,7 +329,6 @@ function setupNavigator() {

// https://html.spec.whatwg.org/multipage/system-state.html#the-navigator-object
exposeLazyInterfaces(globalThis, 'internal/navigator', ['Navigator']);
internalBinding('config').language = Intl?.Collator().resolvedOptions().locale || 'en-US';
defineReplaceableLazyAttribute(globalThis, 'internal/navigator', ['navigator'], false);
}

Expand Down
23 changes: 21 additions & 2 deletions src/node_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
#include "memory_tracker.h"
#include "node.h"
#include "node_builtins.h"
#include "node_external_reference.h"
#include "node_i18n.h"
#include "node_options.h"
#include "util-inl.h"

namespace node {

using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Value;

void GetDefaultLocale(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
std::string locale = isolate->GetDefaultLocale();
Local<Value> result;
if (ToV8Value(context, locale).ToLocal(&result)) {
args.GetReturnValue().Set(result);
}
}

// The config binding is used to provide an internal view of compile time
// config options that are required internally by lib/*.js code. This is an
// alternative to dropping additional properties onto the process object as
Expand All @@ -23,7 +35,7 @@ using v8::Value;
// Command line arguments are already accessible in the JS land via
// require('internal/options').getOptionValue('--some-option'). Do not add them
// here.
static void Initialize(Local<Object> target,
static void InitConfig(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Expand Down Expand Up @@ -76,8 +88,15 @@ static void Initialize(Local<Object> target,
#endif // NODE_NO_BROWSER_GLOBALS

READONLY_PROPERTY(target, "bits", Number::New(isolate, 8 * sizeof(intptr_t)));

SetMethodNoSideEffect(context, target, "getDefaultLocale", GetDefaultLocale);
} // InitConfig

void RegisterConfigExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetDefaultLocale);
}

} // namespace node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(config, node::Initialize)
NODE_BINDING_CONTEXT_AWARE_INTERNAL(config, node::InitConfig)
NODE_BINDING_EXTERNAL_REFERENCE(config, node::RegisterConfigExternalReferences)
1 change: 1 addition & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class ExternalReferenceRegistry {
V(buffer) \
V(builtins) \
V(cares_wrap) \
V(config) \
V(contextify) \
V(credentials) \
V(encoding_binding) \
Expand Down
Loading