| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* Intl.Segmenter implementation. */ |
| 8 |
|
| 9 |
#include "builtin/intl/Segmenter.h" |
| 10 |
|
| 11 |
#include "mozilla/Assertions.h" |
| 12 |
#include "mozilla/IntegerTypeTraits.h" |
| 13 |
#include "mozilla/intl/ICU4XGeckoDataProvider.h" |
| 14 |
#include "mozilla/Range.h" |
| 15 |
#include "mozilla/UniquePtr.h" |
| 16 |
|
| 17 |
#include "ICU4XGraphemeClusterSegmenter.h" |
| 18 |
#include "ICU4XSentenceSegmenter.h" |
| 19 |
#include "ICU4XWordSegmenter.h" |
| 20 |
#include "jspubtd.h" |
| 21 |
#include "NamespaceImports.h" |
| 22 |
|
| 23 |
#include "builtin/Array.h" |
| 24 |
#include "builtin/intl/CommonFunctions.h" |
| 25 |
#include "gc/AllocKind.h" |
| 26 |
#include "gc/GCContext.h" |
| 27 |
#include "js/CallArgs.h" |
| 28 |
#include "js/PropertyDescriptor.h" |
| 29 |
#include "js/PropertySpec.h" |
| 30 |
#include "js/RootingAPI.h" |
| 31 |
#include "js/StableStringChars.h" |
| 32 |
#include "js/TypeDecls.h" |
| 33 |
#include "js/Value.h" |
| 34 |
#include "util/Unicode.h" |
| 35 |
#include "vm/ArrayObject.h" |
| 36 |
#include "vm/GlobalObject.h" |
| 37 |
#include "vm/JSContext.h" |
| 38 |
#include "vm/PlainObject.h" |
| 39 |
#include "vm/WellKnownAtom.h" |
| 40 |
|
| 41 |
#include "vm/JSObject-inl.h" |
| 42 |
#include "vm/NativeObject-inl.h" |
| 43 |
|
| 44 |
using namespace js; |
| 45 |
|
| 46 |
const JSClassOps SegmenterObject::classOps_ = { |
| 47 |
nullptr, // addProperty |
| 48 |
nullptr, // delProperty |
| 49 |
nullptr, // enumerate |
| 50 |
nullptr, // newEnumerate |
| 51 |
nullptr, // resolve |
| 52 |
nullptr, // mayResolve |
| 53 |
SegmenterObject::finalize, // finalize |
| 54 |
nullptr, // call |
| 55 |
nullptr, // construct |
| 56 |
nullptr, // trace |
| 57 |
}; |
| 58 |
|
| 59 |
const JSClass SegmenterObject::class_ = { |
| 60 |
"Intl.Segmenter", |
| 61 |
JSCLASS_HAS_RESERVED_SLOTS(SegmenterObject::SLOT_COUNT) | |
| 62 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Segmenter) | |
| 63 |
JSCLASS_FOREGROUND_FINALIZE, |
| 64 |
&SegmenterObject::classOps_, |
| 65 |
&SegmenterObject::classSpec_, |
| 66 |
}; |
| 67 |
|
| 68 |
const JSClass& SegmenterObject::protoClass_ = PlainObject::class_; |
| 69 |
|
| 70 |
static bool segmenter_toSource(JSContext* cx, unsigned argc, Value* vp) { |
| 71 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 72 |
args.rval().setString(cx->names().Segmenter); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
static const JSFunctionSpec segmenter_static_methods[] = { |
| 77 |
JS_SELF_HOSTED_FN("supportedLocalesOf", "Intl_Segmenter_supportedLocalesOf", |
| 78 |
1, 0), |
| 79 |
JS_FS_END, |
| 80 |
}; |
| 81 |
|
| 82 |
static const JSFunctionSpec segmenter_methods[] = { |
| 83 |
JS_SELF_HOSTED_FN("resolvedOptions", "Intl_Segmenter_resolvedOptions", 0, |
| 84 |
0), |
| 85 |
JS_SELF_HOSTED_FN("segment", "Intl_Segmenter_segment", 1, 0), |
| 86 |
JS_FN("toSource", segmenter_toSource, 0, 0), |
| 87 |
JS_FS_END, |
| 88 |
}; |
| 89 |
|
| 90 |
static const JSPropertySpec segmenter_properties[] = { |
| 91 |
JS_STRING_SYM_PS(toStringTag, "Intl.Segmenter", JSPROP_READONLY), |
| 92 |
JS_PS_END, |
| 93 |
}; |
| 94 |
|
| 95 |
static bool Segmenter(JSContext* cx, unsigned argc, Value* vp); |
| 96 |
|
| 97 |
const ClassSpec SegmenterObject::classSpec_ = { |
| 98 |
GenericCreateConstructor<Segmenter, 0, gc::AllocKind::FUNCTION>, |
| 99 |
GenericCreatePrototype<SegmenterObject>, |
| 100 |
segmenter_static_methods, |
| 101 |
nullptr, |
| 102 |
segmenter_methods, |
| 103 |
segmenter_properties, |
| 104 |
nullptr, |
| 105 |
ClassSpec::DontDefineConstructor, |
| 106 |
}; |
| 107 |
|
| 108 |
/** |
| 109 |
* Intl.Segmenter ([ locales [ , options ]]) |
| 110 |
*/ |
| 111 |
static bool Segmenter(JSContext* cx, unsigned argc, Value* vp) { |
| 112 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 113 |
|
| 114 |
// Step 1. |
| 115 |
if (!ThrowIfNotConstructing(cx, args, "Intl.Segmenter")) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
// Steps 2-3 (Inlined 9.1.14, OrdinaryCreateFromConstructor). |
| 120 |
Rooted<JSObject*> proto(cx); |
| 121 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_Segmenter, |
| 122 |
&proto)) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
Rooted<SegmenterObject*> segmenter(cx); |
| 127 |
segmenter = NewObjectWithClassProto<SegmenterObject>(cx, proto); |
| 128 |
if (!segmenter) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
HandleValue locales = args.get(0); |
| 133 |
HandleValue options = args.get(1); |
| 134 |
|
| 135 |
// Steps 4-13. |
| 136 |
if (!intl::InitializeObject(cx, segmenter, cx->names().InitializeSegmenter, |
| 137 |
locales, options)) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
// Step 14. |
| 142 |
args.rval().setObject(*segmenter); |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
const JSClassOps SegmentsObject::classOps_ = { |
| 147 |
nullptr, // addProperty |
| 148 |
nullptr, // delProperty |
| 149 |
nullptr, // enumerate |
| 150 |
nullptr, // newEnumerate |
| 151 |
nullptr, // resolve |
| 152 |
nullptr, // mayResolve |
| 153 |
SegmentsObject::finalize, // finalize |
| 154 |
nullptr, // call |
| 155 |
nullptr, // construct |
| 156 |
nullptr, // trace |
| 157 |
}; |
| 158 |
|
| 159 |
const JSClass SegmentsObject::class_ = { |
| 160 |
"Intl.Segments", |
| 161 |
JSCLASS_HAS_RESERVED_SLOTS(SegmentsObject::SLOT_COUNT) | |
| 162 |
JSCLASS_FOREGROUND_FINALIZE, |
| 163 |
&SegmentsObject::classOps_, |
| 164 |
}; |
| 165 |
|
| 166 |
static const JSFunctionSpec segments_methods[] = { |
| 167 |
JS_SELF_HOSTED_FN("containing", "Intl_Segments_containing", 1, 0), |
| 168 |
JS_SELF_HOSTED_SYM_FN(iterator, "Intl_Segments_iterator", 0, 0), |
| 169 |
JS_FS_END, |
| 170 |
}; |
| 171 |
|
| 172 |
bool GlobalObject::initSegmentsProto(JSContext* cx, |
| 173 |
Handle<GlobalObject*> global) { |
| 174 |
Rooted<JSObject*> proto( |
| 175 |
cx, GlobalObject::createBlankPrototype<PlainObject>(cx, global)); |
| 176 |
if (!proto) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
if (!JS_DefineFunctions(cx, proto, segments_methods)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
global->initBuiltinProto(ProtoKind::SegmentsProto, proto); |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
const JSClassOps SegmentIteratorObject::classOps_ = { |
| 189 |
nullptr, // addProperty |
| 190 |
nullptr, // delProperty |
| 191 |
nullptr, // enumerate |
| 192 |
nullptr, // newEnumerate |
| 193 |
nullptr, // resolve |
| 194 |
nullptr, // mayResolve |
| 195 |
SegmentIteratorObject::finalize, // finalize |
| 196 |
nullptr, // call |
| 197 |
nullptr, // construct |
| 198 |
nullptr, // trace |
| 199 |
}; |
| 200 |
|
| 201 |
const JSClass SegmentIteratorObject::class_ = { |
| 202 |
"Intl.SegmentIterator", |
| 203 |
JSCLASS_HAS_RESERVED_SLOTS(SegmentIteratorObject::SLOT_COUNT) | |
| 204 |
JSCLASS_FOREGROUND_FINALIZE, |
| 205 |
&SegmentIteratorObject::classOps_, |
| 206 |
}; |
| 207 |
|
| 208 |
static const JSFunctionSpec segment_iterator_methods[] = { |
| 209 |
JS_SELF_HOSTED_FN("next", "Intl_SegmentIterator_next", 0, 0), |
| 210 |
JS_FS_END, |
| 211 |
}; |
| 212 |
|
| 213 |
static const JSPropertySpec segment_iterator_properties[] = { |
| 214 |
JS_STRING_SYM_PS(toStringTag, "Segmenter String Iterator", JSPROP_READONLY), |
| 215 |
JS_PS_END, |
| 216 |
}; |
| 217 |
|
| 218 |
bool GlobalObject::initSegmentIteratorProto(JSContext* cx, |
| 219 |
Handle<GlobalObject*> global) { |
| 220 |
Rooted<JSObject*> iteratorProto( |
| 221 |
cx, GlobalObject::getOrCreateIteratorPrototype(cx, global)); |
| 222 |
if (!iteratorProto) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
Rooted<JSObject*> proto( |
| 227 |
cx, GlobalObject::createBlankPrototypeInheriting<PlainObject>( |
| 228 |
cx, iteratorProto)); |
| 229 |
if (!proto) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
if (!JS_DefineFunctions(cx, proto, segment_iterator_methods)) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
if (!JS_DefineProperties(cx, proto, segment_iterator_properties)) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
global->initBuiltinProto(ProtoKind::SegmentIteratorProto, proto); |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
struct Boundaries { |
| 246 |
// Start index of this segmentation boundary. |
| 247 |
int32_t startIndex = 0; |
| 248 |
|
| 249 |
// End index of this segmentation boundary. |
| 250 |
int32_t endIndex = 0; |
| 251 |
|
| 252 |
// |true| if the segment is word-like. (Only used for word segmentation.) |
| 253 |
bool isWordLike = false; |
| 254 |
}; |
| 255 |
|
| 256 |
/** |
| 257 |
* Find the segmentation boundary for the string character whose position is |
| 258 |
* |index|. The end position of the last segment boundary is |previousIndex|. |
| 259 |
*/ |
| 260 |
template <class T> |
| 261 |
static Boundaries FindBoundaryFrom(const T& iter, int32_t previousIndex, |
| 262 |
int32_t index) { |
| 263 |
MOZ_ASSERT(previousIndex <= index, |
| 264 |
"previous index must not exceed the search index"); |
| 265 |
|
| 266 |
int32_t previous = previousIndex; |
| 267 |
while (true) { |
| 268 |
// Find the next possible break index. |
| 269 |
int32_t next = iter.next(); |
| 270 |
|
| 271 |
// If |next| is larger than the search index, we've found our segment end |
| 272 |
// index. |
| 273 |
if (next > index) { |
| 274 |
return {previous, next, iter.isWordLike()}; |
| 275 |
} |
| 276 |
|
| 277 |
// Otherwise store |next| as the start index of the next segment, |
| 278 |
previous = next; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
// TODO: Consider switching to the ICU4X C++ headers when the C++ headers |
| 283 |
// are in better shape: https://github.com/rust-diplomat/diplomat/issues/280 |
| 284 |
|
| 285 |
template <typename Interface> |
| 286 |
class SegmenterBreakIteratorType { |
| 287 |
typename Interface::BreakIterator* impl_; |
| 288 |
|
| 289 |
public: |
| 290 |
explicit SegmenterBreakIteratorType(void* impl) |
| 291 |
: impl_(static_cast<typename Interface::BreakIterator*>(impl)) { |
| 292 |
MOZ_ASSERT(impl); |
| 293 |
} |
| 294 |
|
| 295 |
int32_t next() const { return Interface::next(impl_); } |
| 296 |
|
| 297 |
bool isWordLike() const { return Interface::isWordLike(impl_); } |
| 298 |
}; |
| 299 |
|
| 300 |
// Each SegmenterBreakIterator interface contains the following definitions: |
| 301 |
// |
| 302 |
// - BreakIterator: Type of the ICU4X break iterator. |
| 303 |
// - Segmenter: Type of the ICU4X segmenter. |
| 304 |
// - Char: Character type, either `JS::Latin1Char` or `char16_t`. |
| 305 |
// - create: Static method to create a new instance of `BreakIterator`. |
| 306 |
// - destroy: Static method to destroy an instance of `BreakIterator`. |
| 307 |
// - next: Static method to fetch the next break iteration index. |
| 308 |
// - isWordLike: Static method to determine if the current segment is word-like. |
| 309 |
// |
| 310 |
// |
| 311 |
// Each Segmenter interface contains the following definitions: |
| 312 |
// |
| 313 |
// - Segmenter: Type of the ICU4X segmenter. |
| 314 |
// - BreakIteratorLatin1: SegmenterBreakIterator interface to Latin1 strings. |
| 315 |
// - BreakIteratorTwoByte: SegmenterBreakIterator interface to TwoByte strings. |
| 316 |
// - create: Static method to create a new instance of `Segmenter`. |
| 317 |
// - destroy: Static method to destroy an instance of `Segmenter`. |
| 318 |
|
| 319 |
struct GraphemeClusterSegmenterBreakIteratorLatin1 { |
| 320 |
using BreakIterator = capi::ICU4XGraphemeClusterBreakIteratorLatin1; |
| 321 |
using Segmenter = capi::ICU4XGraphemeClusterSegmenter; |
| 322 |
using Char = JS::Latin1Char; |
| 323 |
|
| 324 |
static constexpr auto& create = |
| 325 |
capi::ICU4XGraphemeClusterSegmenter_segment_latin1; |
| 326 |
static constexpr auto& destroy = |
| 327 |
capi::ICU4XGraphemeClusterBreakIteratorLatin1_destroy; |
| 328 |
static constexpr auto& next = |
| 329 |
capi::ICU4XGraphemeClusterBreakIteratorLatin1_next; |
| 330 |
|
| 331 |
static bool isWordLike(const BreakIterator*) { return false; } |
| 332 |
}; |
| 333 |
|
| 334 |
struct GraphemeClusterSegmenterBreakIteratorTwoByte { |
| 335 |
using BreakIterator = capi::ICU4XGraphemeClusterBreakIteratorUtf16; |
| 336 |
using Segmenter = capi::ICU4XGraphemeClusterSegmenter; |
| 337 |
using Char = char16_t; |
| 338 |
|
| 339 |
static constexpr auto& create = |
| 340 |
capi::ICU4XGraphemeClusterSegmenter_segment_utf16; |
| 341 |
static constexpr auto& destroy = |
| 342 |
capi::ICU4XGraphemeClusterBreakIteratorUtf16_destroy; |
| 343 |
static constexpr auto& next = |
| 344 |
capi::ICU4XGraphemeClusterBreakIteratorUtf16_next; |
| 345 |
|
| 346 |
static bool isWordLike(const BreakIterator*) { return false; } |
| 347 |
}; |
| 348 |
|
| 349 |
struct GraphemeClusterSegmenter { |
| 350 |
using Segmenter = capi::ICU4XGraphemeClusterSegmenter; |
| 351 |
using BreakIteratorLatin1 = |
| 352 |
SegmenterBreakIteratorType<GraphemeClusterSegmenterBreakIteratorLatin1>; |
| 353 |
using BreakIteratorTwoByte = |
| 354 |
SegmenterBreakIteratorType<GraphemeClusterSegmenterBreakIteratorTwoByte>; |
| 355 |
|
| 356 |
static constexpr auto& create = capi::ICU4XGraphemeClusterSegmenter_create; |
| 357 |
static constexpr auto& destroy = capi::ICU4XGraphemeClusterSegmenter_destroy; |
| 358 |
}; |
| 359 |
|
| 360 |
struct WordSegmenterBreakIteratorLatin1 { |
| 361 |
using BreakIterator = capi::ICU4XWordBreakIteratorLatin1; |
| 362 |
using Segmenter = capi::ICU4XWordSegmenter; |
| 363 |
using Char = JS::Latin1Char; |
| 364 |
|
| 365 |
static constexpr auto& create = capi::ICU4XWordSegmenter_segment_latin1; |
| 366 |
static constexpr auto& destroy = capi::ICU4XWordBreakIteratorLatin1_destroy; |
| 367 |
static constexpr auto& next = capi::ICU4XWordBreakIteratorLatin1_next; |
| 368 |
static constexpr auto& isWordLike = |
| 369 |
capi::ICU4XWordBreakIteratorLatin1_is_word_like; |
| 370 |
}; |
| 371 |
|
| 372 |
struct WordSegmenterBreakIteratorTwoByte { |
| 373 |
using BreakIterator = capi::ICU4XWordBreakIteratorUtf16; |
| 374 |
using Segmenter = capi::ICU4XWordSegmenter; |
| 375 |
using Char = char16_t; |
| 376 |
|
| 377 |
static constexpr auto& create = capi::ICU4XWordSegmenter_segment_utf16; |
| 378 |
static constexpr auto& destroy = capi::ICU4XWordBreakIteratorUtf16_destroy; |
| 379 |
static constexpr auto& next = capi::ICU4XWordBreakIteratorUtf16_next; |
| 380 |
static constexpr auto& isWordLike = |
| 381 |
capi::ICU4XWordBreakIteratorUtf16_is_word_like; |
| 382 |
}; |
| 383 |
|
| 384 |
struct WordSegmenter { |
| 385 |
using Segmenter = capi::ICU4XWordSegmenter; |
| 386 |
using BreakIteratorLatin1 = |
| 387 |
SegmenterBreakIteratorType<WordSegmenterBreakIteratorLatin1>; |
| 388 |
using BreakIteratorTwoByte = |
| 389 |
SegmenterBreakIteratorType<WordSegmenterBreakIteratorTwoByte>; |
| 390 |
|
| 391 |
static constexpr auto& create = capi::ICU4XWordSegmenter_create_auto; |
| 392 |
static constexpr auto& destroy = capi::ICU4XWordSegmenter_destroy; |
| 393 |
}; |
| 394 |
|
| 395 |
struct SentenceSegmenterBreakIteratorLatin1 { |
| 396 |
using BreakIterator = capi::ICU4XSentenceBreakIteratorLatin1; |
| 397 |
using Segmenter = capi::ICU4XSentenceSegmenter; |
| 398 |
using Char = JS::Latin1Char; |
| 399 |
|
| 400 |
static constexpr auto& create = capi::ICU4XSentenceSegmenter_segment_latin1; |
| 401 |
static constexpr auto& destroy = |
| 402 |
capi::ICU4XSentenceBreakIteratorLatin1_destroy; |
| 403 |
static constexpr auto& next = capi::ICU4XSentenceBreakIteratorLatin1_next; |
| 404 |
|
| 405 |
static bool isWordLike(const BreakIterator*) { return false; } |
| 406 |
}; |
| 407 |
|
| 408 |
struct SentenceSegmenterBreakIteratorTwoByte { |
| 409 |
using BreakIterator = capi::ICU4XSentenceBreakIteratorUtf16; |
| 410 |
using Segmenter = capi::ICU4XSentenceSegmenter; |
| 411 |
using Char = char16_t; |
| 412 |
|
| 413 |
static constexpr auto& create = capi::ICU4XSentenceSegmenter_segment_utf16; |
| 414 |
static constexpr auto& destroy = |
| 415 |
capi::ICU4XSentenceBreakIteratorUtf16_destroy; |
| 416 |
static constexpr auto& next = capi::ICU4XSentenceBreakIteratorUtf16_next; |
| 417 |
|
| 418 |
static bool isWordLike(const BreakIterator*) { return false; } |
| 419 |
}; |
| 420 |
|
| 421 |
struct SentenceSegmenter { |
| 422 |
using Segmenter = capi::ICU4XSentenceSegmenter; |
| 423 |
using BreakIteratorLatin1 = |
| 424 |
SegmenterBreakIteratorType<SentenceSegmenterBreakIteratorLatin1>; |
| 425 |
using BreakIteratorTwoByte = |
| 426 |
SegmenterBreakIteratorType<SentenceSegmenterBreakIteratorTwoByte>; |
| 427 |
|
| 428 |
static constexpr auto& create = capi::ICU4XSentenceSegmenter_create; |
| 429 |
static constexpr auto& destroy = capi::ICU4XSentenceSegmenter_destroy; |
| 430 |
}; |
| 431 |
|
| 432 |
/** |
| 433 |
* Create a new ICU4X segmenter instance. |
| 434 |
*/ |
| 435 |
template <typename Interface> |
| 436 |
static typename Interface::Segmenter* CreateSegmenter(JSContext* cx) { |
| 437 |
auto result = Interface::create(mozilla::intl::GetDataProvider()); |
| 438 |
if (!result.is_ok) { |
| 439 |
intl::ReportInternalError(cx); |
| 440 |
return nullptr; |
| 441 |
} |
| 442 |
return result.ok; |
| 443 |
} |
| 444 |
|
| 445 |
static bool EnsureInternalsResolved(JSContext* cx, |
| 446 |
Handle<SegmenterObject*> segmenter) { |
| 447 |
if (segmenter->getLocale()) { |
| 448 |
return true; |
| 449 |
} |
| 450 |
|
| 451 |
Rooted<JS::Value> value(cx); |
| 452 |
|
| 453 |
Rooted<JSObject*> internals(cx, intl::GetInternalsObject(cx, segmenter)); |
| 454 |
if (!internals) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
if (!GetProperty(cx, internals, internals, cx->names().locale, &value)) { |
| 459 |
return false; |
| 460 |
} |
| 461 |
Rooted<JSString*> locale(cx, value.toString()); |
| 462 |
|
| 463 |
if (!GetProperty(cx, internals, internals, cx->names().granularity, &value)) { |
| 464 |
return false; |
| 465 |
} |
| 466 |
|
| 467 |
SegmenterGranularity granularity; |
| 468 |
{ |
| 469 |
JSLinearString* linear = value.toString()->ensureLinear(cx); |
| 470 |
if (!linear) { |
| 471 |
return false; |
| 472 |
} |
| 473 |
|
| 474 |
if (StringEqualsLiteral(linear, "grapheme")) { |
| 475 |
granularity = SegmenterGranularity::Grapheme; |
| 476 |
} else if (StringEqualsLiteral(linear, "word")) { |
| 477 |
granularity = SegmenterGranularity::Word; |
| 478 |
} else { |
| 479 |
MOZ_ASSERT(StringEqualsLiteral(linear, "sentence")); |
| 480 |
granularity = SegmenterGranularity::Sentence; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
switch (granularity) { |
| 485 |
case SegmenterGranularity::Grapheme: { |
| 486 |
auto* seg = CreateSegmenter<GraphemeClusterSegmenter>(cx); |
| 487 |
if (!seg) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
segmenter->setSegmenter(seg); |
| 491 |
break; |
| 492 |
} |
| 493 |
case SegmenterGranularity::Word: { |
| 494 |
auto* seg = CreateSegmenter<WordSegmenter>(cx); |
| 495 |
if (!seg) { |
| 496 |
return false; |
| 497 |
} |
| 498 |
segmenter->setSegmenter(seg); |
| 499 |
break; |
| 500 |
} |
| 501 |
case SegmenterGranularity::Sentence: { |
| 502 |
auto* seg = CreateSegmenter<SentenceSegmenter>(cx); |
| 503 |
if (!seg) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
segmenter->setSegmenter(seg); |
| 507 |
break; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
segmenter->setLocale(locale); |
| 512 |
segmenter->setGranularity(granularity); |
| 513 |
|
| 514 |
return true; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Destroy an ICU4X segmenter instance. |
| 519 |
*/ |
| 520 |
template <typename Interface> |
| 521 |
static void DestroySegmenter(void* seg) { |
| 522 |
auto* segmenter = static_cast<typename Interface::Segmenter*>(seg); |
| 523 |
Interface::destroy(segmenter); |
| 524 |
} |
| 525 |
|
| 526 |
void SegmenterObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 527 |
MOZ_ASSERT(gcx->onMainThread()); |
| 528 |
|
| 529 |
auto& segmenter = obj->as<SegmenterObject>(); |
| 530 |
if (void* seg = segmenter.getSegmenter()) { |
| 531 |
switch (segmenter.getGranularity()) { |
| 532 |
case SegmenterGranularity::Grapheme: { |
| 533 |
DestroySegmenter<GraphemeClusterSegmenter>(seg); |
| 534 |
break; |
| 535 |
} |
| 536 |
case SegmenterGranularity::Word: { |
| 537 |
DestroySegmenter<WordSegmenter>(seg); |
| 538 |
break; |
| 539 |
} |
| 540 |
case SegmenterGranularity::Sentence: { |
| 541 |
DestroySegmenter<SentenceSegmenter>(seg); |
| 542 |
break; |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Destroy an ICU4X break iterator instance. |
| 550 |
*/ |
| 551 |
template <typename Interface> |
| 552 |
static void DestroyBreakIterator(void* brk) { |
| 553 |
auto* breakIterator = static_cast<typename Interface::BreakIterator*>(brk); |
| 554 |
Interface::destroy(breakIterator); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Destroy the ICU4X break iterator attached to |segments|. |
| 559 |
*/ |
| 560 |
template <typename T> |
| 561 |
static void DestroyBreakIterator(const T* segments) { |
| 562 |
void* brk = segments->getBreakIterator(); |
| 563 |
MOZ_ASSERT(brk); |
| 564 |
|
| 565 |
bool isLatin1 = segments->hasLatin1StringChars(); |
| 566 |
|
| 567 |
switch (segments->getGranularity()) { |
| 568 |
case SegmenterGranularity::Grapheme: { |
| 569 |
if (isLatin1) { |
| 570 |
DestroyBreakIterator<GraphemeClusterSegmenterBreakIteratorLatin1>(brk); |
| 571 |
} else { |
| 572 |
DestroyBreakIterator<GraphemeClusterSegmenterBreakIteratorTwoByte>(brk); |
| 573 |
} |
| 574 |
break; |
| 575 |
} |
| 576 |
case SegmenterGranularity::Word: { |
| 577 |
if (isLatin1) { |
| 578 |
DestroyBreakIterator<WordSegmenterBreakIteratorLatin1>(brk); |
| 579 |
} else { |
| 580 |
DestroyBreakIterator<WordSegmenterBreakIteratorTwoByte>(brk); |
| 581 |
} |
| 582 |
break; |
| 583 |
} |
| 584 |
case SegmenterGranularity::Sentence: { |
| 585 |
if (isLatin1) { |
| 586 |
DestroyBreakIterator<SentenceSegmenterBreakIteratorLatin1>(brk); |
| 587 |
} else { |
| 588 |
DestroyBreakIterator<SentenceSegmenterBreakIteratorTwoByte>(brk); |
| 589 |
} |
| 590 |
break; |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
void SegmentsObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 596 |
MOZ_ASSERT(gcx->onMainThread()); |
| 597 |
|
| 598 |
auto* segments = &obj->as<SegmentsObject>(); |
| 599 |
|
| 600 |
if (auto chars = segments->getStringChars()) { |
| 601 |
size_t length = segments->getString()->length(); |
| 602 |
if (chars.has<JS::Latin1Char>()) { |
| 603 |
intl::RemoveICUCellMemory(gcx, segments, length * sizeof(JS::Latin1Char)); |
| 604 |
js_free(chars.data<JS::Latin1Char>()); |
| 605 |
} else { |
| 606 |
intl::RemoveICUCellMemory(gcx, segments, length * sizeof(char16_t)); |
| 607 |
js_free(chars.data<char16_t>()); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
if (segments->getBreakIterator()) { |
| 612 |
DestroyBreakIterator(segments); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
void SegmentIteratorObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 617 |
MOZ_ASSERT(gcx->onMainThread()); |
| 618 |
|
| 619 |
auto* iterator = &obj->as<SegmentIteratorObject>(); |
| 620 |
|
| 621 |
if (auto chars = iterator->getStringChars()) { |
| 622 |
size_t length = iterator->getString()->length(); |
| 623 |
if (chars.has<JS::Latin1Char>()) { |
| 624 |
intl::RemoveICUCellMemory(gcx, iterator, length * sizeof(JS::Latin1Char)); |
| 625 |
js_free(chars.data<JS::Latin1Char>()); |
| 626 |
} else { |
| 627 |
intl::RemoveICUCellMemory(gcx, iterator, length * sizeof(char16_t)); |
| 628 |
js_free(chars.data<char16_t>()); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
if (iterator->getBreakIterator()) { |
| 633 |
DestroyBreakIterator(iterator); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
template <typename Iterator, typename T> |
| 638 |
static Boundaries FindBoundaryFrom(Handle<T*> segments, int32_t index) { |
| 639 |
MOZ_ASSERT(0 <= index && uint32_t(index) < segments->getString()->length()); |
| 640 |
|
| 641 |
Iterator iter(segments->getBreakIterator()); |
| 642 |
return FindBoundaryFrom(iter, segments->getIndex(), index); |
| 643 |
} |
| 644 |
|
| 645 |
template <typename T> |
| 646 |
static Boundaries GraphemeBoundaries(Handle<T*> segments, int32_t index) { |
| 647 |
if (segments->hasLatin1StringChars()) { |
| 648 |
return FindBoundaryFrom<GraphemeClusterSegmenter::BreakIteratorLatin1>( |
| 649 |
segments, index); |
| 650 |
} |
| 651 |
return FindBoundaryFrom<GraphemeClusterSegmenter::BreakIteratorTwoByte>( |
| 652 |
segments, index); |
| 653 |
} |
| 654 |
|
| 655 |
template <typename T> |
| 656 |
static Boundaries WordBoundaries(Handle<T*> segments, int32_t index) { |
| 657 |
if (segments->hasLatin1StringChars()) { |
| 658 |
return FindBoundaryFrom<WordSegmenter::BreakIteratorLatin1>(segments, |
| 659 |
index); |
| 660 |
} |
| 661 |
return FindBoundaryFrom<WordSegmenter::BreakIteratorTwoByte>(segments, index); |
| 662 |
} |
| 663 |
|
| 664 |
template <typename T> |
| 665 |
static Boundaries SentenceBoundaries(Handle<T*> segments, int32_t index) { |
| 666 |
if (segments->hasLatin1StringChars()) { |
| 667 |
return FindBoundaryFrom<SentenceSegmenter::BreakIteratorLatin1>(segments, |
| 668 |
index); |
| 669 |
} |
| 670 |
return FindBoundaryFrom<SentenceSegmenter::BreakIteratorTwoByte>(segments, |
| 671 |
index); |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Ensure the string characters have been copied into |segments| in preparation |
| 676 |
* for passing the string characters to ICU4X. |
| 677 |
*/ |
| 678 |
template <typename T> |
| 679 |
static bool EnsureStringChars(JSContext* cx, Handle<T*> segments) { |
| 680 |
if (segments->hasStringChars()) { |
| 681 |
return true; |
| 682 |
} |
| 683 |
|
| 684 |
Rooted<JSLinearString*> string(cx, segments->getString()->ensureLinear(cx)); |
| 685 |
if (!string) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
|
| 689 |
size_t length = string->length(); |
| 690 |
|
| 691 |
JS::AutoCheckCannotGC nogc; |
| 692 |
if (string->hasLatin1Chars()) { |
| 693 |
auto chars = DuplicateString(cx, string->latin1Chars(nogc), length); |
| 694 |
if (!chars) { |
| 695 |
return false; |
| 696 |
} |
| 697 |
segments->setStringChars(SegmentsStringChars{chars.release()}); |
| 698 |
|
| 699 |
intl::AddICUCellMemory(segments, length * sizeof(JS::Latin1Char)); |
| 700 |
} else { |
| 701 |
auto chars = DuplicateString(cx, string->twoByteChars(nogc), length); |
| 702 |
if (!chars) { |
| 703 |
return false; |
| 704 |
} |
| 705 |
segments->setStringChars(SegmentsStringChars{chars.release()}); |
| 706 |
|
| 707 |
intl::AddICUCellMemory(segments, length * sizeof(char16_t)); |
| 708 |
} |
| 709 |
return true; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Create a new ICU4X break iterator instance. |
| 714 |
*/ |
| 715 |
template <typename Interface, typename T> |
| 716 |
static auto* CreateBreakIterator(Handle<T*> segments) { |
| 717 |
void* segmenter = segments->getSegmenter()->getSegmenter(); |
| 718 |
MOZ_ASSERT(segmenter); |
| 719 |
|
| 720 |
auto chars = segments->getStringChars(); |
| 721 |
MOZ_ASSERT(chars); |
| 722 |
|
| 723 |
size_t length = segments->getString()->length(); |
| 724 |
|
| 725 |
auto* seg = static_cast<const typename Interface::Segmenter*>(segmenter); |
| 726 |
auto* ch = chars.template data<typename Interface::Char>(); |
| 727 |
return Interface::create(seg, ch, length); |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Ensure |segments| has a break iterator whose current segment index is at most |
| 732 |
* |index|. |
| 733 |
*/ |
| 734 |
template <typename T> |
| 735 |
static bool EnsureBreakIterator(JSContext* cx, Handle<T*> segments, |
| 736 |
int32_t index) { |
| 737 |
if (segments->getBreakIterator()) { |
| 738 |
// Reuse the break iterator if its current segment index is at most |index|. |
| 739 |
if (index >= segments->getIndex()) { |
| 740 |
return true; |
| 741 |
} |
| 742 |
|
| 743 |
// Reverse iteration not supported. Destroy the previous break iterator and |
| 744 |
// start from fresh. |
| 745 |
DestroyBreakIterator(segments.get()); |
| 746 |
|
| 747 |
// Reset internal state. |
| 748 |
segments->setBreakIterator(nullptr); |
| 749 |
segments->setIndex(0); |
| 750 |
} |
| 751 |
|
| 752 |
// Ensure the string characters can be passed to ICU4X. |
| 753 |
if (!EnsureStringChars(cx, segments)) { |
| 754 |
return false; |
| 755 |
} |
| 756 |
|
| 757 |
bool isLatin1 = segments->hasLatin1StringChars(); |
| 758 |
|
| 759 |
// Create a new break iterator based on the granularity and character type. |
| 760 |
void* brk; |
| 761 |
switch (segments->getGranularity()) { |
| 762 |
case SegmenterGranularity::Grapheme: { |
| 763 |
if (isLatin1) { |
| 764 |
brk = CreateBreakIterator<GraphemeClusterSegmenterBreakIteratorLatin1>( |
| 765 |
segments); |
| 766 |
} else { |
| 767 |
brk = CreateBreakIterator<GraphemeClusterSegmenterBreakIteratorTwoByte>( |
| 768 |
segments); |
| 769 |
} |
| 770 |
break; |
| 771 |
} |
| 772 |
case SegmenterGranularity::Word: { |
| 773 |
if (isLatin1) { |
| 774 |
brk = CreateBreakIterator<WordSegmenterBreakIteratorLatin1>(segments); |
| 775 |
} else { |
| 776 |
brk = CreateBreakIterator<WordSegmenterBreakIteratorTwoByte>(segments); |
| 777 |
} |
| 778 |
break; |
| 779 |
} |
| 780 |
case SegmenterGranularity::Sentence: { |
| 781 |
if (isLatin1) { |
| 782 |
brk = |
| 783 |
CreateBreakIterator<SentenceSegmenterBreakIteratorLatin1>(segments); |
| 784 |
} else { |
| 785 |
brk = CreateBreakIterator<SentenceSegmenterBreakIteratorTwoByte>( |
| 786 |
segments); |
| 787 |
} |
| 788 |
break; |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
MOZ_RELEASE_ASSERT(brk); |
| 793 |
segments->setBreakIterator(brk); |
| 794 |
|
| 795 |
MOZ_ASSERT(segments->getIndex() == 0, "index is initially zero"); |
| 796 |
|
| 797 |
return true; |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
* Create the boundaries result array for self-hosted code. |
| 802 |
*/ |
| 803 |
static ArrayObject* CreateBoundaries(JSContext* cx, Boundaries boundaries, |
| 804 |
SegmenterGranularity granularity) { |
| 805 |
auto [startIndex, endIndex, isWordLike] = boundaries; |
| 806 |
|
| 807 |
auto* result = NewDenseFullyAllocatedArray(cx, 3); |
| 808 |
if (!result) { |
| 809 |
return nullptr; |
| 810 |
} |
| 811 |
result->setDenseInitializedLength(3); |
| 812 |
result->initDenseElement(0, Int32Value(startIndex)); |
| 813 |
result->initDenseElement(1, Int32Value(endIndex)); |
| 814 |
if (granularity == SegmenterGranularity::Word) { |
| 815 |
result->initDenseElement(2, BooleanValue(isWordLike)); |
| 816 |
} else { |
| 817 |
result->initDenseElement(2, UndefinedValue()); |
| 818 |
} |
| 819 |
return result; |
| 820 |
} |
| 821 |
|
| 822 |
template <typename T> |
| 823 |
static ArrayObject* FindSegmentBoundaries(JSContext* cx, Handle<T*> segments, |
| 824 |
int32_t index) { |
| 825 |
// Ensure break iteration can start at |index|. |
| 826 |
if (!EnsureBreakIterator(cx, segments, index)) { |
| 827 |
return nullptr; |
| 828 |
} |
| 829 |
|
| 830 |
// Find the actual segment boundaries. |
| 831 |
Boundaries boundaries{}; |
| 832 |
switch (segments->getGranularity()) { |
| 833 |
case SegmenterGranularity::Grapheme: { |
| 834 |
boundaries = GraphemeBoundaries(segments, index); |
| 835 |
break; |
| 836 |
} |
| 837 |
case SegmenterGranularity::Word: { |
| 838 |
boundaries = WordBoundaries(segments, index); |
| 839 |
break; |
| 840 |
} |
| 841 |
case SegmenterGranularity::Sentence: { |
| 842 |
boundaries = SentenceBoundaries(segments, index); |
| 843 |
break; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
// Remember the end index of the current boundary segment. |
| 848 |
segments->setIndex(boundaries.endIndex); |
| 849 |
|
| 850 |
return CreateBoundaries(cx, boundaries, segments->getGranularity()); |
| 851 |
} |
| 852 |
|
| 853 |
bool js::intl_CreateSegmentsObject(JSContext* cx, unsigned argc, Value* vp) { |
| 854 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 855 |
MOZ_ASSERT(args.length() == 2); |
| 856 |
|
| 857 |
Rooted<SegmenterObject*> segmenter(cx, |
| 858 |
&args[0].toObject().as<SegmenterObject>()); |
| 859 |
Rooted<JSString*> string(cx, args[1].toString()); |
| 860 |
|
| 861 |
// Ensure the internal properties are resolved. |
| 862 |
if (!EnsureInternalsResolved(cx, segmenter)) { |
| 863 |
return false; |
| 864 |
} |
| 865 |
|
| 866 |
Rooted<JSObject*> proto( |
| 867 |
cx, GlobalObject::getOrCreateSegmentsPrototype(cx, cx->global())); |
| 868 |
if (!proto) { |
| 869 |
return false; |
| 870 |
} |
| 871 |
|
| 872 |
auto* segments = NewObjectWithGivenProto<SegmentsObject>(cx, proto); |
| 873 |
if (!segments) { |
| 874 |
return false; |
| 875 |
} |
| 876 |
|
| 877 |
segments->setSegmenter(segmenter); |
| 878 |
segments->setGranularity(segmenter->getGranularity()); |
| 879 |
segments->setString(string); |
| 880 |
segments->setIndex(0); |
| 881 |
|
| 882 |
args.rval().setObject(*segments); |
| 883 |
return true; |
| 884 |
} |
| 885 |
|
| 886 |
bool js::intl_CreateSegmentIterator(JSContext* cx, unsigned argc, Value* vp) { |
| 887 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 888 |
MOZ_ASSERT(args.length() == 1); |
| 889 |
|
| 890 |
Rooted<SegmentsObject*> segments(cx, |
| 891 |
&args[0].toObject().as<SegmentsObject>()); |
| 892 |
|
| 893 |
Rooted<JSObject*> proto( |
| 894 |
cx, GlobalObject::getOrCreateSegmentIteratorPrototype(cx, cx->global())); |
| 895 |
if (!proto) { |
| 896 |
return false; |
| 897 |
} |
| 898 |
|
| 899 |
auto* iterator = NewObjectWithGivenProto<SegmentIteratorObject>(cx, proto); |
| 900 |
if (!iterator) { |
| 901 |
return false; |
| 902 |
} |
| 903 |
|
| 904 |
iterator->setSegmenter(segments->getSegmenter()); |
| 905 |
iterator->setGranularity(segments->getGranularity()); |
| 906 |
iterator->setString(segments->getString()); |
| 907 |
iterator->setIndex(0); |
| 908 |
|
| 909 |
args.rval().setObject(*iterator); |
| 910 |
return true; |
| 911 |
} |
| 912 |
|
| 913 |
bool js::intl_FindSegmentBoundaries(JSContext* cx, unsigned argc, Value* vp) { |
| 914 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 915 |
MOZ_ASSERT(args.length() == 2); |
| 916 |
|
| 917 |
Rooted<SegmentsObject*> segments(cx, |
| 918 |
&args[0].toObject().as<SegmentsObject>()); |
| 919 |
|
| 920 |
int32_t index = args[1].toInt32(); |
| 921 |
MOZ_ASSERT(index >= 0); |
| 922 |
MOZ_ASSERT(uint32_t(index) < segments->getString()->length()); |
| 923 |
|
| 924 |
auto* result = FindSegmentBoundaries( |
| 925 |
cx, static_cast<Handle<SegmentsObject*>>(segments), index); |
| 926 |
if (!result) { |
| 927 |
return false; |
| 928 |
} |
| 929 |
|
| 930 |
args.rval().setObject(*result); |
| 931 |
return true; |
| 932 |
} |
| 933 |
|
| 934 |
bool js::intl_FindNextSegmentBoundaries(JSContext* cx, unsigned argc, |
| 935 |
Value* vp) { |
| 936 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 937 |
MOZ_ASSERT(args.length() == 1); |
| 938 |
|
| 939 |
Rooted<SegmentIteratorObject*> iterator( |
| 940 |
cx, &args[0].toObject().as<SegmentIteratorObject>()); |
| 941 |
|
| 942 |
int32_t index = iterator->getIndex(); |
| 943 |
MOZ_ASSERT(index >= 0); |
| 944 |
MOZ_ASSERT(uint32_t(index) < iterator->getString()->length()); |
| 945 |
|
| 946 |
auto* result = FindSegmentBoundaries( |
| 947 |
cx, static_cast<Handle<SegmentIteratorObject*>>(iterator), index); |
| 948 |
if (!result) { |
| 949 |
return false; |
| 950 |
} |
| 951 |
|
| 952 |
args.rval().setObject(*result); |
| 953 |
return true; |
| 954 |
} |