summaryrefslogtreecommitdiff
path: root/deps/v8/src/objects/js-segments.cc
blob: f0a7a4789632caa1772af3850083d3c2e0e3eefc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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_INTL_SUPPORT
#error Internationalization is expected to be enabled.
#endif  // V8_INTL_SUPPORT

#include "src/objects/js-segments.h"

#include <map>
#include <memory>
#include <string>

#include "src/execution/isolate.h"
#include "src/heap/factory.h"
#include "src/objects/intl-objects.h"
#include "src/objects/js-segment-iterator.h"
#include "src/objects/js-segmenter-inl.h"
#include "src/objects/js-segments-inl.h"
#include "src/objects/managed.h"
#include "src/objects/objects-inl.h"
#include "unicode/brkiter.h"

namespace v8 {
namespace internal {

// ecma402 #sec-createsegmentsobject
MaybeHandle<JSSegments> JSSegments::Create(Isolate* isolate,
                                           Handle<JSSegmenter> segmenter,
                                           Handle<String> string) {
  icu::BreakIterator* break_iterator =
      segmenter->icu_break_iterator().raw()->clone();
  DCHECK_NOT_NULL(break_iterator);

  Handle<Managed<icu::UnicodeString>> unicode_string =
      Intl::SetTextToBreakIterator(isolate, string, break_iterator);
  Handle<Managed<icu::BreakIterator>> managed_break_iterator =
      Managed<icu::BreakIterator>::FromRawPtr(isolate, 0, break_iterator);

  // 1. Let internalSlotsList be « [[SegmentsSegmenter]], [[SegmentsString]] ».
  // 2. Let segments be ! ObjectCreate(%Segments.prototype%, internalSlotsList).
  Handle<Map> map(isolate->native_context()->intl_segments_map(), isolate);
  Handle<JSObject> result = isolate->factory()->NewJSObjectFromMap(map);

  Handle<JSSegments> segments = Handle<JSSegments>::cast(result);
  segments->set_flags(0);

  // 3. Set segments.[[SegmentsSegmenter]] to segmenter.
  segments->set_icu_break_iterator(*managed_break_iterator);
  segments->set_granularity(segmenter->granularity());

  // 4. Set segments.[[SegmentsString]] to string.
  segments->set_unicode_string(*unicode_string);

  // 5. Return segments.
  return segments;
}

// ecma402 #sec-%segmentsprototype%.containing
MaybeHandle<Object> JSSegments::Containing(Isolate* isolate,
                                           Handle<JSSegments> segments,
                                           int32_t n) {
  // 5. Let len be the length of string.
  int32_t len = segments->unicode_string().raw()->length();

  // 7. If n < 0 or n ≥ len, return undefined.
  if (n < 0 || n >= len) {
    return isolate->factory()->undefined_value();
  }

  // n may point to the surrogate tail- adjust it back to the lead.
  n = segments->unicode_string().raw()->getChar32Start(n);

  icu::BreakIterator* break_iterator = segments->icu_break_iterator().raw();
  // 8. Let startIndex be ! FindBoundary(segmenter, string, n, before).
  int32_t start_index =
      break_iterator->isBoundary(n) ? n : break_iterator->preceding(n);

  // 9. Let endIndex be ! FindBoundary(segmenter, string, n, after).
  int32_t end_index = break_iterator->following(n);

  // 10. Return ! CreateSegmentDataObject(segmenter, string, startIndex,
  // endIndex).
  return CreateSegmentDataObject(
      isolate, segments->granularity(), break_iterator,
      *(segments->unicode_string().raw()), start_index, end_index);
}

namespace {

bool CurrentSegmentIsWordLike(icu::BreakIterator* break_iterator) {
  int32_t rule_status = break_iterator->getRuleStatus();
  return (rule_status >= UBRK_WORD_NUMBER &&
          rule_status < UBRK_WORD_NUMBER_LIMIT) ||
         (rule_status >= UBRK_WORD_LETTER &&
          rule_status < UBRK_WORD_LETTER_LIMIT) ||
         (rule_status >= UBRK_WORD_KANA &&
          rule_status < UBRK_WORD_KANA_LIMIT) ||
         (rule_status >= UBRK_WORD_IDEO && rule_status < UBRK_WORD_IDEO_LIMIT);
}

}  // namespace

// ecma402 #sec-createsegmentdataobject
MaybeHandle<Object> JSSegments::CreateSegmentDataObject(
    Isolate* isolate, JSSegmenter::Granularity granularity,
    icu::BreakIterator* break_iterator, const icu::UnicodeString& string,
    int32_t start_index, int32_t end_index) {
  Factory* factory = isolate->factory();

  // 1. Let len be the length of string.
  // 2. Assert: startIndex ≥ 0.
  DCHECK_GE(start_index, 0);
  // 3. Assert: endIndex ≤ len.
  DCHECK_LE(end_index, string.length());
  // 4. Assert: startIndex < endIndex.
  DCHECK_LT(start_index, end_index);

  // 5. Let result be ! ObjectCreate(%ObjectPrototype%).
  Handle<JSObject> result = factory->NewJSObject(isolate->object_function());

  // 6. Let segment be the String value equal to the substring of string
  // consisting of the code units at indices startIndex (inclusive) through
  // endIndex (exclusive).
  Handle<String> segment;
  ASSIGN_RETURN_ON_EXCEPTION(
      isolate, segment, Intl::ToString(isolate, string, start_index, end_index),
      JSObject);

  // 7. Perform ! CreateDataPropertyOrThrow(result, "segment", segment).
  Maybe<bool> maybe_create_segment = JSReceiver::CreateDataProperty(
      isolate, result, factory->segment_string(), segment, Just(kDontThrow));
  DCHECK(maybe_create_segment.FromJust());
  USE(maybe_create_segment);

  // 8. Perform ! CreateDataPropertyOrThrow(result, "index", startIndex).
  Maybe<bool> maybe_create_index = JSReceiver::CreateDataProperty(
      isolate, result, factory->index_string(),
      factory->NewNumberFromInt(start_index), Just(kDontThrow));
  DCHECK(maybe_create_index.FromJust());
  USE(maybe_create_index);

  // 9. Perform ! CreateDataPropertyOrThrow(result, "input", string).
  Handle<String> input_string;
  ASSIGN_RETURN_ON_EXCEPTION(isolate, input_string,
                             Intl::ToString(isolate, string), JSObject);
  Maybe<bool> maybe_create_input = JSReceiver::CreateDataProperty(
      isolate, result, factory->input_string(), input_string, Just(kDontThrow));
  DCHECK(maybe_create_input.FromJust());
  USE(maybe_create_input);

  Handle<Object> is_word_like;
  // 10. Let granularity be segmenter.[[SegmenterGranularity]].
  // 11. If granularity is "word", then
  if (granularity == JSSegmenter::Granularity::WORD) {
    // a. Let isWordLike be a Boolean value indicating whether the word segment
    //    segment in string is "word-like" according to locale
    //    segmenter.[[Locale]].
    is_word_like = CurrentSegmentIsWordLike(break_iterator)
                       ? factory->true_value()
                       : factory->false_value();
    // b. Perform ! CreateDataPropertyOrThrow(result, "isWordLike", isWordLike).
    Maybe<bool> maybe_create_is_word_like = JSReceiver::CreateDataProperty(
        isolate, result, factory->isWordLike_string(), is_word_like,
        Just(kDontThrow));
    DCHECK(maybe_create_is_word_like.FromJust());
    USE(maybe_create_is_word_like);
  }
  return result;
}

Handle<String> JSSegments::GranularityAsString(Isolate* isolate) const {
  return JSSegmenter::GetGranularityString(isolate, granularity());
}

}  // namespace internal
}  // namespace v8