blob: 0689d93bf0e033cdddd69edeb532dee8a6ad48d5 [file]
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_JS_SEGMENTER_H_
#define V8_OBJECTS_JS_SEGMENTER_H_
#ifndef V8_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif // V8_INTL_SUPPORT
#include <set>
#include <string>
#include "src/base/bit-field.h"
#include "src/execution/isolate.h"
#include "src/heap/factory.h"
#include "src/objects/managed.h"
#include "src/objects/objects.h"
#include "unicode/uversion.h"
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"
namespace U_ICU_NAMESPACE {
class BreakIterator;
} // namespace U_ICU_NAMESPACE
namespace v8 {
namespace internal {
V8_OBJECT class JSSegmenter : public JSObject {
public:
// Creates segmenter object with properties derived from input locales and
// options.
V8_WARN_UNUSED_RESULT static MaybeDirectHandle<JSSegmenter> New(
Isolate* isolate, DirectHandle<Map> map, DirectHandle<Object> locales,
DirectHandle<Object> options, const char* method_name);
V8_WARN_UNUSED_RESULT static DirectHandle<JSObject> ResolvedOptions(
Isolate* isolate, DirectHandle<JSSegmenter> segmenter_holder);
V8_EXPORT_PRIVATE static const std::set<std::string>& GetAvailableLocales();
Handle<String> GranularityAsString(Isolate* isolate) const;
// Segmenter accessors.
inline Tagged<String> locale() const;
inline void set_locale(Tagged<String> value,
WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
inline Tagged<Managed<icu::BreakIterator>> icu_break_iterator() const;
inline void set_icu_break_iterator(
Tagged<Managed<icu::BreakIterator>> value,
WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
inline int flags() const;
inline void set_flags(int value);
// Granularity: identifying the segmenter used.
//
// https://tc39.es/ecma402/#sec-segmenter-internal-slots
enum class Granularity {
GRAPHEME, // for character-breaks
WORD, // for word-breaks
SENTENCE // for sentence-breaks
};
inline void set_granularity(Granularity granularity);
inline Granularity granularity() const;
Handle<String> static GetGranularityString(Isolate* isolate,
Granularity granularity);
// Bit positions in |flags|.
using GranularityBits =
base::BitField<JSSegmenter::Granularity, 0, 2, uint32_t>;
static_assert(GranularityBits::is_valid(Granularity::GRAPHEME));
static_assert(GranularityBits::is_valid(Granularity::WORD));
static_assert(GranularityBits::is_valid(Granularity::SENTENCE));
DECL_PRINTER(JSSegmenter)
DECL_VERIFIER(JSSegmenter)
static const int kHeaderSize;
public:
TaggedMember<String> locale_;
TaggedMember<Foreign> icu_break_iterator_;
TaggedMember<Smi> flags_;
} V8_OBJECT_END;
inline constexpr int JSSegmenter::kHeaderSize = sizeof(JSSegmenter);
} // namespace internal
} // namespace v8
#include "src/objects/object-macros-undef.h"
#endif // V8_OBJECTS_JS_SEGMENTER_H_